YARP  2.3.70
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 #ifdef YARP_HAS_ACE
65  if (this==root) {
66  ACE_Log_Msg *acer = ACE_Log_Msg::instance();
67  acer->set_flags(8);
68  acer->clr_flags(1);
69  acer->msg_callback(this);
70  }
71 #endif
72  }
73 
74  Logger(const char *prefix, Logger& parent) {
75  this->prefix = prefix;
76  this->parent = &parent;
77  verbose = 0;
78  stream = YARP_NULLPTR;
79  low = DEFAULT_WARN;
80  }
81 
82  static Logger& get();
83  static void fini();
84 
85 #ifdef YARP_HAS_ACE
86  virtual void log(ACE_Log_Record& log_record) override {
87  show(log_record.type(), log_record.msg_data());
88  }
89 #endif
90 
91  void println(const ConstString& txt) {
92  internal_debug(txt);
93  }
94 
95 
96 
97  void internal_debug(const ConstString& txt) {
98  show(LM_DEBUG, txt);
99  }
100 
101  void internal_info(const ConstString& txt) {
102  show(LM_INFO, txt);
103  }
104 
105  void internal_warning(const ConstString& txt) {
106  show(LM_WARNING, txt);
107  }
108 
109  void internal_error(const ConstString& txt) {
110  show(LM_ERROR, txt);
111  }
112 
113  void internal_fail(const ConstString& txt) {
114  show(LM_ERROR, txt);
115  exit(1);
116  }
117 
118 
119 
120  void internal_debug(const char *txt) {
121  ConstString stxt(txt);
122  show(LM_DEBUG, stxt);
123  }
124 
125  void internal_info(const char *txt) {
126  ConstString stxt(txt);
127  show(LM_INFO, stxt);
128  }
129 
130  void internal_warning(const char *txt) {
131  ConstString stxt(txt);
132  show(LM_WARNING, stxt);
133  }
134 
135  void internal_error(const char *txt) {
136  ConstString stxt(txt);
137  show(LM_ERROR, stxt);
138  }
139 
140  void internal_fail(const char *txt) {
141  ConstString stxt(txt);
142  show(LM_ERROR, stxt);
143  exit(1);
144  }
145 
146 
147  void assertion(bool cond) {
148  if (!cond) {
149  internal_fail("assertion failure");
150  }
151  }
152 
153  void setVerbosity(int verbose = 0) {
154  this->verbose = verbose;
155  }
156 
157  void setPrefix(const char *prefix) {
158  this->prefix = prefix;
159  }
160 
161  void setPid();
162 
163  long int getPid() {
164  return pid;
165  }
166 
167  bool shouldShowInfo() {
168  return (verbose>=0);
169  }
170 
172  return true;
173  }
174 
176  return (verbose>0);
177  }
178 
179 private:
180  void show(unsigned YARP_INT32 level, const ConstString& txt);
181  void exit(int level);
182 
183  static Logger *root;
186  int verbose;
187  unsigned YARP_INT32 low;
188  long int pid;
189  FILE *stream;
190 };
191 
192 // compromise - use macros so that debugging can evaporate in optimized code.
193 // also, make a printf-style adaptor since c++ is a bit of a pain to
194 // build strings in.
195 #define YARP_ERROR(log, x) ((Logger*)&(log))->internal_error(x)
196 #define YARP_WARN(log, x) ((Logger*)&(log))->internal_warning(x)
197 #define YARP_INFO(log, x) ((Logger*)&(log))->internal_info(x)
198 #ifndef NDEBUG
199 # define YARP_DEBUG(log, x) ((Logger*)&(log))->internal_debug(x)
200 #else
201 # define YARP_DEBUG(log, x) YARP_UNUSED(log)
202 #endif
203 #define YARP_FAIL(log, x) ((Logger*)&(log))->internal_fail(x)
204 
205 #define YARP_LONGEST_MESSAGE 1000
206 #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])); }
207 #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])); }
208 #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])); }
209 #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])); }
210 #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])); }
211 #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])); }
212 
213 
214 
215 #endif // YARP_OS_IMPL_LOGGER_H
void setVerbosity(int verbose=0)
Definition: Logger.h:153
void internal_warning(const ConstString &txt)
Definition: Logger.h:105
void internal_fail(const char *txt)
Definition: Logger.h:140
void setPrefix(const char *prefix)
Definition: Logger.h:157
void internal_debug(const char *txt)
Definition: Logger.h:120
unsigned YARP_INT32 low
Definition: Logger.h:187
#define LM_INFO
Definition: Logger.h:27
The main, catch-all namespace for YARP.
static Logger * root
Definition: Logger.h:183
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:175
#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:91
void exit(int exit_code)
Portable wrapper for the exit() function.
Definition: Os.cpp:40
ConstString prefix
Definition: Logger.h:184
void internal_debug(const ConstString &txt)
Definition: Logger.h:97
void internal_info(const ConstString &txt)
Definition: Logger.h:101
void internal_error(const char *txt)
Definition: Logger.h:135
void internal_fail(const ConstString &txt)
Definition: Logger.h:113
bool verbose
Definition: yarpros.cpp:20
void internal_error(const ConstString &txt)
Definition: Logger.h:109
long int getPid()
Definition: Logger.h:163
void internal_warning(const char *txt)
Definition: Logger.h:130
void internal_info(const char *txt)
Definition: Logger.h:125
Logger(const char *prefix, Logger &parent)
Definition: Logger.h:74
Logger(const char *prefix, Logger *parent=YARP_NULLPTR)
Definition: Logger.h:58
bool shouldShowError()
Definition: Logger.h:171
#define LM_DEBUG
Definition: Logger.h:26
void assertion(bool cond)
Definition: Logger.h:147