blob: 0530dcc3da2817f3c99f388d938a71708bc7b687 [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
Junxiao Shid9ee45c2014-02-27 15:38:11 -070013#include "tests/test-common.hpp"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080014
15namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070016namespace tests {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080017
Junxiao Shid9ee45c2014-02-27 15:38:11 -070018BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080019
Junxiao Shid9ee45c2014-02-27 15:38:11 -070020struct LoggerFixture : protected BaseFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080021{
Alexander Afanasyev66886812014-01-31 14:48:48 -080022 LoggerFixture()
23 : m_savedBuf(std::cerr.rdbuf())
24 {
25 std::cerr.rdbuf(m_buffer.rdbuf());
26 }
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080027
Alexander Afanasyev66886812014-01-31 14:48:48 -080028 ~LoggerFixture()
29 {
30 std::cerr.rdbuf(m_savedBuf);
31 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080032
Alexander Afanasyev66886812014-01-31 14:48:48 -080033 std::stringstream m_buffer;
34 std::streambuf* m_savedBuf;
35};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080036
Alexander Afanasyev66886812014-01-31 14:48:48 -080037BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
38{
39 NFD_LOG_INIT("BasicTests");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080040 g_logger.setLogLevel(LOG_ALL);
41
Alexander Afanasyev66886812014-01-31 14:48:48 -080042 NFD_LOG_TRACE("trace message JHGFDSR^1");
43 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
44 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080045 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -080046 NFD_LOG_ERROR("error message !#$&^%$#@");
47 NFD_LOG_FATAL("fatal message JJSjaamcng");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080048
Alexander Afanasyev66886812014-01-31 14:48:48 -080049 BOOST_CHECK_EQUAL(m_buffer.str(),
50 "TRACE: [BasicTests] trace message JHGFDSR^1\n"
51 "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n"
52 "WARNING: [BasicTests] warning message XXXhdhd111x\n"
53 "INFO: [BasicTests] info message Jjxjshj13\n"
54 "ERROR: [BasicTests] error message !#$&^%$#@\n"
55 "FATAL: [BasicTests] fatal message JJSjaamcng\n"
56 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080057}
58
Alexander Afanasyev66886812014-01-31 14:48:48 -080059class InClassLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080060{
Alexander Afanasyev66886812014-01-31 14:48:48 -080061public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080062 InClassLogger()
63 {
64 g_logger.setLogLevel(LOG_ALL);
65 }
66
Alexander Afanasyev66886812014-01-31 14:48:48 -080067 void
68 writeLogs()
69 {
70 NFD_LOG_TRACE("trace message JHGFDSR^1");
71 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
72 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080073 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -080074 NFD_LOG_ERROR("error message !#$&^%$#@");
75 NFD_LOG_FATAL("fatal message JJSjaamcng");
76 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080077
Alexander Afanasyev66886812014-01-31 14:48:48 -080078private:
79 NFD_LOG_INCLASS_DECLARE();
80};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080081
Alexander Afanasyev66886812014-01-31 14:48:48 -080082NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080083
Alexander Afanasyev66886812014-01-31 14:48:48 -080084BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
85{
86 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080087
Alexander Afanasyev66886812014-01-31 14:48:48 -080088 BOOST_CHECK_EQUAL(m_buffer.str(),
89 "TRACE: [InClassLogger] trace message JHGFDSR^1\n"
90 "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n"
91 "WARNING: [InClassLogger] warning message XXXhdhd111x\n"
92 "INFO: [InClassLogger] info message Jjxjshj13\n"
93 "ERROR: [InClassLogger] error message !#$&^%$#@\n"
94 "FATAL: [InClassLogger] fatal message JJSjaamcng\n"
95 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080096}
97
Alexander Afanasyev66886812014-01-31 14:48:48 -080098
99template<class T>
100class InClassTemplateLogger : public LoggerFixture
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800101{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800102public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800103 InClassTemplateLogger()
104 {
105 g_logger.setLogLevel(LOG_ALL);
106 }
107
Alexander Afanasyev66886812014-01-31 14:48:48 -0800108 void
109 writeLogs()
110 {
111 NFD_LOG_TRACE("trace message JHGFDSR^1");
112 NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
113 NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800114 NFD_LOG_INFO("info message Jjxjshj13");
Alexander Afanasyev66886812014-01-31 14:48:48 -0800115 NFD_LOG_ERROR("error message !#$&^%$#@");
116 NFD_LOG_FATAL("fatal message JJSjaamcng");
117 }
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800118
Alexander Afanasyev66886812014-01-31 14:48:48 -0800119private:
120 NFD_LOG_INCLASS_DECLARE();
121};
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800122
Alexander Afanasyev66886812014-01-31 14:48:48 -0800123NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
124NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800125
Alexander Afanasyev66886812014-01-31 14:48:48 -0800126BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
127{
128 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800129
Alexander Afanasyev66886812014-01-31 14:48:48 -0800130 BOOST_CHECK_EQUAL(m_buffer.str(),
131 "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n"
132 "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n"
133 "WARNING: [GenericInClassTemplateLogger] warning message XXXhdhd111x\n"
134 "INFO: [GenericInClassTemplateLogger] info message Jjxjshj13\n"
135 "ERROR: [GenericInClassTemplateLogger] error message !#$&^%$#@\n"
136 "FATAL: [GenericInClassTemplateLogger] fatal message JJSjaamcng\n"
137 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800138}
139
Alexander Afanasyev66886812014-01-31 14:48:48 -0800140BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800141{
Alexander Afanasyev66886812014-01-31 14:48:48 -0800142 writeLogs();
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800143
Alexander Afanasyev66886812014-01-31 14:48:48 -0800144 BOOST_CHECK_EQUAL(m_buffer.str(),
145 "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n"
146 "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n"
147 "WARNING: [IntInClassLogger] warning message XXXhdhd111x\n"
148 "INFO: [IntInClassLogger] info message Jjxjshj13\n"
149 "ERROR: [IntInClassLogger] error message !#$&^%$#@\n"
150 "FATAL: [IntInClassLogger] fatal message JJSjaamcng\n"
151 );
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800152}
153
154BOOST_AUTO_TEST_SUITE_END()
155
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700156} // namespace tests
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800157} // namespace nfd