Class Logger

Nested Relationships

Nested Types

Class Documentation

class Logger

Static utility class for time profiling and logging in ROS2.

Provides static methods to time code sections, collect and print log messages, and print a formatted report. Uses rclcpp logging macros and supports both immediate and deferred printing.

Public Static Functions

static void set_logger(const rclcpp::Logger &logger)

Set the logger to be used for RCLCPP macros.

Parameters:

logger – The rclcpp::Logger instance (e.g., from your node).

static void tick(const std::string &clockName)

Start timing a named code section.

Parameters:

clockName – Name of the code section/task.

static std::chrono::duration<double> tock(const std::string &clockName)

Stop timing a named code section and optionally print the result.

Parameters:

clockName – Name of the code section/task.

Returns:

Duration of this run.

static void loginfo(const std::string &msg)

Add an info message to the log queue or print immediately.

Parameters:

msg – The message to log.

static void logwarn(const std::string &msg)

Add a warning message to the log queue or print immediately.

Parameters:

msg – The warning message to log.

static void print_report()

Print a formatted report of all timed tasks and queued log messages.

Public Static Attributes

static bool print_immediately

If true, logs and timings are printed immediately; otherwise, they are queued for the report.

struct Task

Stores timing metrics for a named code section.

Public Functions

Task(std::chrono::steady_clock::time_point start)

Construct a new Task object and start timing.

Parameters:

start – Start time point.

void activate(std::chrono::steady_clock::time_point now)

Activate (start) the timer.

Parameters:

now – Current time point.

std::chrono::duration<double> stop(std::chrono::steady_clock::time_point now)

Stop the timer and update statistics.

Parameters:

now – Current time point.

Returns:

Duration of this run.

Public Members

bool active

True if the timer is running.

std::chrono::steady_clock::time_point start_time

Start time of the task.

std::chrono::duration<double> last_duration

Duration of the last run.

std::chrono::duration<double> max_duration

Maximum duration recorded.

std::chrono::duration<double> total_time_active

Total accumulated time.

int count

Number of times the task was timed.