StormByte-Logger 2.0.0
C++26 logger module of the StormByte suite
 
Loading...
Searching...
No Matches
threaded_log.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Logger.
5 *
6 * StormByte-Logger original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Logger source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte-String tree and
23 * the StormByte Base tree it vendors), which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Logger is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Logger. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
45
46#include <memory>
47#include <ostream>
48#include <span>
49#include <string_view>
50
55namespace StormByte::Logger {
71 public:
78 ThreadedLog(std::ostream& out, const Level& level = Level::Info, std::string_view format = "[%L] %T")
79 : ThreadedLog(&OStreamWrite, &OStreamManip, &out, level, format) {}
80
85 ThreadedLog(const ThreadedLog&) = default;
86
90 ThreadedLog(ThreadedLog&&) noexcept = default;
91
95 ~ThreadedLog() noexcept override;
96
102 ThreadedLog& operator=(const ThreadedLog&);
103
108 ThreadedLog& operator=(ThreadedLog&&) noexcept;
109
116 Log& Color(const Level& level, const StormByte::Logger::Color& color) override;
117
123 StormByte::Logger::Color Color(const Level& level) const override;
124
132 Log& Color(std::string_view component, const Level& level, const StormByte::Logger::Color& color) override;
133
140 StormByte::Logger::Color Color(std::string_view component, const Level& level) const override;
141
147 Log& Format(std::string_view format) override;
148
153 StormByte::String::String Format() const override;
154
161 Log& Format(std::string_view component, std::string_view format) override;
162
168 StormByte::String::String Format(std::string_view component) const override;
169
175 Log& Throttle(const ThrottleSpec& spec) override;
176
182 Log& NoThrottle(const ThrottleSpec& spec) override;
183
190 Log& Throttle(double rate, std::size_t burst) override;
191
201 Log& Throttle(double rate, std::size_t burst, ThrottlePolicy policy, std::size_t value, std::size_t period = 0) override;
202
210 Log& Throttle(const Level& level, double rate, std::size_t burst) override;
211
219 Log& Throttle(GroupManip group, double rate, std::size_t burst) override;
220
228 Log& Throttle(ComponentManip component, double rate, std::size_t burst) override;
229
242 Log& Throttle(ComponentManip component, const Level& level, GroupManip group, double rate, std::size_t burst, ThrottlePolicy policy = ThrottlePolicy::Drop, std::size_t value = 0, std::size_t period = 0) override;
243
248 Log& NoThrottle() override;
249
255 Log& NoThrottle(const Level& level) override;
256
262 Log& NoThrottle(GroupManip group) override;
263
269 Log& NoThrottle(ComponentManip component) override;
270
278 Log& NoThrottle(ComponentManip component, const Level& level, GroupManip group) override;
279
284 Log& FlushThrottle() override;
285
291 Log& FlushThrottle(const ThrottleSpec& spec) override;
292
293 protected:
298 PointerType Clone() const override;
299
304 PointerType Move() override;
305
310 bool BeginPayload() override;
311
316 void Write(std::wstring_view v) override;
317
322 void Write(const wchar_t* v) override;
323
328 void Write(std::span<const std::byte> v) override;
329
334 void Write(const Level& level) override;
335
340 void Write(std::ostream& (*manip)(std::ostream&)) override;
341
346 void Write(Log& (*manip)(Log&) noexcept) override;
347
352 void Write(RedactManip m) override;
353
358 void Write(HexManip m) override;
359
364 void Write(NoHexManip m) override;
365
370 void Write(ColorManip manip) override;
371
376 void Write(NoColorManip manip) override;
377
382 void Write(FormatManip manip) override;
383
388 void Write(PopFormatManip manip) override;
389
390 protected:
399 ThreadedLog(SinkWrite write, SinkManip manip, void* context, const Level& level, std::string_view format);
400
401 private:
402 std::shared_ptr<ThreadLock> m_lock;
403 };
404}
Public streaming facade for the StormByte logger.
Definition log.hxx:111
StormByte::Clonable< Log >::PointerType PointerType
Owner returned by Clone, Move and Scope.
Definition log.hxx:121
Thread-safe logging facade.
Definition threaded_log.hxx:70
ThreadedLog(std::ostream &out, const Level &level=Level::Info, std::string_view format="[%L] %T")
Construct a ThreadedLog writing to out.
Definition threaded_log.hxx:78
ThreadedLog(const ThreadedLog &)=default
Copy constructor.
ThreadedLog(ThreadedLog &&) noexcept=default
Move constructor.
Logger module of the StormByte suite.
void OStreamWrite(void *context, const char *data, std::size_t size)
Write raw bytes into an std::ostream owned by the caller.
Definition log.hxx:77
ThrottlePolicy
Count policy applied before the optional rate bucket.
Definition manipulators.hxx:62
void(*)(void *context, std::ostream &(*manip)(std::ostream &)) SinkManip
Caller-side application of an std::ostream manipulator.
Definition typedefs.hxx:128
Level
Severity levels used by the logger.
Definition typedefs.hxx:87
Color
ANSI foreground colors supported by the logger.
Definition typedefs.hxx:58
void(*)(void *context, const char *data, std::size_t size) SinkWrite
Caller-side write of raw bytes into the sink.
Definition typedefs.hxx:120
void OStreamManip(void *context, std::ostream &(*manip)(std::ostream &))
Apply an std::ostream manipulator in the caller's module.
Definition log.hxx:88
STL namespace.
Temporarily selects a configured or explicit content color.
Definition manipulators.hxx:208
Selects the sticky component for the current thread.
Definition manipulators.hxx:119
Temporarily replaces the logger format and saves the previous one.
Definition manipulators.hxx:173
Labels the current logging line with a producer group.
Definition manipulators.hxx:91
Dump subsequent payloads as hex bytes until nohex.
Definition manipulators.hxx:306
Disables color for subsequent content until changed.
Definition manipulators.hxx:230
Disable hex dumps and restore default payload formatting.
Definition manipulators.hxx:328
Restores the most recently saved logger format.
Definition manipulators.hxx:181
Stateful redaction manipulator.
Definition manipulators.hxx:255
Immutable rule description used by Log::Throttle.
Definition manipulators.hxx:75
#define STORMBYTE_LOGGER_PUBLIC
Definition visibility.h:53