YARP
Yet Another Robot Platform
Logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 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 #include <string>
16 #include <yarp/os/Log.h>
18 
19 #define YARP_LM_DEBUG 04
20 #define YARP_LM_INFO 010
21 #define YARP_LM_WARNING 040
22 #define YARP_LM_ERROR 0200
23 
24 
25 namespace yarp {
26 namespace os {
27 namespace impl {
28 
33 {
34 public:
35  enum Level {
36  MAJOR_DEBUG=YARP_LM_INFO,
37  DEFAULT_WARN=YARP_LM_INFO
38  };
39 
40  Logger(const char *prefix, Logger *parent = nullptr);
41  Logger(const char *prefix, Logger& parent);
42 
43  static Logger& get();
44 
45  void println(const std::string& txt);
46  void internal_debug(const std::string& txt);
47  void internal_info(const std::string& txt);
48  void internal_warning(const std::string& txt);
49  void internal_error(const std::string& txt);
50  YARP_NORETURN void internal_fail(const std::string& txt);
51  void internal_debug(const char *txt);
52  void internal_info(const char *txt);
53  void internal_warning(const char *txt);
54  void internal_error(const char *txt);
55  YARP_NORETURN void internal_fail(const char *txt);
56  void assertion(bool cond);
57  void setVerbosity(int verbose = 0);
58  void setPrefix(const char *prefix);
59  void setPid();
60  long int getPid();
61  bool shouldShowInfo();
62  bool shouldShowError();
63  bool shouldShowDebug();
64 
65 private:
66  void show(std::uint32_t level, const std::string& txt);
67 
68  std::string prefix;
70  int verbose;
71  std::uint32_t low;
72  long int pid;
73  FILE *stream;
74 };
75 
76 } // namespace impl
77 } // namespace os
78 } // namespace yarp
79 
80 
81 // compromise - use macros so that debugging can evaporate in optimized code.
82 // also, make a printf-style adaptor since c++ is a bit of a pain to
83 // build strings in.
84 #define YARP_ERROR(log, x) ((yarp::os::impl::Logger*)&(log))->internal_error(x)
85 #define YARP_WARN(log, x) ((yarp::os::impl::Logger*)&(log))->internal_warning(x)
86 #define YARP_INFO(log, x) ((yarp::os::impl::Logger*)&(log))->internal_info(x)
87 #ifndef NDEBUG
88 # define YARP_DEBUG(log, x) ((yarp::os::impl::Logger*)&(log))->internal_debug(x)
89 #else
90 # define YARP_DEBUG(log, x) YARP_UNUSED(log)
91 #endif
92 #define YARP_FAIL(log, x) ((yarp::os::impl::Logger*)&(log))->internal_fail(x)
93 
94 #define YARP_LONGEST_MESSAGE 1000
95 #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])); }
96 #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])); }
97 #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])); }
98 #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])); }
99 #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])); }
100 #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])); }
101 
102 
103 
104 #endif // YARP_OS_IMPL_LOGGER_H
std::string prefix
Definition: Logger.h:68
#define YARP_LM_INFO
Definition: Logger.h:20
#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:32
bool verbose
Definition: yarpros.cpp:21
#define YARP_NORETURN
Definition: api.h:153
std::uint32_t low
Definition: Logger.h:71