YARP
Yet Another Robot Platform
Log.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_LOG_H
10 #define YARP_OS_LOG_H
11 
12 #include <yarp/os/api.h>
13 
14 #include <iosfwd>
15 
16 #if defined(__GNUC__)
17 # define __YFUNCTION__ __PRETTY_FUNCTION__
18 #elif defined(_MSC_VER)
19 # define __YFUNCTION__ __FUNCSIG__
20 #elif (__cplusplus <= 199711)
21 # define __YFUNCTION__ __func__
22 #else
23 # define __YFUNCTION__ "(unknown function)"
24 #endif // __GNUC__
25 
26 // check arguments of the c-style debug functions to make sure that the
27 // arguments supplied have types appropriate to the format string
28 // specified, and that the conversions specified in the format string
29 // make sense. On gcc the warning is enabled by -Wformat.
30 #if defined(__GNUC__)
31 # define YARP_ATTRIBUTE_FORMAT(style, fmt, args) __attribute__((format(printf, (fmt), (args))))
32 #else
33 # define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
34 #endif
35 
36 
37 // Forward declarations
38 namespace yarp {
39 namespace os {
40 class LogStream;
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 namespace impl {
43 class LogImpl;
44 } // namespace impl
45 #endif // DOXYGEN_SHOULD_SKIP_THIS
46 } // namespace os
47 } // namespace yarp
48 
49 
50 namespace yarp {
51 namespace os {
52 
54 {
55 public:
56  Log(const char* file,
57  const unsigned int line,
58  const char* func);
59  Log();
60  virtual ~Log();
61 
62  enum LogType
63  {
69  FatalType
70  };
71 
72  void trace(const char* msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
73  void debug(const char* msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
74  void info(const char* msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
75  void warning(const char* msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
76  void error(const char* msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
77  YARP_NORETURN void fatal(const char* msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
78 
79  LogStream trace() const;
80  LogStream debug() const;
81  LogStream info() const;
82  LogStream warning() const;
83  LogStream error() const;
84  LogStream fatal() const;
85 
86  typedef void (*LogCallback)(yarp::os::Log::LogType,
87  const char*,
88  const char*,
89  const unsigned int,
90  const char*);
91 
92  static void setLogCallback(LogCallback);
93 
94 private:
95 #ifndef DOXYGEN_SHOULD_SKIP_THIS
96  yarp::os::impl::LogImpl* const mPriv;
97 #endif // DOXYGEN_SHOULD_SKIP_THIS
98 
99  static LogCallback print_callback;
100  static LogCallback forward_callback;
101 
102  friend class LogStream;
103 }; // class Log
104 
105 } // namespace os
106 } // namespace yarp
107 
108 
109 #define yTrace yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).trace
110 #define yDebug yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).debug
111 #define yInfo yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).info
112 #define yWarning yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).warning
113 #define yError yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).error
114 #define yFatal yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).fatal
115 
116 #define yAssert(x) \
117  if (!(x)) { \
118  yFatal("Assertion failure at %s:%d (%s)", __FILE__, __LINE__, #x); \
119  }
120 
121 #define YARP_FIXME_NOTIMPLEMENTED(what) yWarning("FIXME: %s not yet implemented", what);
122 
123 
127 YARP_OS_API void yarp_print_trace(FILE* out, const char* file, int line);
128 
129 
130 #endif // YARP_OS_LOG_H
#define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
Definition: Log.h:33
#define YARP_OS_API
Definition: api.h:19
static LogCallback forward_callback
Definition: Log.h:100
The main, catch-all namespace for YARP.
Definition: numeric.h:47
static LogCallback print_callback
Definition: Log.h:99
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:420
#define YARP_NORETURN
Definition: api.h:153