File: LoggingTest.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (126 lines) | stat: -rw-r--r-- 3,835 bytes parent folder | download | duplicates (3)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * LoggingTest.cpp
 * Test fixture for the Logging framework
 * Copyright (C) 2005 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>
#include <deque>
#include <string>
#include <utility>
#include <vector>

#include "ola/Logging.h"
#include "ola/StringUtils.h"
#include "ola/testing/TestUtils.h"


using std::deque;
using std::vector;
using std::string;
using ola::IncrementLogLevel;
using ola::log_level;


class LoggingTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(LoggingTest);
  CPPUNIT_TEST(testLogging);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testLogging();
};


class MockLogDestination: public ola::LogDestination {
 public:
    void AddExpected(log_level level, string log_line);
    void Write(log_level level, const string &log_line);
    int LinesRemaining() const { return m_log_lines.size(); }
 private:
    deque<std::pair<log_level, string> > m_log_lines;
};


CPPUNIT_TEST_SUITE_REGISTRATION(LoggingTest);


void MockLogDestination::AddExpected(log_level level, string log_line) {
  std::pair<log_level, string> expected_result(level, log_line);
  m_log_lines.push_back(expected_result);
}


/*
 * Check that what gets written is what we expected
 */
void MockLogDestination::Write(log_level level, const string &log_line) {
  vector<string> tokens;
  ola::StringSplit(log_line, &tokens, ":");
  OLA_ASSERT_EQ(tokens.size() , (size_t) 3);
  OLA_ASSERT_GT(m_log_lines.size(), 0);
  std::pair<log_level, string> expected_result = m_log_lines.at(0);
  m_log_lines.pop_front();
  OLA_ASSERT_EQ(expected_result.first, level);
  OLA_ASSERT_EQ(expected_result.second, tokens.at(2));
}

/*
 * Check that logging works correctly.
 */
void LoggingTest::testLogging() {
  MockLogDestination *destination = new MockLogDestination();
  InitLogging(ola::OLA_LOG_DEBUG, destination);
  destination->AddExpected(ola::OLA_LOG_DEBUG, " debug\n");
  OLA_DEBUG << "debug";
  destination->AddExpected(ola::OLA_LOG_INFO, " info\n");
  OLA_INFO << "info";
  destination->AddExpected(ola::OLA_LOG_WARN, " warn\n");
  OLA_WARN << "warn";
  destination->AddExpected(ola::OLA_LOG_FATAL, " fatal\n");
  OLA_FATAL << "fatal";

  // Now make sure nothing below WARN is logged
  ola::SetLogLevel(ola::OLA_LOG_WARN);
  OLA_DEBUG << "debug";
  OLA_INFO << "info";
  destination->AddExpected(ola::OLA_LOG_WARN, " warn\n");
  OLA_WARN << "warn";
  destination->AddExpected(ola::OLA_LOG_FATAL, " fatal\n");
  OLA_FATAL << "fatal";
  OLA_ASSERT_EQ(destination->LinesRemaining(), 0);

  // set the log level to INFO
  IncrementLogLevel();
  OLA_DEBUG << "debug";
  destination->AddExpected(ola::OLA_LOG_INFO, " info\n");
  OLA_INFO << "info";
  destination->AddExpected(ola::OLA_LOG_WARN, " warn\n");
  OLA_WARN << "warn";
  destination->AddExpected(ola::OLA_LOG_FATAL, " fatal\n");
  OLA_FATAL << "fatal";
  OLA_ASSERT_EQ(destination->LinesRemaining(), 0);

  IncrementLogLevel();
  // this should wrap to NONE
  IncrementLogLevel();
  OLA_DEBUG << "debug";
  OLA_INFO << "info";
  OLA_WARN << "warn";
  OLA_FATAL << "fatal";
  OLA_ASSERT_EQ(destination->LinesRemaining(), 0);
}