From 19ca61ac888a47a2eb020649de915efc6c054ec2 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sat, 8 Aug 2026 12:47:46 +0200 Subject: [PATCH] log: add log_callback --- kernel/log.cc | 30 ++++++++++++++++-------------- kernel/log.h | 3 +++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/kernel/log.cc b/kernel/log.cc index 1ce836749..ab7ff40a7 100644 --- a/kernel/log.cc +++ b/kernel/log.cc @@ -80,6 +80,20 @@ static void log_id_cache_clear() log_id_cache.clear(); } +void log_default_callback(std::string_view time_str, std::string_view prefix, std::string_view msg, LogSeverity severity) +{ + (void)severity; + std::string str = stringf("%s%s%s",time_str,prefix,msg); + for (auto f : log_files) + fputs(str.c_str(), f); + + for (auto f : log_streams) + *f << str; +} + +void (*log_callback)(std::string_view time_str, std::string_view prefix, std::string_view msg, LogSeverity severity) = log_default_callback; + + #if defined(_WIN32) && !defined(__MINGW32__) // this will get time information and return it in timeval, simulating gettimeofday() int gettimeofday(struct timeval *tv, struct timezone *tz) @@ -125,10 +139,9 @@ static void logv_string(std::string_view prefix, std::string_view format, std::s if (log_hasher) log_hasher->update(str); + std::string time_str; if (log_time) { - std::string time_str; - if (next_print_log || initial_tv.tv_sec == 0) { next_print_log = false; struct timeval tv; @@ -152,19 +165,8 @@ static void logv_string(std::string_view prefix, std::string_view format, std::s // is then in the first formatted argument if (format == "%s" && str.back() == '\n') next_print_log = true; - - for (auto f : log_files) - fputs(time_str.c_str(), f); - - for (auto f : log_streams) - *f << time_str; } - - for (auto f : log_files) - fputs(str.c_str(), f); - - for (auto f : log_streams) - *f << str; + log_callback(time_str, prefix, strin, severity); RTLIL::Design *design = yosys_get_design(); if (design != nullptr) diff --git a/kernel/log.h b/kernel/log.h index ec273ac79..baf9e8673 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -122,6 +122,9 @@ extern int log_debug_suppressed; void set_verific_logging(void (*cb)(int msg_type, const char *message_id, const char* file_path, unsigned int left_line, unsigned int left_col, unsigned int right_line, unsigned int right_col, const char *msg)); extern void (*log_verific_callback)(int msg_type, const char *message_id, const char* file_path, unsigned int left_line, unsigned int left_col, unsigned int right_line, unsigned int right_col, const char *msg); +void log_default_callback(std::string_view time_str, std::string_view prefix, std::string_view msg, LogSeverity severity); +extern void (*log_callback)(std::string_view time_str, std::string_view prefix, std::string_view msg, LogSeverity severity); + #ifndef NDEBUG static inline bool ys_debug(int n = 0) { if (log_force_debug) return true; log_debug_suppressed += n; return false; } #else