Flow 2.0.0
Flow project: Full implementation reference.
log.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#include "flow/log/log.hpp"
20#include "flow/error/error.hpp"
22
23namespace flow::log
24{
25
26// Global initializations.
27
28boost::thread_specific_ptr<Msg_metadata> this_thread_sync_msg_metadata_ptr;
29
30// Static initializations.
31
32boost::thread_specific_ptr<std::string> Logger::s_this_thread_nickname_ptr;
33
34// Logger implementations.
35
37 bool also_set_os_name) // Static.
38{
39 using std::string;
40 using boost::system::system_category;
41 using ::pthread_setname_np;
42 using ::pthread_self;
43
44 /* Either set or delete (0 means no nickname, which is the original state of things).
45 * Note: This value is saved in a thread-local fashion. This has no effect on the
46 * value of s_this_thread_nickname.get() or dereference thereof in any thread except
47 * the one in which we currently execute. */
48 s_this_thread_nickname_ptr.reset(thread_nickname.empty()
49 ? nullptr
50 : new string{thread_nickname});
51
52 // Log about it if given an object capable of logging about itself.
53 if (logger_ptr)
54 {
55 FLOW_LOG_SET_CONTEXT(logger_ptr, Flow_log_component::S_LOG);
56 FLOW_LOG_INFO("Set new thread nickname for current thread ID "
57 "[" << util::this_thread::get_id() << "].");
58 }
59
60 if (also_set_os_name)
61 {
62#ifndef FLOW_OS_LINUX
63 static_assert(false, "this_thread_set_logged_nickname() also_set_os_name implementation is for Linux only "
64 "for now.");
65 /* `man pthread_setname_np` works in Darwin/Mac but is very short, and there is no `man pthread_getname_np`.
66 * It might work fine for Mac, but it's untested, and I (ygoldfel) didn't want to deal with it yet.
67 * In particular it's unclear if the MAX_PTHREAD_NAME_SZ would apply (it's not in `man` for Mac)... etc.
68 * @todo Look into it. */
69#endif
70 string os_name = thread_nickname.empty() ? util::ostream_op_string(util::this_thread::get_id())
71 : string(thread_nickname);
72
73 // See `man pthread_setname_np`. There is a hard limit on the length of the name, and it is:
74 constexpr size_t MAX_PTHREAD_NAME_SZ = 15;
75 if (os_name.size() > MAX_PTHREAD_NAME_SZ)
76 {
77 // As advertised: Truncate. `man` indicates not doing so shall lead to ERANGE error.
78 os_name.erase(MAX_PTHREAD_NAME_SZ);
79 }
80
81 const auto result_code = pthread_setname_np(pthread_self(), os_name.c_str());
82
83 // Log about it if given an object capable of logging about itself.
84 if (logger_ptr)
85 {
86 FLOW_LOG_SET_CONTEXT(logger_ptr, Flow_log_component::S_LOG);
87 if (result_code == -1)
88 {
89 const Error_code sys_err_code{errno, system_category()};
90 FLOW_LOG_WARNING("Unable to set OS thread name to [" << os_name << "], possibly truncated "
91 "to [" << MAX_PTHREAD_NAME_SZ << "] characters, via pthread_setname_np(). "
92 "This should only occur due to an overlong name, which we guard against, so this is "
93 "highly unexpected. Details follow.");
95 }
96 else
97 {
98 FLOW_LOG_INFO("OS thread name has been set to [" << os_name << "], possibly truncated "
99 "to [" << MAX_PTHREAD_NAME_SZ << "] characters.");
100 }
101 }
102
103 // We could continue, but it's indicative of a newly-broken API or misunderstanding of `man`: better to be alerted.
104 assert(result_code != -1);
105 } // if (also_set_os_name)
106} // Logger::this_thread_set_logged_nickname()
107
108std::ostream& Logger::this_thread_logged_name_os_manip(std::ostream& os) // Static.
109{
110 // Reminder: we are an ostream manipulator, invoked like flush or endl: os << flush;
111
112 /* If there's a thread nickname, output that. Otherwise default to actual thread ID.
113 * ATTN: This must be consistent with behavior in set_thread_info*().
114 * We could also just use the latter here, but the following is a bit quicker. @todo Reconsider maybe. */
115
116 auto const this_thread_nickname_ptr = s_this_thread_nickname_ptr.get();
117 if (this_thread_nickname_ptr)
118 {
119 return os << (*this_thread_nickname_ptr);
120 }
121 // else
122 return os << util::this_thread::get_id();
123}
124
125void Logger::set_thread_info(std::string* call_thread_nickname, flow::util::Thread_id* call_thread_id) // Static.
126{
127 assert(call_thread_nickname);
128 assert(call_thread_id);
129
130 /* If there's a thread nickname, output that. Otherwise default to actual thread ID.
131 * ATTN: This must be consistent with behavior in this_thread_logged_name_os_manip(). */
132
133 auto const this_thread_nickname_ptr = s_this_thread_nickname_ptr.get();
134 if (this_thread_nickname_ptr)
135 {
136 *call_thread_nickname = (*this_thread_nickname_ptr);
137 }
138 else
139 {
140 *call_thread_id = util::this_thread::get_id();
141 }
142}
143
145{
146 assert(msg_metadata);
147 set_thread_info(&msg_metadata->m_call_thread_nickname, &msg_metadata->m_call_thread_id);
148}
149
150std::ostream* Logger::this_thread_ostream() const
151{
152 // Grab the stream used for the current thread by this particular Logger.
154}
155
156// Component implementations.
157
159 m_payload_type_or_null(nullptr), // <=> empty() == true.
160 m_payload_enum_raw_value() // Should not be necessary (uninit=OK), but in some contexts at least gcc-9 warns.
161{
162 // That's it.
163}
164
165Component::Component(const Component& src) = default;
166Component::Component(Component&& src_moved) = default; // Note it doesn't empty()-ify src_moved.
167Component& Component::operator=(const Component& src) = default;
168Component& Component::operator=(Component&& src_moved) = default; // Note it doesn't empty()-ify src_moved.
169
171{
173}
174
175const std::type_info& Component::payload_type() const
176{
177 assert(!empty()); // We advertised undefined behavior in this case.
179}
180
182{
183 assert(!empty()); // We advertised undefined behavior in this case.
185}
186
187// Log_context implementations.
188
190 m_logger(logger)
191{
192 // Nothing.
193}
194
195Log_context::Log_context(const Log_context& src) = default;
196
198{
199 operator=(std::move(src));
200}
201
202Log_context& Log_context::operator=(const Log_context& src) = default;
203
205{
206 if (&src != this)
207 {
208 operator=(static_cast<const Log_context&>(src));
209 src.m_logger = nullptr;
210 src.m_component = {};
211 }
212 return *this;
213}
214
216{
217 return m_logger;
218}
219
221{
222 m_logger = logger;
223 return logger;
224}
225
227{
228 return m_component;
229}
230
232{
233 using std::swap;
234
235 swap(m_logger, other.m_logger);
237}
238
239void swap(Log_context& val1, Log_context& val2)
240{
241 val1.swap(val2);
242}
243
244// Log_context_mt implementations.
245
247 Log_context(logger)
248{
249 // Nothing.
250}
251
253 Log_context() // Eliminate possible warning at tiny (if any) perf cost.
254{
255 // We could just do `operator=(src)`; but to avoid unnecessary locking of this->m_mutex do it manually.
256 util::Lock_guard<decltype(m_mutex)> lock{src.m_mutex};
258}
259
261 Log_context() // Clear *this in preparation for swap.
262{
263 using std::swap; // This enables proper ADL.
264
265 // We could just do `operator=(move(src))`; but to avoid unnecessary locking of this->m_mutex do it manually.
266 util::Lock_guard<decltype(m_mutex)> lock{src.m_mutex};
267 swap(static_cast<Log_context&>(*this),
268 static_cast<Log_context&>(src));
269}
270
272{
273 using Lock = util::Lock_guard<decltype(m_mutex)>;
274
275 if (&src != this)
276 {
277 /* Naively we'd do something close to:
278 * Lock lock1{m_mutex};
279 * Lock lock2{src.m_mutex};
280 * Log_context::operator=(src);
281 * However conceivably this could cause an obscure deadlock for reasons similar to those cited in swap(). As there:
282 * Seems there's no choice but to lock things piecewise and execute the copy-assignment via a temporary
283 * intermediary Log_context. */
284
285 Log_context obj_tmp;
286 {
287 Lock lock{src.m_mutex};
288 obj_tmp = src;
289 }
290 {
291 Lock lock{m_mutex};
292 Log_context::operator=(obj_tmp);
293 }
294 }
295 return *this;
296}
297
299{
300 using Lock = util::Lock_guard<decltype(m_mutex)>;
301 using std::swap; // This enables proper ADL.
302
303 if (&src != this)
304 {
305 // Same deal as in copy ctor; just have to add the clearing of `src` which we do by using swap(L_c&, L_c&).
306
307 Log_context obj_tmp;
308 {
309 Lock lock{src.m_mutex};
310 swap(obj_tmp, static_cast<Log_context&>(src));
311 }
312 {
313 Lock lock{m_mutex};
314 Log_context::operator=(obj_tmp);
315 }
316 }
317
318 return *this;
319} // Log_context_mt::operator=(&&)
320
322{
323 util::Lock_guard<decltype(m_mutex)> lock{m_mutex};
325}
326
328{
329 util::Lock_guard<decltype(m_mutex)> lock{m_mutex};
330 return Log_context::set_logger(logger);
331}
332
334{
335 util::Lock_guard<decltype(m_mutex)> lock{m_mutex};
337}
338
340{
341 using Lock = util::Lock_guard<decltype(m_mutex)>;
342 using std::swap; // This enables proper ADL.
343
344 /* Naively we'd do something close to:
345 * Lock lock1{m_mutex};
346 * Lock lock2{other.m_mutex};
347 * Log_context::swap(other);
348 * However conceivably this could cause an obscure deadlock; e.g. at least if one concurrently tries
349 * lc_mt1.swap(lc_mt2);
350 * and
351 * lc_mt2.swap(lc_mt1);
352 * Strange thing to do, but it is legal, and a classic AB-BA deadlock results.
353 * Seems there's no choice but to lock things in series and use a temporary intermediary.
354 * (We could've also let the default std::swap() just do one move-construct and two move-assignments, but
355 * perf-wise that'd do some unnecessary stuff.) */
356
357 auto& obj1_mt = *this;
358 auto& obj2_mt = other;
359 auto& obj1_rw = static_cast<Log_context&>(obj1_mt);
360 auto& obj2_rw = static_cast<Log_context&>(obj2_mt);
361
362 Log_context tmp_rw;
363 {
364 Lock lock{obj1_mt.m_mutex};
365 tmp_rw = obj1_rw;
366 }
367 {
368 Lock lock{obj2_mt.m_mutex};
369 swap(tmp_rw, obj2_rw);
370 }
371 {
372 Lock lock{obj1_mt.m_mutex};
373 obj1_rw = tmp_rw;
374 }
375} // Log_context_mt::swap()
376
378{
379 val1.swap(val2);
380}
381
382// Sev implementations.
383
384std::ostream& operator<<(std::ostream& os, Sev val)
385{
386 // Note: Must satisfy istream_to_enum() requirements.
387 switch (val)
388 {
389 case Sev::S_NONE: return os << "NONE";
390 case Sev::S_FATAL: return os << "FATAL";
391 case Sev::S_ERROR: return os << "ERROR";
392 case Sev::S_WARNING: return os << "WARNING";
393 case Sev::S_INFO: return os << "INFO";
394 case Sev::S_DEBUG: return os << "DEBUG";
395 case Sev::S_TRACE: return os << "TRACE";
396 case Sev::S_DATA: return os << "DATA";
397 case Sev::S_END_SENTINEL: assert(false && "Should not be printing sentinel.");
398 }
399
400 assert(false && "Looks like a corrupt/sentinel log::Sev value. gcc would've caught an incomplete switch().");
401 return os;
402}
403
404std::istream& operator>>(std::istream& is, Sev& val)
405{
406 // Range [NONE, END_SENTINEL); no match => NONE; allow for number instead of ostream<< string; case-insensitive.
408 return is;
409}
410
411// Free function implementations.
412
414{
416
417 if (logger_ptr)
418 {
420 }
421}
422
423size_t deep_size(const Msg_metadata& val)
424{
425 // We're following the loose pattern explained at the end of Async_file_logger::mem_cost().
426
427 using util::deep_size;
428
429 /* Reminder: exclude sizeof(val); include only non-shallow memory used on val's behalf; so
430 * sum of deep_size(X), for each X that is (1) a member of Msg_metadata; and (2) has a deep_size(X) free function.
431 * As of this writing there is just one: */
433}
434
435} // namespace flow::log
A light-weight class, each object storing a component payload encoding an enum value from enum type o...
Definition: log.hpp:842
Component & operator=(const Component &src)
Overwrites *this with a copy of src.
unsigned int enum_raw_t
The type Payload must be enum class Payload : enum_raw_t: an enum type encoded via this integer type.
Definition: log.hpp:847
std::type_info const * m_payload_type_or_null
The typeid() of the Payload passed to the 1-arg constructor; if 0-arg ctor was used (empty() is true)...
Definition: log.hpp:1019
const std::type_info & payload_type() const
Returns typeid(Payload), where Payload was the template param used when calling the originating one-a...
Definition: log.cpp:175
bool empty() const
Returns true if *this is as if default-constructed (a null Component); false if as if constructed via...
Definition: log.cpp:170
enum_raw_t m_payload_enum_raw_value
The internally stored integer representation of the enum value passed to the 1-arg constructor; meani...
Definition: log.hpp:1025
enum_raw_t payload_enum_raw_value() const
Returns the numeric value of the enum payload stored by this Component, originating in the one-arg co...
Definition: log.cpp:181
Component()
Constructs a Component that stores no payload; meaning an unspecified-component Component that return...
Definition: log.cpp:158
Identical to Log_context but is safe w/r/t to set_logger(), assignment, and swap() done concurrently ...
Definition: log.hpp:1774
void swap(Log_context_mt &other)
Identical to Log_context API; but safe against concurrent operations on a *this.
Definition: log.cpp:339
Log_context_mt & operator=(const Log_context_mt &src)
Identical to Log_context API; but safe against concurrent operations on a *this.
Definition: log.cpp:271
util::Mutex_non_recursive m_mutex
Protects access to data in static_cast<Log_context&>(*this) (especially Log_context::m_logger).
Definition: log.hpp:1861
Log_context_mt(Logger *logger=nullptr)
Identical to Log_context API.
Definition: log.cpp:246
const Component & get_log_component() const
Identical to Log_context API.
Definition: log.cpp:333
Logger * get_logger() const
Identical to Log_context API; but safe against concurrent operations on a *this.
Definition: log.cpp:321
Logger * set_logger(Logger *logger)
Identical to Log_context API; but safe against concurrent operations on a *this.
Definition: log.cpp:327
Convenience class that simply stores a Logger and/or Component passed into a constructor; and returns...
Definition: log.hpp:1638
Log_context & operator=(const Log_context &src)
Assignment operator that behaves similarly to the copy constructor.
const Component & get_log_component() const
Returns reference to the stored Component object, particularly as many FLOW_LOG_*() macros expect.
Definition: log.cpp:226
Log_context(Logger *logger=nullptr)
Constructs Log_context by storing the given pointer to a Logger and a null Component.
Definition: log.cpp:189
Logger * set_logger(Logger *logger)
Sets the value to be returned by the next get_logger() call; returns get_logger() from before the cha...
Definition: log.cpp:220
void swap(Log_context &other)
Swaps Logger pointers and Component objects held by *this and other.
Definition: log.cpp:231
Component m_component
The held Component object. Making the object non-const to allow operator=() to work.
Definition: log.hpp:1753
Logger * m_logger
The held Logger pointer. Making the pointer itself non-const to allow operator=() to work.
Definition: log.hpp:1751
Logger * get_logger() const
Returns the stored Logger pointer, particularly as many FLOW_LOG_*() macros expect.
Definition: log.cpp:215
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1286
static void set_thread_info_in_msg_metadata(Msg_metadata *msg_metadata)
Loads msg_metadata->m_call_thread_nickname (if set) or else msg_metadata->m_call_thread_id,...
Definition: log.cpp:144
static boost::thread_specific_ptr< std::string > s_this_thread_nickname_ptr
Thread-local storage for each thread's logged name (null pointer, which is default,...
Definition: log.hpp:1554
static std::ostream & this_thread_logged_name_os_manip(std::ostream &os)
ostream manipulator function that, if output via operator<< to an ostream, will cause the current thr...
Definition: log.cpp:108
static void this_thread_set_logged_nickname(util::String_view thread_nickname={}, Logger *logger_ptr=nullptr, bool also_set_os_name=true)
Sets or unsets the current thread's logging-worthy string name; optionally sets the OS thread name (s...
Definition: log.cpp:36
std::ostream * this_thread_ostream() const
Returns the stream dedicated to the executing thread and this Logger, so that the caller can apply st...
Definition: log.cpp:150
static void set_thread_info(std::string *call_thread_nickname, util::Thread_id *call_thread_id)
Same as set_thread_info_in_msg_metadata() but targets the given two variables as opposed to a Msg_met...
Definition: log.cpp:125
static Thread_local_string_appender * get_this_thread_string_appender(const util::Unique_id_holder &source_obj_id)
Returns a pointer to the exactly one Thread_local_string_appender object that is accessible from the ...
std::ostream * appender_ostream()
Same as fresh_appender_ostream() but does not clear the result string, enabling piecemeal writing to ...
#define FLOW_ERROR_SYS_ERROR_LOG_WARNING()
Logs a warning about the (often errno-based or from a library) error code in sys_err_code.
Definition: error.hpp:269
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Definition: log.hpp:197
#define FLOW_LOG_WARNING(ARG_stream_fragment)
Logs a WARNING message into flow::log::Logger *get_logger() with flow::log::Component get_log_compone...
Definition: log.hpp:152
#define FLOW_LOG_SET_CONTEXT(ARG_logger_ptr, ARG_component_payload)
For the rest of the block within which this macro is instantiated, causes all FLOW_LOG_....
Definition: log.hpp:405
Flow module providing logging functionality.
std::ostream & operator<<(std::ostream &os, Sev val)
Serializes a log::Sev to a standard output stream.
Definition: log.cpp:384
void swap(Log_context &val1, Log_context &val2)
Log_context ADL-friendly swap: Equivalent to val1.swap(val2).
Definition: log.cpp:239
size_t deep_size(const Msg_metadata &val)
Estimate of memory footprint of the given value, including memory allocated on its behalf – but exclu...
Definition: log.cpp:423
std::istream & operator>>(std::istream &is, Sev &val)
Deserializes a log::Sev from a standard input stream.
Definition: log.cpp:404
void swap(Log_context_mt &val1, Log_context_mt &val2)
Log_context_mt ADL-friendly swap: Equivalent to val1.swap(val2).
Definition: log.cpp:377
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
Definition: log_fwd.hpp:225
@ S_DATA
Message satisfies Sev::S_TRACE description AND contains variable-length structure (like packet,...
@ S_TRACE
Message indicates any condition that may occur with great frequency (thus verbose if logged).
@ S_WARNING
Message indicates a "bad" condition that is not frequent enough to be of severity Sev::S_TRACE.
@ S_END_SENTINEL
Not an actual value but rather stores the highest numerical payload, useful for validity checks.
@ S_NONE
Sentinel log level that must not be specified for any actual message (at risk of undefined behavior s...
@ S_DEBUG
Message indicates a condition with, perhaps, no significant perf impact if enabled (like Sev::S_INFO)...
@ S_ERROR
Message indicates a "bad" condition with "worse" impact than that of Sev::S_WARNING.
@ S_INFO
Message indicates a not-"bad" condition that is not frequent enough to be of severity Sev::S_TRACE.
@ S_FATAL
Message indicates a "fatally bad" condition, such that the program shall imminently abort,...
void beautify_chrono_logger_this_thread(Logger *logger_ptr)
Sets certain chrono-related formatting on the given Logger in the current thread that results in a co...
Definition: log.cpp:413
boost::thread_specific_ptr< Msg_metadata > this_thread_sync_msg_metadata_ptr
Thread-local Msg_metadata object used by FLOW_LOG_WITHOUT_CHECKING() for an alleged perf bonus in the...
Definition: log.cpp:28
void beautify_chrono_ostream(std::ostream *os_ptr)
Sets certain chrono-related formatting on the given ostream that results in a consistent,...
Definition: util.cpp:173
Thread::id Thread_id
Short-hand for an OS-provided ID of a util::Thread.
Definition: util_fwd.hpp:102
Enum istream_to_enum(std::istream *is_ptr, Enum enum_default, Enum enum_sentinel, bool accept_num_encoding, bool case_sensitive, Enum enum_lowest)
Deserializes an enum class value from a standard input stream.
Definition: util.hpp:522
size_t deep_size(const std::string &val)
Estimate of memory footprint of the given value, including memory allocated on its behalf – but exclu...
Definition: util.cpp:186
boost::unique_lock< Mutex > Lock_guard
Short-hand for advanced-capability RAII lock guard for any mutex, ensuring exclusive ownership of tha...
Definition: util_fwd.hpp:277
std::string ostream_op_string(T &&... ostream_args)
Equivalent to ostream_op_to_string() but returns a new string by value instead of writing to the call...
Definition: util.hpp:381
Basic_string_view< char > String_view
Commonly used char-based Basic_string_view. See its doc header.
boost::system::error_code Error_code
Short-hand for a boost.system error code (which basically encapsulates an integer/enum error code and...
Definition: common.hpp:497
Simple data store containing all of the information generated at every logging call site by flow::log...
Definition: log.hpp:1043
util::Thread_id m_call_thread_id
Thread ID of the thread from which the log call was invoked; or a default-constructed (no-thread) suc...
Definition: log.hpp:1139
std::string m_call_thread_nickname
Thread nickname, as for Logger::this_thread_set_logged_nickname(), of the thread from which the log c...
Definition: log.hpp:1129