A simple Logger
Article Metadata
Contents |
A Simple Logger
Number of times we need a very simple file based logger, which can be used in debugging. Such logger is really helpful while understangin what exactly happening on target device when no remote debugging is possible. Its always useful if the logger is really lightweight and less complex. In following example, in Symbian C++, a very simple logger is presented with fewer lines.
Example Logger
A FileLogger class containing RFile object is present. The file name in which the contents will be logged is hard-coded on can replace it with there own logger file name. Numerous times the Logger file is fix and single. So purposefully a facility to set the filename through methods is avoided. One can customize the contructor to provide the file name.
This logger can be extended and can be changed as it has been provided in the most possible raw form. Basic code of opening file and adding a line to file has been presented. Also each line added gets tagged with the time and date.
Code
The header file
FileLogger.h
#ifndef FILELOGGER_H_
#define FILELOGGER_H_
#include "f32file.h"
class FileLogger
{
public:
FileLogger();
virtual ~FileLogger();
void AddLog(TDes& aLine);
private:
RFile iFile;
};
#endif /* FILELOGGER_H_ */
FileLogger.cpp
#include "FileLogger.h"
#include "utf.h"
_LIT(KFile,"c:\\log.txt");
FileLogger::FileLogger()
{
// TODO Auto-generated constructor stub
}
FileLogger::~FileLogger()
{
// TODO Auto-generated destructor stub
}
void FileLogger::AddLog(TDes& aLine)
{
TInt pos = 0;
TBuf8<500> buff;
TTime now;
now.HomeTime();
TDateTime time;
time = now.DateTime();
TBuf<50> temp;
temp.Format(_L("%d/%d/%d %d:%d:%d "),time.Day()+1,time.Month()+1,time.Year(),time.Hour(),time.Minute(),time.Second());
temp.Append(aLine);
//aLine.Append(KNewLine);
RFs fs;
fs.Connect();
TInt err;
err = iFile.Open(fs,KFile, EFileWrite | EFileStreamText);
if(err == KErrNotFound)
{
err = iFile.Create(fs,KFile, EFileWrite | EFileStreamText);
}
CnvUtfConverter::ConvertFromUnicodeToUtf8(buff,temp);
iFile.Seek(ESeekEnd,pos);
iFile.Write(buff);
iFile.Close();
fs.Close();
}
Libary Reference Needed
efsrv.lib and charconv.lib

