Skip to content

Commit

Permalink
short logs (by short file) and short UUID for yasmin_node
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Oct 30, 2024
1 parent e98f5f8 commit bd4c3a0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
21 changes: 17 additions & 4 deletions yasmin/include/yasmin/logs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cstdarg>
#include <cstdio>
#include <cstring>

namespace yasmin {

Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion yasmin/yasmin/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import os
import yasmin
import logging
import inspect
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions yasmin_ros/src/yasmin_ros/yasmin_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 5 additions & 13 deletions yasmin_ros/yasmin_ros/ros_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,34 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.


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}")

Expand Down
2 changes: 1 addition & 1 deletion yasmin_ros/yasmin_ros/yasmin_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit bd4c3a0

Please sign in to comment.