3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-08-03 09:50:24 +00:00

Make log() use the FmtString infrastructure.

Now `log()` supports `std::string`.

We have to fix a few places where the format parameter was not a compile time constant.
This is mostly trivial.
This commit is contained in:
Robert O'Callahan 2025-07-10 05:24:59 +00:00
parent 422ec05322
commit 2e28feed94
9 changed files with 67 additions and 38 deletions

View file

@ -101,17 +101,16 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
}
#endif
static void logv(const char *format, va_list ap)
{
while (format[0] == '\n' && format[1] != 0) {
log("\n");
format++;
static void logv_string(std::string_view format, std::string str) {
size_t remove_leading = 0;
while (format.size() > 1 && format[0] == '\n') {
logv_string("\n", "\n");
format = format.substr(1);
++remove_leading;
}
if (remove_leading > 0) {
str = str.substr(remove_leading);
}
if (log_make_debug && !ys_debug(1))
return;
std::string str = vstringf(format, ap);
if (str.empty())
return;
@ -144,13 +143,13 @@ static void logv(const char *format, va_list ap)
time_str += stringf("[%05d.%06d] ", int(tv.tv_sec), int(tv.tv_usec));
}
if (format[0] && format[strlen(format)-1] == '\n')
if (!format.empty() && format[format.size() - 1] == '\n')
next_print_log = true;
// Special case to detect newlines in Python log output, since
// the binding always calls `log("%s", payload)` and the newline
// is then in the first formatted argument
if (!strcmp(format, "%s") && str.back() == '\n')
if (format == "%s" && str.back() == '\n')
next_print_log = true;
for (auto f : log_files)
@ -203,6 +202,20 @@ static void logv(const char *format, va_list ap)
}
}
static void logv(const char *format, va_list ap)
{
if (log_make_debug && !ys_debug(1))
return;
logv_string(format, vstringf(format, ap));
}
void log_formatted_string(std::string_view format, std::string str)
{
if (log_make_debug && !ys_debug(1))
return;
logv_string(format, std::move(str));
}
static void logv_header(RTLIL::Design *design, const char *format, va_list ap)
{
bool pop_errfile = false;
@ -401,14 +414,6 @@ void log_file_error(const string &filename, int lineno,
logv_file_error(filename, lineno, format, ap);
}
void log(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logv(format, ap);
va_end(ap);
}
void log_header(RTLIL::Design *design, const char *format, ...)
{
va_list ap;