blob: ca55c37e57abe574075d542ef6443df536b8fce1 [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 *
6 * Author: Ilya Moiseenko <iliamo@ucla.edu>
7 */
8
9#include "core/logger.hpp"
10#include <boost/test/unit_test.hpp>
11#include <iostream>
12
13
14namespace nfd {
15
16BOOST_AUTO_TEST_SUITE(CoreLogger)
17
Alexander Afanasyev66886812014-01-31 14:48:48 -080018struct LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080019{
Alexander Afanasyev66886812014-01-31 14:48:48 -080020 LoggerFixture()
21 : m_savedBuf(std::cerr.rdbuf())
22 {
23 std::cerr.rdbuf(m_buffer.rdbuf());
24 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080025
Alexander Afanasyev66886812014-01-31 14:48:48 -080026 ~LoggerFixture()
27 {
28 std::cerr.rdbuf(m_savedBuf);
29 }
30
31 std::stringstream m_buffer;
32 std::streambuf* m_savedBuf;
33};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080034
Alexander Afanasyev66886812014-01-31 14:48:48 -080035BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
36{
37 NFD_LOG_INIT("BasicTests");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080038
Alexander Afanasyev66886812014-01-31 14:48:48 -080039 NFD_LOG_TRACE("trace message JHGFDSR^1");
40 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
41 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
42 NFD_LOG_INFO("info message Jjxjshj13");
43 NFD_LOG_ERROR("error message !#$&^%$#@");
44 NFD_LOG_FATAL("fatal message JJSjaamcng");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080045
Alexander Afanasyev66886812014-01-31 14:48:48 -080046 BOOST_CHECK_EQUAL(m_buffer.str(),
47 "TRACE: [BasicTests] trace message JHGFDSR^1\n"
48 "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n"
49 "WARNING: [BasicTests] warning message XXXhdhd111x\n"
50 "INFO: [BasicTests] info message Jjxjshj13\n"
51 "ERROR: [BasicTests] error message !#$&^%$#@\n"
52 "FATAL: [BasicTests] fatal message JJSjaamcng\n"
53 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080054}
55
Alexander Afanasyev66886812014-01-31 14:48:48 -080056class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080057{
Alexander Afanasyev66886812014-01-31 14:48:48 -080058public:
59 void
60 writeLogs()
61 {
62 NFD_LOG_TRACE("trace message JHGFDSR^1");
63 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
64 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
65 NFD_LOG_INFO("info message Jjxjshj13");
66 NFD_LOG_ERROR("error message !#$&^%$#@");
67 NFD_LOG_FATAL("fatal message JJSjaamcng");
68 }
69
70private:
71 NFD_LOG_INCLASS_DECLARE();
72};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080073
Alexander Afanasyev66886812014-01-31 14:48:48 -080074NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080075
Alexander Afanasyev66886812014-01-31 14:48:48 -080076BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
77{
78 writeLogs();
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080079
Alexander Afanasyev66886812014-01-31 14:48:48 -080080 BOOST_CHECK_EQUAL(m_buffer.str(),
81 "TRACE: [InClassLogger] trace message JHGFDSR^1\n"
82 "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n"
83 "WARNING: [InClassLogger] warning message XXXhdhd111x\n"
84 "INFO: [InClassLogger] info message Jjxjshj13\n"
85 "ERROR: [InClassLogger] error message !#$&^%$#@\n"
86 "FATAL: [InClassLogger] fatal message JJSjaamcng\n"
87 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080088}
89
Alexander Afanasyev66886812014-01-31 14:48:48 -080090
91template<class T>
92class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080093{
Alexander Afanasyev66886812014-01-31 14:48:48 -080094public:
95 void
96 writeLogs()
97 {
98 NFD_LOG_TRACE("trace message JHGFDSR^1");
99 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
100 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
101 NFD_LOG_INFO("info message Jjxjshj13");
102 NFD_LOG_ERROR("error message !#$&^%$#@");
103 NFD_LOG_FATAL("fatal message JJSjaamcng");
104 }
105
106private:
107 NFD_LOG_INCLASS_DECLARE();
108};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800109
Alexander Afanasyev66886812014-01-31 14:48:48 -0800110NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
111NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800112
Alexander Afanasyev66886812014-01-31 14:48:48 -0800113BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
114{
115 writeLogs();
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800116
Alexander Afanasyev66886812014-01-31 14:48:48 -0800117 BOOST_CHECK_EQUAL(m_buffer.str(),
118 "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n"
119 "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n"
120 "WARNING: [GenericInClassTemplateLogger] warning message XXXhdhd111x\n"
121 "INFO: [GenericInClassTemplateLogger] info message Jjxjshj13\n"
122 "ERROR: [GenericInClassTemplateLogger] error message !#$&^%$#@\n"
123 "FATAL: [GenericInClassTemplateLogger] fatal message JJSjaamcng\n"
124 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800125}
126
Alexander Afanasyev66886812014-01-31 14:48:48 -0800127BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800128{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800129 writeLogs();
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800130
Alexander Afanasyev66886812014-01-31 14:48:48 -0800131 BOOST_CHECK_EQUAL(m_buffer.str(),
132 "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n"
133 "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n"
134 "WARNING: [IntInClassLogger] warning message XXXhdhd111x\n"
135 "INFO: [IntInClassLogger] info message Jjxjshj13\n"
136 "ERROR: [IntInClassLogger] error message !#$&^%$#@\n"
137 "FATAL: [IntInClassLogger] fatal message JJSjaamcng\n"
138 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800139}
140
141BOOST_AUTO_TEST_SUITE_END()
142
143} // namespace nfd