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