YARP  2.3.70.1
Yet Another Robot Platform
Logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 RobotCub Consortium
3  * Authors: Paul Fitzpatrick
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
5  */
6 
7 #ifndef YARP_OS_IMPL_LOGGER_H
8 #define YARP_OS_IMPL_LOGGER_H
9 
10 #include <yarp/conf/api.h>
11 #include <yarp/conf/system.h>
12 #include <yarp/os/ConstString.h>
13 #include <yarp/os/Log.h>
14 
16 
17 #ifdef YARP_HAS_ACE
18 # include <ace/Log_Msg.h>
19 # include <ace/Log_Record.h>
20 # include <ace/Log_Msg_Callback.h>
21 // In one of these files or their inclusions, there is a definition of "main"
22 # ifdef main
23 # undef main
24 # endif
25 #else
26 # define LM_DEBUG 04
27 # define LM_INFO 010
28 # define LM_WARNING 040
29 # define LM_ERROR 0200
30 #endif
31 #include <cstdio>
32 
33 
34 namespace yarp {
35  namespace os {
36  namespace impl {
37  class Logger;
38  }
39  }
40 }
41 
47 class YARP_OS_impl_API yarp::os::impl::Logger : public yarp::os::Log
48 #ifdef YARP_HAS_ACE
49  , public ACE_Log_Msg_Callback
50 #endif
51 {
52 public:
53  enum Level {
54  MAJOR_DEBUG=LM_INFO,
55  DEFAULT_WARN=LM_INFO
56  };
57 
58  Logger(const char *prefix, Logger *parent = YARP_NULLPTR) {
59  this->prefix = prefix;
60  this->parent = parent;
61  verbose = 0;
62  low = DEFAULT_WARN;
63  stream = YARP_NULLPTR;
64  pid = -1;
65 #ifdef YARP_HAS_ACE
66  if (this==root) {
67  ACE_Log_Msg *acer = ACE_Log_Msg::instance();
68  acer->set_flags(8);
69  acer->clr_flags(1);
70  acer->msg_callback(this);
71  }
72 #endif
73  }
74 
75  Logger(const char *prefix, Logger& parent) {
76  this->prefix = prefix;
77  this->parent = &parent;
78  verbose = 0;
79  stream = YARP_NULLPTR;
80  low = DEFAULT_WARN;
81  pid = -1;
82  }
83 
84  static Logger& get();
85  static void fini();
86 
87 #ifdef YARP_HAS_ACE
88  virtual void log(ACE_Log_Record& log_record) override {
89  show(log_record.type(), log_record.msg_data());
90  }
91 #endif
92 
93  void println(const ConstString& txt) {
94  internal_debug(txt);
95  }
96 
97 
98 
99  void internal_debug(const ConstString& txt) {
100  show(LM_DEBUG, txt);
101  }
102 
103  void internal_info(const ConstString& txt) {
104  show(LM_INFO, txt);
105  }
106 
107  void internal_warning(const ConstString& txt) {
108  show(LM_WARNING, txt);
109  }
110 
111  void internal_error(const ConstString& txt) {
112  show(LM_ERROR, txt);
113  }
114 
115  void internal_fail(const ConstString& txt) {
116  show(LM_ERROR, txt);
117  exit(1);
118  }
119 
120 
121 
122  void internal_debug(const char *txt) {
123  ConstString stxt(txt);
124  show(LM_DEBUG, stxt);
125  }
126 
127  void internal_info(const char *txt) {
128  ConstString stxt(txt);
129  show(LM_INFO, stxt);
130  }
131 
132  void internal_warning(const char *txt) {
133  ConstString stxt(txt);
134  show(LM_WARNING, stxt);
135  }
136 
137  void internal_error(const char *txt) {
138  ConstString stxt(txt);
139  show(LM_ERROR, stxt);
140  }
141 
142  void internal_fail(const char *txt) {
143  ConstString stxt(txt);
144  show(LM_ERROR, stxt);
145  exit(1);
146  }
147 
148 
149  void assertion(bool cond) {
150  if (!cond) {
151  internal_fail("assertion failure");
152  }
153  }
154 
155  void setVerbosity(int verbose = 0) {
156  this->verbose = verbose;
157  }
158 
159  void setPrefix(const char *prefix) {
160  this->prefix = prefix;
161  }
162 
163  void setPid();
164 
165  long int getPid() {
166  return pid;
167  }
168 
169  bool shouldShowInfo() {
170  return (verbose>=0);
171  }
172 
174  return true;
175  }
176 
178  return (verbose>0);
179  }
180 
181 private:
182  void show(unsigned YARP_INT32 level, const ConstString& txt);
183  void exit(int level);
184 
185  static Logger *root;
188  int verbose;
189  unsigned YARP_INT32 low;
190  long int pid;
191  FILE *stream;
192 };
193 
194 // compromise - use macros so that debugging can evaporate in optimized code.
195 // also, make a printf-style adaptor since c++ is a bit of a pain to
196 // build strings in.
197 #define YARP_ERROR(log, x) ((Logger*)&(log))->internal_error(x)
198 #define YARP_WARN(log, x) ((Logger*)&(log))->internal_warning(x)
199 #define YARP_INFO(log, x) ((Logger*)&(log))->internal_info(x)
200 #ifndef NDEBUG
201 # define YARP_DEBUG(log, x) ((Logger*)&(log))->internal_debug(x)
202 #else
203 # define YARP_DEBUG(log, x) YARP_UNUSED(log)
204 #endif
205 #define YARP_FAIL(log, x) ((Logger*)&(log))->internal_fail(x)
206 
207 #define YARP_LONGEST_MESSAGE 1000
208 #define YARP_SPRINTF0(log, mode, msg) { char _yarp_buf[YARP_LONGEST_MESSAGE]; snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg); (log).internal_ ## mode(&(_yarp_buf[0])); }
209 #define YARP_SPRINTF1(log, mode, msg, a) { char _yarp_buf[YARP_LONGEST_MESSAGE]; snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a); (log).internal_ ## mode(&(_yarp_buf[0])); }
210 #define YARP_SPRINTF2(log, mode, msg, a, b) { char _yarp_buf[YARP_LONGEST_MESSAGE]; snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b); (log).internal_ ## mode(&(_yarp_buf[0])); }
211 #define YARP_SPRINTF3(log, mode, msg, a, b, c) { char _yarp_buf[YARP_LONGEST_MESSAGE]; snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b, c); (log).internal_ ## mode(&(_yarp_buf[0])); }
212 #define YARP_SPRINTF4(log, mode, msg, a, b, c, d) { char _yarp_buf[YARP_LONGEST_MESSAGE]; snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b, c, d); (log).internal_ ## mode(&(_yarp_buf[0])); }
213 #define YARP_SPRINTF5(log, mode, msg, a, b, c, d, e) { char _yarp_buf[YARP_LONGEST_MESSAGE]; snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b, c, d, e); (log).internal_ ## mode(&(_yarp_buf[0])); }
214 
215 
216 
217 #endif // YARP_OS_IMPL_LOGGER_H
void setVerbosity(int verbose=0)
Definition: Logger.h:155
void internal_warning(const ConstString &txt)
Definition: Logger.h:107
void internal_fail(const char *txt)
Definition: Logger.h:142
void setPrefix(const char *prefix)
Definition: Logger.h:159
void internal_debug(const char *txt)
Definition: Logger.h:122
unsigned YARP_INT32 low
Definition: Logger.h:189
#define LM_INFO
Definition: Logger.h:27
The main, catch-all namespace for YARP.
static Logger * root
Definition: Logger.h:185
A string with almost the same api as std::string.
Definition: ConstString.h:44
#define LM_ERROR
Definition: Logger.h:29
bool shouldShowDebug()
Definition: Logger.h:177
#define LM_WARNING
Definition: Logger.h:28
This is a wrapper for message logging.
Definition: Logger.h:47
void println(const ConstString &txt)
Definition: Logger.h:93
void exit(int exit_code)
Portable wrapper for the exit() function.
Definition: Os.cpp:40
ConstString prefix
Definition: Logger.h:186
void internal_debug(const ConstString &txt)
Definition: Logger.h:99
void internal_info(const ConstString &txt)
Definition: Logger.h:103
void internal_error(const char *txt)
Definition: Logger.h:137
void internal_fail(const ConstString &txt)
Definition: Logger.h:115
bool verbose
Definition: yarpros.cpp:20
void internal_error(const ConstString &txt)
Definition: Logger.h:111
long int getPid()
Definition: Logger.h:165
void internal_warning(const char *txt)
Definition: Logger.h:132
void internal_info(const char *txt)
Definition: Logger.h:127
Logger(const char *prefix, Logger &parent)
Definition: Logger.h:75
Logger(const char *prefix, Logger *parent=YARP_NULLPTR)
Definition: Logger.h:58
bool shouldShowError()
Definition: Logger.h:173
#define LM_DEBUG
Definition: Logger.h:26
void assertion(bool cond)
Definition: Logger.h:149