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 , flags_{os_.flags()}
49 {
50 os_.set_storage(os_.make_storage());
51 }
53 {
54 const auto size{os_.size()};
55 write_log(ts_, level_, os_.release_storage(), size);
56 // Restore saved flags.
57 os_.flags(flags_);
58 }
59
60 // Copy.
61 Log(const Log&) = delete;
62 Log& operator=(const Log&) = delete;
63
64 // Move.
65 Log(Log&&) = delete;
66 Log& operator=(Log&&) = delete;
67
68 constexpr explicit operator bool() const { return true; }
70 Log& operator()(const char* data, std::streamsize size)
71 {
72 os_.write(data, size);
73 return *this;
74 }
76 Log& operator()() noexcept { return *this; }
77
78 private:
79 const WallTime ts_;
80 const LogLevel level_;
81 LogStream& os_;
82 const std::ios::fmtflags flags_;
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:70
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:76
OStream uses a dynamic storage acquired from the custom allocator.
Definition Stream.hpp:98
void set_storage(StoragePtr< MaxN > storage) noexcept
Update the internal storage and clear i/o state.
Definition Stream.hpp:130
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:180
WallClock::time_point WallTime
Definition Time.hpp:111
LogStream & log_stream() noexcept
Definition Log.cpp:26
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
constexpr auto bind() noexcept
Definition Slot.hpp:92