Implementation:CARLA simulator Carla Logging
| Knowledge Sources | |
|---|---|
| Domains | Logging, Infrastructure |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Logging provides compile-time configurable log level functions and macros for outputting debug, info, warning, error, and critical messages throughout the LibCarla codebase.
Description
The Logging.h header defines a hierarchical logging system controlled by the LIBCARLA_LOG_LEVEL preprocessor macro. Log levels are defined as numeric constants:
- LIBCARLA_LOG_LEVEL_DEBUG = 10
- LIBCARLA_LOG_LEVEL_INFO = 20
- LIBCARLA_LOG_LEVEL_WARNING = 30
- LIBCARLA_LOG_LEVEL_ERROR = 40
- LIBCARLA_LOG_LEVEL_CRITICAL = 50
- LIBCARLA_LOG_LEVEL_NONE = 100
If LIBCARLA_LOG_LEVEL is not explicitly defined, it defaults to WARNING in release builds (NDEBUG defined) or INFO in debug builds.
Log functions:
Each log level has a corresponding function template: log_debug, log_info, log_warning, log_error, log_critical. When the compile-time log level is at or below the function's level, the function writes a prefixed message to std::cout (debug/info) or std::cerr (warning/error/critical). When the level is above, the function compiles to a no-op (empty body with unused variadic arguments).
The core write_to_stream function in carla::logging namespace uses a parameter pack expansion trick with std::initializer_list<int> to print all arguments space-separated with std::boolalpha formatting. It is marked LIBCARLA_NOINLINE to prevent inlining of the output logic.
A general-purpose carla::logging::log function is always available and writes to std::cout without any prefix.
Macros:
- LOG_DEBUG_ONLY(code): Compiles the enclosed code only if debug logging is active
- LOG_INFO_ONLY(code): Compiles the enclosed code only if info logging is active
Usage
Include Logging.h in any LibCarla source file and call log_debug, log_info, log_warning, log_error, or log_critical with any number of arguments. The arguments are automatically space-separated in the output. Set LIBCARLA_LOG_LEVEL at compile time to control which messages are included.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/Logging.h
Signature
namespace carla {
namespace logging {
template <typename Arg, typename... Args>
static void write_to_stream(std::ostream &out, Arg &&arg, Args &&... args);
template <typename... Args>
static inline void log(Args &&... args);
}
template <typename... Args> static inline void log_debug(Args &&... args);
template <typename... Args> static inline void log_info(Args &&... args);
template <typename... Args> static inline void log_warning(Args &&... args);
template <typename... Args> static inline void log_error(Args &&... args);
template <typename... Args> static inline void log_critical(Args &&... args);
} // namespace carla
#define LOG_DEBUG_ONLY(code) ...
#define LOG_INFO_ONLY(code) ...
Import
#include "carla/Logging.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| args | Args&&... | Yes | Variadic arguments to be printed space-separated |
Outputs
| Name | Type | Description |
|---|---|---|
| (console output) | text | Formatted log message written to stdout (debug/info) or stderr (warning/error/critical) |
Usage Examples
#include "carla/Logging.h"
// Basic logging at different levels
carla::log_debug("session", sessionId, "started at", timestamp);
carla::log_info("Connected to server:", address, "port:", port);
carla::log_warning("Buffer pool running low:", available, "remaining");
carla::log_error("Failed to connect:", ec.message());
carla::log_critical("Unrecoverable error in sensor", sensorId);
// Conditional debug code
LOG_DEBUG_ONLY(
auto stats = ComputeStats();
carla::log_debug("Stats:", stats.fps, "fps");
)