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 #include <iosfwd>
14 
15 #if defined (__GNUC__)
16 # define __YFUNCTION__ __PRETTY_FUNCTION__
17 #elif defined(_MSC_VER)
18 # define __YFUNCTION__ __FUNCSIG__
19 #elif (__cplusplus <= 199711)
20 # define __YFUNCTION__ __func__
21 #else
22 # define __YFUNCTION__ "(unknown function)"
23 #endif // __GNUC__
24 
25 // check arguments of the c-style debug functions to make sure that the
26 // arguments supplied have types appropriate to the format string
27 // specified, and that the conversions specified in the format string
28 // make sense. On gcc the warning is enabled by -Wformat.
29 #if defined(__GNUC__)
30 # define YARP_ATTRIBUTE_FORMAT(style, fmt, args) __attribute__((format(printf, (fmt), (args))))
31 #else
32 # define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
33 #endif
34 
35 
36 namespace yarp { namespace os { class LogStream; }}
37 namespace yarp { namespace os { namespace impl { class LogImpl; }}}
38 
39 
40 namespace yarp {
41 namespace os {
42 
44 {
45 public:
46 
47  Log(const char *file,
48  const unsigned int line,
49  const char *func);
50  Log();
51  virtual ~Log();
52 
53  enum LogType {
59  FatalType
60  };
61 
62  void trace(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
63  void debug(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
64  void info(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
65  void warning(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
66  void error(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
67  YARP_NORETURN void fatal(const char *msg, ...) const YARP_ATTRIBUTE_FORMAT(printf, 2, 3);
68 
69  LogStream trace() const;
70  LogStream debug() const;
71  LogStream info() const;
72  LogStream warning() const;
73  LogStream error() const;
74  LogStream fatal() const;
75 
76  typedef void (*LogCallback)(yarp::os::Log::LogType,
77  const char *,
78  const char *,
79  const unsigned int,
80  const char *);
81 
82  static void setLogCallback(LogCallback);
83 
84 private:
85  yarp::os::impl::LogImpl * const mPriv;
86 
87  static LogCallback print_callback;
88  static LogCallback forward_callback;
89 
90  friend class LogStream;
91 }; // class Log
92 
93 } // namespace os
94 } // namespace yarp
95 
96 
97 #define yTrace yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).trace
98 #define yDebug yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).debug
99 #define yInfo yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).info
100 #define yWarning yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).warning
101 #define yError yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).error
102 #define yFatal yarp::os::Log(__FILE__, __LINE__, __YFUNCTION__).fatal
103 
104 #define yAssert(x) if (!(x)) { yFatal("Assertion failure at %s:%d (%s)", __FILE__, __LINE__, #x); }
105 
106 #define YARP_FIXME_NOTIMPLEMENTED(what) yWarning("FIXME: %s not yet implemented", what);
107 
108 
112 YARP_OS_API void yarp_print_trace(FILE *out, const char *file, int line);
113 
114 
115 #endif // YARP_OS_LOG_H
#define YARP_ATTRIBUTE_FORMAT(style, fmt, args)
Definition: Log.h:32
#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:420
#define YARP_NORETURN
Definition: api.h:153