mirror of
https://github.com/YosysHQ/yosys
synced 2026-06-04 08:07:58 +00:00
Add LogSink & LogMessage classes for a more robust sink solution.
Signed-off-by: Sean Luchen <seanluchen@google.com>
This commit is contained in:
parent
f6863b9f70
commit
b04aadfa44
3 changed files with 54 additions and 11 deletions
|
|
@ -39,7 +39,8 @@
|
||||||
YOSYS_NAMESPACE_BEGIN
|
YOSYS_NAMESPACE_BEGIN
|
||||||
|
|
||||||
std::vector<FILE*> log_files;
|
std::vector<FILE*> log_files;
|
||||||
std::vector<std::ostream*> log_streams, log_warning_streams;
|
std::vector<std::ostream*> log_streams;
|
||||||
|
std::vector<LogSink*> log_sinks;
|
||||||
std::vector<std::string> log_scratchpads;
|
std::vector<std::string> log_scratchpads;
|
||||||
std::map<std::string, std::set<std::string>> log_hdump;
|
std::map<std::string, std::set<std::string>> log_hdump;
|
||||||
std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
|
std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
|
||||||
|
|
@ -156,10 +157,6 @@ static void logv_string(std::string_view format, std::string str, LogSeverity se
|
||||||
|
|
||||||
for (auto f : log_streams)
|
for (auto f : log_streams)
|
||||||
*f << time_str;
|
*f << time_str;
|
||||||
|
|
||||||
if (severity >= LogSeverity::LOG_WARNING)
|
|
||||||
for (auto f : log_warning_streams)
|
|
||||||
*f << time_str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto f : log_files)
|
for (auto f : log_files)
|
||||||
|
|
@ -168,11 +165,9 @@ static void logv_string(std::string_view format, std::string str, LogSeverity se
|
||||||
for (auto f : log_streams)
|
for (auto f : log_streams)
|
||||||
*f << str;
|
*f << str;
|
||||||
|
|
||||||
if (severity >= LogSeverity::LOG_WARNING)
|
LogMessage log_msg(severity, str);
|
||||||
for (auto f : log_warning_streams) {
|
for (LogSink* sink : log_sinks)
|
||||||
*f << str;
|
sink->log(log_msg);
|
||||||
f->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
RTLIL::Design *design = yosys_get_design();
|
RTLIL::Design *design = yosys_get_design();
|
||||||
if (design != nullptr)
|
if (design != nullptr)
|
||||||
|
|
|
||||||
15
kernel/log.h
15
kernel/log.h
|
|
@ -101,9 +101,22 @@ enum LogSeverity {
|
||||||
LOG_ERROR
|
LOG_ERROR
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct LogMessage {
|
||||||
|
LogMessage(LogSeverity severity, std::string_view message) :
|
||||||
|
severity(severity), timestamp(std::time(nullptr)), message(message) {}
|
||||||
|
LogSeverity severity;
|
||||||
|
std::time_t timestamp;
|
||||||
|
std::string message;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LogSink {
|
||||||
|
public:
|
||||||
|
virtual void log(const LogMessage& message) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
extern std::vector<FILE*> log_files;
|
extern std::vector<FILE*> log_files;
|
||||||
extern std::vector<std::ostream*> log_streams;
|
extern std::vector<std::ostream*> log_streams;
|
||||||
extern std::vector<std::ostream*> log_warning_streams;
|
extern std::vector<LogSink*> log_sinks;
|
||||||
extern std::vector<std::string> log_scratchpads;
|
extern std::vector<std::string> log_scratchpads;
|
||||||
extern std::map<std::string, std::set<std::string>> log_hdump;
|
extern std::map<std::string, std::set<std::string>> log_hdump;
|
||||||
extern std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
|
extern std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <gmock/gmock.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include "kernel/yosys.h"
|
#include "kernel/yosys.h"
|
||||||
|
|
@ -11,4 +12,38 @@ TEST(KernelLogTest, logvValidValues)
|
||||||
EXPECT_EQ(7, 7);
|
EXPECT_EQ(7, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TestSink : public LogSink {
|
||||||
|
public:
|
||||||
|
void log(const LogMessage& message) override {
|
||||||
|
messages_.push_back(message);
|
||||||
|
}
|
||||||
|
std::vector<LogMessage> messages_;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(KernelLogTest, logToSink)
|
||||||
|
{
|
||||||
|
TestSink sink;
|
||||||
|
log_sinks.push_back(&sink);
|
||||||
|
log("test info log");
|
||||||
|
log_warning("test warning log");
|
||||||
|
|
||||||
|
std::vector<LogMessage> expected{
|
||||||
|
LogMessage(LogSeverity::LOG_INFO, "test info log"),
|
||||||
|
LogMessage(LogSeverity::LOG_WARNING, "test warning log"),
|
||||||
|
};
|
||||||
|
// Certain calls to the log.h interface may prepend a string to
|
||||||
|
// the provided string. We should ensure that the expected string
|
||||||
|
// is a subset of the actual string. Additionally, we don't want to
|
||||||
|
// compare timestamps. So, we use a custom comparator.
|
||||||
|
for (const LogMessage& expected_msg : expected) {
|
||||||
|
EXPECT_THAT(sink.messages_, ::testing::Contains(::testing::Truly(
|
||||||
|
[&](const LogMessage& actual) {
|
||||||
|
return actual.severity == expected_msg.severity &&
|
||||||
|
actual.message.find(expected_msg.message) != std::string::npos;
|
||||||
|
}
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
EXPECT_NE(sink.messages_[0].timestamp, 0);
|
||||||
|
}
|
||||||
|
|
||||||
YOSYS_NAMESPACE_END
|
YOSYS_NAMESPACE_END
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue