diff --git a/yasmin/include/yasmin/logs.hpp b/yasmin/include/yasmin/logs.hpp index 2942ecf..4d78223 100644 --- a/yasmin/include/yasmin/logs.hpp +++ b/yasmin/include/yasmin/logs.hpp @@ -18,6 +18,7 @@ #include #include +#include namespace yasmin { @@ -32,14 +33,26 @@ extern LogFunction log_info; extern LogFunction log_debug; // Macros to use the function pointers for logging, passing file and function +inline const char *extract_filename(const char *path) { + const char *filename = std::strrchr(path, '/'); + if (!filename) { + filename = std::strrchr(path, '\\'); // Handle Windows-style paths + } + return filename ? filename + 1 : path; +} + #define YASMIN_LOG_ERROR(text, ...) \ - yasmin::log_error(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + yasmin::log_error(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \ + ##__VA_ARGS__) #define YASMIN_LOG_WARN(text, ...) \ - yasmin::log_warn(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + yasmin::log_warn(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \ + ##__VA_ARGS__) #define YASMIN_LOG_INFO(text, ...) \ - yasmin::log_info(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + yasmin::log_info(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \ + ##__VA_ARGS__) #define YASMIN_LOG_DEBUG(text, ...) \ - yasmin::log_debug(__FILE__, __FUNCTION__, __LINE__, text, ##__VA_ARGS__) + yasmin::log_debug(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \ + ##__VA_ARGS__) // Function to set custom log functions void set_loggers(LogFunction error, LogFunction warn, LogFunction info, diff --git a/yasmin/yasmin/logs.py b/yasmin/yasmin/logs.py index 38fbd92..b61b225 100644 --- a/yasmin/yasmin/logs.py +++ b/yasmin/yasmin/logs.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os import yasmin import logging import inspect @@ -24,6 +25,7 @@ "YASMIN_LOG_WARN", "YASMIN_LOG_INFO", "YASMIN_LOG_DEBUG", + "get_caller_info", ] # define the logging configuration with custom format to include location data @@ -32,7 +34,7 @@ def get_caller_info(): frame = inspect.stack()[2] - file = frame.filename + file = os.path.basename(frame.filename) line = frame.lineno function = frame.function return file, function, line diff --git a/yasmin_ros/src/yasmin_ros/yasmin_node.cpp b/yasmin_ros/src/yasmin_ros/yasmin_node.cpp index ad17afe..2dafd0d 100644 --- a/yasmin_ros/src/yasmin_ros/yasmin_node.cpp +++ b/yasmin_ros/src/yasmin_ros/yasmin_node.cpp @@ -25,15 +25,14 @@ std::string generateUUID() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, 15); + auto rand_hex_digit = [&gen, &dis]() { constexpr char hex_digits[] = "0123456789abcdef"; return hex_digits[dis(gen)]; }; std::stringstream ss; - for (int i = 0; i < 32; ++i) { - if (i == 8 || i == 12 || i == 16 || i == 20) - ss << "_"; + for (int i = 0; i < 16; ++i) { ss << rand_hex_digit(); } return ss.str(); diff --git a/yasmin_ros/yasmin_ros/ros_logs.py b/yasmin_ros/yasmin_ros/ros_logs.py index fe576a6..8e0a73b 100644 --- a/yasmin_ros/yasmin_ros/ros_logs.py +++ b/yasmin_ros/yasmin_ros/ros_logs.py @@ -14,42 +14,34 @@ # along with this program. If not, see . -import inspect import yasmin +import yasmin.logs from yasmin_ros.yasmin_node import YasminNode __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() + file, function, line = yasmin.logs.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() + file, function, line = yasmin.logs.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() + file, function, line = yasmin.logs.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() + file, function, line = yasmin.logs.get_caller_info() node = YasminNode.get_instance() node.get_logger().debug(f"[{file}:{function}:{line}] {text}") diff --git a/yasmin_ros/yasmin_ros/yasmin_node.py b/yasmin_ros/yasmin_ros/yasmin_node.py index b7f1be4..8ea45ee 100644 --- a/yasmin_ros/yasmin_ros/yasmin_node.py +++ b/yasmin_ros/yasmin_ros/yasmin_node.py @@ -42,7 +42,7 @@ def __init__(self) -> None: if not YasminNode._instance is None: raise RuntimeError("This class is a Singleton") - super().__init__(f"yasmin_{str(uuid.uuid4()).replace('-', '_')}_node") + super().__init__(f"yasmin_{str(uuid.uuid4()).replace('-', '')[:16]}_node") # executor self._executor = MultiThreadedExecutor()