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 
16 
17 #ifndef RCUTILS__LOGGING_H_
18 #define RCUTILS__LOGGING_H_
19 
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 
24 #include "rcutils/allocator.h"
25 #include "rcutils/error_handling.h"
26 #include "rcutils/macros.h"
27 #include "rcutils/time.h"
30 
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif
35 
37 #define RCUTILS_LOGGING_SEPARATOR_STRING "."
38 
43 #define RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL RCUTILS_LOG_SEVERITY_INFO
44 
48 
50 
108 
110 
133 
135 
154 
157 {
159  const char * function_name;
161  const char * file_name;
163  size_t line_number;
165 
168 {
175 };
176 
179 extern const char * const g_rcutils_log_severity_names[RCUTILS_LOG_SEVERITY_FATAL + 1];
180 
182 
202  const char * severity_string, rcutils_allocator_t allocator, int * severity);
203 
205 
214  const rcutils_log_location_t *, // location
215  int, // severity
216  const char *, // name
217  rcutils_time_point_value_t, // timestamp
218  const char *, // format
219  va_list * // args
220 );
221 
225 
227 
241 
243 
256 
258 
282  const rcutils_log_location_t * location,
283  int severity, const char * name, rcutils_time_point_value_t timestamp,
284  const char * msg, rcutils_char_array_t * logging_output);
285 
287 
295 
297 
311 
313 
330 
332 
354 int rcutils_logging_get_logger_level(const char * name);
355 
357 
379 int rcutils_logging_get_logger_leveln(const char * name, size_t name_length);
380 
382 
403 rcutils_ret_t rcutils_logging_set_logger_level(const char * name, int level);
404 
406 
423 bool rcutils_logging_logger_is_enabled_for(const char * name, int severity);
424 
426 
452 int rcutils_logging_get_logger_effective_level(const char * name);
453 
455 
475 void rcutils_log(
476  const rcutils_log_location_t * location,
477  int severity,
478  const char * name,
479  const char * format,
480  ...)
484 ;
485 
487 
514  const rcutils_log_location_t * location,
515  int severity, const char * name, rcutils_time_point_value_t timestamp,
516  const char * format, va_list * args);
517 
524 #define RCUTILS_LOGGING_AUTOINIT \
525  do { \
526  if (RCUTILS_UNLIKELY(!g_rcutils_logging_initialized)) { \
527  if (rcutils_logging_initialize() != RCUTILS_RET_OK) { \
528  RCUTILS_SAFE_FWRITE_TO_STDERR( \
529  "[rcutils|" __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) \
530  "] error initializing logging: "); \
531  RCUTILS_SAFE_FWRITE_TO_STDERR(rcutils_get_error_string().str); \
532  RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); \
533  rcutils_reset_error(); \
534  } \
535  } \
536  } while (0)
537 
538 #ifdef __cplusplus
539 }
540 #endif
541 
542 #endif // RCUTILS__LOGGING_H_
rcutils_logging_logger_is_enabled_for
bool rcutils_logging_logger_is_enabled_for(const char *name, int severity)
Determine if a logger is enabled for a severity level.
rcutils_logging_severity_level_from_string
rcutils_ret_t rcutils_logging_severity_level_from_string(const char *severity_string, rcutils_allocator_t allocator, int *severity)
Get a severity value from its string representation (e.g. DEBUG).
rcutils_logging_initialize_with_allocator
rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allocator)
Initialize the logging system using the specified allocator.
rcutils_logging_shutdown
rcutils_ret_t rcutils_logging_shutdown(void)
Shutdown the logging system.
rcutils_log
void rcutils_log(const rcutils_log_location_t *location, int severity, const char *name, const char *format,...)
Log a message.
rcutils_logging_set_logger_level
rcutils_ret_t rcutils_logging_set_logger_level(const char *name, int level)
Set the severity level for a logger.
rcutils_logging_get_default_logger_level
int rcutils_logging_get_default_logger_level()
Get the default level for loggers.
RCUTILS_ATTRIBUTE_PRINTF_FORMAT
#define RCUTILS_ATTRIBUTE_PRINTF_FORMAT(format_string_index, first_to_check_index)
Definition: macros.h:102
RCUTILS_LOG_SEVERITY_DEBUG
@ RCUTILS_LOG_SEVERITY_DEBUG
The debug log level.
Definition: logging.h:170
rcutils_logging_initialize
rcutils_ret_t rcutils_logging_initialize(void)
Initialize the logging system.
macros.h
rcutils_log_location_t
struct rcutils_log_location_t rcutils_log_location_t
The structure identifying the caller location in the source code.
rcutils_logging_get_logger_level
int rcutils_logging_get_logger_level(const char *name)
Get the severity level for a logger.
RCUTILS_LOG_SEVERITY
RCUTILS_LOG_SEVERITY
The severity levels of log messages / loggers.
Definition: logging.h:167
rcutils_logging_set_output_handler
void rcutils_logging_set_output_handler(rcutils_logging_output_handler_t function)
Set the current output handler.
rcutils_logging_output_handler_t
void(* rcutils_logging_output_handler_t)(const rcutils_log_location_t *, int, const char *, rcutils_time_point_value_t, const char *, va_list *)
The function signature to log messages.
Definition: logging.h:213
g_rcutils_logging_default_logger_level
int g_rcutils_logging_default_logger_level
The default severity level for loggers.
rcutils_log_location_t::function_name
const char * function_name
The name of the function containing the log call.
Definition: logging.h:159
RCUTILS_LOG_SEVERITY_INFO
@ RCUTILS_LOG_SEVERITY_INFO
The info log level.
Definition: logging.h:171
g_rcutils_logging_initialized
bool g_rcutils_logging_initialized
The flag if the logging system has been initialized.
rcutils_logging_get_output_handler
rcutils_logging_output_handler_t rcutils_logging_get_output_handler()
Get the current output handler.
time.h
g_rcutils_logging_output_handler
rcutils_logging_output_handler_t g_rcutils_logging_output_handler
The function pointer of the current output handler.
rcutils_logging_console_output_handler
void rcutils_logging_console_output_handler(const rcutils_log_location_t *location, int severity, const char *name, rcutils_time_point_value_t timestamp, const char *format, va_list *args)
The default output handler outputs log messages to the standard streams.
rcutils_ret_t
int rcutils_ret_t
The type that holds a return value for an rcutils operation.
Definition: rcutils_ret.h:26
rcutils_logging_get_logger_leveln
int rcutils_logging_get_logger_leveln(const char *name, size_t name_length)
Get the level for a logger and its name length.
rcutils_logging_set_default_logger_level
void rcutils_logging_set_default_logger_level(int level)
Set the default severity level for loggers.
RCUTILS_LOG_SEVERITY_UNSET
@ RCUTILS_LOG_SEVERITY_UNSET
The unset log level.
Definition: logging.h:169
RCUTILS_WARN_UNUSED
#define RCUTILS_WARN_UNUSED
A macro to make the compiler warn when the return value of a function is not used.
Definition: macros.h:27
rcutils_ret.h
rcutils_time_point_value_t
int64_t rcutils_time_point_value_t
A single point in time, measured in nanoseconds since the Unix epoch.
Definition: time.h:48
RCUTILS_LOG_SEVERITY_FATAL
@ RCUTILS_LOG_SEVERITY_FATAL
The fatal log level.
Definition: logging.h:174
visibility_control.h
error_handling.h
rcutils_log_location_t
The structure identifying the caller location in the source code.
Definition: logging.h:156
rcutils_logging_format_message
rcutils_ret_t rcutils_logging_format_message(const rcutils_log_location_t *location, int severity, const char *name, rcutils_time_point_value_t timestamp, const char *msg, rcutils_char_array_t *logging_output)
Formats a log message according to RCUTILS_CONSOLE_OUTPUT_FORMAT.
rcutils_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:47
allocator.h
g_rcutils_log_severity_names
const char *const g_rcutils_log_severity_names[RCUTILS_LOG_SEVERITY_FATAL+1]
The names of severity levels.
RCUTILS_PUBLIC
#define RCUTILS_PUBLIC
Definition: visibility_control.h:23
RCUTILS_LOG_SEVERITY_ERROR
@ RCUTILS_LOG_SEVERITY_ERROR
The error log level.
Definition: logging.h:173
RCUTILS_LOG_SEVERITY_WARN
@ RCUTILS_LOG_SEVERITY_WARN
The warn log level.
Definition: logging.h:172
va_list
rcutils_char_array_t
The structure holding the metadata for a char array.
Definition: char_array.h:32
rcutils_logging_get_logger_effective_level
int rcutils_logging_get_logger_effective_level(const char *name)
Determine the effective level for a logger.
rcutils_log_location_t::line_number
size_t line_number
The line number containing the log call.
Definition: logging.h:163
rcutils_log_location_t::file_name
const char * file_name
The name of the source file containing the log call.
Definition: logging.h:161