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
8 changes: 4 additions & 4 deletions src/sysc/kernel/sc_simcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ SC_API void sc_unregister_stage_callback(sc_stage_callback_if & cb,
// Implementation defined Dynamic log verbosity dispatch
//
void sc_simcontext::set_log_verbosity_fn(
std::function<sc_core::sc_log_level(sc_log_logger_cache &, const char *, int, std::string_view)> fn)
std::function<sc_core::sc_verbosity(sc_log_logger_cache &, const char *, int, std::string_view)> fn)
{
if (dynamic_log_verbosity) {
SC_REPORT_WARNING(SC_LOG_OVERWRITE_VERBOSITY_FN_, 0);
Expand All @@ -2085,7 +2085,7 @@ void sc_simcontext::set_log_verbosity_fn(
}
}

sc_log_level sc_simcontext::get_log_verbosity(sc_log_logger_cache &logger,
sc_verbosity sc_simcontext::get_log_verbosity(sc_log_logger_cache &logger,
const char *file,
int line,
std::string_view local_tag) {
Expand All @@ -2098,11 +2098,11 @@ sc_log_level sc_simcontext::get_log_verbosity(sc_log_logger_cache &logger,
// NB these functions are NOT a standard API, but are
// exposed to provide access to the (implementation defined) dynamic logging configuration
void sc_log_impl::sc_set_log_verbosity_fn(
std::function<sc_log_level(sc_log_logger_cache &, const char *, int, std::string_view)> fn)
std::function<sc_verbosity(sc_log_logger_cache &, const char *, int, std::string_view)> fn)
{
sc_get_curr_simcontext()->set_log_verbosity_fn(std::move(fn));
}
sc_log_level sc_log_impl::sc_get_log_verbosity(sc_log_logger_cache &logger, const char *file,
sc_verbosity sc_log_impl::sc_get_log_verbosity(sc_log_logger_cache &logger, const char *file,
int line, std::string_view local_tag) {
return sc_get_curr_simcontext()->get_log_verbosity(logger, file, line, local_tag);
}
Expand Down
10 changes: 5 additions & 5 deletions src/sysc/kernel/sc_simcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ struct sc_log_impl {
// | logger cache and local_tag. The cache parameter may be used to remember
// | a computed level, once set the function will not be re-called.
// +------------------------------------------------------------------------------------------------
static void sc_set_log_verbosity_fn(std::function<sc_core::sc_log_level(
static void sc_set_log_verbosity_fn(std::function<sc_core::sc_verbosity(
sc_core::sc_log_logger_cache &, const char *,
int, std::string_view)>
fn);
Expand All @@ -165,7 +165,7 @@ static void sc_set_log_verbosity_fn(std::function<sc_core::sc_log_level(
// | If no callback has been installed, the implementation falls back to
// | the global report verbosity.
// +------------------------------------------------------------------------------------------------
static sc_core::sc_log_level sc_get_log_verbosity(sc_core::sc_log_logger_cache &logger,
static sc_core::sc_verbosity sc_get_log_verbosity(sc_core::sc_log_logger_cache &logger,
const char *file, int line,
std::string_view local_tag = {});
};
Expand Down Expand Up @@ -356,11 +356,11 @@ class SC_API sc_simcontext
void post_suspend() const;

private:
void set_log_verbosity_fn(std::function<sc_core::sc_log_level(
void set_log_verbosity_fn(std::function<sc_core::sc_verbosity(
sc_core::sc_log_logger_cache &, const char *,
int, std::string_view)>
fn);
sc_core::sc_log_level get_log_verbosity(
sc_core::sc_verbosity get_log_verbosity(
sc_core::sc_log_logger_cache &,
const char *file,
int line,
Expand Down Expand Up @@ -415,7 +415,7 @@ class SC_API sc_simcontext

inline void set_simulation_status(sc_status status);

std::function<sc_log_level(sc_log_logger_cache &, const char* , int, std::string_view)> dynamic_log_verbosity;
std::function<sc_verbosity(sc_log_logger_cache &, const char* , int, std::string_view)> dynamic_log_verbosity;

private:

Expand Down
19 changes: 4 additions & 15 deletions src/sysc/log/sc_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,15 @@

namespace sc_core {

// Definition of log level map (declared as extern in sc_log_types.h)
// This avoids creating duplicate copies in each translation unit.
const std::map<sc_log_level, std::string> log_level_map = {
{sc_log_level::CRITICAL, "CRITICAL"},
{sc_log_level::NONE, "NONE"},
{sc_log_level::WARN, "WARN"},
{sc_log_level::INFO, "INFO"},
{sc_log_level::DEBUG, "DEBUG"},
{sc_log_level::TRACE, "TRACE"}
};

static thread_local sc_log_logger_cache* s_current = nullptr;

sc_log_logger_cache* sc_log_logger_cache::get_current() { return s_current; }
void sc_log_logger_cache::set_current(sc_log_logger_cache* p) { s_current = p; }

sc_log_level sc_log_logger_cache::get_log_verbosity_cached(
int sc_log_logger_cache::get_log_verbosity_cached(
const char *file, int line, std::string_view local_tag) {
s_current = this;
if (level != sc_log_level::UNSET) {
if (level != SC_UNSET) {
return level;
}

Expand All @@ -56,15 +45,15 @@ sc_log_level sc_log_logger_cache::get_log_verbosity_cached(

void sc_log_logger_cache::set_tag(std::string new_tag) {
tag = std::move(new_tag);
level = sc_log_level::UNSET;
level = SC_UNSET;
}

} // namespace sc_core

// Global default logger with empty tag (in global namespace for proper name
// shadowing). This logger is used when no specific logger handle is provided.
sc_core::sc_log_logger_cache SC_LOG_LOG_LEVEL_CACHE{
sc_core::sc_log_level::UNSET, // level
sc_core::SC_UNSET, // level
{}, // tag (empty)
{}, // scname (empty)
nullptr // typename_str (no type info)
Expand Down
47 changes: 30 additions & 17 deletions src/sysc/log/sc_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,29 @@ static const char *SC_LOG_PRIV__FMT_EMPTY_STR = "";
// Internal handle variants (used by public SC_LOG_HANDLE macro)
#define SC_LOG_PRIV__HANDLE0() \
sc_core::sc_log_logger_cache SC_LOG_LOG_LEVEL_CACHE = \
sc_core::sc_log_handle_factory::make(sc_core::sc_log_level::UNSET, "", \
sc_core::sc_log_handle_factory::make(sc_core::SC_UNSET, "", \
this)

#define SC_LOG_PRIV__HANDLE1(tag_str) \
sc_core::sc_log_logger_cache SC_LOG_LOG_LEVEL_CACHE = \
sc_core::sc_log_handle_factory::make(sc_core::sc_log_level::UNSET, \
sc_core::sc_log_handle_factory::make(sc_core::SC_UNSET, \
tag_str, this)

#define SC_LOG_PRIV__HANDLE2(logger_name, tag_str) \
sc_core::sc_log_logger_cache SC_LOG_PRIV__HANDLE_NAME(logger_name) = \
sc_core::sc_log_handle_factory::make(sc_core::sc_log_level::UNSET, \
sc_core::sc_log_handle_factory::make(sc_core::SC_UNSET, \
tag_str, this)

// Internal static handle variants (used by public SC_LOG_HANDLE_STATIC macro)
#define SC_LOG_PRIV__HANDLE_STATIC1(tag_str) \
static sc_core::sc_log_logger_cache SC_LOG_LOG_LEVEL_CACHE = \
sc_core::sc_log_handle_factory::make_static( \
sc_core::sc_log_level::UNSET, tag_str)
sc_core::SC_UNSET, tag_str)

#define SC_LOG_PRIV__HANDLE_STATIC2(logger_name, tag_str) \
static sc_core::sc_log_logger_cache SC_LOG_PRIV__HANDLE_NAME(logger_name) = \
sc_core::sc_log_handle_factory::make_static( \
sc_core::sc_log_level::UNSET, tag_str)
sc_core::SC_UNSET, tag_str)

// Helper to get logger handle name
#define SC_LOG_PRIV__HANDLE_NAME(x) x
Expand Down Expand Up @@ -195,15 +195,15 @@ static const char *SC_LOG_PRIV__FMT_EMPTY_STR = "";
#define SC_LOG_HANDLE_VECTOR_PUSH_BACK(NAME, tag_str) \
SC_LOG_PRIV__HANDLE_NAME(NAME).push_back( \
sc_core::sc_log_handle_factory::make( \
sc_core::sc_log_level::UNSET, tag_str, this))
sc_core::SC_UNSET, tag_str, this))

/**
* SC_LOG_AT - Log a message at a specific level
*
* Usage:
* SC_LOG_AT(sc_core::sc_log_level::INFO) << "message";
* SC_LOG_AT(sc_core::sc_log_level::INFO, my_logger) << "message";
* SC_LOG_AT(sc_core::sc_log_level::INFO, "tag") << "message";
* SC_LOG_AT(sc_core::SC_HIGH) << "message";
* SC_LOG_AT(sc_core::SC_HIGH, my_logger) << "message";
* SC_LOG_AT(sc_core::SC_HIGH, "tag") << "message";
*/
#define SC_LOG_AT(lvl, ...) \
if (SC_LOG_PRIV__VBSTY_CHECK_IMPL(SC_LOG_PRIV__NARG(__VA_ARGS__), lvl, \
Expand All @@ -214,18 +214,31 @@ static const char *SC_LOG_PRIV__FMT_EMPTY_STR = "";
<< SC_LOG_PRIV__FMT_EMPTY_STR

/**
* Convenience macros for common log levels
* Convenience logging macros, one per logging level.
*
* The level names describe the *significance* (importance) of a message to a
* reader, deliberately chosen to be distinct from SystemC's existing
* vocabularies — they are NOT sc_severity (SC_INFO/SC_WARNING/SC_ERROR/
* SC_FATAL) and NOT the raw sc_verbosity names. Ordered from most significant
* to least, mapping onto increasing verbosity: the more verbose the level, the
* less significant (more incidental) the message.
*
* SC_CRITICAL most significant -> SC_LOW (emitted even at low verbosity)
* SC_ALERT -> SC_MEDIUM
* SC_NOTE -> SC_HIGH
* SC_DETAIL -> SC_FULL
* SC_INTERNAL least significant -> SC_DEBUG (only at the highest verbosity)
*
* Usage:
* SC_CRITICAL() << "message";
* SC_WARN(my_logger) << "message";
* SC_INFO("tag") << "message";
* SC_ALERT(my_logger) << "message";
* SC_NOTE("tag") << "message";
*/
#define SC_CRITICAL(...) \
SC_LOG_AT(sc_core::sc_log_level::CRITICAL, ##__VA_ARGS__)
#define SC_WARN(...) SC_LOG_AT(sc_core::sc_log_level::WARN, ##__VA_ARGS__)
#define SC_INFO(...) SC_LOG_AT(sc_core::sc_log_level::INFO, ##__VA_ARGS__)
#define SC_DEBUG(...) SC_LOG_AT(sc_core::sc_log_level::DEBUG, ##__VA_ARGS__)
#define SC_TRACE(...) SC_LOG_AT(sc_core::sc_log_level::TRACE, ##__VA_ARGS__)
SC_LOG_AT(sc_core::SC_LOW, ##__VA_ARGS__)
#define SC_ALERT(...) SC_LOG_AT(sc_core::SC_MEDIUM, ##__VA_ARGS__)
#define SC_NOTE(...) SC_LOG_AT(sc_core::SC_HIGH, ##__VA_ARGS__)
#define SC_DETAIL(...) SC_LOG_AT(sc_core::SC_FULL, ##__VA_ARGS__)
#define SC_INTERNAL(...) SC_LOG_AT(sc_core::SC_DEBUG, ##__VA_ARGS__)

#endif /* _SC_LOG_H_ */
105 changes: 30 additions & 75 deletions src/sysc/log/sc_log_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,82 +45,37 @@
namespace sc_core {

/************************
* Provide a set of names and conversions that are suitable for logging levels
*based on SystemC "verbosity's"
* Logging levels are expressed directly as sc_core::sc_verbosity values:
* SC_LOW (100)=CRITICAL SC_MEDIUM (200)=ALERT SC_HIGH (300)=NOTE
* SC_FULL (400)=DETAIL SC_DEBUG (500)=INTERNAL. SC_NONE (0) is "off".
*
* A logging level is carried as a plain int (SC_LOG_AT accepts any
* user-defined level), so the logger cache stores an int and uses the
* sentinel below for "not yet resolved / inherit from context".
************************/

enum class sc_log_level {
NONE = sc_core::SC_NONE,
CRITICAL = sc_core::SC_NONE,
WARN = sc_core::SC_LOW,
INFO = sc_core::SC_MEDIUM,
DEBUG = sc_core::SC_HIGH,
TRACE = sc_core::SC_DEBUG,

UNSET = INT_MAX
};

// Map of log levels to their string representations (defined in sc_log.cpp)
extern const std::map<sc_log_level, std::string> log_level_map;
// "Level not yet resolved" sentinel for the logger cache. This is an
// internal implementation marker (NOT part of the IEEE 1666 standard) and a
// plain int — deliberately NOT an sc_verbosity enumerator — so it never
// appears in the standardized sc_verbosity enum.
inline constexpr int SC_UNSET = INT_MAX;

/**
* @fn log as_log(int)
* @brief safely convert an integer into a log level
* @fn sc_verbosity as_log(int)
* @brief bucket a raw verbosity to the nearest level at or above it
*
* @param logLevel the logging level
* @return the log level
*/
inline sc_log_level as_log(int logLevel) {
auto m = log_level_map;
for (auto l : m) {
if (logLevel <= static_cast<int>(l.first)) {
return l.first;
}
}
return sc_log_level::TRACE;
}

/**
* @fn log as_log(std::string)
* @brief safely convert a string into a log level
*
* @param logName the string name for the log level
* @return the log level
*/
inline sc_log_level as_log(std::string logName) {
auto m = log_level_map;
for (auto l : m) {
if (logName == l.second)
return l.first;
}
return sc_log_level::TRACE;
}
/**
* @fn std::istream& operator >>(std::istream&, log&)
* @brief read a log level from input stream e.g. used by boost::lexical_cast
*
* @param is input stream holding the string representation
* @param val the value holding the resulting value
* @return the input stream
*/
inline std::istream &operator>>(std::istream &is, sc_log_level &val) {
std::string buf;
is >> buf;
val = as_log(buf);
return is;
}
/**
* @fn std::ostream& operator <<(std::ostream&, const log&)
* @brief output the textual representation of the log level
* v <= 100 -> CRITICAL, <= 200 -> ALERT, <= 300 -> NOTE,
* <= 400 -> DETAIL, otherwise INTERNAL.
*
* @param os output stream
* @param val logging level
* @return reference to the stream for chaining
* @param logLevel the raw verbosity
* @return the bucketed log level
*/
inline std::ostream &operator<<(std::ostream &os, sc_log_level const &val) {
auto m = log_level_map;
os << m[val];
return os;
inline sc_verbosity as_log(int logLevel) {
if (logLevel <= sc_core::SC_LOW) return sc_core::SC_LOW;
if (logLevel <= sc_core::SC_MEDIUM) return sc_core::SC_MEDIUM;
if (logLevel <= sc_core::SC_HIGH) return sc_core::SC_HIGH;
if (logLevel <= sc_core::SC_FULL) return sc_core::SC_FULL;
return sc_core::SC_DEBUG;
}

/* Convenience helper to detect if a type has a name() method.
Expand Down Expand Up @@ -168,7 +123,7 @@ struct sc_log_impl;
* since all referenced strings have appropriate lifetimes.
*/
struct sc_log_logger_cache {
sc_log_level level = sc_log_level::UNSET;
int level = SC_UNSET;
std::string tag{}; // Logger tag/identifier (owns the string)
std::string scname{}; // Captured sc_object name (owns the string)
const char* typename_str = nullptr; // Captured type name (from typeid(*this).name())
Expand All @@ -184,7 +139,7 @@ struct sc_log_logger_cache {
* @param local_tag optional local tag that overrides the effective name
* @return log level for this logger
*/
sc_log_level get_log_verbosity_cached(const char *file, int line,
int get_log_verbosity_cached(const char *file, int line,
std::string_view local_tag = {});

/**
Expand Down Expand Up @@ -220,7 +175,7 @@ struct sc_log_logger_cache {
*/
struct sc_log_handle_factory {
template <class TYPE>
static sc_log_logger_cache make(sc_log_level lvl, const char *tag_str,
static sc_log_logger_cache make(int lvl, const char *tag_str,
TYPE *p) {
const char *n = sc_log_priv__call_sc_name_fn{}(p);
const char *t = typeid(*p).name();
Expand All @@ -232,7 +187,7 @@ struct sc_log_handle_factory {
};
}

static sc_log_logger_cache make_static(sc_log_level lvl,
static sc_log_logger_cache make_static(int lvl,
const char *tag_str) {
return sc_log_logger_cache{
lvl,
Expand Down Expand Up @@ -269,7 +224,7 @@ struct sc_logger {
* @param verbosity the log level
*/
sc_logger(const char *file, int line,
sc_log_level verbosity = sc_core::sc_log_level::INFO)
int verbosity = sc_core::SC_MEDIUM)
: t(nullptr), file(file), line(line), level(verbosity) {}


Expand Down Expand Up @@ -348,7 +303,7 @@ struct sc_logger {
char *t{nullptr};
const char *file;
const int line;
const sc_log_level level;
const int level;
};

} // namespace sc_core
Expand Down
6 changes: 5 additions & 1 deletion src/sysc/utils/sc_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#ifndef SC_REPORT_H
#define SC_REPORT_H 1

#include <climits>
#include <exception>
#include <string>
#include "sysc/kernel/sc_cmnhdr.h"
Expand Down Expand Up @@ -65,7 +66,10 @@ enum sc_severity {
// Enumeration of message verbosity.
// ----------------------------------------------------------------------------

enum sc_verbosity {
// The underlying type is fixed to int so that an arbitrary integer logging
// level (SC_LOG_AT accepts any user-defined level) converts to/from
// sc_verbosity with a well-defined value, even outside the named range.
enum sc_verbosity : int {
SC_NONE = 0,
SC_LOW = 100,
SC_MEDIUM = 200,
Expand Down
Loading
Loading