log_test.cc 4.45 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
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
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <log_test.h>
#include <embb/base/c/internal/unused.h>
#include <embb/base/log.h>

Marcus Winter committed
31
#include <string>
32

33 34 35 36 37 38 39 40 41
namespace embb {
namespace base {
namespace test {

LogTest::LogTest() {
  CreateUnit("Test all").Add(&LogTest::Test, this);
}

static void test_log_function(void * context, char const * msg) {
42 43
  std::string * logged_message = reinterpret_cast<std::string*>(context);
  *logged_message = msg;
44 45 46 47 48
}

void LogTest::Test() {
  using embb::base::Log;
  char const * test_msg = "hello";
49
  std::string logged_message;
50

51
  Log::SetLogFunction(&logged_message, test_log_function);
52 53

  Log::SetLogLevel(EMBB_LOG_LEVEL_TRACE);
Marcus Winter committed
54
  logged_message = "none";
55
  Log::Trace("chn", test_msg);
56
#ifdef EMBB_DEBUG
Marcus Winter committed
57
  PT_EXPECT(logged_message == "[chn] - [TRACE] hello");
58
#else
Marcus Winter committed
59
  PT_EXPECT(logged_message == "none");
60
#endif
Marcus Winter committed
61
  logged_message = "none";
62
  Log::Info("chn", test_msg);
63
#ifdef EMBB_DEBUG
Marcus Winter committed
64
  PT_EXPECT(logged_message == "[chn] - [INFO ] hello");
65
#else
Marcus Winter committed
66
  PT_EXPECT(logged_message == "none");
67
#endif
Marcus Winter committed
68
  logged_message = "none";
69
  Log::Warning("chn", test_msg);
Marcus Winter committed
70 71
  PT_EXPECT(logged_message == "[chn] - [WARN ] hello");
  logged_message = "none";
72
  Log::Error("chn", test_msg);
Marcus Winter committed
73
  PT_EXPECT(logged_message == "[chn] - [ERROR] hello");
74 75

  Log::SetLogLevel(EMBB_LOG_LEVEL_INFO);
Marcus Winter committed
76
  logged_message = "none";
77
  Log::Trace("chn", test_msg);
Marcus Winter committed
78 79
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
80
  Log::Info("chn", test_msg);
81
#ifdef EMBB_DEBUG
Marcus Winter committed
82
  PT_EXPECT(logged_message == "[chn] - [INFO ] hello");
83
#else
Marcus Winter committed
84
  PT_EXPECT(logged_message == "none");
85
#endif
Marcus Winter committed
86
  logged_message = "none";
87
  Log::Warning("chn", test_msg);
Marcus Winter committed
88 89
  PT_EXPECT(logged_message == "[chn] - [WARN ] hello");
  logged_message = "none";
90
  Log::Error("chn", test_msg);
Marcus Winter committed
91
  PT_EXPECT(logged_message == "[chn] - [ERROR] hello");
92 93

  Log::SetLogLevel(EMBB_LOG_LEVEL_WARNING);
Marcus Winter committed
94
  logged_message = "none";
95
  Log::Trace("chn", test_msg);
Marcus Winter committed
96 97
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
98
  Log::Info("chn", test_msg);
Marcus Winter committed
99 100
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
101
  Log::Warning("chn", test_msg);
Marcus Winter committed
102 103
  PT_EXPECT(logged_message == "[chn] - [WARN ] hello");
  logged_message = "none";
104
  Log::Error("chn", test_msg);
Marcus Winter committed
105
  PT_EXPECT(logged_message == "[chn] - [ERROR] hello");
106 107

  Log::SetLogLevel(EMBB_LOG_LEVEL_ERROR);
Marcus Winter committed
108
  logged_message = "none";
109
  Log::Trace("chn", test_msg);
Marcus Winter committed
110 111
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
112
  Log::Info("chn", test_msg);
Marcus Winter committed
113 114
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
115
  Log::Warning("chn", test_msg);
Marcus Winter committed
116 117
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
118
  Log::Error("chn", test_msg);
Marcus Winter committed
119
  PT_EXPECT(logged_message == "[chn] - [ERROR] hello");
120 121

  Log::SetLogLevel(EMBB_LOG_LEVEL_NONE);
Marcus Winter committed
122
  logged_message = "none";
123
  Log::Trace("chn", test_msg);
Marcus Winter committed
124 125
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
126
  Log::Info("chn", test_msg);
Marcus Winter committed
127 128
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
129
  Log::Warning("chn", test_msg);
Marcus Winter committed
130 131
  PT_EXPECT(logged_message == "none");
  logged_message = "none";
132
  Log::Error("chn", test_msg);
Marcus Winter committed
133
  PT_EXPECT(logged_message == "none");
134 135 136 137 138 139 140 141
}

} // namespace test
} // namespace base
} // namespace embb