24 #include "configurer.h"
29 #include <spdlog/async.h>
30 #include <spdlog/sinks/basic_file_sink.h>
31 #include <spdlog/sinks/stdout_color_sinks.h>
32 #include <spdlog/spdlog.h>
35 #include <unordered_map>
36 #include <util/logging.h>
38 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
39 #if GCC_VERSION < 40900
42 #define likely(x) __builtin_expect(x, 1)
43 #define unlikely(x) __builtin_expect(x, 0)
58 using namespace sc_core;
62 struct char_equal_to :
public std::equal_to<char const*> {
63 bool operator()(
char const* __x,
char const* __y)
const {
return strcmp(__x, __y) == 0; }
68 uint64_t operator()(
char const* str)
const {
69 constexpr
unsigned int seed = 131;
72 hash = (hash * seed) + (*str);
79 std::unordered_map<char const*, sc_core::sc_verbosity, char_hash, char_equal_to> table;
80 std::vector<std::string> cache;
81 void insert(
char const* key, sc_core::sc_verbosity verb) {
83 table.insert({cache.back().c_str(), verb});
91 static const cci::cci_originator originator;
93 static const cci::cci_originator originator(
"reporting");
96 bool& inst_based_logging() {
97 thread_local
bool active = getenv(
"SCC_DISABLE_INSTANCE_BASED_LOGGING") ==
nullptr;
102 shared_ptr<spdlog::logger> file_logger;
103 shared_ptr<spdlog::logger> console_logger;
105 regex_t start_state{};
109 sc_time cycle_base{0, SC_NS};
111 scc::LogConfig::operator=(o);
114 auto match(
const char* type) ->
bool {
116 return regexec(&start_state, type, 0,
nullptr, 0) == 0;
118 return regex_search(type, reg_ex);
121 bool initialized{
false};
124 thread_local ExtLogConfig log_cfg;
126 auto get_tuple(
const sc_time& t) -> tuple<sc_time::value_type, sc_time_unit> {
127 auto val = t.value();
128 auto tr = (uint64_t)(sc_time::from_value(1).to_seconds() * 1E15);
130 while((tr % 10) == 0) {
137 while(tu < SC_SEC && (val % 10) == 0) {
140 tu += (0 == (scale % 3));
142 for(scale %= 3; scale != 0; scale--)
144 return make_tuple(val,
static_cast<sc_time_unit
>(tu));
147 auto time2string(
const sc_time& t) ->
string {
148 const array<const char*, 6> time_units{
"fs",
"ps",
"ns",
"us",
"ms",
"s "};
149 const array<uint64_t, 6> multiplier{
150 1ULL, 1000ULL, 1000ULL * 1000, 1000ULL * 1000 * 1000, 1000ULL * 1000 * 1000 * 1000, 1000ULL * 1000 * 1000 * 1000 * 1000};
155 const auto tt = get_tuple(t);
156 const auto val = get<0>(tt);
157 const auto scale = get<1>(tt);
158 const auto fs_val = val * multiplier[scale];
159 for(
int j = multiplier.size() - 1; j >= scale; --j) {
160 if(fs_val >= multiplier[j]) {
161 const auto i = val / multiplier[j - scale];
162 const auto f = val % multiplier[j - scale];
163 oss << i <<
'.' << setw(3 * (j - scale)) << setfill(
'0') << right << f <<
' ' << time_units[j];
170 auto compose_message(
const sc_report& rep,
const scc::LogConfig& cfg) ->
const string {
171 if(rep.get_severity() > SC_INFO || cfg.log_filter_regex.length() == 0 || rep.get_verbosity() == sc_core::SC_MEDIUM ||
172 log_cfg.match(rep.get_msg_type())) {
174 if(unlikely(cfg.print_sys_time))
176 if(likely(cfg.print_sim_time)) {
177 if(unlikely(log_cfg.cycle_base.value())) {
178 if(unlikely(cfg.print_delta))
179 os <<
"[" << std::setw(7) << std::setfill(
' ') << sc_time_stamp().value() / log_cfg.cycle_base.value() <<
"(" << setw(5)
180 << sc_delta_count() <<
")]";
182 os <<
"[" << std::setw(7) << std::setfill(
' ') << sc_time_stamp().value() / log_cfg.cycle_base.value() <<
"]";
184 auto t = time2string(sc_time_stamp());
185 if(unlikely(cfg.print_delta))
186 os <<
"[" << std::setw(20) << std::setfill(
' ') << t <<
"(" << setw(5) << sc_delta_count() <<
")]";
188 os <<
"[" << std::setw(20) << std::setfill(
' ') << t <<
"]";
191 if(unlikely(rep.get_id() >= 0))
193 <<
"IWEF"[rep.get_severity()] << rep.get_id() <<
") " << rep.get_msg_type() <<
": ";
194 else if(cfg.msg_type_field_width) {
195 if(cfg.msg_type_field_width == std::numeric_limits<unsigned>::max())
196 os << rep.get_msg_type() <<
": ";
198 os <<
util::padded(rep.get_msg_type(), cfg.msg_type_field_width) <<
": ";
202 if(rep.get_severity() > SC_INFO) {
203 if(rep.get_line_number())
204 os <<
"\n [FILE:" << rep.get_file_name() <<
":" << rep.get_line_number() <<
"]";
205 sc_simcontext* simc = sc_get_curr_simcontext();
206 if(simc && sc_is_running()) {
207 const char* proc_name = rep.get_process_name();
209 os <<
"\n [PROCESS:" << proc_name <<
"]";
217 inline void log2logger(spdlog::logger& logger,
const sc_report& rep,
const scc::LogConfig& cfg) {
218 auto msg = compose_message(rep, cfg);
221 switch(rep.get_severity()) {
223 switch(rep.get_verbosity()) {
243 logger.critical(msg);
250 inline void log2logger(spdlog::logger& logger,
scc::log lvl,
const string& msg) {
252 case scc::log::DBGTRACE:
253 case scc::log::TRACE:
256 case scc::log::DEBUG:
262 case scc::log::WARNING:
265 case scc::log::ERROR:
268 case scc::log::FATAL:
269 logger.critical(msg);
276 void report_handler(
const sc_report& rep,
const sc_actions& actions) {
277 thread_local
bool sc_stop_called =
false;
278 if(actions & SC_DO_NOTHING)
280 if(rep.get_severity() == sc_core::SC_INFO || !log_cfg.report_only_first_error || sc_report_handler::get_count(SC_ERROR) < 2) {
281 if((actions & SC_DISPLAY) && (!log_cfg.file_logger || rep.get_verbosity() < SC_HIGH))
282 log2logger(*log_cfg.console_logger, rep, log_cfg);
283 if((actions & SC_LOG) && log_cfg.file_logger) {
285 lcfg.print_sim_time =
true;
286 if(!lcfg.msg_type_field_width)
287 lcfg.msg_type_field_width = 24;
288 log2logger(*log_cfg.file_logger, rep, lcfg);
291 if(actions & SC_STOP) {
292 this_thread::sleep_for(chrono::milliseconds(
static_cast<unsigned>(log_cfg.level) * 10));
293 if(sc_is_running() && !sc_stop_called) {
295 sc_stop_called =
true;
298 if(actions & SC_ABORT) {
299 this_thread::sleep_for(chrono::milliseconds(
static_cast<unsigned>(log_cfg.level) * 20));
302 if(actions & SC_THROW) {
303 this_thread::sleep_for(chrono::milliseconds(
static_cast<unsigned>(log_cfg.level) * 20));
306 if(sc_time_stamp().value() && !sc_is_running()) {
307 log_cfg.console_logger->flush();
308 if(log_cfg.file_logger)
309 log_cfg.file_logger->flush();
310 this_thread::sleep_for(chrono::milliseconds(
static_cast<unsigned>(log_cfg.level) * 10));
318 old_buf = os.rdbuf(
this);
330 auto scc::stream_redirection::xsputn(
const char_type* s, streamsize n) -> streamsize {
331 auto sz = stringbuf::xsputn(s, n);
332 if(s[n - 1] ==
'\n') {
338 static const array<sc_severity, 8> severity = {SC_FATAL,
346 static const array<sc_verbosity, 8> verbosity = {SC_NONE,
355 auto scc::stream_redirection::sync() ->
int {
356 if(level <= log_cfg.level) {
357 auto timestr = time2string(sc_time_stamp());
358 istringstream buf(str());
360 while(getline(buf, line)) {
361 ::sc_report_handler::report(severity[
static_cast<unsigned>(level)],
"SystemC", line.c_str(),
362 verbosity[
static_cast<unsigned>(level)],
"", 0);
369 static std::mutex cfg_guard;
370 static void configure_logging() {
371 std::lock_guard<mutex> lock(cfg_guard);
372 static bool spdlog_initialized =
false;
373 if(!log_cfg.dont_create_broker)
374 scc::init_cci(
"SCCBroker");
375 if(log_cfg.install_handler) {
376 if(!log_cfg.instance_based_log_levels || getenv(
"SCC_DISABLE_INSTANCE_BASED_LOGGING"))
377 inst_based_logging() =
false;
378 sc_report_handler::set_verbosity_level(verbosity[
static_cast<unsigned>(log_cfg.level)]);
379 sc_report_handler::set_handler(report_handler);
380 if(!spdlog_initialized) {
381 spdlog::init_thread_pool(1024U,
382 log_cfg.log_file_name.size() ? 2U : 1U);
383 log_cfg.console_logger = log_cfg.log_async ? spdlog::stdout_color_mt<spdlog::async_factory>(
"console_logger")
384 : spdlog::stdout_color_mt(
"console_logger");
385 auto logger_fmt = log_cfg.print_severity ?
"[%L] %v" :
"%v";
386 if(log_cfg.colored_output) {
387 std::ostringstream os;
388 os <<
"%^" << logger_fmt <<
"%$";
389 log_cfg.console_logger->set_pattern(os.str());
391 log_cfg.console_logger->set_pattern(
"[%L] %v");
392 log_cfg.console_logger->flush_on(spdlog::level::warn);
393 log_cfg.console_logger->set_level(spdlog::level::level_enum::trace);
394 if(log_cfg.log_file_name.size()) {
397 ofs.open(log_cfg.log_file_name, ios::out | ios::trunc);
399 log_cfg.file_logger = log_cfg.log_async
400 ? spdlog::basic_logger_mt<spdlog::async_factory>(
"file_logger", log_cfg.log_file_name)
401 : spdlog::basic_logger_mt(
"file_logger", log_cfg.log_file_name);
402 if(log_cfg.print_severity)
403 log_cfg.file_logger->set_pattern(
"[%8l] %v");
405 log_cfg.file_logger->set_pattern(
"%v");
406 log_cfg.file_logger->flush_on(spdlog::level::warn);
407 log_cfg.file_logger->set_level(spdlog::level::level_enum::trace);
409 spdlog_initialized =
true;
411 log_cfg.console_logger = spdlog::get(
"console_logger");
412 if(log_cfg.log_file_name.size())
413 log_cfg.file_logger = spdlog::get(
"file_logger");
415 if(log_cfg.log_filter_regex.size()) {
417 regcomp(&log_cfg.start_state, log_cfg.log_filter_regex.c_str(), REG_EXTENDED);
419 log_cfg.reg_ex = regex(log_cfg.log_filter_regex, regex::extended | regex::icase);
425 void scc::reinit_logging() { reinit_logging(log_cfg.level); }
427 void scc::reinit_logging(
scc::log level) {
428 if(log_cfg.install_handler)
429 sc_report_handler::set_handler(report_handler);
430 log_cfg.level = level;
432 if(!log_cfg.instance_based_log_levels || getenv(
"SCC_DISABLE_INSTANCE_BASED_LOGGING"))
433 inst_based_logging() =
false;
434 log_cfg.initialized =
true;
440 log_cfg.msg_type_field_width = type_field_width;
441 log_cfg.print_sys_time = print_time;
442 log_cfg.level = level;
444 log_cfg.initialized =
true;
448 log_cfg = log_config;
450 log_cfg.initialized =
true;
454 log_cfg.level = level;
455 sc_report_handler::set_verbosity_level(verbosity[
static_cast<unsigned>(level)]);
456 log_cfg.console_logger->set_level(
457 static_cast<spdlog::level::level_enum
>(SPDLOG_LEVEL_OFF - min<int>(SPDLOG_LEVEL_OFF,
static_cast<int>(log_cfg.level))));
458 log_cfg.initialized =
true;
471 this->msg_type_field_width = width;
476 this->print_sys_time = enable;
481 this->print_sim_time = enable;
486 this->print_delta = enable;
491 this->print_severity = enable;
496 this->log_file_name = name;
501 this->log_file_name = name;
506 this->colored_output = enable;
511 this->log_filter_regex = expr;
516 this->log_filter_regex = expr;
526 this->dont_create_broker = v;
531 this->report_only_first_error = v;
535 this->instance_based_log_levels = v;
539 this->install_handler = v;
544 if(inst_based_logging()) {
545 auto it = lut.table.find(str);
546 if(it != lut.table.end())
548 if(strchr(str,
'.') ==
nullptr || sc_core::sc_get_current_object()) {
549 string current_name = std::string(str);
550 auto broker = sc_core::sc_get_current_object() ? cci::cci_get_broker() : cci::cci_get_global_broker(originator);
552 string param_name = (current_name.empty()) ? SCC_LOG_LEVEL_PARAM_NAME : current_name +
"." SCC_LOG_LEVEL_PARAM_NAME;
553 auto h = broker.get_param_handle(param_name);
555 sc_core::sc_verbosity ret = verbosity.at(std::min<unsigned>(h.get_cci_value().get_int(), verbosity.size() - 1));
556 lut.insert(str, ret);
559 auto val = broker.get_preset_cci_value(param_name);
561 sc_core::sc_verbosity ret = verbosity.at(std::min<unsigned>(val.get_int(), verbosity.size() - 1));
562 lut.insert(str, ret);
565 if(current_name.empty()) {
566 sc_core::sc_verbosity ret =
567 static_cast<sc_core::sc_verbosity
>(::sc_core::sc_report_handler::get_verbosity_level());
568 lut.insert(str, ret);
571 auto pos = current_name.rfind(
".");
572 if(pos == std::string::npos) {
575 current_name = current_name.substr(0, pos);
582 return static_cast<sc_core::sc_verbosity
>(::sc_core::sc_report_handler::get_verbosity_level());
~stream_redirection()
destructor restoring the output stream buffer
void reset()
reset the stream redirection and restore output buffer of the stream
stream_redirection(std::ostream &os, log level)
constructor redirecting the given stream to a SystemC log message of given llog level
bool is_logging_initialized()
get the state of the SCC logging system
void set_logging_level(log level)
sets the SystemC logging level
void init_logging(log level=log::WARNING, unsigned type_field_width=24, bool print_time=false)
initializes the SystemC logging system with a particular logging level
log get_logging_level()
get the SystemC logging level
void set_cycle_base(sc_core::sc_time period)
sets the cycle base for cycle based logging
sc_core::sc_verbosity get_log_verbosity()
get the global verbosity level
log
enum defining the log levels
std::string padded(std::string str, size_t width, bool show_ellipsis=true)
pad a string to a given length by either cutting of the overflow or inserting an ellipsis
the configuration class for the logging setup
LogConfig & printSeverity(bool=true)
LogConfig & printSysTime(bool=true)
LogConfig & logFilterRegex(std::string &&)
LogConfig & reportOnlyFirstError(bool=true)
LogConfig & coloredOutput(bool=true)
LogConfig & dontCreateBroker(bool=true)
LogConfig & logLevel(log)
LogConfig & msgTypeFieldWidth(unsigned)
LogConfig & printSimTime(bool=true)
LogConfig & logAsync(bool=true)
LogConfig & printDelta(bool=true)
LogConfig & instanceBasedLogLevels(bool=true)
LogConfig & installHandler(bool=true)
LogConfig & logFileName(std::string &&)