17#include "configurer.h"
19#include <cci_configuration>
22#include <nonstd/optional.hpp>
23#include <spdlog/async.h>
24#include <spdlog/sinks/basic_file_sink.h>
25#include <spdlog/sinks/stdout_color_sinks.h>
26#include <spdlog/spdlog.h>
28#include <unordered_map>
29#include <util/logging.h>
31#include <boost/stacktrace.hpp>
35#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
36#if GCC_VERSION < 40900
39#define likely(x) __builtin_expect(x, 1)
40#define unlikely(x) __builtin_expect(x, 0)
55using namespace sc_core;
59struct char_equal_to :
public std::equal_to<char const*> {
60 bool operator()(
char const* __x,
char const* __y)
const {
return strcmp(__x, __y) == 0; }
65 uint64_t operator()(
char const* str)
const {
66 constexpr unsigned int seed = 131;
69 hash = (hash * seed) + (*str);
76 std::unordered_map<char const*, sc_core::sc_verbosity, char_hash, char_equal_to> table;
77 std::vector<std::string> cache;
81 std::tuple<bool, sc_core::sc_verbosity> get(
char const* key) {
82 std::lock_guard<std::mutex> lock(mtx);
83 auto it = table.find(key);
85 return {
true, it->second};
87 return {
false, sc_core::SC_DEBUG};
89 void insert(
char const* key, sc_core::sc_verbosity verb) {
90 std::lock_guard<std::mutex> lock(mtx);
92 table.insert({cache.back().c_str(), verb});
95 std::lock_guard<std::mutex> lock(mtx);
101static const cci::cci_originator originator;
103static const cci::cci_originator originator(
"reporting");
106bool& inst_based_logging() {
107 thread_local bool active = getenv(
"SCC_DISABLE_INSTANCE_BASED_LOGGING") ==
nullptr;
112 shared_ptr<spdlog::logger> file_logger;
113 shared_ptr<spdlog::logger> console_logger;
115 regex_t start_state{};
119 sc_time cycle_base{0, SC_NS};
120 auto operator=(
const scc::LogConfig& o) -> ExtLogConfig& {
121 scc::LogConfig::operator=(o);
124 auto match(
const char* type) ->
bool {
126 return regexec(&start_state, type, 0,
nullptr, 0) == 0;
128 return regex_search(type, reg_ex);
131 bool initialized{
false};
133 nonstd::optional<cci::cci_broker_handle> broker;
136auto get_tuple(
const sc_time& t) -> tuple<sc_time::value_type, sc_time_unit> {
137 auto val = t.value();
138 auto tr = (uint64_t)(sc_time::from_value(1).to_seconds() * 1E15);
140 while((tr % 10) == 0) {
147 while(tu < SC_SEC && (val % 10) == 0) {
150 tu += (0 == (scale % 3));
152 for(scale %= 3; scale != 0; scale--)
154 return make_tuple(val,
static_cast<sc_time_unit
>(tu));
157auto time2string(
const sc_time& t) ->
string {
158 const array<const char*, 6> time_units{
"fs",
"ps",
"ns",
"us",
"ms",
"s "};
159 const array<uint64_t, 6> multiplier{
160 1ULL, 1000ULL, 1000ULL * 1000, 1000ULL * 1000 * 1000, 1000ULL * 1000 * 1000 * 1000, 1000ULL * 1000 * 1000 * 1000 * 1000};
165 const auto tt = get_tuple(t);
166 const auto val = get<0>(tt);
167 const auto scale = get<1>(tt);
168 const auto fs_val = val * multiplier[scale];
169 for(
int j = multiplier.size() - 1; j >= scale; --j) {
170 if(fs_val >= multiplier[j]) {
171 const auto i = val / multiplier[j - scale];
172 const auto f = val % multiplier[j - scale];
173 oss << i <<
'.' << setw(3 * (j - scale)) << setfill(
'0') << right << f <<
' ' << time_units[j];
180auto compose_message(
const sc_report& rep,
const scc::LogConfig& cfg) ->
const string {
181 if(rep.get_severity() > SC_INFO || cfg.log_filter_regex.length() == 0 || rep.get_verbosity() == sc_core::SC_MEDIUM ||
182 log_cfg.match(rep.get_msg_type())) {
184 if(unlikely(cfg.print_sys_time))
186 if(likely(cfg.print_sim_time)) {
187 if(unlikely(log_cfg.cycle_base.value())) {
188 if(unlikely(cfg.print_delta))
189 os <<
"[" << std::setw(7) << std::setfill(
' ') << sc_time_stamp().value() / log_cfg.cycle_base.value() <<
"(" << setw(5)
190 << sc_delta_count() <<
")]";
192 os <<
"[" << std::setw(7) << std::setfill(
' ') << sc_time_stamp().value() / log_cfg.cycle_base.value() <<
"]";
194 auto t = time2string(sc_time_stamp());
195 if(unlikely(cfg.print_delta))
196 os <<
"[" << std::setw(20) << std::setfill(
' ') << t <<
"(" << setw(5) << sc_delta_count() <<
")]";
198 os <<
"[" << std::setw(20) << std::setfill(
' ') << t <<
"]";
201 if(unlikely(rep.get_id() >= 0))
203 <<
"IWEF"[rep.get_severity()] << rep.get_id() <<
") " << rep.get_msg_type() <<
": ";
204 else if(cfg.msg_type_field_width) {
205 if(cfg.msg_type_field_width == std::numeric_limits<unsigned>::max())
206 os <<
" " << rep.get_msg_type() <<
": ";
208 os <<
" " <<
util::padded(rep.get_msg_type(), cfg.msg_type_field_width) <<
": ";
212 if(rep.get_severity() > SC_INFO) {
213 if(rep.get_line_number())
214 os <<
"\n [FILE:" << rep.get_file_name() <<
":" << rep.get_line_number() <<
"]";
215 sc_simcontext* simc = sc_get_curr_simcontext();
216 if(simc && sc_is_running()) {
217 const char* proc_name = rep.get_process_name();
219 os <<
"\n [PROCESS:" << proc_name <<
"]";
227inline void log2logger(spdlog::logger& logger,
const sc_report& rep,
const scc::LogConfig& cfg) {
228 auto msg = compose_message(rep, cfg);
231 switch(rep.get_severity()) {
233 switch(rep.get_verbosity()) {
251#ifdef WITH_STACKTRACE
252 if(getenv(
"SCC_PRINT_STACK_ON_ERROR"))
253 logger.error(boost::stacktrace::to_string(boost::stacktrace::stacktrace()));
257 logger.critical(msg);
258#ifdef WITH_STACKTRACE
259 if(getenv(
"SCC_PRINT_STACK_ON_ERROR"))
260 logger.error(boost::stacktrace::to_string(boost::stacktrace::stacktrace()));
268inline void flush_loggers() {
269 log_cfg.console_logger->flush();
270 if(log_cfg.file_logger)
271 log_cfg.file_logger->flush();
274void report_handler(
const sc_report& rep,
const sc_actions& actions) {
275 thread_local bool sc_stop_called =
false;
276 if(actions & SC_DO_NOTHING)
278 if(rep.get_severity() == sc_core::SC_INFO || !log_cfg.report_only_first_error || sc_report_handler::get_count(SC_ERROR) < 2) {
279 if((actions & SC_DISPLAY) && (!log_cfg.file_logger || rep.get_verbosity() < SC_HIGH))
281 log2logger(*log_cfg.console_logger, rep, log_cfg);
282 }
catch(spdlog::spdlog_ex e) {
284 if((actions & SC_LOG) && log_cfg.file_logger) {
286 lcfg.print_sim_time =
true;
287 if(!lcfg.msg_type_field_width)
288 lcfg.msg_type_field_width = 24;
289 log2logger(*log_cfg.file_logger, rep, lcfg);
292 if(actions & SC_STOP) {
295 }
catch(spdlog::spdlog_ex e) {
297 if(sc_is_running() && !sc_stop_called) {
299 sc_stop_called =
true;
302 if(actions & SC_ABORT) {
305 }
catch(spdlog::spdlog_ex e) {
310 if(actions & SC_THROW) {
313 }
catch(spdlog::spdlog_ex e) {
317 if(sc_time_stamp().value() && !sc_is_running()) {
320 }
catch(spdlog::spdlog_ex e) {
331 old_buf = os.rdbuf(
this);
343auto scc::stream_redirection::xsputn(
const char_type* s, streamsize n) -> streamsize {
344 auto sz = stringbuf::xsputn(s, n);
345 if(s[n - 1] ==
'\n') {
351static const array<sc_severity, 8> severity = {SC_FATAL,
359static const array<sc_verbosity, 8> verbosity = {SC_NONE,
368auto scc::stream_redirection::sync() ->
int {
369 if(level <= log_cfg.level) {
370 auto timestr = time2string(sc_time_stamp());
371 istringstream buf(str());
373 while(getline(buf, line)) {
374 ::sc_report_handler::report(severity[
static_cast<unsigned>(level)],
"SystemC", line.c_str(),
375 verbosity[
static_cast<unsigned>(level)],
"", 0);
382static void configure_logging() {
383 std::lock_guard<mutex> lock(log_cfg.mtx);
384 static bool spdlog_initialized =
false;
385 if(!log_cfg.dont_create_broker)
386 scc::init_cci(
"SCCBroker");
387 log_cfg.broker = cci::cci_get_global_broker(originator);
388 if(log_cfg.install_handler) {
389 if(!log_cfg.instance_based_log_levels || getenv(
"SCC_DISABLE_INSTANCE_BASED_LOGGING"))
390 inst_based_logging() =
false;
391 sc_report_handler::set_verbosity_level(verbosity[
static_cast<unsigned>(log_cfg.level)]);
392 sc_report_handler::set_handler(report_handler);
393 if(!spdlog_initialized) {
394 spdlog::init_thread_pool(1024U,
395 log_cfg.log_file_name.size() ? 2U : 1U);
396 log_cfg.console_logger = log_cfg.log_async ? spdlog::stdout_color_mt<spdlog::async_factory>(
"console_logger")
397 : spdlog::stdout_color_mt(
"console_logger");
398 auto logger_fmt = log_cfg.print_severity ?
"[%L] %v" :
"%v";
399 if(log_cfg.colored_output) {
400 std::ostringstream os;
401 os <<
"%^" << logger_fmt <<
"%$";
402 log_cfg.console_logger->set_pattern(os.str());
404 log_cfg.console_logger->set_pattern(
"[%L] %v");
405 log_cfg.console_logger->flush_on(spdlog::level::err);
406 log_cfg.console_logger->set_level(spdlog::level::level_enum::trace);
407 if(log_cfg.log_file_name.size()) {
410 ofs.open(log_cfg.log_file_name, ios::out | ios::trunc);
412 log_cfg.file_logger = log_cfg.log_async
413 ? spdlog::basic_logger_mt<spdlog::async_factory>(
"file_logger", log_cfg.log_file_name)
414 : spdlog::basic_logger_mt(
"file_logger", log_cfg.log_file_name);
415 if(log_cfg.print_severity)
416 log_cfg.file_logger->set_pattern(
"[%8l] %v");
418 log_cfg.file_logger->set_pattern(
"%v");
419 log_cfg.file_logger->flush_on(spdlog::level::err);
420 log_cfg.file_logger->set_level(spdlog::level::level_enum::trace);
422 spdlog_initialized =
true;
424 log_cfg.console_logger = spdlog::get(
"console_logger");
425 if(log_cfg.log_file_name.size())
426 log_cfg.file_logger = spdlog::get(
"file_logger");
428 if(log_cfg.log_filter_regex.size()) {
430 regcomp(&log_cfg.start_state, log_cfg.log_filter_regex.c_str(), REG_EXTENDED);
432 log_cfg.reg_ex = regex(log_cfg.log_filter_regex, regex::extended | regex::icase);
435 logging::LoggerCallbacks::set_output_cb([](
logging::log_level lvl, std::string
const& msg_type, std::string
const& msg) {
437 case logging::log_level::FATAL:
438 ::scc ::ScLogger<::sc_core ::SC_FATAL>(
"", 0, sc_core ::SC_MEDIUM)
439 .type(msg_type.size() ? msg_type : std::string(
"C++"))
443 case logging::log_level::ERR:
444 ::scc ::ScLogger<::sc_core ::SC_ERROR>(
"", 0, sc_core ::SC_MEDIUM)
445 .type(msg_type.size() ? msg_type : std::string(
"C++"))
449 case logging::log_level::WARN:
450 if(::scc ::get_log_verbosity(msg_type) >= sc_core ::SC_LOW)
451 ::scc ::ScLogger<::sc_core ::SC_WARNING>(
"", 0, sc_core ::SC_MEDIUM)
452 .type(msg_type.size() ? msg_type : std::string(
"C++"))
456 case logging::log_level::INFO:
457 if(::scc ::get_log_verbosity(msg_type) >= sc_core ::SC_MEDIUM)
458 ::scc ::ScLogger<::sc_core ::SC_INFO>(
"", 0, sc_core ::SC_MEDIUM)
459 .type(msg_type.size() ? msg_type : std::string(
"C++"))
463 case logging::log_level::DEBUG:
464 if(::scc ::get_log_verbosity(msg_type) >= sc_core ::SC_HIGH)
465 ::scc ::ScLogger<::sc_core ::SC_INFO>(
"", 0, sc_core ::SC_HIGH)
466 .type(msg_type.size() ? msg_type : std::string(
"C++"))
470 case logging::log_level::TRACE:
471 if(::scc ::get_log_verbosity(msg_type) >= sc_core ::SC_FULL)
472 ::scc ::ScLogger<::sc_core ::SC_INFO>(
"", 0, sc_core ::SC_FULL).type(msg_type).get() << msg;
474 case logging::log_level::TRACEALL:
475 if(::scc ::get_log_verbosity(msg_type) >= sc_core ::SC_DEBUG)
476 ::scc ::ScLogger<::sc_core ::SC_INFO>(
"", 0, sc_core ::SC_DEBUG)
477 .type(msg_type.size() ? msg_type : std::string(
"C++"))
488void scc::reinit_logging() { reinit_logging(log_cfg.level); }
490void scc::reinit_logging(
scc::log level) {
491 if(log_cfg.install_handler)
492 sc_report_handler::set_handler(report_handler);
493 log_cfg.level = level;
495 if(!log_cfg.instance_based_log_levels || getenv(
"SCC_DISABLE_INSTANCE_BASED_LOGGING"))
496 inst_based_logging() =
false;
497 log_cfg.initialized =
true;
503 log_cfg.msg_type_field_width = type_field_width;
504 log_cfg.print_sys_time = print_time;
505 log_cfg.level = level;
507 log_cfg.initialized =
true;
511 log_cfg = log_config;
513 log_cfg.initialized =
true;
517 log_cfg.level = level;
518 sc_report_handler::set_verbosity_level(verbosity[
static_cast<unsigned>(level)]);
519 log_cfg.console_logger->set_level(
520 static_cast<spdlog::level::level_enum
>(SPDLOG_LEVEL_OFF - min<int>(SPDLOG_LEVEL_OFF,
static_cast<int>(log_cfg.level))));
521 log_cfg.initialized =
true;
534 this->msg_type_field_width = width;
539 this->print_sys_time = enable;
544 this->print_sim_time = enable;
549 this->print_delta = enable;
554 this->print_severity = enable;
559 this->log_file_name = name;
564 this->log_file_name = name;
569 this->colored_output = enable;
574 this->log_filter_regex = expr;
579 this->log_filter_regex = expr;
589 this->dont_create_broker = v;
594 this->report_only_first_error = v;
598 this->instance_based_log_levels = v;
602 this->install_handler = v;
607sc_core::sc_verbosity __get_log_verbosity(
string current_name,
char const* str, cci::cci_broker_handle
const& broker,
608 sc_core::sc_verbosity verb) {
609 std::lock_guard<std::mutex> lk(mtx);
611 string param_name = (current_name.empty()) ? SCC_LOG_LEVEL_PARAM_NAME : current_name +
"." SCC_LOG_LEVEL_PARAM_NAME;
612 auto h = broker.get_param_handle(param_name);
614 sc_core::sc_verbosity ret = verbosity.at(std::min<unsigned>(h.get_cci_value().get_int(), verbosity.size() - 1));
615 lut.insert(str, ret);
618 auto val = broker.get_preset_cci_value(param_name);
620 sc_core::sc_verbosity ret = verbosity.at(std::min<unsigned>(val.get_int(), verbosity.size() - 1));
621 lut.insert(str, ret);
624 if(current_name.empty()) {
625 lut.insert(str, verb);
628 auto pos = current_name.rfind(
".");
629 if(pos == std::string::npos) {
632 current_name = current_name.substr(0, pos);
643 auto global_verb =
static_cast<sc_core::sc_verbosity
>(::sc_core::sc_report_handler::get_verbosity_level());
645 if(inst_based_logging()) {
646 auto res = lut.get(str);
648 return std::get<1>(res);
649 auto* curr_object = sc_core::sc_get_current_object();
650 if(strchr(str,
'.') ==
nullptr || curr_object) {
651 string current_name = std::string(str);
653 return __get_log_verbosity(current_name, str, log_cfg.broker.value(), global_verb);
655 return __get_log_verbosity(current_name, str, curr_object ? cci::cci_get_broker() : cci::cci_get_global_broker(originator),
~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
log_level
enum defining the log levels
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
bool is_logging_initialized()
get the state of the SCC logging system
void set_cycle_base(sc_core::sc_time period)
sets the cycle base for cycle based logging
log get_logging_level()
get the SystemC logging level
sc_core::sc_verbosity get_log_verbosity()
get the global verbosity level
log
enum defining the log levels
std::mutex verbosity_mtx
a mutex needed to syncronize verbosity manipulations
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 & reportOnlyFirstError(bool=true)
LogConfig & coloredOutput(bool=true)
LogConfig & logFileName(std::string &&)
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 & logFilterRegex(std::string &&)