Files
Work/proto/source/LOGFILE.HPP
2024-08-07 09:16:27 -04:00

112 lines
2.3 KiB
C++

#ifndef _PROTO_LOGFILE_HPP_
#define _PROTO_LOGFILE_HPP_
#ifndef _COMMON_PUREVIEWOFFILE_HPP_
#include <common/pview.hpp>
#endif
#ifndef _COMMON_FILEMAP_HPP_
#include <common/filemap.hpp>
#endif
#ifndef _THREAD_EVENT_HPP_
#include <thread/event.hpp>
#endif
#ifndef _THREAD_MUTEX_HPP_
#include <thread/mutex.hpp>
#endif
class LogFile
{
public:
LogFile(const String &strLogName);
virtual ~LogFile();
BOOL write(const String &strLine);
BOOL write(Block<String> &strLines);
BOOL read(String &strLine);
BOOL read(Block<String> &strLines);
void synchronize(void);
private:
void log(const String &msg);
FileMap mFileMap;
PureViewOfFile mFileView;
Event mIOEvent;
Event mIOAckEvent;
Mutex mMutex;
BOOL mIsAckPending;
};
LogFile::LogFile(const String &strLogName)
: mFileMap(strLogName,0,1000000), mFileView(mFileMap), mIOEvent("IOPUTEVENT",FALSE),
mIOAckEvent("IOACKEVENT",FALSE), mMutex("IOENTERREQUEST",FALSE), mIsAckPending(FALSE)
{
}
LogFile::~LogFile()
{
}
BOOL LogFile::write(const String &strLine)
{
mIOAckEvent.waitEvent();
mIOAckEvent.resetEvent();
mMutex.requestMutex();
mFileView.rewind();
mFileView.writeLine(strLine);
mMutex.releaseMutex();
mIOEvent.setEvent();
return TRUE;
}
BOOL LogFile::write(Block<String> &strLines)
{
mIOAckEvent.waitEvent();
mIOAckEvent.resetEvent();
mMutex.requestMutex();
mFileView.rewind();
for(int lineIndex=0;lineIndex<strLines.size();lineIndex++)mFileView.writeLine(strLines[lineIndex]);
mMutex.releaseMutex();
mIOEvent.setEvent();
return TRUE;
}
inline
BOOL LogFile::read(String &strLine)
{
String lineString;
mIOEvent.waitEvent();
mMutex.requestMutex();
mIOEvent.resetEvent();
mFileView.rewind();
mFileView.getLine(lineString);
strLine=lineString;
mMutex.releaseMutex();
mIOAckEvent.setEvent();
return lineString.isNull();
}
inline
BOOL LogFile::read(Block<String> &strLines)
{
String strLine;
strLines.remove();
mIOEvent.waitEvent();
mMutex.requestMutex();
mIOEvent.resetEvent();
mFileView.rewind();
while(mFileView.getLine(strLine)&&!strLine.isNull())strLines.insert(&strLine);
mMutex.releaseMutex();
mIOAckEvent.setEvent();
return TRUE;
}
void LogFile::synchronize(void)
{
mIOAckEvent.setEvent();
}
void LogFile::log(const String &msg)
{
::OutputDebugString((String&)msg+String("\n"));
}
#endif