Bus Messages 1.0
Bus Message Serialization Library
Loading...
Searching...
No Matches
buslogstream.h
Go to the documentation of this file.
1/*
2* Copyright 2025 Ingemar Hedvall
3* SPDX-License-Identifier: MIT
4*/
5
12
13#pragma once
14
15#include <cstdint>
16#include <string_view>
17#include <string>
18#include <atomic>
19#include <sstream>
20#include <source_location>
21#include <functional>
22
23
24namespace bus {
25
32enum class BusLogSeverity : uint8_t {
33 kTrace = 0,
34 kDebug = 1,
35 kInfo = 2,
36 kNotice = 3,
38 kError = 5,
40 kAlert = 7,
42};
43
45static std::string_view BusLogServerityToText(BusLogSeverity severity);
46
48#define BUS_TRACE() BusLogStream(std::source_location::current(), BusLogSeverity::kTrace)
49
51#define BUS_DEBUG() BusLogStream(std::source_location::current(), BusLogSeverity::kDebug)
52
54#define BUS_INFO() BusLogStream(std::source_location::current(), BusLogSeverity::kInfo)
55
57#define BUS_NOTICE() BusLogStream(std::source_location::current(), BusLogSeverity::kNotice)
58
60#define BUS_WARNING() BusLogStream(std::source_location::current(), BusLogSeverity::kWarning)
61
63#define BUS_ERROR() BusLogStream(std::source_location::current(), BusLogSeverity::kError)
64
66#define BUS_CRITICAL() BusLogStream(std::source_location::current(), BusLogSeverity::kCritical)
67
69#define BUS_ALERT() BusLogStream(std::source_location::current(), BusLogSeverity::kAlert)
70
72#define BUS_EMERGENCY() BusLogStream(std::source_location::current(), BusLogSeverity::kEmergency)
73
82class BusLogStream : public std::ostringstream {
83public:
84 BusLogStream() = delete;
92BusLogStream(std::source_location location, BusLogSeverity severity);
93 ~BusLogStream() override;
94
102 static std::function<void(const std::source_location& location,
103 BusLogSeverity severity,
104 const std::string& text)> UserLogFunction;
105
114 static uint64_t ErrorCount() { return error_count_; }
115
117 static void ResetErrorCount() { error_count_ = 0;}
118
120 static void BusConsoleLogFunction(const std::source_location& location,
121 BusLogSeverity severity, const std::string& text);
123 static void BusNoLogFunction(const std::source_location& location,
124 BusLogSeverity severity, const std::string& text);
125private:
126 BusLogSeverity severity_;
127 std::source_location location_;
128 static std::atomic<uint64_t> error_count_;
129
130 static void LogString(const std::source_location& location,
131 BusLogSeverity severity,
132 const std::string& text);
133};
134
135} // bus
136
137
static void BusConsoleLogFunction(const std::source_location &location, BusLogSeverity severity, const std::string &text)
Simple function that sends all logs to the std::cout.
BusLogStream(std::source_location location, BusLogSeverity severity)
Constructor that is a simple wrapper around an outpout stream.
static void BusNoLogFunction(const std::source_location &location, BusLogSeverity severity, const std::string &text)
Simple function that doesn't do anything.
static void ResetErrorCount()
Resets the error counter.
Definition buslogstream.h:117
static uint64_t ErrorCount()
Returns number of error messages.
Definition buslogstream.h:114
static std::function< void(const std::source_location &location, BusLogSeverity severity, const std::string &text)> UserLogFunction
The end-user should supply a function that redirect the logs.
Definition buslogstream.h:104
Main namespace for the MDF library.
Definition buslogstream.h:24
BusLogSeverity
Defines the log severity level.
Definition buslogstream.h:32
@ kInfo
Informational message.
Definition buslogstream.h:35
@ kEmergency
Fatal error message.
Definition buslogstream.h:41
@ kCritical
Critical message (device error)
Definition buslogstream.h:39
@ kNotice
Notice message. Notify the user.
Definition buslogstream.h:36
@ kDebug
Debug message.
Definition buslogstream.h:34
@ kAlert
Alert or alarm message.
Definition buslogstream.h:40
@ kError
Error message.
Definition buslogstream.h:38
@ kWarning
Warning message.
Definition buslogstream.h:37
@ kTrace
Trace or listen message.
Definition buslogstream.h:33