20#include "flow/error/error.hpp"
30static_assert(std::is_trivially_destructible_v<Msg_metadata>,
31 "We want this fully available even while thread_local deinit is happening, for FLOW_LOG_DO_LOG() et al.");
36static_assert(std::is_trivially_destructible_v<Fixed_string>,
37 "We want this fully available even while thread_local deinit is happening, for FLOW_LOG_DO_LOG() et al.");
42 bool also_set_os_name)
45 using boost::system::system_category;
46 using ::pthread_setname_np;
55 const auto thread_nickname_maybe_trunc = thread_nickname.substr(0, Fixed_string::static_capacity);
64 FLOW_LOG_INFO(
"Set new thread nickname for current thread ID [" << util::this_thread::get_id() <<
"] / "
76 static_assert(
false,
"this_thread_set_logged_nickname() also_set_os_name implementation is for Linux only "
84 : string(thread_nickname);
87 constexpr size_t MAX_PTHREAD_NAME_SZ = 15;
88 if (os_name.size() > MAX_PTHREAD_NAME_SZ)
91 os_name.erase(MAX_PTHREAD_NAME_SZ);
94 const auto result_code = pthread_setname_np(pthread_self(), os_name.c_str());
100 if (result_code == -1)
102 const Error_code sys_err_code{errno, system_category()};
103 FLOW_LOG_WARNING(
"Unable to set OS thread name to [" << os_name <<
"], possibly truncated "
104 "to [" << MAX_PTHREAD_NAME_SZ <<
"] characters, via pthread_setname_np(). "
105 "This should only occur due to an overlong name, which we guard against, so this is "
106 "highly unexpected. Details follow.");
111 FLOW_LOG_INFO(
"OS thread name has been set to [" << os_name <<
"], possibly truncated "
112 "to [" << MAX_PTHREAD_NAME_SZ <<
"] characters.");
117 assert(result_code != -1);
134 return os << util::this_thread::get_id();
139 assert(call_thread_nickname);
140 assert(call_thread_id);
147 *call_thread_id = util::this_thread::get_id();
157 assert(call_thread_nickname);
158 assert(call_thread_id);
163 *call_thread_id = util::this_thread::get_id();
173 assert(call_thread_nickname);
174 assert(call_thread_id);
179 *call_thread_id = util::this_thread::get_id();
190 assert(msg_metadata);
198 return appender_or_null ? appender_or_null->appender_ostream() :
nullptr;
204 m_payload_type_or_null(nullptr),
205 m_payload_enum_raw_value()
254 src.m_logger =
nullptr;
255 src.m_component = {};
355 Lock lock{src.m_mutex};
391 auto& obj1_mt = *
this;
392 auto& obj2_mt = other;
393 auto& obj1_rw =
static_cast<Log_context&
>(obj1_mt);
394 auto& obj2_rw =
static_cast<Log_context&
>(obj2_mt);
398 Lock lock{obj1_mt.m_mutex};
402 Lock lock{obj2_mt.m_mutex};
403 swap(tmp_rw, obj2_rw);
406 Lock lock{obj1_mt.m_mutex};
434 assert(
false &&
"Looks like a corrupt/sentinel log::Sev value. gcc would've caught an incomplete switch().");
A light-weight class, each object storing a component payload encoding an enum value from enum type o...
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.
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)...
const std::type_info & payload_type() const
Returns typeid(Payload), where Payload was the template param used when calling the originating one-a...
bool empty() const
Returns true if *this is as if default-constructed (a null Component); false if as if constructed via...
enum_raw_t m_payload_enum_raw_value
The internally stored integer representation of the enum value passed to the 1-arg constructor; meani...
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...
Component()
Constructs a Component that stores no payload; meaning an unspecified-component Component that return...
Similar mission to Log_context but enables one to safely log under the possibility of a concurrent se...
void swap(Log_context_mt &other)
Identical to Log_context API; but safe against concurrent operations on a *this.
Log_context_mt & operator=(const Log_context_mt &src)
Identical to Log_context API; but safe against concurrent operations on a *this.
util::Mutex_non_recursive m_mutex
Protects access to data in static_cast<Log_context&>(*this) (especially Log_context::m_logger).
Log_context_mt(Logger *logger=nullptr)
Identical to Log_context API.
Logger * set_logger(Logger *logger)
Identical to Log_context API; but safe against concurrent operations on a *this.
Convenience class that simply stores a Logger and/or Component passed into a constructor; and returns...
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.
Log_context(Logger *logger=nullptr)
Constructs Log_context by storing the given pointer to a Logger and a null Component.
Logger * set_logger(Logger *logger)
Sets the value to be returned by the next get_logger() call; returns get_logger() from before the cha...
void swap(Log_context &other)
Swaps Logger pointers and Component objects held by *this and other.
Component m_component
The held Component object. Making the object non-const to allow operator=() to work.
Logger * m_logger
The held Logger pointer. Making the pointer itself non-const to allow operator=() to work.
Logger * get_logger() const
Returns the stored Logger pointer, particularly as many FLOW_LOG_*() macros expect.
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
static void set_thread_info_in_msg_metadata(Msg_metadata *msg_metadata)
Sets msg_metadata->m_call_thread_nickname (if set) or else msg_metadata->m_call_thread_id,...
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...
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...
static thread_local Fixed_string s_this_thread_nickname
Thread-local storage for each thread's logged name (empty, which is default, means no nickname stored...
std::ostream * this_thread_ostream() const
Returns the stream dedicated to the executing thread and this Logger, so that the caller can apply st...
static void set_thread_info(Fixed_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...
static Thread_local_string_appender * 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 ...
#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.
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
#define FLOW_LOG_WARNING(ARG_stream_fragment)
Logs a WARNING message into flow::log::Logger *get_logger() with flow::log::Component get_log_compone...
#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_....
Flow module providing logging functionality.
std::ostream & operator<<(std::ostream &os, Sev val)
Serializes a log::Sev to a standard output stream.
void swap(Log_context &val1, Log_context &val2)
Log_context ADL-friendly swap: Equivalent to val1.swap(val2).
size_t deep_size(const Msg_metadata &)
Estimate of memory footprint of the given value, including memory allocated on its behalf – but exclu...
std::istream & operator>>(std::istream &is, Sev &val)
Deserializes a log::Sev from a standard input stream.
void swap(Log_context_mt &val1, Log_context_mt &val2)
Log_context_mt ADL-friendly swap: Equivalent to val1.swap(val2).
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
@ 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,...
thread_local Msg_metadata this_thread_sync_msg_metadata
Thread-local Msg_metadata object used by FLOW_LOG_WITHOUT_CHECKING() for an alleged perf bonus in the...
boost::static_string< 128 > Fixed_string
Alias for a heap-free, array-backed, trivial-destructor-having string used for certain simple strings...
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...
void beautify_chrono_ostream(std::ostream *os_ptr)
Sets certain chrono-related formatting on the given ostream that results in a consistent,...
Thread::id Thread_id
Short-hand for an OS-provided ID of a util::Thread.
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.
boost::unique_lock< Mutex > Lock_guard
Short-hand for advanced-capability RAII lock guard for any mutex, ensuring exclusive ownership of tha...
Thread_token this_thread_unique_token()
Returns a value conceptually similar to this_thread::get_id() – a thread ID – but that is never recyc...
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...
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...