YARP
Yet Another Robot Platform
Logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_IMPL_LOGGER_H
11 #define YARP_OS_IMPL_LOGGER_H
12 
13 #include <yarp/conf/api.h>
14 #include <yarp/conf/system.h>
15 
16 #include <yarp/os/Log.h>
18 
19 #include <string>
20 
21 #define YARP_LM_DEBUG 04
22 #define YARP_LM_INFO 010
23 #define YARP_LM_WARNING 040
24 #define YARP_LM_ERROR 0200
25 
26 
27 namespace yarp {
28 namespace os {
29 namespace impl {
30 
35 {
36 public:
37  enum Level
38  {
39  MAJOR_DEBUG = YARP_LM_INFO,
40  DEFAULT_WARN = YARP_LM_INFO
41  };
42 
43  Logger(const char* prefix, Logger* parent = nullptr);
44  Logger(const char* prefix, Logger& parent);
45 
46  static Logger& get();
47 
48  void println(const std::string& txt);
49  void internal_debug(const std::string& txt);
50  void internal_info(const std::string& txt);
51  void internal_warning(const std::string& txt);
52  void internal_error(const std::string& txt);
53  YARP_NORETURN void internal_fail(const std::string& txt);
54  void internal_debug(const char* txt);
55  void internal_info(const char* txt);
56  void internal_warning(const char* txt);
57  void internal_error(const char* txt);
58  YARP_NORETURN void internal_fail(const char* txt);
59  void assertion(bool cond);
60  void setVerbosity(int verbose = 0);
61  void setPrefix(const char* prefix);
62  void setPid();
63  long int getPid();
64  bool shouldShowInfo();
65  bool shouldShowError();
66  bool shouldShowDebug();
67 
68 private:
69  void show(std::uint32_t level, const std::string& txt);
70 
71  std::string prefix;
72  Logger* parent;
73  int verbose;
74  std::uint32_t low;
75  long int pid;
76  FILE* stream;
77 };
78 
79 } // namespace impl
80 } // namespace os
81 } // namespace yarp
82 
83 
84 // compromise - use macros so that debugging can evaporate in optimized code.
85 // also, make a printf-style adaptor since c++ is a bit of a pain to
86 // build strings in.
87 #define YARP_ERROR(log, x) ((yarp::os::impl::Logger*)&(log))->internal_error(x)
88 #define YARP_WARN(log, x) ((yarp::os::impl::Logger*)&(log))->internal_warning(x)
89 #define YARP_INFO(log, x) ((yarp::os::impl::Logger*)&(log))->internal_info(x)
90 #ifndef NDEBUG
91 # define YARP_DEBUG(log, x) ((yarp::os::impl::Logger*)&(log))->internal_debug(x)
92 #else
93 # define YARP_DEBUG(log, x) YARP_UNUSED(log)
94 #endif
95 #define YARP_FAIL(log, x) ((yarp::os::impl::Logger*)&(log))->internal_fail(x)
96 
97 #define YARP_LONGEST_MESSAGE 1000
98 #define YARP_SPRINTF0(log, mode, msg) \
99  { \
100  char _yarp_buf[YARP_LONGEST_MESSAGE]; \
101  snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg); \
102  (log).internal_##mode(&(_yarp_buf[0])); \
103  }
104 #define YARP_SPRINTF1(log, mode, msg, a) \
105  { \
106  char _yarp_buf[YARP_LONGEST_MESSAGE]; \
107  snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a); \
108  (log).internal_##mode(&(_yarp_buf[0])); \
109  }
110 #define YARP_SPRINTF2(log, mode, msg, a, b) \
111  { \
112  char _yarp_buf[YARP_LONGEST_MESSAGE]; \
113  snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b); \
114  (log).internal_##mode(&(_yarp_buf[0])); \
115  }
116 #define YARP_SPRINTF3(log, mode, msg, a, b, c) \
117  { \
118  char _yarp_buf[YARP_LONGEST_MESSAGE]; \
119  snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b, c); \
120  (log).internal_##mode(&(_yarp_buf[0])); \
121  }
122 #define YARP_SPRINTF4(log, mode, msg, a, b, c, d) \
123  { \
124  char _yarp_buf[YARP_LONGEST_MESSAGE]; \
125  snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b, c, d); \
126  (log).internal_##mode(&(_yarp_buf[0])); \
127  }
128 #define YARP_SPRINTF5(log, mode, msg, a, b, c, d, e) \
129  { \
130  char _yarp_buf[YARP_LONGEST_MESSAGE]; \
131  snprintf(&(_yarp_buf[0]), YARP_LONGEST_MESSAGE, msg, a, b, c, d, e); \
132  (log).internal_##mode(&(_yarp_buf[0])); \
133  }
134 
135 
136 #endif // YARP_OS_IMPL_LOGGER_H
#define YARP_LM_INFO
Definition: Logger.h:22
#define YARP_OS_impl_API
Definition: api.h:45
The main, catch-all namespace for YARP.
Definition: numeric.h:47
This is a wrapper for message logging.
Definition: Logger.h:34
bool verbose
Definition: yarpros.cpp:23
#define YARP_NORETURN
Definition: api.h:153