SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
Debug.hxx
Go to the documentation of this file.
1/*
2 Copyright (c) 2014, 2025 Matthew H. Reilly (kb1vc)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#pragma once
30
31#include <string>
32#include <mutex>
33#include <SoDa/Format.hxx>
34
35namespace SoDa {
39 class Debug {
40 public:
41 Debug(std::string _unit_name = std::string("UNKNOWN")) {
42 unit_name = _unit_name;
44 }
45
46 Debug(const char * _unit_name_cstr) {
47 unit_name = std::string(_unit_name_cstr);
49 }
50
51 Debug(unsigned int _debug_level, std::string _unit_name = std::string("UNKNOWN")) {
52 unit_name = _unit_name;
53 debug_level = _debug_level;
54 }
55
56 Debug(unsigned int _debug_level, const char * _unit_name_cstr) {
57 unit_name = std::string(_unit_name_cstr);
58 debug_level = _debug_level;
59 }
60
61 void debugMsg(const std::string & msg, unsigned int threshold = 1) {
62 std::lock_guard<std::mutex> lock(debug_msg_mutex);
63 if((debug_level >= threshold) || (global_debug_level >= threshold)) {
64 std::cerr << SoDa::Format("%0 %1\t%2\n")
65 .addS(unit_name, 20)
66 .addS(curDateTime())
67 .addS(msg);
68 }
69 }
70
71 void debugMsg(const SoDa::Format & fmt, unsigned int threshold = 1) {
72 debugMsg(fmt.str(), threshold);
73 }
74
75 void debugMsg(const char * msg, unsigned int threshold = 1) {
76 debugMsg(std::string(msg), threshold);
77 }
78
79 void setDebugLevel(unsigned int v) { debug_level = v; }
80 unsigned int getDebugLevel() { return debug_level; }
81
82 static void setDefaultLevel(unsigned int v) { default_debug_level = v; }
83 static unsigned int getDefaultLevel() { return default_debug_level; }
84
85 static void setGlobalLevel(unsigned int v) { global_debug_level = v; }
86 static unsigned int getGlobalLevel() { return global_debug_level; }
87
88 static std::mutex debug_msg_mutex;
89
90 protected:
91
92 std::string curDateTime();
93
94 std::string unit_name;
95 unsigned int debug_level;
96
97 static unsigned int default_debug_level;
98 static unsigned int global_debug_level;
99 };
100}
101
std::string unit_name
the name of the unit reporting status
Definition Debug.hxx:94
void setDebugLevel(unsigned int v)
Definition Debug.hxx:79
Debug(unsigned int _debug_level, const char *_unit_name_cstr)
Definition Debug.hxx:56
void debugMsg(const SoDa::Format &fmt, unsigned int threshold=1)
Definition Debug.hxx:71
void debugMsg(const char *msg, unsigned int threshold=1)
Definition Debug.hxx:75
std::string curDateTime()
static void setDefaultLevel(unsigned int v)
Definition Debug.hxx:82
unsigned int debug_level
the debug level (threshold) for messages
Definition Debug.hxx:95
Debug(std::string _unit_name=std::string("UNKNOWN"))
Definition Debug.hxx:41
static unsigned int global_debug_level
Definition Debug.hxx:98
Debug(const char *_unit_name_cstr)
Definition Debug.hxx:46
void debugMsg(const std::string &msg, unsigned int threshold=1)
Definition Debug.hxx:61
Debug(unsigned int _debug_level, std::string _unit_name=std::string("UNKNOWN"))
Definition Debug.hxx:51
static unsigned int getDefaultLevel()
Definition Debug.hxx:83
static std::mutex debug_msg_mutex
Definition Debug.hxx:88
static void setGlobalLevel(unsigned int v)
Definition Debug.hxx:85
static unsigned int getGlobalLevel()
Definition Debug.hxx:86
static unsigned int default_debug_level
Definition Debug.hxx:97
unsigned int getDebugLevel()
Definition Debug.hxx:80