Skip to content

Commit 9cf5234

Browse files
committed
cx_utils: also created latest symlink
1 parent f196077 commit 9cf5234

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

cx_utils/src/clips_env_context.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "cx_utils/clips_env_context.hpp"
1717

1818
#include "rclcpp/logging.hpp"
19+
#include <filesystem>
1920
#include <spdlog/sinks/basic_file_sink.h>
2021
#include <spdlog/sinks/stdout_color_sinks.h>
2122

@@ -38,16 +39,27 @@ CLIPSLogger::CLIPSLogger(const char *component, bool log_to_file,
3839
auto now = std::chrono::system_clock::now();
3940
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
4041

41-
std::ostringstream oss;
42-
oss << std::put_time(std::localtime(&now_time), "%Y-%m-%d-%H-%M-%S");
43-
44-
std::string formatted_time = oss.str();
4542
if (log_to_file) {
46-
clips_logger_ = spdlog::basic_logger_st(
47-
(component_ ? (std::string)component_ : "CLIPS"),
48-
(rclcpp::get_logging_directory().string() + "/" +
49-
(component_ ? (std::string)component_ : "clips") + "_" +
50-
formatted_time + ".log"));
43+
std::ostringstream oss;
44+
oss << std::put_time(std::localtime(&now_time), "%Y-%m-%d-%H-%M-%S");
45+
46+
std::string formatted_time = oss.str();
47+
std::string base_name = component_ ? std::string(component_) : "clips";
48+
std::string log_filename = rclcpp::get_logging_directory().string() + "/" +
49+
base_name + "_" + formatted_time + ".log";
50+
clips_logger_ = spdlog::basic_logger_st(base_name, log_filename);
51+
std::string symlink_path = rclcpp::get_logging_directory().string() + "/" +
52+
base_name + "_latest.log";
53+
54+
namespace fs = std::filesystem;
55+
try {
56+
if (fs::exists(symlink_path) || fs::is_symlink(symlink_path)) {
57+
fs::remove(symlink_path);
58+
}
59+
fs::create_symlink(log_filename, symlink_path);
60+
} catch (const std::exception &e) {
61+
std::cerr << "Failed to create symlink: " << e.what() << std::endl;
62+
}
5163
} else {
5264
// Disable the logger by setting the log level to a level that filters out
5365
// all messages

0 commit comments

Comments
 (0)