libcyberradio  22.01.24
Debuggable.cpp
1 /***************************************************************************
2  * \file Debuggable.cpp
3  *
4  * \brief Class that supports debug output.
5  *
6  * \author DA
7  * \copyright 2016 CyberRadio Solutions, Inc.
8  */
9 
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 
14 #include "LibCyberRadio/Common/Debuggable.h"
15 #include "LibCyberRadio/Common/Pythonesque.h"
16 #include <sstream>
17 #include <iomanip>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <time.h>
22 #include <ctype.h>
23 
24 
25 namespace LibCyberRadio
26 {
27 
29  bool debug,
30  const std::string& debug_name,
31  FILE* debug_fp,
32  const std::string& debug_timefmt
33  ) :
34  _debug(debug),
35  _debugName(debug_name),
36  _debugFp(debug_fp),
37  _debugTimeFmt(debug_timefmt),
38  _debugTimestamp(NULL),
39  _debugTimestampSize(80)
40  {
41  _debugTimestamp = new char[_debugTimestampSize];
42  }
43 
45  {
46  delete _debugTimestamp;
47  }
48 
50  _debug(other._debug),
51  _debugName(other._debugName),
52  _debugFp(other._debugFp),
53  _debugTimeFmt(other._debugTimeFmt),
54  _debugTimestamp(other._debugTimestamp),
55  _debugTimestampSize(other._debugTimestampSize)
56  {
57  }
58 
60  {
61  // Protect against self-assignment
62  if (this != &other)
63  {
64  _debug = other._debug;
65  _debugName = other._debugName;
66  _debugFp = other._debugFp;
67  _debugTimeFmt = other._debugTimeFmt;
68  _debugTimestamp = other._debugTimestamp;
69  _debugTimestampSize = other._debugTimestampSize;
70  }
71  return *this;
72  }
73 
75  const std::string& debug_name
76  )
77  {
78  _debugName = debug_name;
79  }
80 
82  FILE* debug_fp
83  )
84  {
85  _debugFp = debug_fp;
86  }
87 
89  const std::string& debug_timefmt
90  )
91  {
92  _debugTimeFmt = debug_timefmt;
93  }
94 
96  const char *format,
97  ...
98  )
99  {
100  int ret = 0;
101  if ( _debug && (_debugFp != NULL) )
102  {
103  if (_debugTimeFmt.length() > 0)
104  {
105  time_t now = time(NULL);
106  memset(_debugTimestamp, 0, _debugTimestampSize);
107  strftime(_debugTimestamp,
108  _debugTimestampSize,
109  _debugTimeFmt.c_str(),
110  localtime(&now));
111  ret += fprintf(_debugFp, "[%s]", _debugTimestamp);
112  }
113  if (_debugName.length() > 0)
114  ret += fprintf(_debugFp, "[%s] ", _debugName.c_str());
115  if (ret >= 0)
116  {
117  va_list ap;
118  va_start(ap, format);
119  ret += vfprintf(_debugFp, format, ap);
120  va_end(ap);
121  }
122  }
123  return ret;
124  }
125 
127  bool x
128  )
129  {
130  return ( x ? "true" : "false" );
131  }
132 
133  bool Debuggable::isDebug() const
134  {
135  return _debug;
136  }
137 
138  std::string Debuggable::getDebugName() const
139  {
140  return _debugName;
141  }
142 
143  std::string Debuggable::rawString(const std::string& data)
144  {
145  std::string ret;
146  std::ostringstream oss;
147  for (std::string::const_iterator it = data.begin(); it!= data.end(); it++)
148  {
149  if ( !isalnum(*it) && !ispunct(*it) && !isspace(*it) )
150  {
151  oss << "\\x" << std::hex << std::setw(2)
152  << std::setfill('0') << (int)((unsigned char)(*it));
153  }
154  else
155  oss << (char)(*it);
156  }
157  ret = oss.str();
158  ret = Pythonesque::Replace(ret, "\r", "\\r");
159  ret = Pythonesque::Replace(ret, "\n", "\\n");
160  ret = Pythonesque::Replace(ret, "\t", "\\t");
161  ret = Pythonesque::Replace(ret, "\v", "\\v");
162  ret = Pythonesque::Replace(ret, "\f", "\\f");
163  return ret;
164  }
165 
166 
167 } /* namespace LibCyberRadio */
168 
Debuggable & operator=(const Debuggable &other)
Assignment operator for Debuggable objects.
Definition: Debuggable.cpp:59
virtual ~Debuggable()
Destroys a Debuggable object.
Definition: Debuggable.cpp:44
static std::string Replace(const std::string &str, const std::string &oldstr, const std::string &newstr, int count=INT_MAX)
Replaces occurrences of one substring with another within the given string.
Definition: Pythonesque.cpp:62
virtual void setDebugFile(FILE *debug_fp)
Sets the debug file pointer for this object.
Definition: Debuggable.cpp:81
Class that supports debug output.
Definition: Debuggable.h:38
virtual void setDebugTimeFormat(const std::string &debug_timefmt)
Sets the debug time format for this object.
Definition: Debuggable.cpp:88
virtual int debug(const char *format,...)
Outputs debug information.
Definition: Debuggable.cpp:95
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
Debuggable(bool debug=false, const std::string &debug_name="", FILE *debug_fp=DEBUG_FP, const std::string &debug_timefmt=DEBUG_TIME_FMT)
Constructs a Debuggable object.
Definition: Debuggable.cpp:28
virtual const char * debugBool(bool x)
Gets a debug output string for a Boolean value.
Definition: Debuggable.cpp:126
virtual std::string rawString(const std::string &data)
Gets a "raw" string representation of a given data string.
Definition: Debuggable.cpp:143
virtual bool isDebug() const
Gets whether this object produces debug output.
Definition: Debuggable.cpp:133
virtual std::string getDebugName() const
Gets the debug name for this object.
Definition: Debuggable.cpp:138
virtual void setDebugName(const std::string &debug_name)
Sets the debug name for this object.
Definition: Debuggable.cpp:74