天天看點

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;
}
           

繼續閱讀