PageRenderTime 109ms CodeModel.GetById 85ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/mordor/tests/log.cpp

http://github.com/mozy/mordor
C++ | 134 lines | 116 code | 17 blank | 1 comment | 6 complexity | 3ef37d953ab9f7d56fd809342f5dd9c4 MD5 | raw file
  1// Copyright (c) 2009 - Mozy, Inc.
  2
  3#include "mordor/log.h"
  4#include "mordor/test/test.h"
  5
  6using namespace Mordor;
  7
  8class TestLogSink : public LogSink
  9{
 10public:
 11    typedef boost::shared_ptr<TestLogSink> ptr;
 12
 13    void log(const std::string &logger,
 14             boost::posix_time::ptime now, unsigned long long elapsed,
 15             tid_t thread, void *fiber,
 16             Log::Level level, const std::string &str,
 17             const char* file, int line)
 18    {
 19        m_str = str;
 20    }
 21
 22    std::string m_str;
 23};
 24
 25static void testLogger(const std::vector<Logger::ptr> &loggers)
 26{
 27    if(loggers.empty())
 28        return;
 29    std::vector<TestLogSink::ptr> sinks;
 30    sinks.resize(loggers.size());
 31    for(size_t i = 0; i < loggers.size(); ++i) {
 32        sinks[i].reset(new TestLogSink());
 33        loggers[i]->addSink(sinks[i]);
 34    }
 35    for (size_t i = 0; i < loggers.size(); i++) {
 36        MORDOR_LOG_INFO(loggers[i]) << "Hello world";
 37        for (size_t j = 0; j <= i; ++j) {
 38            MORDOR_TEST_ASSERT_EQUAL(sinks[j]->m_str, "Hello world");
 39            sinks[j]->m_str.clear();
 40        }
 41        for (size_t j = i + 1; j < loggers.size(); ++j)
 42            MORDOR_TEST_ASSERT(sinks[j]->m_str.empty());
 43    }
 44    for (size_t i = 0; i < loggers.size(); i++)
 45        loggers[i]->clearSinks();
 46}
 47
 48MORDOR_UNITTEST(Log, two_asc)
 49{
 50    std::vector<Logger::ptr> vec;
 51    vec.push_back(Log::lookup("twoasc"));
 52    vec.push_back(Log::lookup("twoasc:child"));
 53    testLogger(vec);
 54}
 55
 56MORDOR_UNITTEST(Log, two_des){
 57    std::vector<Logger::ptr> vec;
 58    Logger::ptr a = Log::lookup("twodes:child");
 59    Logger::ptr b = Log::lookup("twodes");
 60    vec.push_back(b);
 61    vec.push_back(a);
 62    testLogger(vec);
 63}
 64
 65MORDOR_UNITTEST(Log, three_asc)
 66{
 67    std::vector<Logger::ptr> vec;
 68    vec.push_back(Log::lookup("threeasc"));
 69    vec.push_back(Log::lookup("threeasc:child1"));
 70    Log::lookup("threeasc:child1:child2");
 71    vec.push_back(Log::lookup("threeasc:child1:child2"));
 72    testLogger(vec);
 73}
 74
 75MORDOR_UNITTEST(Log, three_des1){
 76    std::vector<Logger::ptr> vec;
 77    Logger::ptr a = Log::lookup("threedes1:child1:child2");
 78    Logger::ptr b = Log::lookup("threedes1:child1");
 79    Logger::ptr c = Log::lookup("threedes1");
 80    vec.push_back(c);
 81    vec.push_back(b);
 82    vec.push_back(a);
 83    testLogger(vec);
 84}
 85
 86MORDOR_UNITTEST(Log, three_des2){
 87    std::vector<Logger::ptr> vec;
 88    Logger::ptr b = Log::lookup("threedes2:child1");
 89    Logger::ptr a = Log::lookup("threedes2:child1:child2");
 90    Logger::ptr c = Log::lookup("threedes2");
 91    vec.push_back(c);
 92    vec.push_back(b);
 93    vec.push_back(a);
 94    testLogger(vec);
 95}
 96
 97MORDOR_UNITTEST(Log, multi_children){
 98    std::vector<Logger::ptr> vec;
 99    Logger::ptr c1 = Log::lookup("mc:mc1:::::c1::");
100    Logger::ptr c2 = Log::lookup("mc:mc1:c2");
101    Logger::ptr g = Log::lookup("mc");
102    Logger::ptr p = Log::lookup("mc:mc1");
103    Logger::ptr cc = Log::lookup("mc:mc1:c1:cc1");
104
105    vec.push_back(g);
106    vec.push_back(p);
107    vec.push_back(c1);
108    vec.push_back(cc);
109    testLogger(vec);
110    vec.clear();
111    vec.push_back(p);
112    vec.push_back(c2);
113    testLogger(vec);
114}
115
116MORDOR_UNITTEST(Log, levelPropagate)
117{
118    Logger::ptr l = Log::lookup("app:logger");
119    Logger::ptr l2 = Log::lookup("app");
120
121    MORDOR_TEST_ASSERT_NOT_EQUAL(l->level(), Log::DEBUG);
122    l2->level(Log::DEBUG);
123    MORDOR_TEST_ASSERT_EQUAL(l->level(), Log::DEBUG);
124}
125
126MORDOR_UNITTEST(Log, levelNoPropagate)
127{
128    Logger::ptr l = Log::lookup("app2:logger");
129    Logger::ptr l2 = Log::lookup("app2");
130
131    MORDOR_TEST_ASSERT_NOT_EQUAL(l->level(), Log::DEBUG);
132    l2->level(Log::DEBUG, false);
133    MORDOR_TEST_ASSERT_NOT_EQUAL(l->level(), Log::DEBUG);
134}