Program Listing for File logging.h

Return to documentation for file (include/rcutils/logging.h)

// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#ifndef RCUTILS__LOGGING_H_
#define RCUTILS__LOGGING_H_

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>

#include "rcutils/allocator.h"
#include "rcutils/error_handling.h"
#include "rcutils/macros.h"
#include "rcutils/time.h"
#include "rcutils/types/rcutils_ret.h"
#include "rcutils/visibility_control.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define RCUTILS_LOGGING_SEPARATOR_STRING "."

#define RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL RCUTILS_LOG_SEVERITY_INFO

RCUTILS_PUBLIC
extern bool g_rcutils_logging_initialized;


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allocator);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t rcutils_logging_initialize(void);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t rcutils_logging_shutdown(void);

typedef struct rcutils_log_location_s
{
  const char * function_name;
  const char * file_name;
  size_t line_number;
} rcutils_log_location_t;

enum RCUTILS_LOG_SEVERITY
{
  RCUTILS_LOG_SEVERITY_UNSET = 0,
  RCUTILS_LOG_SEVERITY_DEBUG = 10,
  RCUTILS_LOG_SEVERITY_INFO = 20,
  RCUTILS_LOG_SEVERITY_WARN = 30,
  RCUTILS_LOG_SEVERITY_ERROR = 40,
  RCUTILS_LOG_SEVERITY_FATAL = 50,
};

RCUTILS_PUBLIC
extern const char * const g_rcutils_log_severity_names[RCUTILS_LOG_SEVERITY_FATAL + 1];


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t
rcutils_logging_severity_level_from_string(
  const char * severity_string, rcutils_allocator_t allocator, int * severity);


typedef void (* rcutils_logging_output_handler_t)(
  const rcutils_log_location_t *,  // location
  int,  // severity
  const char *,  // name
  rcutils_time_point_value_t,  // timestamp
  const char *,  // format
  va_list *  // args
);

RCUTILS_PUBLIC
extern rcutils_logging_output_handler_t g_rcutils_logging_output_handler;


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_logging_output_handler_t rcutils_logging_get_output_handler();


RCUTILS_PUBLIC
void rcutils_logging_set_output_handler(rcutils_logging_output_handler_t function);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
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);


RCUTILS_PUBLIC
extern int g_rcutils_logging_default_logger_level;


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
int rcutils_logging_get_default_logger_level();


RCUTILS_PUBLIC
void rcutils_logging_set_default_logger_level(int level);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
int rcutils_logging_get_logger_level(const char * name);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
int rcutils_logging_get_logger_leveln(const char * name, size_t name_length);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t rcutils_logging_set_logger_level(const char * name, int level);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
bool rcutils_logging_logger_is_enabled_for(const char * name, int severity);


RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
int rcutils_logging_get_logger_effective_level(const char * name);


RCUTILS_PUBLIC
void rcutils_log(
  const rcutils_log_location_t * location,
  int severity,
  const char * name,
  const char * format,
  ...)
RCUTILS_ATTRIBUTE_PRINTF_FORMAT(4, 5)
;


RCUTILS_PUBLIC
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);

#define RCUTILS_LOGGING_AUTOINIT \
  do { \
    if (RCUTILS_UNLIKELY(!g_rcutils_logging_initialized)) { \
      if (rcutils_logging_initialize() != RCUTILS_RET_OK) { \
        RCUTILS_SAFE_FWRITE_TO_STDERR( \
          "[rcutils|" __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) \
          "] error initializing logging: "); \
        RCUTILS_SAFE_FWRITE_TO_STDERR(rcutils_get_error_string().str); \
        RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); \
        rcutils_reset_error(); \
      } \
    } \
  } while (0)

#ifdef __cplusplus
}
#endif

#endif  // RCUTILS__LOGGING_H_