SoDaRadio-5.0.3-master:8901fb5
Debug.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, 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 #ifndef DEBUG_HDR
30 #define DEBUG_HDR
31 
32 #include <string>
33 #include <boost/format.hpp>
34 #include <boost/thread/mutex.hpp>
35 #include <boost/thread/condition.hpp>
36 #include <boost/date_time/posix_time/posix_time.hpp>
37 
38 namespace SoDa {
42  class Debug {
43  public:
44  Debug(std::string _unit_name = std::string("UNKNOWN")) {
45  unit_name = _unit_name;
47  }
48 
49  Debug(const char * _unit_name_cstr) {
50  unit_name = std::string(_unit_name_cstr);
52  }
53 
54  Debug(unsigned int _debug_level, std::string _unit_name = std::string("UNKNOWN")) {
55  unit_name = _unit_name;
56  debug_level = _debug_level;
57  }
58 
59  Debug(unsigned int _debug_level, const char * _unit_name_cstr) {
60  unit_name = std::string(_unit_name_cstr);
61  debug_level = _debug_level;
62  }
63 
64  void debugMsg(const std::string & msg, unsigned int threshold = 1) {
65  boost::mutex::scoped_lock lock(debug_msg_mutex);
66  if((debug_level >= threshold) || (global_debug_level >= threshold)) {
67  std::cerr << boost::format("%-20s %s\t%s\n") % unit_name % curDateTime() % msg;
68  }
69  }
70 
71  void debugMsg(const boost::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 boost::mutex debug_msg_mutex;
89 
90  protected:
91 
92  std::string curDateTime() {
93  boost::posix_time::ptime t1;
94  t1 = boost::posix_time::microsec_clock::local_time();
95  return to_simple_string(t1);
96  }
97 
98  std::string unit_name;
99  unsigned int debug_level;
100 
101  static unsigned int default_debug_level;
102  static unsigned int global_debug_level;
103  };
104 }
105 
106 
107 #endif
static unsigned int getGlobalLevel()
Definition: Debug.hxx:86
std::string curDateTime()
Definition: Debug.hxx:92
Debug(const char *_unit_name_cstr)
Definition: Debug.hxx:49
void debugMsg(const std::string &msg, unsigned int threshold=1)
Definition: Debug.hxx:64
std::string unit_name
the name of the unit reporting status
Definition: Debug.hxx:98
void debugMsg(const char *msg, unsigned int threshold=1)
Definition: Debug.hxx:75
Debug(unsigned int _debug_level, const char *_unit_name_cstr)
Definition: Debug.hxx:59
static unsigned int global_debug_level
Definition: Debug.hxx:102
Debug(std::string _unit_name=std::string("UNKNOWN"))
Definition: Debug.hxx:44
Debug(unsigned int _debug_level, std::string _unit_name=std::string("UNKNOWN"))
Definition: Debug.hxx:54
void debugMsg(const boost::format &fmt, unsigned int threshold=1)
Definition: Debug.hxx:71
unsigned int debug_level
the debug level (threshold) for messages
Definition: Debug.hxx:99
static unsigned int default_debug_level
Definition: Debug.hxx:101
A simple base class to provide debug messaging from any derived class.
Definition: Debug.hxx:42
static void setDefaultLevel(unsigned int v)
Definition: Debug.hxx:82
static boost::mutex debug_msg_mutex
Definition: Debug.hxx:88
static void setGlobalLevel(unsigned int v)
Definition: Debug.hxx:85
void setDebugLevel(unsigned int v)
Definition: Debug.hxx:79
static unsigned int getDefaultLevel()
Definition: Debug.hxx:83
unsigned int getDebugLevel()
Definition: Debug.hxx:80