rcutils  master
C API providing common utilities and data structures.
logging.h
Go to the documentation of this file.
1 // Copyright 2017 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCUTILS__LOGGING_H_
16 #define RCUTILS__LOGGING_H_
17 
18 #include <stdarg.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 
22 #include "rcutils/allocator.h"
23 #include "rcutils/error_handling.h"
26 
27 #if __cplusplus
28 extern "C"
29 {
30 #endif
31 
35 
37 
86 
88 
111 
113 
132 
135 {
137  const char * function_name;
139  const char * file_name;
141  size_t line_number;
143 
146 {
153 };
154 
157 
159 
167  rcutils_log_location_t *, // location
168  int, // severity
169  const char *, // name
170  const char *, // format
171  va_list * // args
172 );
173 
177 
179 
193 
195 
208 
210 
218 
220 
234 
236 
249 
251 
273 int rcutils_logging_get_logger_level(const char * name);
274 
276 
298 int rcutils_logging_get_logger_leveln(const char * name, size_t name_length);
299 
301 
322 rcutils_ret_t rcutils_logging_set_logger_level(const char * name, int level);
323 
325 
341 bool rcutils_logging_logger_is_enabled_for(const char * name, int severity);
342 
344 
370 int rcutils_logging_get_logger_effective_level(const char * name);
371 
373 
392 void rcutils_log(
393  rcutils_log_location_t * location,
394  int severity,
395  const char * name,
396  const char * format,
397  ...);
398 
400 
425  rcutils_log_location_t * location,
426  int severity, const char * name, const char * format, va_list * args);
427 
428 // Provide the compiler with branch prediction information
429 #ifndef _WIN32
430 
434 # define RCUTILS_LIKELY(x) __builtin_expect((x), 1)
435 
439 # define RCUTILS_UNLIKELY(x) __builtin_expect((x), 0)
440 #else
441 
445 # define RCUTILS_LIKELY(x) (x)
446 
450 # define RCUTILS_UNLIKELY(x) (x)
451 #endif // _WIN32
452 
459 #define RCUTILS_LOGGING_AUTOINIT \
460  if (RCUTILS_UNLIKELY(!g_rcutils_logging_initialized)) { \
461  rcutils_ret_t ret = rcutils_logging_initialize(); \
462  if (ret != RCUTILS_RET_OK) { \
463  RCUTILS_SAFE_FWRITE_TO_STDERR( \
464  "[rcutils|" __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) \
465  "] error initializing logging: "); \
466  RCUTILS_SAFE_FWRITE_TO_STDERR(rcutils_get_error_string_safe()); \
467  RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); \
468  rcutils_reset_error(); \
469  } \
470  }
471 
472 #if __cplusplus
473 }
474 #endif
475 
476 #endif // RCUTILS__LOGGING_H_
const char * g_rcutils_log_severity_names[RCUTILS_LOG_SEVERITY_FATAL+1]
The names of severity levels.
int rcutils_logging_get_logger_leveln(const char *name, size_t name_length)
Get the level for a logger and its name length.
The info log level.
Definition: logging.h:149
The warn log level.
Definition: logging.h:150
void rcutils_logging_console_output_handler(rcutils_log_location_t *location, int severity, const char *name, const char *format, va_list *args)
The default output handler outputs log messages to the standard streams.
The unset log level.
Definition: logging.h:147
int rcutils_ret_t
Definition: rcutils_ret.h:23
bool g_rcutils_logging_initialized
The flag if the logging system has been initialized.
void rcutils_log(rcutils_log_location_t *location, int severity, const char *name, const char *format,...)
Log a message.
void rcutils_logging_set_output_handler(rcutils_logging_output_handler_t function)
Set the current output handler.
size_t line_number
The line number containing the log call.
Definition: logging.h:141
int g_rcutils_logging_default_logger_level
The default severity level for loggers.
rcutils_ret_t rcutils_logging_initialize()
Initialize the logging system.
Encapsulation of an allocator.
Definition: allocator.h:43
rcutils_ret_t rcutils_logging_set_logger_level(const char *name, int level)
Set the severity level for a logger.
void(* rcutils_logging_output_handler_t)(rcutils_log_location_t *, int, const char *, const char *, va_list *)
The function signature to log messages.
Definition: logging.h:166
rcutils_logging_output_handler_t rcutils_logging_get_output_handler()
Get the current output handler.
void rcutils_logging_set_default_logger_level(int level)
Set the default severity level for loggers.
rcutils_ret_t rcutils_logging_shutdown()
Shutdown the logging system.
RCUTILS_LOG_SEVERITY
The severity levels of log messages / loggers.
Definition: logging.h:145
const char * file_name
The name of the source file containing the log call.
Definition: logging.h:139
The structure identifying the caller location in the source code.
Definition: logging.h:134
#define RCUTILS_WARN_UNUSED
Definition: macros.h:24
#define RCUTILS_PUBLIC
Definition: visibility_control.h:48
The fatal log level.
Definition: logging.h:152
rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allocator)
Initialize the logging system using the specified allocator.
bool rcutils_logging_logger_is_enabled_for(const char *name, int severity)
Determine if a logger is enabled for a severity level.
int rcutils_logging_get_logger_effective_level(const char *name)
Determine the effective level for a logger.
struct rcutils_log_location_t rcutils_log_location_t
The structure identifying the caller location in the source code.
rcutils_logging_output_handler_t g_rcutils_logging_output_handler
The function pointer of the current output handler.
The debug log level.
Definition: logging.h:148
const char * function_name
The name of the function containing the log call.
Definition: logging.h:137
int rcutils_logging_get_default_logger_level()
Get the default level for loggers.
The error log level.
Definition: logging.h:151
int rcutils_logging_get_logger_level(const char *name)
Get the severity level for a logger.