forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StdoutChannelTest.h
72 lines (61 loc) · 2.41 KB
/
StdoutChannelTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Logger.h"
#include "MantidKernel/StdoutChannel.h"
#include <Poco/AutoPtr.h>
#include <Poco/Logger.h>
#include <Poco/NullChannel.h>
using namespace Mantid::Kernel;
class StdoutChannelTest : public CxxTest::TestSuite {
public:
void testContructor() { TS_ASSERT_THROWS_NOTHING(Poco::StdoutChannel 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 has empty name
Logger log("");
// Test null channel first
Poco::AutoPtr<Poco::NullChannel> nullChannel(new Poco::NullChannel);
Poco::Logger::root().setChannel(nullChannel);
log.error() << "Error Message 1\n";
// cout and clog should be empty
TS_ASSERT_EQUALS(obuffer.str(), "");
TS_ASSERT_EQUALS(lbuffer.str(), "");
obuffer.str("");
lbuffer.str("");
// Test console channel
Poco::AutoPtr<Poco::ConsoleChannel> consoleChannel(new Poco::ConsoleChannel(std::clog));
Poco::Logger::root().setChannel(consoleChannel);
log.error() << "Error Message 2\n";
// 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::AutoPtr<Poco::StdoutChannel> stdoutChannel(new Poco::StdoutChannel);
Poco::Logger::root().setChannel(stdoutChannel);
log.error() << "Error Message 3\n";
// 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);
}
};