Skip to content

Commit

Permalink
file, function and line added to yasmin logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Oct 28, 2024
1 parent 91c8eef commit af4fd9f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 45 deletions.
25 changes: 15 additions & 10 deletions yasmin/include/yasmin/logs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@

namespace yasmin {

// define raw function pointer type for logging
typedef void (*LogFunction)(const char *, ...);
// Define raw function pointer type for logging with file and function
// parameters
typedef void (*LogFunction)(const char *, const char *, int, const char *, ...);

// declare function pointers as extern
// Declare function pointers as extern
extern LogFunction log_error;
extern LogFunction log_warn;
extern LogFunction log_info;
extern LogFunction log_debug;

// macros to use the function pointers for logging
#define YASMIN_LOG_ERROR(text, ...) yasmin::log_error(text, ##__VA_ARGS__)
#define YASMIN_LOG_WARN(text, ...) yasmin::log_warn(text, ##__VA_ARGS__)
#define YASMIN_LOG_INFO(text, ...) yasmin::log_info(text, ##__VA_ARGS__)
#define YASMIN_LOG_DEBUG(text, ...) yasmin::log_debug(text, ##__VA_ARGS__)

// function to set custom log functions
// Macros to use the function pointers for logging, passing file and function
#define YASMIN_LOG_ERROR(text, ...) \
yasmin::log_error(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__)
#define YASMIN_LOG_WARN(text, ...) \
yasmin::log_warn(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__)
#define YASMIN_LOG_INFO(text, ...) \
yasmin::log_info(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__)
#define YASMIN_LOG_DEBUG(text, ...) \
yasmin::log_debug(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__)

// Function to set custom log functions
void set_loggers(LogFunction error, LogFunction warn, LogFunction info,
LogFunction debug);

Expand Down
26 changes: 15 additions & 11 deletions yasmin/src/yasmin/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,54 @@

namespace yasmin {

// define the default log functions
void default_log_error(const char *text, ...) {
// Define the default log functions with file and function parameters
void default_log_error(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[ERROR] ");
fprintf(stderr, "[ERROR] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

void default_log_warn(const char *text, ...) {
void default_log_warn(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[WARN] ");
fprintf(stderr, "[WARN] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

void default_log_info(const char *text, ...) {
void default_log_info(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[INFO] ");
fprintf(stderr, "[INFO] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

void default_log_debug(const char *text, ...) {
void default_log_debug(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);
fprintf(stderr, "[DEBUG] ");
fprintf(stderr, "[DEBUG] [%s:%s:%d] ", file, function, line);
vfprintf(stderr, text, args);
fprintf(stderr, "\n");
va_end(args);
}

// define the function pointers to be initialized with default log functions
// Define the function pointers, initialized with default log functions
LogFunction log_error = default_log_error;
LogFunction log_warn = default_log_warn;
LogFunction log_info = default_log_info;
LogFunction log_debug = default_log_debug;

// implement the set_loggers function
// Implement the set_loggers function with updated signature
void set_loggers(LogFunction error, LogFunction warn, LogFunction info,
LogFunction debug) {
log_error = error ? error : default_log_error;
Expand Down
34 changes: 25 additions & 9 deletions yasmin/yasmin/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import yasmin
import logging

import yasmin.logs
import inspect
from typing import Callable

__all__ = [
"set_loggers",
Expand All @@ -26,28 +26,44 @@
"YASMIN_LOG_DEBUG",
]


# Define the logging configuration
# define the logging configuration with custom format to include location data
logging.basicConfig(level=logging.NOTSET, format="%(message)s")


def get_caller_info():
frame = inspect.stack()[2]
file = frame.filename
line = frame.lineno
function = frame.function
return file, function, line


def YASMIN_LOG_ERROR(text: str) -> None:
logging.error("[ERROR] " + text)
file, function, line = get_caller_info()
logging.error(f"[ERROR] [{file}:{function}:{line}] {text}")


def YASMIN_LOG_WARN(text: str) -> None:
logging.warning("[WARN] " + text)
file, function, line = get_caller_info()
logging.warning(f"[WARN] [{file}:{function}:{line}] {text}")


def YASMIN_LOG_INFO(text: str) -> None:
logging.info("[INFO] " + text)
file, function, line = get_caller_info()
logging.info(f"[INFO] [{file}:{function}:{line}] {text}")


def YASMIN_LOG_DEBUG(text: str) -> None:
logging.debug("[DEBUG] " + text)
file, function, line = get_caller_info()
logging.debug(f"[DEBUG] [{file}:{function}:{line}] {text}")


def set_loggers(info, warn, debug, error):
def set_loggers(
info: Callable,
warn: Callable,
debug: Callable,
error: Callable,
) -> None:
yasmin.YASMIN_LOG_ERROR = error
yasmin.YASMIN_LOG_WARN = warn
yasmin.YASMIN_LOG_INFO = info
Expand Down
28 changes: 18 additions & 10 deletions yasmin_ros/src/yasmin_ros/ros_logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

namespace yasmin_ros {

void ros_log_error(const char *text, ...) {
void ros_log_error(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);

Expand All @@ -35,10 +36,12 @@ void ros_log_error(const char *text, ...) {
vsnprintf(&buffer[0], buffer.size(), text, args);
va_end(args);

RCLCPP_ERROR(YasminNode::get_instance()->get_logger(), "%s", buffer.c_str());
RCLCPP_ERROR(YasminNode::get_instance()->get_logger(), "[%s:%s:%d] %s", file,
function, line, buffer.c_str());
}

void ros_log_warn(const char *text, ...) {
void ros_log_warn(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);

Expand All @@ -51,10 +54,12 @@ void ros_log_warn(const char *text, ...) {
vsnprintf(&buffer[0], buffer.size(), text, args);
va_end(args);

RCLCPP_WARN(YasminNode::get_instance()->get_logger(), "%s", buffer.c_str());
RCLCPP_WARN(YasminNode::get_instance()->get_logger(), "[%s:%s:%d] %s", file,
function, line, buffer.c_str());
}

void ros_log_info(const char *text, ...) {
void ros_log_info(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);

Expand All @@ -67,10 +72,12 @@ void ros_log_info(const char *text, ...) {
vsnprintf(&buffer[0], buffer.size(), text, args);
va_end(args);

RCLCPP_INFO(YasminNode::get_instance()->get_logger(), "%s", buffer.c_str());
RCLCPP_INFO(YasminNode::get_instance()->get_logger(), "[%s:%s:%d] %s", file,
function, line, buffer.c_str());
}

void ros_log_debug(const char *text, ...) {
void ros_log_debug(const char *file, const char *function, int line,
const char *text, ...) {
va_list args;
va_start(args, text);

Expand All @@ -83,11 +90,12 @@ void ros_log_debug(const char *text, ...) {
vsnprintf(&buffer[0], buffer.size(), text, args);
va_end(args);

RCLCPP_DEBUG(YasminNode::get_instance()->get_logger(), "%s", buffer.c_str());
RCLCPP_DEBUG(YasminNode::get_instance()->get_logger(), "[%s:%s:%d] %s", file,
function, line, buffer.c_str());
}

void set_ros_loggers() {
yasmin::set_loggers(ros_log_info, ros_log_warn, ros_log_debug, ros_log_error);
yasmin::set_loggers(ros_log_error, ros_log_warn, ros_log_info, ros_log_debug);
}

} // namespace yasmin_ros
} // namespace yasmin_ros
45 changes: 40 additions & 5 deletions yasmin_ros/yasmin_ros/ros_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,50 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.


import inspect
import yasmin
from yasmin_ros.yasmin_node import YasminNode


def set_ros_loggers() -> None:
__all__ = ["set_ros_loggers"]


def get_caller_info():
frame = inspect.stack()[2]
file = frame.filename
line = frame.lineno
function = frame.function
return file, function, line


def ros_log_error(text: str) -> None:
file, function, line = get_caller_info()
node = YasminNode.get_instance()
node.get_logger().error(f"[{file}:{function}:{line}] {text}")


def ros_log_warn(text: str) -> None:
file, function, line = get_caller_info()
node = YasminNode.get_instance()
node.get_logger().warn(f"[{file}:{function}:{line}] {text}")


def ros_log_info(text: str) -> None:
file, function, line = get_caller_info()
node = YasminNode.get_instance()
node.get_logger().info(f"[{file}:{function}:{line}] {text}")


def ros_log_debug(text: str) -> None:
file, function, line = get_caller_info()
node = YasminNode.get_instance()
node.get_logger().debug(f"[{file}:{function}:{line}] {text}")


def set_ros_loggers() -> None:
yasmin.set_loggers(
node.get_logger().info,
node.get_logger().warn,
node.get_logger().debug,
node.get_logger().error,
ros_log_info,
ros_log_warn,
ros_log_debug,
ros_log_error,
)

0 comments on commit af4fd9f

Please sign in to comment.