Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Log.hpp
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#ifndef TOOLBOX_SYS_LOG_HPP
18#define TOOLBOX_SYS_LOG_HPP
19
21
23
24namespace toolbox {
25inline namespace sys {
26
29
33
34// Inspired by techniques developed by Rodrigo Fernandes.
36 template <typename ValueT>
38 {
39 log.os_ << std::forward<ValueT>(val);
40 return log;
41 }
42
43 public:
44 explicit Log(WallTime ts, LogLevel level) noexcept
45 : ts_{ts}
46 , level_{level}
47 , os_{log_stream()}
48 {
50 if (!log_buf_pool().pop(storage)) [[unlikely]] {
52 }
53 os_.set_storage(std::move(storage));
54 }
56 {
57 const auto size{os_.size()};
58 write_log(ts_, level_, os_.release_storage(), size);
59 }
60
61 // Copy.
62 Log(const Log&) = delete;
63 Log& operator=(const Log&) = delete;
64
65 // Move.
66 Log(Log&&) = delete;
67 Log& operator=(Log&&) = delete;
68
69 constexpr explicit operator bool() const { return true; }
71 Log& operator()(const char* data, std::streamsize size)
72 {
73 os_.write(data, size);
74 return *this;
75 }
77 Log& operator()() noexcept { return *this; }
78
79 private:
80 const WallTime ts_;
81 const LogLevel level_;
82 LogStream& os_;
83};
84
85} // namespace sys
86} // namespace toolbox
87
88// clang-format off
89#define TOOLBOX_LOG(LEVEL) \
90toolbox::is_log_level(LEVEL) && toolbox::Log{toolbox::WallClock::now(), LEVEL}()
91
92#define TOOLBOX_CRIT TOOLBOX_LOG(toolbox::LogLevel::Crit)
93#define TOOLBOX_ERROR TOOLBOX_LOG(toolbox::LogLevel::Error)
94#define TOOLBOX_WARN TOOLBOX_LOG(toolbox::LogLevel::Warn)
95#define TOOLBOX_METRIC TOOLBOX_LOG(toolbox::LogLevel::Metric)
96#define TOOLBOX_NOTICE TOOLBOX_LOG(toolbox::LogLevel::Notice)
97#define TOOLBOX_INFO TOOLBOX_LOG(toolbox::LogLevel::Info)
98
99#if TOOLBOX_BUILD_DEBUG
100#define TOOLBOX_DEBUG TOOLBOX_LOG(toolbox::LogLevel::Debug)
101#else
102#define TOOLBOX_DEBUG false && toolbox::Log{WallClock::now(), toolbox::LogLevel::Debug}()
103#endif
104// clang-format on
105
106#endif // TOOLBOX_SYS_LOG_HPP
#define TOOLBOX_API
Definition Config.h:39
Log(Log &&)=delete
Log(WallTime ts, LogLevel level) noexcept
Definition Log.hpp:44
Log & operator=(Log &&)=delete
Log & operator()(const char *data, std::streamsize size)
Function operator provided for writing unformatted data to the log.
Definition Log.hpp:71
friend Log & operator<<(Log &log, ValueT &&val)
Definition Log.hpp:37
Log & operator=(const Log &)=delete
Log(const Log &)=delete
Log & operator()() noexcept
Function operator provided for rvalue to lvalue conversion.
Definition Log.hpp:77
OStream uses a dynamic storage acquired from the custom allocator.
Definition Stream.hpp:38
static StoragePtr< MaxN > make_storage()
Definition Stream.hpp:64
const DataT & data(const MsgEvent &ev) noexcept
Definition Event.hpp:54
void write_log(WallTime ts, LogLevel level, LogMsgPtr &&msg, std::size_t size) noexcept
Definition Logger.cpp:242
WallClock::time_point WallTime
Definition Time.hpp:112
StoragePtr< MaxLogLine > LogMsgPtr
Definition Logger.hpp:54
LogStream & log_stream() noexcept
Definition Log.cpp:26
LogBufPool & log_buf_pool() noexcept
A pool of log buffers, eliminating the need for dynamic memory allocations when logging.
Definition Logger.cpp:187
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