scc  2022.4.0
SystemC components library
report.h
1 /*******************************************************************************
2  * Copyright 2016, 2018 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _SCC_REPORT_H_
18 #define _SCC_REPORT_H_
19 
20 #include "utilities.h"
21 #include <cstring>
22 #include <iomanip>
23 #include <iostream>
24 #include <sstream>
25 #include <sysc/kernel/sc_time.h>
26 #include <sysc/utils/sc_report.h>
27 #include <util/ities.h>
28 
29 #if defined(_MSC_VER) && defined(ERROR)
30 #undef ERROR
31 #endif
32 
74 #define SCC_LOG_LEVEL_PARAM_NAME "log_level"
80 namespace scc {
82 static std::array<const char* const, 8> buffer = {{"NONE", "FATAL", "ERROR", "WARNING", "INFO", "DEBUG", "TRACE", "TRACEALL"}};
84 enum class log { NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE, TRACEALL, DBGTRACE = TRACEALL };
92 inline log as_log(int logLevel) {
93  assert(logLevel >= static_cast<int>(log::NONE) && logLevel <= static_cast<int>(log::TRACEALL));
94  std::array<const log, 8> m = {{log::NONE, log::FATAL, log::ERROR, log::WARNING, log::INFO, log::DEBUG, log::TRACE, log::TRACEALL}};
95  return m[logLevel];
96 }
105 inline std::istream& operator>>(std::istream& is, log& val) {
106  std::string buf;
107  is >> buf;
108  for(auto i = 0U; i <= static_cast<unsigned>(log::TRACEALL); ++i) {
109  if(std::strcmp(buf.c_str(), buffer[i]) == 0) {
110  val = as_log(i);
111  return is;
112  }
113  }
114  return is;
115 }
124 inline std::ostream& operator<<(std::ostream& os, log const& val) {
125  os << buffer[static_cast<unsigned>(val)];
126  return os;
127 }
136 void init_logging(log level = log::WARNING, unsigned type_field_width = 24, bool print_time = false);
145 void reinit_logging(log level = log::WARNING);
152 struct LogConfig {
153  log level{log::WARNING};
154  unsigned msg_type_field_width{24};
155  bool print_sys_time{false};
156  bool print_sim_time{true};
157  bool print_delta{false};
158  bool print_severity{true};
159  bool colored_output{true};
160  std::string log_file_name{""};
161  std::string log_filter_regex{""};
162  bool log_async{true};
163  bool dont_create_broker{false};
164  bool report_only_first_error{false};
165  bool instance_based_log_levels{true};
166  bool install_handler{true};
167 
179  LogConfig& msgTypeFieldWidth(unsigned);
185  LogConfig& printSysTime(bool = true);
191  LogConfig& printSimTime(bool = true);
197  LogConfig& printDelta(bool = true);
203  LogConfig& printSeverity(bool = true);
209  LogConfig& coloredOutput(bool = true);
215  LogConfig& logFileName(std::string&&);
221  LogConfig& logFileName(const std::string&);
227  LogConfig& logFilterRegex(std::string&&);
233  LogConfig& logFilterRegex(const std::string&);
239  LogConfig& logAsync(bool = true);
245  LogConfig& dontCreateBroker(bool = true);
251  LogConfig& reportOnlyFirstError(bool = true);
257  LogConfig& instanceBasedLogLevels(bool = true);
263  LogConfig& installHandler(bool = true);
264 };
271 void init_logging(const LogConfig& log_config);
285 void set_logging_level(log level);
301 void set_cycle_base(sc_core::sc_time period);
308 inline sc_core::sc_verbosity get_log_verbosity() {
309  return static_cast<sc_core::sc_verbosity>(::sc_core::sc_report_handler::get_verbosity_level());
310 }
321 sc_core::sc_verbosity get_log_verbosity(char const* t);
332 inline sc_core::sc_verbosity get_log_verbosity(std::string const& t) { return get_log_verbosity(t.c_str()); }
341 template <sc_core::sc_severity SEVERITY> struct ScLogger {
350  ScLogger(const char* file, int line, int verbosity = sc_core::SC_MEDIUM)
351  : t(nullptr)
352  , file(file)
353  , line(line)
354  , level(verbosity){};
355 
356  ScLogger() = delete;
357 
358  ScLogger(const ScLogger&) = delete;
359 
360  ScLogger(ScLogger&&) = delete;
361 
362  ScLogger& operator=(const ScLogger&) = delete;
363 
364  ScLogger& operator=(ScLogger&&) = delete;
370  virtual ~ScLogger() { ::sc_core::sc_report_handler::report(SEVERITY, t ? t : "SystemC", os.str().c_str(), level, file, line); }
377  inline ScLogger& type() {
378  this->t = nullptr;
379  return *this;
380  }
388  inline ScLogger& type(char const* t) {
389  this->t = const_cast<char*>(t);
390  return *this;
391  }
399  inline ScLogger& type(std::string const& t) {
400  this->t = const_cast<char*>(t.c_str());
401  return *this;
402  }
409  inline std::ostream& get() { return os; };
410 
411 protected:
412  std::ostringstream os{};
413  char* t{nullptr};
414  const char* file;
415  const int line;
416  const int level;
417 };
418 
423 #define SCCLOG(lvl, ...) ::scc::ScLogger<::sc_core::SC_INFO>(__FILE__, __LINE__, lvl / 10).type(__VA_ARGS__).get()
425 #define SCCTRACEALL(...) \
426  if(::scc::get_log_verbosity(__VA_ARGS__) >= sc_core::SC_DEBUG) \
427  SCCLOG(sc_core::SC_DEBUG, __VA_ARGS__)
429 #define SCCTRACE(...) \
430  if(::scc::get_log_verbosity(__VA_ARGS__) >= sc_core::SC_FULL) \
431  SCCLOG(sc_core::SC_FULL, __VA_ARGS__)
433 #define SCCDEBUG(...) \
434  if(::scc::get_log_verbosity(__VA_ARGS__) >= sc_core::SC_HIGH) \
435  SCCLOG(sc_core::SC_HIGH, __VA_ARGS__)
437 #define SCCINFO(...) \
438  if(::scc::get_log_verbosity(__VA_ARGS__) >= sc_core::SC_MEDIUM) \
439  SCCLOG(sc_core::SC_MEDIUM, __VA_ARGS__)
441 #define SCCWARN(...) \
442  if(::scc::get_log_verbosity(__VA_ARGS__) >= sc_core::SC_LOW) \
443  ::scc::ScLogger<::sc_core::SC_WARNING>(__FILE__, __LINE__, sc_core::SC_MEDIUM).type(__VA_ARGS__).get()
445 #define SCCERR(...) ::scc::ScLogger<::sc_core::SC_ERROR>(__FILE__, __LINE__, sc_core::SC_MEDIUM).type(__VA_ARGS__).get()
447 #define SCCFATAL(...) ::scc::ScLogger<::sc_core::SC_FATAL>(__FILE__, __LINE__, sc_core::SC_MEDIUM).type(__VA_ARGS__).get()
448 
449 #ifdef NDEBUG
450 #define SCC_ASSERT(expr) ((void)0)
451 #else
452 #define SCC_ASSERT(expr) ((void)((expr) ? 0 : (SC_REPORT_FATAL(::sc_core::SC_ID_ASSERTION_FAILED_, #expr), 0)))
453 #endif
455 #define SCMOD this->name()
463 class stream_redirection : public std::stringbuf {
464 public:
472  stream_redirection(std::ostream& os, log level);
473 
474  stream_redirection(stream_redirection const&) = delete;
475 
476  stream_redirection& operator=(stream_redirection const&) = delete;
477 
479 
480  stream_redirection& operator=(stream_redirection&&) = delete;
492  void reset();
493 
494 protected:
495  std::streamsize xsputn(const char_type* s, std::streamsize n) override;
496  int sync() override;
497  std::ostream& os;
498  log level;
499  std::streambuf* old_buf{nullptr};
500 };
501 
502 } // namespace scc // end of scc-sysc
504 #endif /* _SCC_REPORT_H_ */
stream redirector
Definition: report.h:463
~stream_redirection()
destructor restoring the output stream buffer
Definition: report.cpp:322
void reset()
reset the stream redirection and restore output buffer of the stream
Definition: report.cpp:326
stream_redirection(std::ostream &os, log level)
constructor redirecting the given stream to a SystemC log message of given llog level
SCC SystemC utilities.
std::istream & operator>>(std::istream &is, log &val)
read a log level from input stream e.g. used by boost::lexical_cast
Definition: report.h:105
bool is_logging_initialized()
get the state of the SCC logging system
Definition: report.cpp:436
void set_logging_level(log level)
sets the SystemC logging level
Definition: report.cpp:452
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
Definition: report.cpp:438
log as_log(int logLevel)
safely convert an integer into a log level
Definition: report.h:92
log get_logging_level()
get the SystemC logging level
Definition: report.cpp:460
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
Definition: report.h:308
std::ostream & operator<<(std::ostream &os, log const &val)
output the textual representation of the log level
Definition: report.h:124
log
enum defining the log levels
Definition: report.h:84
the configuration class for the logging setup
Definition: report.h:152
LogConfig & printSeverity(bool=true)
Definition: report.cpp:489
LogConfig & printSysTime(bool=true)
Definition: report.cpp:474
LogConfig & logFilterRegex(std::string &&)
LogConfig & reportOnlyFirstError(bool=true)
Definition: report.cpp:529
LogConfig & coloredOutput(bool=true)
Definition: report.cpp:504
LogConfig & dontCreateBroker(bool=true)
Definition: report.cpp:524
LogConfig & logLevel(log)
Definition: report.cpp:464
LogConfig & msgTypeFieldWidth(unsigned)
Definition: report.cpp:469
LogConfig & logFileName(const std::string &)
LogConfig & printSimTime(bool=true)
Definition: report.cpp:479
LogConfig & logAsync(bool=true)
Definition: report.cpp:519
LogConfig & printDelta(bool=true)
Definition: report.cpp:484
LogConfig & instanceBasedLogLevels(bool=true)
Definition: report.cpp:533
LogConfig & installHandler(bool=true)
Definition: report.cpp:537
LogConfig & logFilterRegex(const std::string &)
LogConfig & logFileName(std::string &&)
the logger class
Definition: report.h:341
ScLogger & type(char const *t)
set the category of the log entry
Definition: report.h:388
virtual ~ScLogger()
the destructor generating the SystemC report
Definition: report.h:370
ScLogger & type(std::string const &t)
set the category of the log entry
Definition: report.h:399
std::ostream & get()
get the underlying ostringstream
Definition: report.h:409
ScLogger & type()
reset the category of the log entry
Definition: report.h:377
ScLogger(const char *file, int line, int verbosity=sc_core::SC_MEDIUM)
Definition: report.h:350