Introduction
Sometimes, you want to create a log file to store some information. You may create a log every second, every day or every week.
The framework designed by me can make the work easier. You can add it in your project so that you can easily create a record in the log file every N days. (The N can be changed in the source code).
The Framework Source Code
I wrote the framework in C++ and built two classes. One is the main class of the logger which is not necessary for you to change. The other is a class of each record which should be modified when you apply it into your project.
I used template so that you can simply use your own data structure of each record.
Let's take a look at the definition of the main class:
template<typename T>
class WeeklyReport
{
public:
typedef void (*WeekFunction) (T&);
WeeklyReport(string LogFileName,WeekFunction CallbackFunc,float TimeSpan):
m_LogFileName(LogFileName),Callback(CallbackFunc),m_Timespan(TimeSpan){};
static time_t getSecondCount(int Year,int Month,int Day);
void Write(); private:
bool WriteData();
time_t m_Today;
time_t m_LastWroteTime;
string m_LogFileName; void ReadLastWroteTime();
T* WeeklyLogData; char * TodayTag() const;
WeekFunction Callback;
float m_Timespan; };
The class WeeklyReport
has a private
member WeeklyLogData
. It's a pointer to a data structure which is a template data structure so that you can declare a class using your own data structure.
The constructor of the class has three parameters:
- Log File name
- A callback function pointer
- A time span
It has a member function Write
which will create/open a log file and check if the program should create a new record.
ReadLastWroteTime();
m_Today=time(NULL);
float timeSpan=(difftime(m_Today,m_LastWroteTime)/60/60/24); if(timeSpan>m_Timespan) {
If it's not necessary to create a new record, nothing will be done (no extra memory space).
When the code finds that a new record should be created, a new object of the specific type (the template parameter) will be created. After creating the Object
, the callback function will be called:
WeeklyLogData=new T;
Callback(*WeeklyLogData);
WriteData(); delete WeeklyLogData;
Using the Code
It's very simple to use the class. There are three steps in building a new logger:
- Append the Indicator Data to WeeklyItems.h's
public
members:
class WeeklyItem
{
int MaxApps;
string AppsOpened;
string Plugins;
};
I have added three example data in it. Applications Opened, Max Application Number and Plugins installed. You may delete them and add some new members which you want to record.
- Modify the output style in WeeklyItems.h:
ostream& operator<<(ostream & ostr, const WeeklyItem & item)
{
ostr<<"Max Opened:"<<item.MaxApps<<",
Apps:"<<item.AppsOpened<<",Plugins:"<<item.Plugins;
return (ostr);
}
You should change the operation overload function and make a user-friendly output style. Remember, don't add "endl
" at the end.
- Add a function in
GatherWeeklyData
to generate that indicator:
void GatherTheWeeklyData(WeeklyItem &inItem)
{
inItem.AppsOpened.append(SumUpApplications());
inItem.Plugins.append(SumUpPlugins());
inItem.MaxApps=SumUpMaxApplications();
}
You may change the function to sum up all the information you need. The example showed how to set the data members.
Finally, you can simply apply the class and define a new object. Then invoke the Write
method for writing a new record. Just add the following line into your project:
WeeklyReport<WeeklyItem> WeekReporter(WEEK_REPORT_FILE_NAME,&GatherWeeklyData);
WeekReporter.Run();
Output of the Log File
After applying the logger framework into your project, a log file may look as follows:
2007-03-10,Max Opened:54,Apps:Install Shield/MSDN,Plugins:Google Hints
2007-03-17,Max Opened:40,Apps:Internet Explorer/Visual Studio,Plugins:MSN Space
2007-03-24,Max Opened:35,Apps.....
...
This log file will be changed every week!
Points of Interest
In this framework, I found that templates and operator overloading are very useful to build a programmer-friendly library or a class. Maybe it's more complicated to apply these advanced skills into your project, but it definitely will make the customer code much simpler.
History
- 15th April, 2007: Initial post