CARLA
Logging.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/Platform.h"
10 
11 #define LIBCARLA_LOG_LEVEL_DEBUG 10
12 #define LIBCARLA_LOG_LEVEL_INFO 20
13 #define LIBCARLA_LOG_LEVEL_WARNING 30
14 #define LIBCARLA_LOG_LEVEL_ERROR 40
15 #define LIBCARLA_LOG_LEVEL_CRITICAL 50
16 #define LIBCARLA_LOG_LEVEL_NONE 100
17 
18 #ifndef LIBCARLA_LOG_LEVEL
19 # ifdef NDEBUG
20 # define LIBCARLA_LOG_LEVEL LIBCARLA_LOG_LEVEL_WARNING
21 # else
22 # define LIBCARLA_LOG_LEVEL LIBCARLA_LOG_LEVEL_INFO
23 # endif // NDEBUG
24 #endif // LIBCARLA_LOG_LEVEL
25 
26 // The following log functions are available, they are only active if
27 // LIBCARLA_LOG_LEVEL is greater equal the function's log level.
28 //
29 // * log_debug
30 // * log_info
31 // * log_error
32 // * log_critical
33 //
34 // And macros
35 //
36 // * LOG_DEBUG_ONLY(/* code here */)
37 // * LOG_INFO_ONLY(/* code here */)
38 
39 // =============================================================================
40 // -- Implementation of log functions ------------------------------------------
41 // =============================================================================
42 
43 #include <iostream>
44 
45 namespace carla {
46 
47 namespace logging {
48 
49  // https://stackoverflow.com/a/27375675
50  template <typename Arg, typename ... Args>
52  static void write_to_stream(std::ostream &out, Arg &&arg, Args && ... args) {
53  out << std::boolalpha << std::forward<Arg>(arg);
54  using expander = int[];
55  (void) expander{0, (void(out << ' ' << std::forward<Args>(args)), 0) ...};
56  }
57 
58  template <typename ... Args>
59  static inline void log(Args && ... args) {
60  logging::write_to_stream(std::cout, std::forward<Args>(args) ..., '\n');
61  }
62 
63 } // namespace logging
64 
65 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_DEBUG
66 
67  template <typename ... Args>
68  static inline void log_debug(Args && ... args) {
69  logging::write_to_stream(std::cout, "DEBUG:", std::forward<Args>(args) ..., '\n');
70  }
71 
72 #else
73 
74  template <typename ... Args>
75  static inline void log_debug(Args && ...) {}
76 
77 #endif
78 
79 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_INFO
80 
81  template <typename ... Args>
82  static inline void log_info(Args && ... args) {
83  logging::write_to_stream(std::cout, "INFO: ", std::forward<Args>(args) ..., '\n');
84  }
85 
86 #else
87 
88  template <typename ... Args>
89  static inline void log_info(Args && ...) {}
90 
91 #endif
92 
93 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_WARNING
94 
95  template <typename ... Args>
96  static inline void log_warning(Args && ... args) {
97  logging::write_to_stream(std::cerr, "WARNING:", std::forward<Args>(args) ..., '\n');
98  }
99 
100 #else
101 
102  template <typename ... Args>
103  static inline void log_warning(Args && ...) {}
104 
105 #endif
106 
107 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_ERROR
108 
109  template <typename ... Args>
110  static inline void log_error(Args && ... args) {
111  logging::write_to_stream(std::cerr, "ERROR:", std::forward<Args>(args) ..., '\n');
112  }
113 
114 #else
115 
116  template <typename ... Args>
117  static inline void log_error(Args && ...) {}
118 
119 #endif
120 
121 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_CRITICAL
122 
123  template <typename ... Args>
124  static inline void log_critical(Args && ... args) {
125  logging::write_to_stream(std::cerr, "CRITICAL:", std::forward<Args>(args) ..., '\n');
126  }
127 
128 #else
129 
130  template <typename ... Args>
131  static inline void log_critical(Args && ...) {}
132 
133 #endif
134 
135 } // namespace carla
136 
137 // =============================================================================
138 // -- Implementation of macros -------------------------------------------------
139 // =============================================================================
140 
141 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_DEBUG
142 # define LOG_DEBUG_ONLY(code) code
143 #else
144 # define LOG_DEBUG_ONLY(code)
145 #endif
146 
147 #if LIBCARLA_LOG_LEVEL <= LIBCARLA_LOG_LEVEL_INFO
148 # define LOG_INFO_ONLY(code) code
149 #else
150 # define LOG_INFO_ONLY(code)
151 #endif
static void log(Args &&... args)
Definition: Logging.h:59
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
static void log_debug(Args &&...)
Definition: Logging.h:75
static LIBCARLA_NOINLINE void write_to_stream(std::ostream &out, Arg &&arg, Args &&... args)
Definition: Logging.h:52
#define LIBCARLA_NOINLINE
Definition: Platform.h:21
static void log_warning(Args &&... args)
Definition: Logging.h:96
static void log_info(Args &&... args)
Definition: Logging.h:82
static void log_critical(Args &&... args)
Definition: Logging.h:124