StormByte C++ Library: Logger module 1.2.0
StormByte-Logger is the logging module of the StormByte C++ suite.
Loading...
Searching...
No Matches
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
22#include <StormByte/clonable.hxx>
25#include <StormByte/type_traits.hxx>
26
27#include <cstddef>
28#include <memory>
29#include <ostream>
30#include <span>
31#include <string>
32#include <string_view>
33
38namespace StormByte::Logger {
39 class Implementation;
40
65 class STORMBYTE_LOGGER_PUBLIC Log : protected StormByte::Clonable<Log, std::shared_ptr<Log>> {
69 friend STORMBYTE_LOGGER_PUBLIC Log& noredact(Log& log) noexcept;
70
71 public:
75 using PointerType = StormByte::Clonable<Log, std::shared_ptr<Log>>::PointerType;
76
83 Log(std::ostream& out, const Level& level = Level::Info, const std::string& format = "[%L] %T");
84
89 Log(const Log&) = default;
90
94 Log(Log&&) noexcept = default;
95
99 ~Log() noexcept override = default;
100
106 Log& operator=(const Log&) = default;
107
112 Log& operator=(Log&&) noexcept = default;
113
121 PointerType Scope(std::string path);
122
129 bool Enabled(const Level& level) const noexcept;
130
138 virtual Log& Color(const Level& level, const StormByte::Logger::Color& color);
139
145 virtual StormByte::Logger::Color Color(const Level& level) const;
146
154 virtual Log& Color(const std::string& component, const Level& level, const StormByte::Logger::Color& color);
155
162 virtual StormByte::Logger::Color Color(const std::string& component, const Level& level) const;
163
169 virtual Log& Format(const std::string& format);
170
175 virtual const std::string& Format() const;
176
183 virtual Log& Format(const std::string& component, const std::string& format);
184
190 virtual const std::string& Format(const std::string& component) const;
191
197 virtual Log& Throttle(const ThrottleSpec& spec);
198
204 virtual Log& NoThrottle(const ThrottleSpec& spec);
205
213 virtual Log& Throttle(double rate, std::size_t burst);
214
225 virtual Log& Throttle(double rate, std::size_t burst, ThrottlePolicy policy, std::size_t value, std::size_t period = 0);
226
235 virtual Log& Throttle(const Level& level, double rate, std::size_t burst);
236
245 virtual Log& Throttle(GroupManip group, double rate, std::size_t burst);
246
254 virtual Log& Throttle(ComponentManip component, double rate, std::size_t burst);
255
268 virtual 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);
269
274 virtual Log& NoThrottle();
275
282 virtual Log& NoThrottle(const Level& level);
283
290 virtual Log& NoThrottle(GroupManip group);
291
297 virtual Log& NoThrottle(ComponentManip component);
298
306 virtual Log& NoThrottle(ComponentManip component, const Level& level, GroupManip group);
307
312 virtual Log& FlushThrottle();
313
319 virtual Log& FlushThrottle(const ThrottleSpec& spec);
320
328 inline Log& operator<<(bool v) {
329 if (!WillWrite()) [[likely]] return *this;
330 Write(v);
331 return *this;
332 }
333 inline Log& operator<<(char v) {
334 if (!WillWrite()) [[likely]] return *this;
335 Write(v);
336 return *this;
337 }
338 inline Log& operator<<(signed char v) {
339 if (!WillWrite()) [[likely]] return *this;
340 Write(v);
341 return *this;
342 }
343 inline Log& operator<<(unsigned char v) {
344 if (!WillWrite()) [[likely]] return *this;
345 Write(v);
346 return *this;
347 }
348 inline Log& operator<<(short v) {
349 if (!WillWrite()) [[likely]] return *this;
350 Write(v);
351 return *this;
352 }
353 inline Log& operator<<(unsigned short v) {
354 if (!WillWrite()) [[likely]] return *this;
355 Write(v);
356 return *this;
357 }
358 inline Log& operator<<(int v) {
359 if (!WillWrite()) [[likely]] return *this;
360 Write(v);
361 return *this;
362 }
363 inline Log& operator<<(unsigned int v) {
364 if (!WillWrite()) [[likely]] return *this;
365 Write(v);
366 return *this;
367 }
368 inline Log& operator<<(long v) {
369 if (!WillWrite()) [[likely]] return *this;
370 Write(v);
371 return *this;
372 }
373 inline Log& operator<<(unsigned long v) {
374 if (!WillWrite()) [[likely]] return *this;
375 Write(v);
376 return *this;
377 }
378 inline Log& operator<<(long long v) {
379 if (!WillWrite()) [[likely]] return *this;
380 Write(v);
381 return *this;
382 }
383 inline Log& operator<<(unsigned long long v) {
384 if (!WillWrite()) [[likely]] return *this;
385 Write(v);
386 return *this;
387 }
388 inline Log& operator<<(float v) {
389 if (!WillWrite()) [[likely]] return *this;
390 Write(v);
391 return *this;
392 }
393 inline Log& operator<<(double v) {
394 if (!WillWrite()) [[likely]] return *this;
395 Write(v);
396 return *this;
397 }
398 inline Log& operator<<(long double v) {
399 if (!WillWrite()) [[likely]] return *this;
400 Write(v);
401 return *this;
402 }
408 inline Log& operator<<(std::string_view v) {
409 if (!WillWrite()) [[likely]] return *this;
410 Write(v);
411 return *this;
412 }
413 inline Log& operator<<(const char* v) {
414 if (!WillWrite()) [[likely]] return *this;
415 Write(v);
416 return *this;
417 }
423 inline Log& operator<<(std::wstring_view v) {
424 if (!WillWrite()) [[likely]] return *this;
425 Write(v);
426 return *this;
427 }
428 inline Log& operator<<(const wchar_t* v) {
429 if (!WillWrite()) [[likely]] return *this;
430 Write(v);
431 return *this;
432 }
439 inline Log& operator<<(std::span<const std::byte> v) {
440 if (!WillWrite()) [[likely]] return *this;
441 Write(v);
442 return *this;
443 }
444 inline Log& operator<<(const Level& level) {
445 Write(level);
446 return *this;
447 }
448 inline Log& operator<<(std::ostream& (*manip)(std::ostream&)) {
449 Write(manip);
450 return *this;
451 }
452 inline Log& operator<<(Log& (*manip)(Log&) noexcept) {
453 Write(manip);
454 return *this;
455 }
462 Write(m);
463 return *this;
464 }
471 Write(m);
472 return *this;
473 }
480 Write(m);
481 return *this;
482 }
488 inline Log& operator<<(ColorManip manip) {
489 Write(manip);
490 return *this;
491 }
497 inline Log& operator<<(NoColorManip manip) {
498 Write(manip);
499 return *this;
500 }
506 inline Log& operator<<(FormatManip manip) {
507 Write(manip);
508 return *this;
509 }
516 Write(manip);
517 return *this;
518 }
524 inline Log& operator<<(GroupManip manip) {
525 Write(manip);
526 return *this;
527 }
534 Write(manip);
535 return *this;
536 }
543 Write(manip);
544 return *this;
545 }
552 Write(manip);
553 return *this;
554 }
556
557 protected:
558 std::shared_ptr<Implementation> m_impl;
559 std::string m_scope_path;
560
565 bool WillWrite() const noexcept;
566
571 bool PrepareLine();
572
577 bool HasOpenOutputLine() const noexcept;
578
583 bool LineDecided() const noexcept;
584
589 bool LineAdmitted() const noexcept;
590
595 PointerType Clone() const override;
596
601 PointerType Move() override;
602
608 virtual void Write(bool v);
609 virtual void Write(char v);
610 virtual void Write(signed char v);
611 virtual void Write(unsigned char v);
612 virtual void Write(short v);
613 virtual void Write(unsigned short v);
614 virtual void Write(int v);
615 virtual void Write(unsigned int v);
616 virtual void Write(long v);
617 virtual void Write(unsigned long v);
618 virtual void Write(long long v);
619 virtual void Write(unsigned long long v);
620 virtual void Write(float v);
621 virtual void Write(double v);
622 virtual void Write(long double v);
623 virtual void Write(std::string_view v);
624 virtual void Write(const char* v);
625 virtual void Write(std::wstring_view v);
626 virtual void Write(const wchar_t* v);
631 virtual void Write(std::span<const std::byte> v);
632 virtual void Write(const Level& level);
633 virtual void Write(std::ostream& (*manip)(std::ostream&));
634 virtual void Write(Log& (*manip)(Log&) noexcept);
639 virtual void Write(RedactManip m);
644 virtual void Write(HexManip m);
649 virtual void Write(NoHexManip m);
654 virtual void Write(ColorManip manip);
659 virtual void Write(NoColorManip manip);
664 virtual void Write(FormatManip manip);
669 virtual void Write(PopFormatManip manip);
674 virtual void Write(GroupManip manip);
679 virtual void Write(ComponentManip manip);
684 virtual void Write(PopComponentManip manip);
689 virtual void Write(ResetComponentManip manip);
691 };
692
701 template <typename Ptr, typename T>
702 Ptr& operator<<(Ptr& logger, const T& value) noexcept
703 requires StormByte::Type::DerivedFrom<typename std::remove_cvref_t<Ptr>::element_type, Log> {
704 if (logger)
705 *logger << value;
706 return logger;
707 }
708
716 template <typename Ptr>
717 Ptr& operator<<(Ptr& logger, const Level& level) noexcept
718 requires StormByte::Type::DerivedFrom<typename std::remove_cvref_t<Ptr>::element_type, Log> {
719 if (logger)
720 *logger << level;
721 return logger;
722 }
723
731 template <typename Ptr>
732 Ptr& operator<<(Ptr& logger, std::ostream& (*manip)(std::ostream&)) noexcept
733 requires StormByte::Type::DerivedFrom<typename std::remove_cvref_t<Ptr>::element_type, Log> {
734 if (logger)
735 *logger << manip;
736 return logger;
737 }
738}
Public streaming facade for the StormByte logger.
Definition log.hxx:65
Log & operator<<(unsigned long v)
Definition log.hxx:373
Log & operator<<(std::span< const std::byte > v)
Stream raw bytes.
Definition log.hxx:439
Log & operator<<(double v)
Definition log.hxx:393
friend Log & nohumanreadable(Log &log) noexcept
Disable human-readable formatting (raw numbers).
Log & operator<<(PopComponentManip manip)
Pop one component segment from the current thread's stack.
Definition log.hxx:542
Log & operator<<(short v)
Definition log.hxx:348
Log & operator<<(float v)
Definition log.hxx:388
Log & operator<<(RedactManip m)
Apply redaction policy (full or keep-last-N).
Definition log.hxx:461
Log & operator<<(FormatManip manip)
Save the current format and activate a temporary format.
Definition log.hxx:506
Log & operator<<(signed char v)
Definition log.hxx:338
Log & operator<<(unsigned char v)
Definition log.hxx:343
Log & operator<<(NoHexManip m)
Disable hex dumps and restore default payload formatting.
Definition log.hxx:479
Log & operator<<(ComponentManip manip)
Push a component segment onto the current thread's stack.
Definition log.hxx:533
Log & operator<<(unsigned short v)
Definition log.hxx:353
Log & operator<<(std::wstring_view v)
Stream wide text.
Definition log.hxx:423
Log & operator<<(ResetComponentManip manip)
Clear the current thread's component stack.
Definition log.hxx:551
friend Log & noredact(Log &log) noexcept
Disable redaction until the next redact / redact(n) / redact_first(n).
Log & operator<<(const char *v)
Definition log.hxx:413
Log & operator<<(unsigned long long v)
Definition log.hxx:383
Log & operator<<(long long v)
Definition log.hxx:378
Log & operator<<(unsigned int v)
Definition log.hxx:363
Log & operator<<(long v)
Definition log.hxx:368
std::string m_scope_path
Sticky component path; empty = root facade.
Definition log.hxx:559
Log & operator<<(int v)
Definition log.hxx:358
Log(Log &&) noexcept=default
Move constructor.
Log & operator<<(ColorManip manip)
Apply a configured or explicit content color.
Definition log.hxx:488
Log & operator<<(PopFormatManip manip)
Restore the most recently saved format.
Definition log.hxx:515
Log & operator<<(GroupManip manip)
Set the producer group for the current line.
Definition log.hxx:524
Log & operator<<(std::ostream &(*manip)(std::ostream &))
Definition log.hxx:448
Log & operator<<(const wchar_t *v)
Definition log.hxx:428
Log & operator<<(Log &(*manip)(Log &) noexcept)
Definition log.hxx:452
Log & operator<<(std::string_view v)
Stream UTF-8 text.
Definition log.hxx:408
friend Log & humanreadable_bytes(Log &log) noexcept
Enable human-readable formatting for byte counts.
bool WillWrite() const noexcept
Whether the current line level will be written.
StormByte::Clonable< Log, std::shared_ptr< Log > >::PointerType PointerType
Smart pointer returned by Clone, Move and Scope.
Definition log.hxx:75
Log & operator<<(HexManip m)
Dump subsequent payloads as hex bytes until nohex.
Definition log.hxx:470
Log(std::ostream &out, const Level &level=Level::Info, const std::string &format="[%L] %T")
Construct a Log writing to out.
Log & operator<<(NoColorManip manip)
Disable color for subsequent content.
Definition log.hxx:497
Log & operator<<(char v)
Definition log.hxx:333
Log(const Log &)=default
Copy constructor.
friend Log & humanreadable_number(Log &log) noexcept
Enable human-readable formatting for numeric values.
std::shared_ptr< Implementation > m_impl
Shared backend (copies of Log share it)
Definition log.hxx:558
Log & operator<<(long double v)
Definition log.hxx:398
Log & operator<<(const Level &level)
Definition log.hxx:444
Logger module of the StormByte suite.
Ptr & operator<<(Ptr &logger, const T &value) noexcept
Stream a value into a smart pointer to Log or a derived logger.
Definition log.hxx:702
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