Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Log.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Log.hpp"
18
20
21#include <boost/test/unit_test.hpp>
22
23#include <cstring>
24
25using namespace std;
26using namespace toolbox;
27
28namespace {
29template <typename T, typename U>
30struct Foo {
31 T first;
32 U second;
33};
34
35template <typename StreamT>
36 requires Streamable<StreamT>
38{
39 os << '(' << val.first << ',' << val.second << ')';
40 return os;
41}
42
43struct TestLogger final : Logger {
44 void do_write_log(WallTime /*ts*/, LogLevel level, int /*tid*/, LogMsgPtr&& msg,
45 size_t size, bool warming_fake) noexcept override
46 {
47 const auto finally = make_finally([&]() noexcept {
48 log_buf_pool().bounded_push(std::move(msg));
49 });
50 if (!warming_fake) {
51 last_level = level;
52 last_msg.assign(static_cast<const char*>(msg.get()), size);
53 }
54 }
55 LogLevel last_level{};
56 string last_msg{};
57};
58
59} // namespace
60
62
64{
65 //NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
67 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::None), "NONE"), 0);
68 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Crit), "CRIT"), 0);
69 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Error), "ERROR"), 0);
70 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Warn), "WARN"), 0);
71 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Metric), "METRIC"), 0);
72 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Notice), "NOTICE"), 0);
73 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Info), "INFO"), 0);
74 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Debug), "DEBUG"), 0);
75 //NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
76 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel{99}), "DEBUG"), 0);
77}
78
80{
81 TestLogger tl;
82
83 auto prev_level = set_log_level(LogLevel::Info);
84 auto& prev_logger = set_logger(tl);
85 // clang-format off
86 const auto finally = make_finally([prev_level, &prev_logger]() noexcept {
89 });
90 // clang-format on
91
92 TOOLBOX_LOG(LogLevel::Info) << "test1: " << Foo<int, int>{10, 20};
93 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
94 BOOST_CHECK_EQUAL(tl.last_msg, "test1: (10,20)");
95
96 TOOLBOX_CRIT << "test2: " << Foo<int, int>{10, 20};
97 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Crit);
98 BOOST_CHECK_EQUAL(tl.last_msg, "test2: (10,20)");
99
100 TOOLBOX_ERROR << "test3: " << Foo<int, int>{10, 20};
101 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Error);
102 BOOST_CHECK_EQUAL(tl.last_msg, "test3: (10,20)");
103
104 TOOLBOX_WARN << "test4: " << Foo<int, int>{10, 20};
105 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Warn);
106 BOOST_CHECK_EQUAL(tl.last_msg, "test4: (10,20)");
107
108 TOOLBOX_METRIC << "test5: " << Foo<int, int>{10, 20};
109 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Metric);
110 BOOST_CHECK_EQUAL(tl.last_msg, "test5: (10,20)");
111
112 TOOLBOX_NOTICE << "test6: " << Foo<int, int>{10, 20};
113 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Notice);
114 BOOST_CHECK_EQUAL(tl.last_msg, "test6: (10,20)");
115
116 TOOLBOX_INFO << "test7: " << Foo<int, int>{10, 20};
117 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
118 BOOST_CHECK_EQUAL(tl.last_msg, "test7: (10,20)");
119
120 // This should not be logged.
121 TOOLBOX_DEBUG << "test8: " << Foo<int, int>{10, 20};
122 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
123 BOOST_CHECK_EQUAL(tl.last_msg, "test7: (10,20)");
124}
125
#define TOOLBOX_METRIC
Definition Log.hpp:95
#define TOOLBOX_DEBUG
Definition Log.hpp:102
#define TOOLBOX_CRIT
Definition Log.hpp:92
#define TOOLBOX_WARN
Definition Log.hpp:94
#define TOOLBOX_NOTICE
Definition Log.hpp:96
#define TOOLBOX_INFO
Definition Log.hpp:97
#define TOOLBOX_ERROR
Definition Log.hpp:93
#define TOOLBOX_LOG(LEVEL)
Definition Log.hpp:89
BOOST_AUTO_TEST_CASE(LogLabelCase)
Definition Log.ut.cpp:63
BOOST_CHECK_EQUAL(v.size(), 10U)
STL namespace.
StreamT & operator<<(StreamT &os, PutPercentiles pp)
Definition Utility.hpp:68
const char * log_label(LogLevel level) noexcept
Return log label for given log level.
Definition Logger.cpp:217
Logger & set_logger(Logger &logger) noexcept
Set logger globally for all threads.
Definition Logger.cpp:237
WallClock::time_point WallTime
Definition Time.hpp:112
StoragePtr< MaxLogLine > LogMsgPtr
Definition Logger.hpp:54
LogBufPool & log_buf_pool() noexcept
A pool of log buffers, eliminating the need for dynamic memory allocations when logging.
Definition Logger.cpp:187
LogLevel set_log_level(LogLevel level) noexcept
Set log level globally for all threads.
Definition Logger.cpp:227
auto make_finally(FnT fn) noexcept
Definition Finally.hpp:48
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
constexpr auto bind() noexcept
Definition Slot.hpp:97