天天看点

SystemC使用sc_report_handler处理log打印

sc_severity是在每次需要打印信息的时候,根据本次信息的类型(info、warning提示,error报错,fatal报错)进行选择。sc_verbosity只在sc_severity为SC_INFO的情况下才有效,这是因为通常情况下,warning/error/fatal信息不会太多,而info信息则比较多,且info信息很多是为了debug使用的,故需要对info信息进行等级区分。

对应sc_severity的枚举类型,分别定义了4个基本宏,另外还有一个针对SC_INFO的增强型宏:

SC_REPORT_INFO( msg_type, msg )

SC_REPORT_WARNING( msg_type, msg )

SC_REPORT_ERROR( msg_type, msg )

SC_REPORT_FATAL( msg_type, msg )

SC_REPORT_INFO_VERB( msg_type, msg, verbosity )

参数 const char*  msg_type一般赋值为this->name(),也就是本sc_module的实例化名字。msg_type在set_actions等函数中使用时,用于分module进行打印设置。

Systemc源码 /src/sysc/utils/sc_report.h中定义如下。

enum sc_severity {
    SC_INFO = 0,  // informative only
    SC_WARNING, // indicates potentially incorrect condition
    SC_ERROR,   // indicates a definite problem
    SC_FATAL,   // indicates a problem from which we cannot recover
    SC_MAX_SEVERITY
};

 enum sc_verbosity { 
     SC_NONE = 0, 
     SC_LOW = 100, 
     SC_MEDIUM = 200, 
     SC_HIGH = 300,
     SC_FULL = 400, 
     SC_DEBUG = 500
 };

enum {
    SC_UNSPECIFIED  = 0x0000, // look for lower-priority rule
    SC_DO_NOTHING   = 0x0001, // take no action (ignore if other bits set)
    SC_THROW        = 0x0002, // throw an exception
    SC_LOG          = 0x0004, // add report to report log
    SC_DISPLAY      = 0x0008, // display report to screen
    SC_CACHE_REPORT = 0x0010, // save report to cache
    SC_INTERRUPT    = 0x0020, // call sc_interrupt_here(...)
    SC_STOP         = 0x0040, // call sc_stop()
    SC_ABORT        = 0x0080  // call abort()
};

static sc_actions set_actions(sc_severity,sc_actions = SC_UNSPECIFIED);
static sc_actions set_actions(const char * msg_type,sc_actions = SC_UNSPECIFIED);
static sc_actions set_actions(const char * msg_type,sc_severity,sc_actions = SC_UNSPECIFIED);

           

sc_report_handler.h中,sc_report_handler_proc是定义的一个函数指针类型,handler是一个函数指针 (成员变量)。

typedef void (* sc_report_handler_proc)(const sc_report&, const sc_actions &);

static sc_report_handler_proc  handler;

sc_report_handler.cpp中,default_handler是一个函数,然后handler 默认指向这个函数;也可以通过set_handler函数来改写 handler。

void sc_report_handler::default_handler(const sc_report& rep,   const sc_actions& actions)

sc_report_handler_proc sc_report_handler::handler = &sc_report_handler::default_handler;

一个应用示例如下

#include <iostream>
#include <fstream>
#include "systemc.h"

using namespace std;
using namespace sc_core;

class TestPlatform
: public sc_module
{
    public:
        SC_HAS_PROCESS(TestPlatform);

        TestPlatform(const sc_module_name&    name)
        : sc_module(name)
        ,m_period (sc_time(1000,SC_PS))
        {
            SC_THREAD(PrintMessage);
        };

    public:

        void PrintMessage();

        ~TestPlatform()
        { ;
        }
        
    public:
        sc_time m_period;
};

void TestPlatform::PrintMessage()
{
    while(1)
    {
        wait(m_period);
        SC_REPORT_INFO_VERB(name(),"this is a SC_LOW info message ",SC_LOW);
        SC_REPORT_INFO_VERB(name(),"this is a SC_MEDIUM info message ",SC_MEDIUM);
        SC_REPORT_INFO_VERB(name(),"this is a SC_HIGH info message ",SC_HIGH);
        SC_REPORT_INFO_VERB(name(),"this is a SC_FULL info message ",SC_FULL);
        SC_REPORT_INFO_VERB(name(),"this is a SC_DEBUG info message ",SC_DEBUG);

        SC_REPORT_WARNING(name(),"this is a SC_WARNING message "); // SC_WARNING
        SC_REPORT_WARNING(name(),"this is a SC_ERROR message ");
        SC_REPORT_WARNING(name(),"this is a SC_FATAL message ");

        cout<<"SC_INFO counter " << sc_report_handler::get_count(SC_INFO)<< endl;   // 得到的是SC_INFO打印的行数(SC_LOW + SC_MEDIUM + ....)
        cout<<"SC_WARNING counter " << sc_report_handler::get_count(SC_WARNING)<< endl;  // 得到的是SC_WARNINNG打印的行数(SC_WARNING * 3 )a

        sc_report_handler::stop_after(SC_INFO, 500);// 当SC_INFO打印的行数达到500行 时 就调用st_stop() 
    }
}

int sc_main(int argc, char** argv)
{
    TestPlatform *     m_platform;
    m_platform = new  TestPlatform("TestPlatform");

    const char* filename = "./log_file" ;
    sc_report_handler::set_log_file_name(filename); 
    sc_report_handler::set_actions(SC_INFO, SC_DISPLAY | SC_LOG);
    sc_report_handler::suppress(SC_LOG);    // 抑制 SC_LOG,也就是不打印到log file
    // sc_report_handler::suppress();       // 不加任何参数,用于清除之前的suppress
    sc_report_handler::force(SC_LOG);  // 强制打开SC_LOG,也就是此行代码之前 对于SC_LOG的suppress都失效
    // sc_report_handler::suppress(SC_DISPLAY);
    sc_report_handler::set_verbosity_level(sc_core::SC_MEDIUM); // 设置全局的 verbosity level,只针对SC_INFO 

    sc_start(500,SC_NS);
    return 0;
}
           

继续阅读