diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index 626ef014c..3ee7c4d9a 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -1942,9 +1942,9 @@ void AstModule::loadconfig() const flag_autowire = autowire; } -void AstNode::formatted_input_error(std::string str) const +void AstNode::formatted_input_error(std::string_view format, std::string str) const { - log_file_error(*location.begin.filename, location.begin.line, "%s", std::move(str)); + logger().log_formatted_file_error(*location.begin.filename, location.begin.line, format, std::move(str)); } YOSYS_NAMESPACE_END diff --git a/frontends/ast/ast.h b/frontends/ast/ast.h index f92b4a5b8..5bca80847 100644 --- a/frontends/ast/ast.h +++ b/frontends/ast/ast.h @@ -379,11 +379,11 @@ namespace AST AstNode *get_struct_member() const; // helper to print errors from simplify/genrtlil code - [[noreturn]] void formatted_input_error(std::string str) const; + [[noreturn]] void formatted_input_error(std::string_view format, std::string str) const; template [[noreturn]] void input_error(FmtString...> fmt, const Args &... args) const { - formatted_input_error(fmt.format(args...)); + formatted_input_error(fmt.format_string(), fmt.format(args...)); } }; diff --git a/kernel/log.cc b/kernel/log.cc index 1eeb2cbbd..ffe8fde7d 100644 --- a/kernel/log.cc +++ b/kernel/log.cc @@ -273,7 +273,7 @@ void LogManager::log_formatted_header(RTLIL::Design *design, std::string_view fo log_stderr_force = false; } -void LogManager::log_formatted_warning(std::string_view prefix, std::string message) +void LogManager::log_formatted_warning(std::string_view prefix, std::string_view format, std::string message) { log_assert(!Multithreading::active()); @@ -294,7 +294,7 @@ void LogManager::log_formatted_warning(std::string_view prefix, std::string mess for (auto &re : log_werror_regexes) if (std::regex_search(message, re)) - log_formatted_error(message); + log_formatted_error(format, message); bool warning_match = false; for (auto &[_, item] : log_expect_warning) @@ -311,12 +311,12 @@ void LogManager::log_formatted_warning(std::string_view prefix, std::string mess if (log_warnings.count(message)) { - log_formatted_string(prefix, "%s", message, LogSeverity::LOG_INFO); + log_formatted_string(prefix, format, message, LogSeverity::LOG_INFO); flush(); } else { - log_formatted_string(prefix, "%s", message, LogSeverity::LOG_WARNING); + log_formatted_string(prefix, format, message, LogSeverity::LOG_WARNING); flush(); log_warnings.insert(message); } @@ -328,15 +328,16 @@ void LogManager::log_formatted_warning(std::string_view prefix, std::string mess } } -void LogManager::log_formatted_file_warning(std::string_view filename, int lineno, std::string str) +void LogManager::log_formatted_file_warning(std::string_view filename, int lineno, std::string_view format, std::string str) { std::string prefix = stringf("%s:%d: Warning: ", filename, lineno); - log_formatted_warning(prefix, std::move(str)); + log_formatted_warning(prefix, format, std::move(str)); } -void LogManager::log_formatted_file_info(std::string_view filename, int lineno, std::string str) +void LogManager::log_formatted_file_info(std::string_view filename, int lineno, std::string_view format, std::string str) { - log("%s:%d: Info: %s", filename, lineno, str); + std::string prefix = stringf("%s:%d: Info: ", filename, lineno); + log_formatted_string(prefix, format, std::move(str), LogSeverity::LOG_INFO); } void LogManager::log_suppressed() { @@ -348,13 +349,13 @@ void LogManager::log_suppressed() { } [[noreturn]] -void LogManager::log_error_with_prefix(std::string_view prefix, std::string message) +void LogManager::log_error_with_prefix(std::string_view prefix, std::string_view format, std::string message) { int bak_log_make_debug = log_make_debug; log_make_debug = 0; log_suppressed(); - log_formatted_string(prefix, "%s", message, LogSeverity::LOG_ERROR); + log_formatted_string(prefix, format, message, LogSeverity::LOG_ERROR); flush(); log_make_debug = bak_log_make_debug; @@ -384,10 +385,10 @@ void LogManager::log_error_with_prefix(std::string_view prefix, std::string mess #endif } -void LogManager::log_formatted_file_error(std::string_view filename, int lineno, std::string str) +void LogManager::log_formatted_file_error(std::string_view filename, int lineno, std::string_view format, std::string str) { std::string prefix = stringf("%s:%d: ERROR: ", filename, lineno); - log_error_with_prefix(prefix, str); + log_error_with_prefix(prefix, format, str); } void LogManager::log_experimental(const std::string &str) @@ -398,9 +399,9 @@ void LogManager::log_experimental(const std::string &str) } } -void LogManager::log_formatted_error(std::string str) +void LogManager::log_formatted_error(std::string_view format, std::string str) { - log_error_with_prefix("ERROR: ", std::move(str)); + log_error_with_prefix("ERROR: ", format, std::move(str)); } void log_assert_failure(const char *expr, const char *file, int line) @@ -418,16 +419,16 @@ void log_yosys_abort_message(std::string_view file, int line, std::string_view f log_error("Abort in %s:%d (%s): %s\n", file, line, func, message); } -void LogManager::log_formatted_cmd_error(std::string message) +void LogManager::log_formatted_cmd_error(std::string_view format, std::string message) { if (log_cmd_error_throw) { - log_formatted_string("ERROR: ", "%s", message, LogSeverity::LOG_ERROR); + log_formatted_string("ERROR: ", format, message, LogSeverity::LOG_ERROR); flush(); throw log_cmd_error_exception(); } - log_formatted_error(message); + log_formatted_error(format, message); } void LogManager::log_spacer() diff --git a/kernel/log.h b/kernel/log.h index 6a54304db..8fe826e13 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -394,14 +394,14 @@ public: void log_formatted_string(std::string_view prefix, std::string_view format, std::string str, LogSeverity severity); void log_formatted_header(RTLIL::Design *design, std::string_view format, std::string str); - void log_formatted_warning(std::string_view prefix, std::string message); - void log_formatted_file_warning(std::string_view filename, int lineno, std::string str); - void log_formatted_file_info(std::string_view filename, int lineno, std::string str); + void log_formatted_warning(std::string_view prefix, std::string_view format, std::string message); + void log_formatted_file_warning(std::string_view filename, int lineno, std::string_view format, std::string str); + void log_formatted_file_info(std::string_view filename, int lineno, std::string_view format, std::string str); void log_suppressed(); - [[noreturn]] void log_formatted_file_error(std::string_view filename, int lineno, std::string str); + [[noreturn]] void log_formatted_file_error(std::string_view filename, int lineno, std::string_view format, std::string str); void log_experimental(const std::string &str); - [[noreturn]] void log_formatted_error(std::string str); - [[noreturn]] void log_formatted_cmd_error(std::string message); + [[noreturn]] void log_formatted_error(std::string_view format, std::string str); + [[noreturn]] void log_formatted_cmd_error(std::string_view format, std::string message); void log_spacer(); void log_push(); void log_pop(); @@ -420,7 +420,7 @@ public: private: void logv_string(std::string_view prefix, std::string_view format, std::string str_in, LogSeverity severity); - [[noreturn]] void log_error_with_prefix(std::string_view prefix, std::string message); + [[noreturn]] void log_error_with_prefix(std::string_view prefix, std::string_view format, std::string message); std::vector> log_sinks; int log_verbose_level = 0; @@ -488,13 +488,13 @@ inline void log_header(RTLIL::Design *design, FmtString...> f template inline void log_warning(FmtString...> fmt, const Args &... args) { - logger().log_formatted_warning("Warning: ", fmt.format(args...)); + logger().log_formatted_warning("Warning: ", fmt.format_string(), fmt.format(args...)); } template inline void log_warning_noprefix(FmtString...> fmt, const Args &... args) { - logger().log_formatted_warning({}, fmt.format(args...)); + logger().log_formatted_warning({}, fmt.format_string(), fmt.format(args...)); } inline void log_experimental(const std::string &str) @@ -506,31 +506,31 @@ inline void log_experimental(const std::string &str) template void log_file_warning(std::string_view filename, int lineno, FmtString...> fmt, const Args &... args) { - logger().log_formatted_file_warning(filename, lineno, fmt.format(args...)); + logger().log_formatted_file_warning(filename, lineno, fmt.format_string(), fmt.format(args...)); } template void log_file_info(std::string_view filename, int lineno, FmtString...> fmt, const Args &... args) { - logger().log_formatted_file_info(filename, lineno, fmt.format(args...)); + logger().log_formatted_file_info(filename, lineno, fmt.format_string(), fmt.format(args...)); } template [[noreturn]] void log_error(FmtString...> fmt, const Args &... args) { - logger().log_formatted_error(fmt.format(args...)); + logger().log_formatted_error(fmt.format_string(), fmt.format(args...)); } template [[noreturn]] void log_file_error(std::string_view filename, int lineno, FmtString...> fmt, const Args &... args) { - logger().log_formatted_file_error(filename, lineno, fmt.format(args...)); + logger().log_formatted_file_error(filename, lineno, fmt.format_string(), fmt.format(args...)); } template [[noreturn]] void log_cmd_error(FmtString...> fmt, const Args &... args) { - logger().log_formatted_cmd_error(fmt.format(args...)); + logger().log_formatted_cmd_error(fmt.format_string(), fmt.format(args...)); } inline void log_suppressed() diff --git a/kernel/log_compat.cc b/kernel/log_compat.cc index badfa5dfb..ba040e5de 100644 --- a/kernel/log_compat.cc +++ b/kernel/log_compat.cc @@ -37,7 +37,7 @@ void log_cmd_error(const char *format, ...) va_start(ap, format); std::string formatted = vstringf(format, ap); va_end(ap); - logger().log_formatted_cmd_error(formatted); + logger().log_formatted_cmd_error(format, formatted); } void log_warning(const char *format, ...) @@ -46,7 +46,7 @@ void log_warning(const char *format, ...) va_start(ap, format); std::string formatted = vstringf(format, ap); va_end(ap); - logger().log_formatted_warning("Warning: ", formatted); + logger().log_formatted_warning("Warning: ", format, formatted); } void log_warning_noprefix(const char *format, ...) @@ -55,7 +55,7 @@ void log_warning_noprefix(const char *format, ...) va_start(ap, format); std::string formatted = vstringf(format, ap); va_end(ap); - logger().log_formatted_warning("", formatted); + logger().log_formatted_warning({}, format, formatted); } void log_error(const char *format, ...) @@ -64,14 +64,7 @@ void log_error(const char *format, ...) va_start(ap, format); std::string formatted = vstringf(format, ap); va_end(ap); - logger().log_formatted_error(formatted); -} - -static inline void log_formatted(std::string const &str) -{ - // We use this inline wrapper as the following becomes ambiguous as soon as - // the `log` function below is declared. - return log("%s", str); + logger().log_formatted_error(format, formatted); } void log(const char *format, ...) @@ -80,7 +73,7 @@ void log(const char *format, ...) va_start(ap, format); std::string formatted = vstringf(format, ap); va_end(ap); - log_formatted(formatted); + logger().log_formatted_string({}, format, formatted, LogSeverity::LOG_INFO); } void log_compat(const char *format, ...) @@ -89,7 +82,7 @@ void log_compat(const char *format, ...) va_start(ap, format); std::string formatted = vstringf(format, ap); va_end(ap); - log_formatted(formatted); + logger().log_formatted_string({}, format, formatted, LogSeverity::LOG_INFO); } YOSYS_NAMESPACE_END