4
Contributors
7
Replies
10
Views
9 Years
Discussion Span
Last Post by Necrolis
0

log files are just simple text files. I usually put the date/time at the beginning of the log entry. I wouldn't bother with any of the MFC file functions such as CFile and CArchive. Use normal fstream objects. The log function should open the log file, write the log entry then close it again.

0

Thanks for your reply..
Can you please give a simple example codes??

0

This has not been compiled or tested, so it might or might not contain bugs.

void log(const char*msg)
{
    time_t now = time(0);
    struct tm* tm = localtime(&now);    
    ofstream out( "logfile.txt",ios::ate);
    out << tm->tm_year << '/' << tm->tm_mon << '/' << tm->tm_mday
         << ' ' << tm->tm_hour << ':' << tm->tm_min << ':' << tm->tm_sec << ": ";
    out << msg << "\n";
    out.close();
}
0

but this code never work out under MFC.... i had include fstrem.h and iostream.h???
any other suggestions???
thanks

0

you just need to include

#include <fstream>
#include <iostream>

don't use .h suffix.
also encapsulate the Dragon's function in some class and make that a static member.

and your simplest logger is ready :).

0

you can also make it into a class like so

class DSErrorLog
{
  private:
    FILE* LogHandle;
	char szLogName[32];
	char szTimeStamp[64];
    char szLastDeleted[32];  

  public:
    DSErrorLog();         
    ~DSErrorLog();     
    void WriteToLog(char* szText);
    void ErrorLogPurge(int DaysOld);
    void WriteError(char* szText, char* File, int Line);
	void CreateTimeStamp(char* Format, bool Millisecs);
	bool __cdecl WriteToLogFormatted(const char* format, ...);
};

and just use the file io funcs from cstdio(i prefer them over fstream, especially as you can use the sprintf formatting, much faster)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.