Flow 1.0.0
Flow project: Full implementation reference.
buffer_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 m_config(config),
29 m_os_writer(*m_config, m_os.os())
30{
31 // Nothing else.
32}
33
34bool Buffer_logger::should_log(Sev sev, const Component& component) const // Virtual.
35{
36 return m_config->output_whether_should_log(sev, component);
37}
38
39bool Buffer_logger::logs_asynchronously() const // Virtual.
40{
41 return false;
42}
43
45{
46 // Prevent simultaneous logging, reading-by-copy.
48
49 // m_os_writer wraps (as of this writing) String_ostream, which wraps std::string. Write msg+metadata to std::string.
50 m_os_writer.log(*metadata, msg);
51}
52
53const std::string& Buffer_logger::buffer_str() const
54{
55 return m_os.str(); // (Mutex lock wouldn't help here, even if we used it, as this returns [basically] a pointer.)
56}
57
58const std::string Buffer_logger::buffer_str_copy() const
59{
60 // Prevent simultaneous logging, reading.
62
63 return buffer_str(); // Copy occurs here; then mutex is unlocked.
64}
65
66} // namespace flow::log
util::String_ostream m_os
Like ostringstream but allows for fast access directly into its internal string buffer.
const std::string & buffer_str() const
Read-only access to the buffer string containing the messages logged thus far.
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...
Buffer_logger(Config *config)
Constructs logger to subsequently log to a newly constructed internal std::string buffer.
Ostream_log_msg_writer m_os_writer
Wrapper around m_os that will take care of prefacing each message with time stamp,...
util::Mutex_non_recursive m_log_mutex
Mutex protecting against log messages being logged, accessing m_os concurrently and thus corrupting (...
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...
bool logs_asynchronously() const override
Implements interface method by returning false, indicating that this Logger will not need the content...
const std::string buffer_str_copy() const
Returns a copy of buffer_str() in thread-safe fashion.
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
void log(const Msg_metadata &metadata, util::String_view msg)
Logs to the wrapped ostream the given message and associated metadata like severity and time stamp; p...
const std::string & str() const
Read-only access to the string being wrapped.
Flow module providing logging functionality.
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
Definition: log_fwd.hpp:224
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