Flow 1.0.2
Flow project: Full implementation reference.
serial_file_logger.hpp
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#pragma once
20
23#include "flow/log/log.hpp"
24
25namespace flow::log
26{
27
28// Types.
29
30/**
31 * An internal-use implementation of Logger that logs messages to a given file-system path, blocking the calling
32 * thread while the I/O occurs, and usable safely only if logging occurs non-concurrently. As of this writing it
33 * is essentially an internal component needed by Async_file_logger; but it is a full-fledged Logger nevertheless.
34 *
35 * ### Thread safety ###
36 * The file I/O-using APIs, notably do_log() and log_flush_and_reopen() but not should_log(), are unsafe to call
37 * concurrently. The logging user -- as of this writing Async_file_logger but it's fully conceivable other uses
38 * exist -- must therefore provide any anti-concurrency measures (use one thread; a strand; mutex; etc.).
39 *
40 * See thread safety notes and to-dos regarding #m_config in Simple_ostream_logger doc header. These apply here also.
41 *
42 * @todo Consider having Serial_file_logger internally buffer any attempted log requests that it couldn't write to
43 * file due to I/O error. The logic already attempts re-open repeatedly but doesn't attempt to re-log failed
44 * lines.
45 */
47 public Logger,
48 protected Log_context
49{
50public:
51 // Constructors/destructor.
52
53 /**
54 * Constructs logger to subsequently log to the given file-system path. It will append. This constructor
55 * itself does not perform any I/O operations.
56 *
57 * @param config
58 * See Async_file_logger ctor.
59 * @param log_path
60 * See Async_file_logger ctor.
61 * @param backup_logger_ptr
62 * See Async_file_logger ctor.
63 */
64 explicit Serial_file_logger(Logger* backup_logger_ptr,
65 Config* config, const fs::path& log_path);
66
67 /// Flushes out anything buffered, returns resources/closes output file(s); then returns.
68 ~Serial_file_logger() override;
69
70 // Methods.
71
72 /**
73 * See Async_file_logger::should_log().
74 *
75 * @param sev
76 * See Async_file_logger::should_log().
77 * @param component
78 * See Async_file_logger::should_log().
79 * @return See Async_file_logger::should_log().
80 */
81 bool should_log(Sev sev, const Component& component) const override;
82
83 /**
84 * Implements interface method by returning `false`, indicating that this Logger will not need the contents of
85 * `*metadata` and `msg` passed to do_log() after that method returns.
86 *
87 * @return See above.
88 */
89 bool logs_asynchronously() const override;
90
91 /**
92 * Implements interface method by synchronously logging the message and some subset of the metadata in a fashion
93 * controlled by #m_config.
94 *
95 * @param metadata
96 * All information to potentially log in addition to `msg`.
97 * @param msg
98 * The message.
99 */
100 void do_log(Msg_metadata* metadata, util::String_view msg) override;
101
102 /**
103 * Causes the log at the file-system path to be flushed/closed (if needed) and
104 * re-opened; this will occur synchronously meaning it will complete before the method returns.
105 */
107
108 // Data. (Public!)
109
110 /// See Async_file_logger::m_config.
112
113private:
114 // Data.
115
116 /// File-system path to which to write subsequently.
117 const fs::path m_log_path;
118
119 /// The file to which to write. Because only the worker thread ever accesses it, no mutex is needed.
120 fs::ofstream m_ofs;
121
122 /**
123 * Stream writer via which to log messages to #m_ofs. A `m_ofs_writer.log()` call synchronously writes to
124 * #m_ofs and flushes it to the output device (file). Same thread safety situation as #m_ofs (only worker thread
125 * access it).
126 */
128
129 /**
130 * Starts at `false`, becomes `true` at entry to log_flush_and_reopen(), then becomes `false` again.
131 * Simple anti-recursion measure.
132 */
134}; // class Serial_file_logger
135
136} // 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
Convenience class that simply stores a Logger and/or Component passed into a constructor; and returns...
Definition: log.hpp:1619
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1291
Utility class, each object of which wraps a given ostream and outputs discrete messages to it adorned...
An internal-use implementation of Logger that logs messages to a given file-system path,...
bool m_reopening
Starts at false, becomes true at entry to log_flush_and_reopen(), then becomes false again.
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...
bool logs_asynchronously() const override
Implements interface method by returning false, indicating that this Logger will not need the content...
~Serial_file_logger() override
Flushes out anything buffered, returns resources/closes output file(s); then returns.
void log_flush_and_reopen()
Causes the log at the file-system path to be flushed/closed (if needed) and re-opened; this will occu...
Config *const m_config
See Async_file_logger::m_config.
Ostream_log_msg_writer m_ofs_writer
Stream writer via which to log messages to m_ofs.
Serial_file_logger(Logger *backup_logger_ptr, Config *config, const fs::path &log_path)
Constructs logger to subsequently log to the given file-system path.
const fs::path m_log_path
File-system path to which to write subsequently.
fs::ofstream m_ofs
The file to which to write. Because only the worker thread ever accesses it, no mutex is needed.
bool should_log(Sev sev, const Component &component) const override
See Async_file_logger::should_log().
Flow module providing logging functionality.
Sev
Enumeration containing one of several message severity levels, ordered from highest to lowest.
Definition: log_fwd.hpp:224
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