YARP
Yet Another Robot Platform
Log.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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 
41 class LogStream;
42 
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 namespace impl {
45 class LogImpl;
46 } // namespace impl
47 #endif // DOXYGEN_SHOULD_SKIP_THIS
48 
50 {
51 public:
52  Log(const char* file,
53  const unsigned int line,
54  const char* func);
55  Log();
56  virtual ~Log();
57 
58  enum LogType
59  {
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  YARP_NORETURN 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  typedef void (*LogCallback)(yarp::os::Log::LogType,
83  const char*,
84  const char*,
85  const unsigned int,
86  const char*);
87 
88  static void setLogCallback(LogCallback);
89 
90 private:
91 #ifndef DOXYGEN_SHOULD_SKIP_THIS
92  yarp::os::impl::LogImpl* const mPriv;
93 #endif // DOXYGEN_SHOULD_SKIP_THIS
94 
95  static LogCallback print_callback;
96  static LogCallback forward_callback;
97 
98  friend class LogStream;
99 }; // class Log
100 
101 } // namespace os
102 } // namespace yarp
103 
104 
105 #define yTrace yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).trace
106 #define yDebug yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).debug
107 #define yInfo yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).info
108 #define yWarning yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).warning
109 #define yError yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).error
110 #define yFatal yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).fatal
111 
112 #define yAssert(x) \
113  if (!(x)) { \
114  yFatal("Assertion failure at %s:%d (%s)", __FILE__, __LINE__, #x); \
115  }
116 
117 #define YARP_FIXME_NOTIMPLEMENTED(what) yWarning("FIXME: %s not yet implemented", what);
118 
119 
123 YARP_OS_API void yarp_print_trace(FILE* out, const char* file, int line);
124 
125 
126 #endif // YARP_OS_LOG_H
#define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
Definition: Log.h:33
#define YARP_OS_API
Definition: api.h:19
The main, catch-all namespace for YARP.
Definition: numeric.h:47
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:421
#define YARP_NORETURN
Definition: api.h:153