StormByte C++ Library: Logger module 1.2.0
StormByte-Logger is the logging module of the StormByte C++ 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 is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-Logger is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-Logger. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
23#include <StormByte/thread_lock.hxx>
24
25#include <cstddef>
26#include <memory>
27#include <span>
28#include <string_view>
29
34namespace StormByte::Logger {
48 public:
55 ThreadedLog(std::ostream& out, const Level& level = Level::Info, const std::string& format = "[%L] %T");
56
61 ThreadedLog(const ThreadedLog&) = default;
62
66 ThreadedLog(ThreadedLog&&) noexcept = default;
67
71 ~ThreadedLog() noexcept override = default;
72
78 ThreadedLog& operator=(const ThreadedLog&) = default;
79
84 ThreadedLog& operator=(ThreadedLog&&) noexcept = default;
85
93 Log& Color(const Level& level, const StormByte::Logger::Color& color) override;
94
100 StormByte::Logger::Color Color(const Level& level) const override;
101
109 Log& Color(const std::string& component, const Level& level, const StormByte::Logger::Color& color) override;
110
117 StormByte::Logger::Color Color(const std::string& component, const Level& level) const override;
118
124 Log& Format(const std::string& format) override;
125
130 const std::string& Format() const override;
131
138 Log& Format(const std::string& component, const std::string& format) override;
139
145 const std::string& Format(const std::string& component) const override;
146
152 Log& Throttle(const ThrottleSpec& spec) override;
153
159 Log& NoThrottle(const ThrottleSpec& spec) override;
160
167 Log& Throttle(double rate, std::size_t burst) override;
168
178 Log& Throttle(double rate, std::size_t burst, ThrottlePolicy policy, std::size_t value, std::size_t period = 0) override;
179
187 Log& Throttle(const Level& level, double rate, std::size_t burst) override;
188
196 Log& Throttle(GroupManip group, double rate, std::size_t burst) override;
197
205 Log& Throttle(ComponentManip component, double rate, std::size_t burst) override;
206
219 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;
220
225 Log& NoThrottle() override;
226
232 Log& NoThrottle(const Level& level) override;
233
239 Log& NoThrottle(GroupManip group) override;
240
246 Log& NoThrottle(ComponentManip component) override;
247
255 Log& NoThrottle(ComponentManip component, const Level& level, GroupManip group) override;
256
261 Log& FlushThrottle() override;
262
268 Log& FlushThrottle(const ThrottleSpec& spec) override;
269
275 inline Log& operator<<(bool v) {
276 if (!WillWrite()) [[likely]] return *this;
277 Write(v);
278 return *this;
279 }
280 inline Log& operator<<(char v) {
281 if (!WillWrite()) [[likely]] return *this;
282 Write(v);
283 return *this;
284 }
285 inline Log& operator<<(signed char v) {
286 if (!WillWrite()) [[likely]] return *this;
287 Write(v);
288 return *this;
289 }
290 inline Log& operator<<(unsigned char v) {
291 if (!WillWrite()) [[likely]] return *this;
292 Write(v);
293 return *this;
294 }
295 inline Log& operator<<(short v) {
296 if (!WillWrite()) [[likely]] return *this;
297 Write(v);
298 return *this;
299 }
300 inline Log& operator<<(unsigned short v) {
301 if (!WillWrite()) [[likely]] return *this;
302 Write(v);
303 return *this;
304 }
305 inline Log& operator<<(int v) {
306 if (!WillWrite()) [[likely]] return *this;
307 Write(v);
308 return *this;
309 }
310 inline Log& operator<<(unsigned int v) {
311 if (!WillWrite()) [[likely]] return *this;
312 Write(v);
313 return *this;
314 }
315 inline Log& operator<<(long v) {
316 if (!WillWrite()) [[likely]] return *this;
317 Write(v);
318 return *this;
319 }
320 inline Log& operator<<(unsigned long v) {
321 if (!WillWrite()) [[likely]] return *this;
322 Write(v);
323 return *this;
324 }
325 inline Log& operator<<(long long v) {
326 if (!WillWrite()) [[likely]] return *this;
327 Write(v);
328 return *this;
329 }
330 inline Log& operator<<(unsigned long long v) {
331 if (!WillWrite()) [[likely]] return *this;
332 Write(v);
333 return *this;
334 }
335 inline Log& operator<<(float v) {
336 if (!WillWrite()) [[likely]] return *this;
337 Write(v);
338 return *this;
339 }
340 inline Log& operator<<(double v) {
341 if (!WillWrite()) [[likely]] return *this;
342 Write(v);
343 return *this;
344 }
345 inline Log& operator<<(long double v) {
346 if (!WillWrite()) [[likely]] return *this;
347 Write(v);
348 return *this;
349 }
355 inline Log& operator<<(std::string_view v) {
356 if (!WillWrite()) [[likely]] return *this;
357 Write(v);
358 return *this;
359 }
360 inline Log& operator<<(const char* v) {
361 if (!WillWrite()) [[likely]] return *this;
362 Write(v);
363 return *this;
364 }
370 inline Log& operator<<(std::wstring_view v) {
371 if (!WillWrite()) [[likely]] return *this;
372 Write(v);
373 return *this;
374 }
375 inline Log& operator<<(const wchar_t* v) {
376 if (!WillWrite()) [[likely]] return *this;
377 Write(v);
378 return *this;
379 }
385 inline Log& operator<<(std::span<const std::byte> v) {
386 if (!WillWrite()) [[likely]] return *this;
387 Write(v);
388 return *this;
389 }
390 inline Log& operator<<(const Level& level) {
391 Write(level);
392 return *this;
393 }
394 inline Log& operator<<(std::ostream& (*manip)(std::ostream&)) {
395 Write(manip);
396 return *this;
397 }
398 inline Log& operator<<(Log& (*manip)(Log&) noexcept) {
399 Write(manip);
400 return *this;
401 }
408 Write(m);
409 return *this;
410 }
417 Write(m);
418 return *this;
419 }
426 Write(m);
427 return *this;
428 }
434 inline Log& operator<<(ColorManip manip) {
435 Write(manip);
436 return *this;
437 }
443 inline Log& operator<<(NoColorManip manip) {
444 Write(manip);
445 return *this;
446 }
452 inline Log& operator<<(FormatManip manip) {
453 Write(manip);
454 return *this;
455 }
462 Write(manip);
463 return *this;
464 }
470 inline Log& operator<<(GroupManip manip) {
471 Write(manip);
472 return *this;
473 }
480 Write(manip);
481 return *this;
482 }
489 Write(manip);
490 return *this;
491 }
498 Write(manip);
499 return *this;
500 }
502
503 protected:
508 PointerType Clone() const override;
509
515 PointerType Move() override;
516
517 private:
518 std::shared_ptr<ThreadLock> m_lock;
519
525 void Write(bool v) override;
526 void Write(char v) override;
527 void Write(signed char v) override;
528 void Write(unsigned char v) override;
529 void Write(short v) override;
530 void Write(unsigned short v) override;
531 void Write(int v) override;
532 void Write(unsigned int v) override;
533 void Write(long v) override;
534 void Write(unsigned long v) override;
535 void Write(long long v) override;
536 void Write(unsigned long long v) override;
537 void Write(float v) override;
538 void Write(double v) override;
539 void Write(long double v) override;
540 void Write(std::string_view v) override;
541 void Write(const char* v) override;
542 void Write(std::wstring_view v) override;
543 void Write(const wchar_t* v) override;
548 void Write(std::span<const std::byte> v) override;
549 void Write(const Level& level) override;
550 void Write(std::ostream& (*manip)(std::ostream&)) override;
551 void Write(Log& (*manip)(Log&) noexcept) override;
556 void Write(RedactManip m) override;
561 void Write(HexManip m) override;
566 void Write(NoHexManip m) override;
571 void Write(ColorManip manip) override;
576 void Write(NoColorManip manip) override;
581 void Write(FormatManip manip) override;
586 void Write(PopFormatManip manip) override;
591 void Write(GroupManip manip) override;
596 void Write(ComponentManip manip) override;
601 void Write(PopComponentManip manip) override;
606 void Write(ResetComponentManip manip) override;
608 };
609}
Public streaming facade for the StormByte logger.
Definition log.hxx:65
StormByte::Clonable< Log, std::shared_ptr< Log > >::PointerType PointerType
Smart pointer returned by Clone, Move and Scope.
Definition log.hxx:75
Thread-safe logging facade.
Definition threaded_log.hxx:47
Log & operator<<(PopFormatManip manip)
Restore a saved format under the line lock.
Definition threaded_log.hxx:461
PointerType Move() override
Move this facade into a shared_ptr.
Log & operator<<(const Level &level)
Definition threaded_log.hxx:390
Log & operator<<(ColorManip manip)
Apply a configured or explicit content color.
Definition threaded_log.hxx:434
Log & operator<<(float v)
Definition threaded_log.hxx:335
Log & operator<<(long v)
Definition threaded_log.hxx:315
Log & operator<<(NoColorManip manip)
Disable color for subsequent content.
Definition threaded_log.hxx:443
Log & operator<<(const char *v)
Definition threaded_log.hxx:360
Log & operator<<(ComponentManip manip)
Push a component segment onto the current thread's stack.
Definition threaded_log.hxx:479
Log & operator<<(unsigned long long v)
Definition threaded_log.hxx:330
Log & operator<<(ResetComponentManip manip)
Clear the current thread's component stack.
Definition threaded_log.hxx:497
Log & operator<<(std::wstring_view v)
Stream wide text.
Definition threaded_log.hxx:370
Log & operator<<(std::ostream &(*manip)(std::ostream &))
Definition threaded_log.hxx:394
Log & operator<<(long long v)
Definition threaded_log.hxx:325
ThreadedLog(std::ostream &out, const Level &level=Level::Info, const std::string &format="[%L] %T")
Construct a ThreadedLog writing to out.
Log & operator<<(double v)
Definition threaded_log.hxx:340
Log & operator<<(std::string_view v)
Stream UTF-8 text.
Definition threaded_log.hxx:355
ThreadedLog(const ThreadedLog &)=default
Copy constructor.
Log & operator<<(int v)
Definition threaded_log.hxx:305
PointerType Clone() const override
Deep-copy this facade into a shared_ptr.
ThreadedLog(ThreadedLog &&) noexcept=default
Move constructor.
Log & operator<<(NoHexManip m)
Disable hex dumps and restore default payload formatting.
Definition threaded_log.hxx:425
Log & operator<<(FormatManip manip)
Save and activate a temporary format under the line lock.
Definition threaded_log.hxx:452
Log & operator<<(PopComponentManip manip)
Pop one component segment from the current thread's stack.
Definition threaded_log.hxx:488
Log & operator<<(unsigned int v)
Definition threaded_log.hxx:310
Log & operator<<(short v)
Definition threaded_log.hxx:295
Log & operator<<(unsigned long v)
Definition threaded_log.hxx:320
Log & operator<<(const wchar_t *v)
Definition threaded_log.hxx:375
Log & operator<<(char v)
Definition threaded_log.hxx:280
Log & operator<<(Log &(*manip)(Log &) noexcept)
Definition threaded_log.hxx:398
Log & operator<<(unsigned short v)
Definition threaded_log.hxx:300
Log & operator<<(RedactManip m)
Apply redaction policy.
Definition threaded_log.hxx:407
Log & operator<<(std::span< const std::byte > v)
Stream raw bytes.
Definition threaded_log.hxx:385
Log & operator<<(HexManip m)
Dump subsequent payloads as hex bytes until nohex.
Definition threaded_log.hxx:416
Log & operator<<(GroupManip manip)
Set the producer group for the current line under the line lock.
Definition threaded_log.hxx:470
Log & operator<<(long double v)
Definition threaded_log.hxx:345
Log & operator<<(unsigned char v)
Definition threaded_log.hxx:290
Log & operator<<(signed char v)
Definition threaded_log.hxx:285
Logger module of the StormByte suite.
ThrottlePolicy
Count policy applied before the optional rate bucket.
Definition manipulators.hxx:40
Level
Severity levels used by the logger.
Definition typedefs.hxx:66
Color
ANSI foreground colors supported by the logger.
Definition typedefs.hxx:37
Temporarily selects a configured or explicit content color.
Definition manipulators.hxx:159
Selects the sticky component for the current thread.
Definition manipulators.hxx:88
Temporarily replaces the logger format and saves the previous one.
Definition manipulators.hxx:133
Labels the current logging line with a producer group.
Definition manipulators.hxx:69
Dump subsequent payloads as hex bytes until nohex.
Definition manipulators.hxx:254
Disables color for subsequent content until changed.
Definition manipulators.hxx:181
Disable hex dumps and restore default payload formatting.
Definition manipulators.hxx:276
Pops one segment from the current thread's component stack.
Definition manipulators.hxx:121
Restores the most recently saved logger format.
Definition manipulators.hxx:141
Stateful redaction manipulator.
Definition manipulators.hxx:206
Clears the component associated with the current thread.
Definition manipulators.hxx:107
Immutable rule description used by Log::Throttle.
Definition manipulators.hxx:53
#define STORMBYTE_LOGGER_PUBLIC
Definition visibility.h:28