YARP  2.3.68+228-20170410.2+git7d0b2e0
Yet Another Robot Platform
Log.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 iCub Facility, Istituto Italiano di Tecnologia
3  * Authors: Daniele E. Domenichelli <daniele.domenichelli@iit.it>
4  * Marco Randazzo <marco.randazzo@iit.it>
5  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
6  */
7 
8 
9 #ifndef YARP_OS_LOG_H
10 #define YARP_OS_LOG_H
11 
12 #include <yarp/os/api.h>
13 #include <iosfwd>
14 
15 #ifndef YARP_NO_DEPRECATED
16 # include <cstdio>
17 # include <cstdlib>
18 # include <yarp/os/ConstString.h>
19 #endif
20 
21 #if defined (__GNUC__)
22 # define __YFUNCTION__ __PRETTY_FUNCTION__
23 #elif defined(_MSC_VER)
24 # define __YFUNCTION__ __FUNCSIG__
25 #elif (__cplusplus <= 199711)
26 # define __YFUNCTION__ __func__
27 #else
28 # define __YFUNCTION__ "(unknown function)"
29 #endif // __GNUC__
30 
31 // check arguments of the c-style debug functions to make sure that the
32 // arguments supplied have types appropriate to the format string
33 // specified, and that the conversions specified in the format string
34 // make sense. On gcc the warning is enabled by -Wformat.
35 #if defined(__GNUC__)
36 # define YARP_ATTRIBUTE_FORMAT(style, fmt, args) __attribute__((format(printf, (fmt), (args))))
37 #else
38 # define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
39 #endif
40 
41 
42 namespace yarp { namespace os { class LogStream; }}
43 namespace yarp { namespace os { namespace impl { class LogImpl; }}}
44 
45 
46 namespace yarp {
47 namespace os {
48 
49 class YARP_OS_API Log
50 {
51 public:
52 
53  Log(const char *file,
54  const unsigned int line,
55  const char *func);
56  Log();
57  virtual ~Log();
58 
59  enum LogType {
65  FatalType
66  };
67 
68  void trace(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
69  void debug(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
70  void info(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
71  void warning(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
72  void error(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
73  void fatal(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
74 
75  LogStream trace() const;
76  LogStream debug() const;
77  LogStream info() const;
78  LogStream warning() const;
79  LogStream error() const;
80  LogStream fatal() const;
81 
82 #ifndef YARP_NO_DEPRECATED
83  YARP_DEPRECATED virtual void debug(const ConstString& txt) const { debug("%s", txt.c_str()); }
84  YARP_DEPRECATED virtual void info(const ConstString& txt) const { info("%s", txt.c_str()); }
85  YARP_DEPRECATED virtual void warning(const ConstString& txt) const { warning("%s", txt.c_str()); }
86  YARP_DEPRECATED virtual void error(const ConstString& txt) const { error("%s", txt.c_str()); }
87  YARP_DEPRECATED virtual void fail(const ConstString& txt) const { fatal("%s", txt.c_str()); }
88 #endif // YARP_NO_DEPRECATED
89 
90  typedef void (*LogCallback)(yarp::os::Log::LogType,
91  const char *,
92  const char *,
93  const unsigned int,
94  const char *);
95 
96  static void setLogCallback(LogCallback);
97 
98 private:
100 
101  static LogCallback print_callback;
102  static LogCallback forward_callback;
103 
104  friend class LogStream;
105 }; // class Log
106 
107 } // namespace os
108 } // namespace yarp
109 
110 
111 #define yTrace yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).trace
112 #define yDebug yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).debug
113 #define yInfo yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).info
114 #define yWarning yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).warning
115 #define yError yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).error
116 #define yFatal yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).fatal
117 
118 #define yAssert(x) if (!(x)) { yFatal("Assertion failure at %s:%d (%s)", __FILE__, __LINE__, #x); }
119 
120 #define YARP_FIXME_NOTIMPLEMENTED(what) yWarning("FIXME: %s not yet implemented", what);
121 
122 
126 YARP_OS_API void yarp_print_trace(FILE *out, const char *file, int line);
127 
128 
129 #ifndef YARP_NO_DEPRECATED
130 
131 YARP_OS_DEPRECATED_API inline void __yarp_debug(const yarp::os::ConstString& str) { yDebug("%s", str.c_str()); }
132 YARP_OS_DEPRECATED_API inline void __yarp_info(const yarp::os::ConstString& str) { yInfo("%s", str.c_str()); }
133 YARP_OS_DEPRECATED_API inline void __yarp_warn(const yarp::os::ConstString& str) { yWarning("%s", str.c_str()); }
134 YARP_OS_DEPRECATED_API inline void __yarp_error(const yarp::os::ConstString& str) { yError("%s", str.c_str()); }
135 
136 
137 YARP_OS_DEPRECATED_API inline bool yarp_show_error() { return true; }
138 YARP_OS_DEPRECATED_API inline bool yarp_show_warn() { return true; }
139 YARP_OS_DEPRECATED_API inline bool yarp_show_info() { return true; }
140 YARP_OS_DEPRECATED_API inline bool yarp_show_debug() { return true; }
141 
145 #define YARP_LOG_ERROR(x) __yarp_error(x)
146 
150 #define YARP_LOG_WARN(x) __yarp_warn(x)
151 
155 #define YARP_LOG_INFO(x) __yarp_info(x)
156 
160 #define YARP_LOG_DEBUG(x) __yarp_debug(x)
161 
167 #define YARP_ASSERT(x) YARP_COMPILER_DEPRECATED_WARNING(YARP_ASSERT is deprecated. Use yAssert instead.); yAssert(x)
168 
169 #endif // YARP_NO_DEPRECATED
170 
171 #endif // YARP_OS_LOG_H
YARP_OS_DEPRECATED_API bool yarp_show_debug()
Definition: Log.h:140
virtual YARP_DEPRECATED void error(const ConstString &txt) const
Definition: Log.h:86
#define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
Definition: Log.h:36
YARP_OS_DEPRECATED_API void __yarp_debug(const yarp::os::ConstString &str)
Definition: Log.h:131
YARP_OS_DEPRECATED_API void __yarp_info(const yarp::os::ConstString &str)
Definition: Log.h:132
virtual YARP_DEPRECATED void info(const ConstString &txt) const
Definition: Log.h:84
YARP_OS_DEPRECATED_API void __yarp_warn(const yarp::os::ConstString &str)
Definition: Log.h:133
yarp::os::impl::LogImpl *const mPriv
Definition: Log.h:99
YARP_OS_DEPRECATED_API bool yarp_show_warn()
Definition: Log.h:138
static LogCallback forward_callback
Definition: Log.h:102
The main, catch-all namespace for YARP.
A string with almost the same api as std::string.
Definition: ConstString.h:44
#define yInfo
Definition: Log.h:113
static LogCallback print_callback
Definition: Log.h:101
#define YARP_DEPRECATED
Definition: api.h:89
void yarp_print_trace(FILE *out, const char *file, int line)
Low level function for printing a stack trace, if implemented (ACE or gcc/Linux). ...
Definition: Log.cpp:391
YARP_OS_DEPRECATED_API bool yarp_show_info()
Definition: Log.h:139
#define yWarning
Definition: Log.h:114
#define yError
Definition: Log.h:115
YARP_OS_DEPRECATED_API void __yarp_error(const yarp::os::ConstString &str)
Definition: Log.h:134
virtual YARP_DEPRECATED void warning(const ConstString &txt) const
Definition: Log.h:85
const char * c_str() const
Accesses the character sequence stored in this object.
Definition: ConstString.cpp:88
virtual YARP_DEPRECATED void fail(const ConstString &txt) const
Definition: Log.h:87
YARP_OS_DEPRECATED_API bool yarp_show_error()
Definition: Log.h:137
#define yDebug
Definition: Log.h:112
virtual YARP_DEPRECATED void debug(const ConstString &txt) const
Definition: Log.h:83