Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef MANTID_STDCHANNELTEST_H_
#define MANTID_STDCHANNELTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/StdChannel.h"
#include "MantidKernel/Logger.h"
#include <iostream>
#include <Poco/Logger.h>
#include <Poco/NullChannel.h>
using namespace Mantid::Kernel;
class StdChannelTest : public CxxTest::TestSuite
{
public:
void testContructor()
{
TS_ASSERT_THROWS_NOTHING(Poco::StdChannel a;)
}
void testLogMessage()
{
//Save root channel
Poco::Channel* rootChannel=Poco::Logger::root().getChannel();
std::stringstream obuffer,lbuffer;
// Save cout's and clog's buffers here
std::streambuf *obuf = std::cout.rdbuf();
std::streambuf *lbuf = std::clog.rdbuf();
// Redirect cout to buffer or any other ostream
std::cout.rdbuf(obuffer.rdbuf());
std::clog.rdbuf(lbuffer.rdbuf());
//root logger
Logger & log(Logger::get(""));
//Test null channel first
Poco::Logger::root().setChannel(new Poco::NullChannel);
log.error() << "Error Message 1" << std::endl;
//cout and clog should be empty
TS_ASSERT_EQUALS(obuffer.str(),"");
TS_ASSERT_EQUALS(lbuffer.str(),"");
obuffer.str("");
lbuffer.str("");
//Test console channel
Poco::Logger::root().setChannel(new Poco::ConsoleChannel);
log.error() << "Error Message 2" << std::endl;
//the error should be in std::clog (or std:err)
TS_ASSERT_EQUALS(obuffer.str(),"");
TS_ASSERT_EQUALS(lbuffer.str(),"Error Message 2\n");
obuffer.str("");
lbuffer.str("");
//Test std channel
Poco::Logger::root().setChannel(new Poco::StdChannel);
log.error() << "Error Message 3" << std::endl;
//the error should be in std::cout
TS_ASSERT_EQUALS(obuffer.str(),"Error Message 3\n");
TS_ASSERT_EQUALS(lbuffer.str(),"");
// When done redirect cout to its old self
std::cout.rdbuf(obuf);
std::clog.rdbuf(lbuf);
//set back the channel on root
Poco::Logger::root().setChannel(rootChannel);
}
};
#endif /*MANTID_STDCHANNELTEST_H_*/