Skip to content

Commit abde11e

Browse files
committed
Logging: Add log stream that only get sent warning+ logs.
1 parent 09742e2 commit abde11e

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

kernel/log.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
YOSYS_NAMESPACE_BEGIN
4040

4141
std::vector<FILE*> log_files;
42-
std::vector<std::ostream*> log_streams;
42+
std::vector<std::ostream*> log_streams, log_warning_streams;
4343
std::vector<std::string> log_scratchpads;
4444
std::map<std::string, std::set<std::string>> log_hdump;
4545
std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
@@ -102,7 +102,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
102102
}
103103
#endif
104104

105-
static void logv_string(std::string_view format, std::string str) {
105+
static void logv_string(std::string_view format, std::string str, LogSeverity severity = LogSeverity::LOG_INFO) {
106106
size_t remove_leading = 0;
107107
while (format.size() > 1 && format[0] == '\n') {
108108
logv_string("\n", "\n");
@@ -158,6 +158,10 @@ static void logv_string(std::string_view format, std::string str) {
158158

159159
for (auto f : log_streams)
160160
*f << time_str;
161+
162+
if (severity >= LogSeverity::LOG_WARNING)
163+
for (auto f : log_warning_streams)
164+
*f << time_str;
161165
}
162166

163167
for (auto f : log_files)
@@ -166,6 +170,12 @@ static void logv_string(std::string_view format, std::string str) {
166170
for (auto f : log_streams)
167171
*f << str;
168172

173+
if (severity >= LogSeverity::LOG_WARNING)
174+
for (auto f : log_warning_streams) {
175+
*f << str;
176+
f->flush();
177+
}
178+
169179
RTLIL::Design *design = yosys_get_design();
170180
if (design != nullptr)
171181
for (auto &scratchpad : log_scratchpads)
@@ -203,11 +213,11 @@ static void logv_string(std::string_view format, std::string str) {
203213
}
204214
}
205215

206-
void log_formatted_string(std::string_view format, std::string str)
216+
void log_formatted_string(std::string_view format, std::string str, LogSeverity severity)
207217
{
208218
if (log_make_debug && !ys_debug(1))
209219
return;
210-
logv_string(format, std::move(str));
220+
logv_string(format, std::move(str), severity);
211221
}
212222

213223
void log_formatted_header(RTLIL::Design *design, std::string_view format, std::string str)
@@ -293,7 +303,7 @@ void log_formatted_warning(std::string_view prefix, std::string message)
293303
if (log_errfile != NULL && !log_quiet_warnings)
294304
log_files.push_back(log_errfile);
295305

296-
log("%s%s", prefix, message);
306+
log_formatted_string("%s", stringf("%s%s", prefix, message), LogSeverity::LOG_WARNING);
297307
log_flush();
298308

299309
if (log_errfile != NULL && !log_quiet_warnings)
@@ -339,7 +349,7 @@ static void log_error_with_prefix(std::string_view prefix, std::string str)
339349
f = stderr;
340350

341351
log_last_error = std::move(str);
342-
log("%s%s", prefix, log_last_error);
352+
log_formatted_string("%s", stringf("%s%s", prefix, log_last_error), LogSeverity::LOG_ERROR);
343353
log_flush();
344354

345355
log_make_debug = bak_log_make_debug;
@@ -420,7 +430,7 @@ void log_formatted_cmd_error(std::string str)
420430
pop_errfile = true;
421431
}
422432

423-
log("ERROR: %s", log_last_error);
433+
log_formatted_string("ERROR: %s", log_last_error, LogSeverity::LOG_ERROR);
424434
log_flush();
425435

426436
if (pop_errfile)

kernel/log.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,15 @@ YOSYS_NAMESPACE_BEGIN
9494

9595
struct log_cmd_error_exception { };
9696

97+
enum LogSeverity {
98+
LOG_INFO,
99+
LOG_WARNING,
100+
LOG_ERROR
101+
};
102+
97103
extern std::vector<FILE*> log_files;
98104
extern std::vector<std::ostream*> log_streams;
105+
extern std::vector<std::ostream*> log_warning_streams;
99106
extern std::vector<std::string> log_scratchpads;
100107
extern std::map<std::string, std::set<std::string>> log_hdump;
101108
extern std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
@@ -131,12 +138,10 @@ static inline bool ys_debug(int = 0) { return false; }
131138
#endif
132139
# define log_debug(...) do { if (ys_debug(1)) log(__VA_ARGS__); } while (0)
133140

134-
void log_formatted_string(std::string_view format, std::string str);
141+
void log_formatted_string(std::string_view format, std::string str, LogSeverity severity = LogSeverity::LOG_INFO);
135142
template <typename... Args>
136143
inline void log(FmtString<TypeIdentity<Args>...> fmt, const Args &... args)
137144
{
138-
if (log_make_debug && !ys_debug(1))
139-
return;
140145
log_formatted_string(fmt.format_string(), fmt.format(args...));
141146
}
142147

0 commit comments

Comments
 (0)