patterncppMinor
C++ thread safe log file writer
Viewed 0 times
filelogwriterthreadsafe
Problem
I made this class (MFC lib) to write a log files. Eventually I want to implement this code in a thread safe way.
Log.h
Log.cpp
```
#include "stdafx.h"
#include "Log.h"
#include
#include
using namespace std;
const unsigned long long MAX_LOG_FILE_SIZE{ 1024 1024 4 };
CStdioFile logFile;
CString _fileName;
CFileStatus fileStatus;
CFileException exceptionEx;
UINT_PTR shownTimer;
BOOL fileOpened;
mutex m;
queue dataLog;
void DoSafeWrite(CString log);
void PrintQueue();
CLog::CLog()
{
/* want to implement auto flush method to write all
queue items into file in here with timer like method.
but at the moment manually call the function "TWriteAllToLog()" to write log file.
*/
}
CLog::~CLog()
{
}
/*
usage:
CString str;
str.Format(L"This is log test");
CLog _log; //create object
_log.Log(str); //call to Log function
*/
int CLog::Log(CString log)
{
try{
CTime t = CTime::GetCurrentTime();
CString TStamp = t.Format("%a, %b %d, %Y %H:%M:%S -> ");
CString newLine("\n");
log = TStamp + log + newLine;
dataLog.push(log);
return 1;
}
catch (...){
return -1;
}
}
void CLog::TWriteAllToLog(){
PrintQueue();
}
void PrintQueue()
{
while (!dataLog.empty())
{
m.lock();
CString str;
CString &data = dataLog.front();
DoSafeWrite(data);
dataLog.pop();
m.unlock();
}
}
CString GetFileName() //crate a file name
{
COleDateTime dtDate = COleDateTime::GetCurrentTime();
CString file = dtDate.Format(L"Log_%d_%m_%Y__%H_%M_%S.log");
return file;
}
CString GetLastFileName(CString filename, bool write)
{
const int FILE_CONTENT_LEN = 255;
CString logfiledb{ "_FILE_.db" };
char fileContent[FILE_CONTENT_LEN];
CStdioFile lastLog;
CFileSta
Log.h
#pragma once
class CLog
{
public:
CLog();
~CLog();
int CLog::Log(CString log);
void CLog::TWriteAllToLog();
};Log.cpp
```
#include "stdafx.h"
#include "Log.h"
#include
#include
using namespace std;
const unsigned long long MAX_LOG_FILE_SIZE{ 1024 1024 4 };
CStdioFile logFile;
CString _fileName;
CFileStatus fileStatus;
CFileException exceptionEx;
UINT_PTR shownTimer;
BOOL fileOpened;
mutex m;
queue dataLog;
void DoSafeWrite(CString log);
void PrintQueue();
CLog::CLog()
{
/* want to implement auto flush method to write all
queue items into file in here with timer like method.
but at the moment manually call the function "TWriteAllToLog()" to write log file.
*/
}
CLog::~CLog()
{
}
/*
usage:
CString str;
str.Format(L"This is log test");
CLog _log; //create object
_log.Log(str); //call to Log function
*/
int CLog::Log(CString log)
{
try{
CTime t = CTime::GetCurrentTime();
CString TStamp = t.Format("%a, %b %d, %Y %H:%M:%S -> ");
CString newLine("\n");
log = TStamp + log + newLine;
dataLog.push(log);
return 1;
}
catch (...){
return -1;
}
}
void CLog::TWriteAllToLog(){
PrintQueue();
}
void PrintQueue()
{
while (!dataLog.empty())
{
m.lock();
CString str;
CString &data = dataLog.front();
DoSafeWrite(data);
dataLog.pop();
m.unlock();
}
}
CString GetFileName() //crate a file name
{
COleDateTime dtDate = COleDateTime::GetCurrentTime();
CString file = dtDate.Format(L"Log_%d_%m_%Y__%H_%M_%S.log");
return file;
}
CString GetLastFileName(CString filename, bool write)
{
const int FILE_CONTENT_LEN = 255;
CString logfiledb{ "_FILE_.db" };
char fileContent[FILE_CONTENT_LEN];
CStdioFile lastLog;
CFileSta
Solution
const unsigned long long MAX_LOG_FILE_SIZE{ 1024 * 1024 * 4 };That's 3 magic numbers right there. The result is
4194304, you don't need a long long for that. A long usually holds till 2147483647.Don't write custom destructors if they're empty anyway. Your compiler will take care of that.
You've included
"stdafx.h". You don't need it.void DoSafeWrite(CString log);Make it an
int instead and let it return whether the write was successful.Namespaces
using namespace std; is considered bad practice. Short code is not a requirement in C++, clear code is preferred.Trace
TRACE is a debugging macro and should NOT be used for released software. User proper exceptions and/or use status codes to indicate what happened. TRACE is not what you want.Code Snippets
const unsigned long long MAX_LOG_FILE_SIZE{ 1024 * 1024 * 4 };void DoSafeWrite(CString log);Context
StackExchange Code Review Q#134731, answer score: 5
Revisions (0)
No revisions yet.