blob: 6694ecef9130872a40d9154cd4d020de99713c2b [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 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080030
Alexander Afanasyev66886812014-01-31 14:48:48 -080031 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");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080038 g_logger.setLogLevel(LOG_ALL);
39
Alexander Afanasyev66886812014-01-31 14:48:48 -080040 NFD_LOG_TRACE("trace message JHGFDSR^1");
41 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
42 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080043 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -080044 NFD_LOG_ERROR("error message !#$&^%$#@");
45 NFD_LOG_FATAL("fatal message JJSjaamcng");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080046
Alexander Afanasyev66886812014-01-31 14:48:48 -080047 BOOST_CHECK_EQUAL(m_buffer.str(),
48 "TRACE: [BasicTests] trace message JHGFDSR^1\n"
49 "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n"
50 "WARNING: [BasicTests] warning message XXXhdhd111x\n"
51 "INFO: [BasicTests] info message Jjxjshj13\n"
52 "ERROR: [BasicTests] error message !#$&^%$#@\n"
53 "FATAL: [BasicTests] fatal message JJSjaamcng\n"
54 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080055}
56
Alexander Afanasyev66886812014-01-31 14:48:48 -080057class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080058{
Alexander Afanasyev66886812014-01-31 14:48:48 -080059public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080060 InClassLogger()
61 {
62 g_logger.setLogLevel(LOG_ALL);
63 }
64
Alexander Afanasyev66886812014-01-31 14:48:48 -080065 void
66 writeLogs()
67 {
68 NFD_LOG_TRACE("trace message JHGFDSR^1");
69 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
70 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080071 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -080072 NFD_LOG_ERROR("error message !#$&^%$#@");
73 NFD_LOG_FATAL("fatal message JJSjaamcng");
74 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080075
Alexander Afanasyev66886812014-01-31 14:48:48 -080076private:
77 NFD_LOG_INCLASS_DECLARE();
78};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080079
Alexander Afanasyev66886812014-01-31 14:48:48 -080080NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080081
Alexander Afanasyev66886812014-01-31 14:48:48 -080082BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
83{
84 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080085
Alexander Afanasyev66886812014-01-31 14:48:48 -080086 BOOST_CHECK_EQUAL(m_buffer.str(),
87 "TRACE: [InClassLogger] trace message JHGFDSR^1\n"
88 "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n"
89 "WARNING: [InClassLogger] warning message XXXhdhd111x\n"
90 "INFO: [InClassLogger] info message Jjxjshj13\n"
91 "ERROR: [InClassLogger] error message !#$&^%$#@\n"
92 "FATAL: [InClassLogger] fatal message JJSjaamcng\n"
93 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080094}
95
Alexander Afanasyev66886812014-01-31 14:48:48 -080096
97template<class T>
98class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080099{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800100public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800101 InClassTemplateLogger()
102 {
103 g_logger.setLogLevel(LOG_ALL);
104 }
105
Alexander Afanasyev66886812014-01-31 14:48:48 -0800106 void
107 writeLogs()
108 {
109 NFD_LOG_TRACE("trace message JHGFDSR^1");
110 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
111 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800112 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800113 NFD_LOG_ERROR("error message !#$&^%$#@");
114 NFD_LOG_FATAL("fatal message JJSjaamcng");
115 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800116
Alexander Afanasyev66886812014-01-31 14:48:48 -0800117private:
118 NFD_LOG_INCLASS_DECLARE();
119};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800120
Alexander Afanasyev66886812014-01-31 14:48:48 -0800121NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
122NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800123
Alexander Afanasyev66886812014-01-31 14:48:48 -0800124BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
125{
126 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800127
Alexander Afanasyev66886812014-01-31 14:48:48 -0800128 BOOST_CHECK_EQUAL(m_buffer.str(),
129 "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n"
130 "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n"
131 "WARNING: [GenericInClassTemplateLogger] warning message XXXhdhd111x\n"
132 "INFO: [GenericInClassTemplateLogger] info message Jjxjshj13\n"
133 "ERROR: [GenericInClassTemplateLogger] error message !#$&^%$#@\n"
134 "FATAL: [GenericInClassTemplateLogger] fatal message JJSjaamcng\n"
135 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800136}
137
Alexander Afanasyev66886812014-01-31 14:48:48 -0800138BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800139{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800140 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800141
Alexander Afanasyev66886812014-01-31 14:48:48 -0800142 BOOST_CHECK_EQUAL(m_buffer.str(),
143 "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n"
144 "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n"
145 "WARNING: [IntInClassLogger] warning message XXXhdhd111x\n"
146 "INFO: [IntInClassLogger] info message Jjxjshj13\n"
147 "ERROR: [IntInClassLogger] error message !#$&^%$#@\n"
148 "FATAL: [IntInClassLogger] fatal message JJSjaamcng\n"
149 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800150}
151
152BOOST_AUTO_TEST_SUITE_END()
153
154} // namespace nfd