Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/setup-build-env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ runs:
shell: bash
run: |
sudo apt-get update
sudo apt-get install gperf build-essential bison flex libfl-dev libreadline-dev gawk tcl-dev libffi-dev git graphviz xdot pkg-config python3 libboost-system-dev libboost-python-dev libboost-filesystem-dev zlib1g-dev libbz2-dev libgtest-dev
sudo apt-get install -y gperf build-essential bison flex libfl-dev libreadline-dev gawk tcl-dev libffi-dev git graphviz xdot pkg-config python3 libboost-system-dev libboost-python-dev libboost-filesystem-dev zlib1g-dev libbz2-dev libgtest-dev libgmock-dev

- name: Install macOS Dependencies
if: runner.os == 'macOS'
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test-verific.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ jobs:
persist-credentials: false
submodules: true
- name: Runtime environment
run: |
echo "procs=$(nproc)" >> $GITHUB_ENV
uses: ./.github/actions/setup-build-env

- name: Build Yosys
run: |
Expand Down
17 changes: 11 additions & 6 deletions kernel/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ YOSYS_NAMESPACE_BEGIN

std::vector<FILE*> log_files;
std::vector<std::ostream*> log_streams;
std::vector<LogSink*> log_sinks;
std::vector<std::string> log_scratchpads;
std::map<std::string, std::set<std::string>> log_hdump;
std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
Expand Down Expand Up @@ -100,7 +101,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
}
#endif

static void logv_string(std::string_view format, std::string str) {
static void logv_string(std::string_view format, std::string str, LogSeverity severity = LogSeverity::LOG_INFO) {
size_t remove_leading = 0;
while (format.size() > 1 && format[0] == '\n') {
logv_string("\n", "\n");
Expand Down Expand Up @@ -164,6 +165,10 @@ static void logv_string(std::string_view format, std::string str) {
for (auto f : log_streams)
*f << str;

LogMessage log_msg(severity, str);
for (LogSink* sink : log_sinks)
sink->log(log_msg);

RTLIL::Design *design = yosys_get_design();
if (design != nullptr)
for (auto &scratchpad : log_scratchpads)
Expand Down Expand Up @@ -201,11 +206,11 @@ static void logv_string(std::string_view format, std::string str) {
}
}

void log_formatted_string(std::string_view format, std::string str)
void log_formatted_string(std::string_view format, std::string str, LogSeverity severity)
{
if (log_make_debug && !ys_debug(1))
return;
logv_string(format, std::move(str));
logv_string(format, std::move(str), severity);
}

void log_formatted_header(RTLIL::Design *design, std::string_view format, std::string str)
Expand Down Expand Up @@ -291,7 +296,7 @@ void log_formatted_warning(std::string_view prefix, std::string message)
if (log_errfile != NULL && !log_quiet_warnings)
log_files.push_back(log_errfile);

log("%s%s", prefix, message);
log_formatted_string("%s", stringf("%s%s", prefix, message), LogSeverity::LOG_WARNING);
log_flush();

if (log_errfile != NULL && !log_quiet_warnings)
Expand Down Expand Up @@ -339,7 +344,7 @@ static void log_error_with_prefix(std::string_view prefix, std::string str)
}

log_last_error = std::move(str);
log("%s%s", prefix, log_last_error);
log_formatted_string("%s", stringf("%s%s", prefix, log_last_error), LogSeverity::LOG_ERROR);
log_flush();

log_make_debug = bak_log_make_debug;
Expand Down Expand Up @@ -425,7 +430,7 @@ void log_formatted_cmd_error(std::string str)
pop_errfile = true;
}

log("ERROR: %s", log_last_error);
log_formatted_string("%s", stringf("ERROR: %s", log_last_error), LogSeverity::LOG_ERROR);
log_flush();

if (pop_errfile)
Expand Down
24 changes: 21 additions & 3 deletions kernel/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,28 @@

struct log_cmd_error_exception { };

enum LogSeverity {
LOG_INFO,
LOG_WARNING,
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<std::ostream*> log_streams;
extern std::vector<LogSink*> log_sinks;
extern std::vector<std::string> log_scratchpads;
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;
Expand Down Expand Up @@ -132,12 +152,10 @@
#endif
# define log_debug(...) do { if (ys_debug(1)) log(__VA_ARGS__); } while (0)

void log_formatted_string(std::string_view format, std::string str);
void log_formatted_string(std::string_view format, std::string str, LogSeverity severity = LogSeverity::LOG_INFO);
template <typename... Args>
inline void log(FmtString<TypeIdentity<Args>...> fmt, const Args &... args)
{
if (log_make_debug && !ys_debug(1))
return;
log_formatted_string(fmt.format_string(), fmt.format(args...));
}

Expand Down Expand Up @@ -279,7 +297,7 @@
static inline void log_assert_worker(bool cond, const char *expr, const char *file, int line) {
if (!cond) log_assert_failure(expr, file, line);
}
# define log_assert(_assert_expr_) YOSYS_NAMESPACE_PREFIX log_assert_worker(_assert_expr_, #_assert_expr_, __FILE__, __LINE__)

Check warning on line 300 in kernel/log.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc-14)

this statement may fall through [-Wimplicit-fallthrough=]

Check warning on line 300 in kernel/log.h

View workflow job for this annotation

GitHub Actions / test-compile (ubuntu-latest, gcc-14)

this statement may fall through [-Wimplicit-fallthrough=]
#else
# define log_assert(_assert_expr_) do { if (0) { (void)(_assert_expr_); } } while(0)
#endif
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ UNAME_S := $(shell uname -s)
GTEST_PREFIX := $(shell brew --prefix googletest 2>/dev/null)
ifeq ($(GTEST_PREFIX),)
GTEST_CXXFLAGS :=
GTEST_LDFLAGS := -lgtest -lgtest_main
GTEST_LDFLAGS := -lgtest -lgtest_main -lgmock
else
GTEST_CXXFLAGS := -I$(GTEST_PREFIX)/include
GTEST_LDFLAGS := -L$(GTEST_PREFIX)/lib -lgtest -lgtest_main
GTEST_LDFLAGS := -L$(GTEST_PREFIX)/lib -lgtest -lgtest_main -lgmock
endif

ifeq ($(UNAME_S),Darwin)
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/kernel/logTest.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "kernel/yosys.h"
Expand All @@ -11,4 +12,38 @@ TEST(KernelLogTest, logvValidValues)
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
Loading