A good way to retrieve information of what is happening in our SystemC simulations is to generate a log file with the information that we need (module name, simulation time, what phase is going on, etc.).
The most elegant and easy way to do that is to create a special class for that task, that the other modules in the simulation can use to send their information and to be stores in disk. This can be achieved designing a singleton type class. This design pattern restricts the instantiation of a class to one single object. In our case, it will cause that all modules use the same object and log file.
To design a singleton class, we may take into account:
- The class itself creates the unique instantiation
- Declare the constructor as private so the class cannot be directly instantiated
- Allow to access to the object only through a class method
This can be done creating a class with a private constructor and a method that creates the object if it is not created yet and returns the reference to the object:
Log.h file
class Log
{
public:
static Log* Instance();
private:
Log(const char*);
static Log* m_pInstance;
std::ofstream m_stream;
...
}
Log.cpp file
Log* Log::m_pInstance = NULL;
Log*
Log::Instance()
{
if (!m_pInstance)
m_pInstance = new Log("Log.txt");
return m_pInstance;
}
Log::Log(const char* filename)
{
m_stream.open(filename);
}
Every module willing to use this class, may call it in the following way:
Log *my_log = Log::Instance();
To finish, we add some methods to store into a file the information we want to the Log class:
// Method to be called this way: my_log->SC_log("My log message");
void
Log::SC_log(std::ostringstream msg)
{
m_stream << "time " << sc_core::sc_time_stamp() << ": "
<< msg << std::endl; } // Method to be used inside C++ streams // we can write: my_log->SC_log() << name() << ". My log message" << std::endl;
std::ofstream&
Log::SC_log()
{
m_stream << "time " << sc_core::sc_time_stamp() << ": ";
return m_stream;
}
In the second case, the log file is in plain text with in each line the simulation time, the module name and the desired text. We can use some other library to generate a XML file or something more structured, but that is another post 😉
And that’s all to have a common log file for all your simulations in a easy and elegant way.
Leave a Reply