Flow 1.0.2
Flow project: Full implementation reference.
simple_ostream_logger.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
20#include "flow/log/config.hpp"
21
22namespace flow::log
23{
24
25// Implementations.
26
28 std::ostream& os, std::ostream& os_for_err) :
29 m_config(config)
30{
31 /* If stream is same for both types of messages, might as well use only one Ostream_log_msg_writer for both.
32 * It's just prettier (e.g., stream state saved/restores 1x instead of 2x) and a little better for performance.
33 * However there could be 2+ Simple_ostream_loggers sharing the same ostream; nothing we can (reasonably) do to
34 * detect that -- nor NEED we worry about it really; just a nicety. */
37 = (&os == &os_for_err)
38 ? m_os_writers[0]
40 // See m_os_writers doc header for details about how they are freed at `*this` destruction.
41}
42
43bool Simple_ostream_logger::should_log(Sev sev, const Component& component) const // Virtual.
44{
45 return m_config->output_whether_should_log(sev, component);
46}
47
49{
50 return false;
51}
52
54{
55 assert(metadata);
56
57 /* Prevent simultaneous logging.
58 * (Consider also mutex per each of m_os_writers[] instead of sharing one. That would reduce lock contention.
59 * However, often m_os_writers[] would really write to the same device such as terminal console or log file.
60 * Thus, possibly this would cause interleaving of output in an undesirable way, which is basically what the mutex
61 * is designed to avoid in the first place. Maybe some buffering aspect would prevent interleaving in that case,
62 * but it seems imprudent to count on it, or at least to have to worry about it.)
63 *
64 * Oh, of course we could also invoke only one mutex if m_os_writers[] streams are the same object -- trivial.
65 * However, even if they are different, the user can then make them write to the same thing using 2>&1 on command
66 * line, for example. So it still seems best to avoid any interleaving even when it's further away from our control,
67 * as in that latter situation. So just don't mess with it and always use one mutex to avoid as much interleaving
68 * as we can. */
70
71 /* This next part will block calling thread for however long the writing takes.
72 * This is likely not an issue with cout/cerr but can be a significant issue with ofstream. Hence why
73 * Async_file_logger exists. */
74 m_os_writers[(metadata->m_msg_sev > Sev::S_WARNING) ? 0 : 1]->log(*metadata, msg);
75}
76
77} // namespace flow::log
A light-weight class, each object storing a component payload encoding an enum value from enum type o...
Definition: log.hpp:840
Class used to configure the filtering and logging behavior of Loggers; its use in your custom Loggers...
Definition: config.hpp:200
Utility class, each object of which wraps a given ostream and outputs discrete messages to it adorned...
bool logs_asynchronously() const override
Implements interface method by returning false, indicating that this Logger will not need the content...
boost::array< Ostream_log_msg_writer_ptr, 2 > m_os_writers
Stream writers via which to log messages, each element corresponding to a certain set of possible sev...
void do_log(Msg_metadata *metadata, util::String_view msg) override
Implements interface method by synchronously logging the message and some subset of the metadata in a...
Config *const m_config
Reference to the config object passed to constructor. Note that object is mutable; see notes on threa...
Simple_ostream_logger(Config *config, std::ostream &os=std::cout, std::ostream &os_for_err=std::cerr)
Constructs logger to subsequently log to the given standard ostream (or 2 thereof).
boost::shared_ptr< Ostream_log_msg_writer > Ostream_log_msg_writer_ptr
Short-hand for ref-counted pointer to a given Ostream_log_msg_writer. See m_os_writers for ref-count ...
bool should_log(Sev sev, const Component &component) const override
Implements interface method by returning true if the severity and component (which is allowed to be n...
util::Mutex_non_recursive m_log_mutex
Mutex protecting against log messages being logged concurrently and thus being garbled.
Flow module providing logging functionality.
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
Definition: log_fwd.hpp:224
@ S_WARNING
Message indicates a "bad" condition that is not frequent enough to be of severity Sev::S_TRACE.
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:265
Basic_string_view< char > String_view
Commonly used char-based Basic_string_view. See its doc header.
Simple data store containing all of the information generated at every logging call site by flow::log...
Definition: log.hpp:1048
Sev m_msg_sev
Severity of message, typically determined by choice of macro (e.g., FLOW_LOG_WARNING() vs.
Definition: log.hpp:1058