Difference between revisions of "How to use MessageCallBackHandler Class"
From Verific Design Automation FAQ
(Created page with "This is a small example showing how to use MessageCallBackHandler Class: In C++: <nowiki> #include <iostream> #include "veri_file.h" #include "Message.h" #include "Strings...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 26: | Line 26: | ||
public: | public: | ||
virtual void Msg(msg_type_t msg_type, const char *message_id, linefile_type linefile, const char *msg_str) ; | virtual void Msg(msg_type_t msg_type, const char *message_id, linefile_type linefile, const char *msg_str) ; | ||
+ | |||
+ | char * GetLinefile(linefile_type linefile) const | ||
+ | { | ||
+ | if (!linefile) | ||
+ | return Strings::save(""); | ||
+ | char * lineNo = Strings::itoa(LineFile::GetLineNo(linefile)); | ||
+ | char * lineFileStr = Strings::save(LineFile::GetFileName(linefile), "(", lineNo, "): "); | ||
+ | Strings::free(lineNo); | ||
+ | return lineFileStr; | ||
+ | } | ||
+ | |||
+ | const char * GetMsgType(msg_type_t msg_type) const | ||
+ | { | ||
+ | if (msg_type == VERIFIC_INFO) | ||
+ | return "INFO: "; | ||
+ | else if (msg_type == VERIFIC_WARNING) | ||
+ | return "WARNING: "; | ||
+ | else if (msg_type == VERIFIC_ERROR) | ||
+ | return "ERROR: "; | ||
+ | else | ||
+ | return "-- "; | ||
+ | } | ||
const char * GetMessageString() const { return _msg_string ; } | const char * GetMessageString() const { return _msg_string ; } | ||
Line 33: | Line 55: | ||
} ; // class MyMsgCallBack | } ; // class MyMsgCallBack | ||
− | void MyMsgCallBack::Msg(msg_type_t | + | void MyMsgCallBack::Msg(msg_type_t msg_type, const char * message_id, linefile_type linefile, const char *msg_str) |
{ | { | ||
if (!_msg_string) { | if (!_msg_string) { | ||
− | _msg_string = Strings::save(msg_str) ; | + | char *line_str = GetLinefile(linefile); |
+ | _msg_string = Strings::save("\t", line_str, GetMsgType(msg_type), msg_str, " (", message_id, ")\n") ; | ||
+ | Strings::free(line_str) ; | ||
} else { | } else { | ||
char *tmp = _msg_string ; | char *tmp = _msg_string ; | ||
− | _msg_string = Strings::save(_msg_string, "\ | + | char *line_str = GetLinefile(linefile); |
+ | _msg_string = Strings::save(_msg_string, "\t", line_str, GetMsgType(msg_type), msg_str, " (", message_id, ")\n") ; | ||
Strings::free(tmp) ; | Strings::free(tmp) ; | ||
+ | Strings::free(line_str) ; | ||
} | } | ||
} | } | ||
Line 53: | Line 79: | ||
const char *msg_str = mmcb.GetMessageString() ; | const char *msg_str = mmcb.GetMessageString() ; | ||
− | std::cout << " | + | std::cout << "\nAccumulated messages: \n" << msg_str ; |
return 0 ; | return 0 ; | ||
Line 72: | Line 98: | ||
-- Analyzing Verilog file 'test.v' (VERI-1482) | -- Analyzing Verilog file 'test.v' (VERI-1482) | ||
test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) | test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) | ||
− | Accumulated messages: Analyzing Verilog file 'test.v' | + | |
− | concatenation with an unsized literal will be treated as 32 bits | + | Accumulated messages: |
+ | -- Analyzing Verilog file 'test.v' (VERI-1482) | ||
+ | test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) | ||
[moh@awing0 15065]$ | [moh@awing0 15065]$ | ||
</nowiki> | </nowiki> |
Latest revision as of 15:22, 5 December 2023
This is a small example showing how to use MessageCallBackHandler Class:
In C++:
#include <iostream> #include "veri_file.h" #include "Message.h" #include "Strings.h" #ifdef VERIFIC_NAMESPACE using namespace Verific ; #endif class MyMsgCallBack : public MessageCallBackHandler { public: MyMsgCallBack() : MessageCallBackHandler(), _msg_string(0) { } virtual ~MyMsgCallBack() { Strings::free(_msg_string) ; _msg_string = 0 ; } private: MyMsgCallBack(const MyMsgCallBack &) ; MyMsgCallBack& operator=(const MyMsgCallBack &) ; public: virtual void Msg(msg_type_t msg_type, const char *message_id, linefile_type linefile, const char *msg_str) ; char * GetLinefile(linefile_type linefile) const { if (!linefile) return Strings::save(""); char * lineNo = Strings::itoa(LineFile::GetLineNo(linefile)); char * lineFileStr = Strings::save(LineFile::GetFileName(linefile), "(", lineNo, "): "); Strings::free(lineNo); return lineFileStr; } const char * GetMsgType(msg_type_t msg_type) const { if (msg_type == VERIFIC_INFO) return "INFO: "; else if (msg_type == VERIFIC_WARNING) return "WARNING: "; else if (msg_type == VERIFIC_ERROR) return "ERROR: "; else return "-- "; } const char * GetMessageString() const { return _msg_string ; } private: char *_msg_string ; } ; // class MyMsgCallBack void MyMsgCallBack::Msg(msg_type_t msg_type, const char * message_id, linefile_type linefile, const char *msg_str) { if (!_msg_string) { char *line_str = GetLinefile(linefile); _msg_string = Strings::save("\t", line_str, GetMsgType(msg_type), msg_str, " (", message_id, ")\n") ; Strings::free(line_str) ; } else { char *tmp = _msg_string ; char *line_str = GetLinefile(linefile); _msg_string = Strings::save(_msg_string, "\t", line_str, GetMsgType(msg_type), msg_str, " (", message_id, ")\n") ; Strings::free(tmp) ; Strings::free(line_str) ; } } int main(int argc, const char **argv) { MyMsgCallBack mmcb ; Message::RegisterCallBackMsg(&mmcb) ; const char *in_file = (argc > 1) ? argv[1] : "test.v" ; if (!veri_file::Analyze(in_file)) return 1 ; const char *msg_str = mmcb.GetMessageString() ; std::cout << "\nAccumulated messages: \n" << msg_str ; return 0 ; }
test.v:
module test (output [3:0] out) ; assign out = {1} ; endmodule
Run:
[moh@awing0 15065]$ ./test-linux -- Analyzing Verilog file 'test.v' (VERI-1482) test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) Accumulated messages: -- Analyzing Verilog file 'test.v' (VERI-1482) test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) [moh@awing0 15065]$
In Python:
#!/usr/bin/python import sys sys.path.append('../py') import Verific class MyMessageCallBack(Verific.MessageCallBackHandler,object): _msg_string = "\nAccumulated message:\n" def __init__(obj): if not obj: return super(MyMessageCallBack,obj).__init__() def Msg(self, msg_type, message_id, linefile, msg_str): self._msg_string = self._msg_string + "\t" + self.GetLinefile(linefile) + self.GetMsgType(msg_type) + msg_str + " (" + message_id + ")\n" def GetLinefile(self, linefile): if not linefile: return "" return Verific.LineFile_GetFileName(linefile) + "(" + str(Verific.LineFile_GetLineNo(linefile)) + "): " def GetMsgType(self, msg_type): if (msg_type==Verific.VERIFIC_INFO): return "INFO: " elif (msg_type==Verific.VERIFIC_WARNING): return "WARNING: " elif (msg_type==Verific.VERIFIC_ERROR): return "ERROR: " return "-- " def GetMessageString(self): return self._msg_string mc = MyMessageCallBack() Verific.Message_RegisterCallBackMsg(mc) if (not Verific.veri_file_Analyze("test.v")): print("Error during analysis. Exiting.") sys.exit(1) print mc.GetMessageString() sys.exit(0)
Run:
[moh@awing0 15065]$ ./test.py -- Analyzing Verilog file 'test.v' (VERI-1482) test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) Accumulated message: -- Analyzing Verilog file 'test.v' (VERI-1482) test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) [moh@awing0 15065]$
In Perl:
#!/usr/bin/perl -w use strict ; use warnings ; use lib "../pm" ; require "Verific.pm" ; use Verific ; { package MyMessageCallBack ; use base 'Verific::MessageCallBackHandler' ; my $msg_string = "\nAccumulated message:\n" ; sub Msg { my ($class, $msg_type, $message_id, $linefile, $msg_str) = (@_) ; $msg_string .= "\t" . GetLinefile($linefile) . GetMsgType($msg_type) . "$msg_str ($message_id)\n" ; } sub GetLinefile { my ($linefile) = (@_) ; if (!$linefile) { return "" ; } ; return Verific::LineFile::GetFileName($linefile) . "(" . Verific::LineFile::GetLineNo($linefile) . "): " ; } sub GetMsgType { my ($msg_type) = (@_) ; if ($msg_type == $Verific::VERIFIC_INFO) { return "INFO: "; } elsif ($msg_type == $Verific::VERIFIC_WARNING) { return "WARNING: "; } elsif ($msg_type == $Verific::VERIFIC_ERROR) { return "ERROR: "; } return "-- "; } sub GetMessageString { return "$msg_string" ; } } my $mc = MyMessageCallBack->new() ; Verific::Message::RegisterCallBackMsg($mc) ; if (!Verific::veri_file::Analyze("test.v")) { print "Error during analysis. Exiting.\n" ; exit(1) ; } print $mc->GetMessageString() ; exit(0) ;
Run:
-- Analyzing Verilog file 'test.v' (VERI-1482) test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) Accumulated message: -- Analyzing Verilog file 'test.v' (VERI-1482) test.v(2): WARNING: concatenation with an unsized literal will be treated as 32 bits (VERI-1320) [moh@awing0 15065]$