Board index » cppbuilder » File Creation and contention

File Creation and contention


2007-03-23 01:15:36 PM
cppbuilder112
I need to create a log file. There may be several process that want to
write to the log file. When creating the log file how do I make sure
that only one process creates the file?
John.
 
 

Re:File Creation and contention

John Grabner wrote:
Quote
I need to create a log file. There may be several process that want to
write to the log file. When creating the log file how do I make sure
that only one process creates the file?

John.
I am sure someone with A LOT more experience will pipe in on this, but I
got here first :)
When you say "processes", do you mean threads within your single
application? If so, then you could wrap your logging code in critical
sections.
If not. Maybe some use of FileOpen() for exclusive use.
Another option that I just remembered. You could create a class that
handles ALL of the logging. Each process could post it's message to a
TStringList property of the class. Then every so often the clas could
open the log file, write out everything, then close the file.
Just some random thoughts... Hope it helps, or at least points you in
the right irection. God Luck.
DC
 

Re:File Creation and contention

Quote
I need to create a log file. There may be several process that want to
write to the log file. When creating the log file how do I make sure that
only one process creates the file?
--- H ---
class TTicketService : public TService
{
// ...
CRITICAL_SECTION ticksrvLogCriticalSection;
// ...
};
__fastcall TTicketService::TTicketService(TComponent* Owner)
: TService(Owner)
{
// ...
InitializeCriticalSection( &ticksrvLogCriticalSection );
// ...
}
__fastcall TTicketService::~TTicketService()
{
// ...
DeleteCriticalSection( &ticksrvLogCriticalSection );
// ...
}
class CSLock
{
public:
LPCRITICAL_SECTION fcs;
CSLock( LPCRITICAL_SECTION acs ) : fcs( acs )
{ if ( fcs ) EnterCriticalSection( fcs ); }
~CSLock()
{ if ( fcs ) LeaveCriticalSection( fcs ); }
};
--- CPP ---
void TTicketService::GoSendMessageToLog( AnsiString AEvent )
{
CSLock lock( &ticksrvLogCriticalSection );
AnsiString LOG_FILE = ExtractFilePath( ParamStr(0) ) + "tickcfg.log";
TStringList *StringList = NULL;
bool IsStringListOK = true;
try
{
try
{
StringList = new TStringList();
StringList->Clear();
try
{
if ( FileExists( LOG_FILE ) )
StringList->LoadFromFile ( LOG_FILE );
}
catch ( const Exception &e )
{
IsStringListOK = false;
LogMessage( "ticksrv.exe : GoSendMessageToLog()>LoadFromFile()
Quote
Exception" );
}
if ( IsStringListOK )
{
AnsiString NEW_ROW = "Ticket server : " + FormatDateTime(
"dd-mm-yyyy, hh:nn:ss", Now() ) + " : " + AEvent;
StringList->Add( NEW_ROW );
}
if ( IsStringListOK )
{
if ( CONFIG_LIMITING == "1" )
{
if ( StringList->Count>StrToInt( CONFIG_LINES ) )
{
int TotalCount = StringList->Count - StrToInt(
CONFIG_LINES );
try
{
for ( int i=0; i<TotalCount; i++ )
StringList->Delete( 0 );
}
catch ( const Exception &e )
{
IsStringListOK = false;
LogMessage( "ticksrv.exe : GoSendMessageToLog()>
Delete(0)>Exception" );
}
}
}
if ( IsStringListOK )
{
try
{
StringList->SaveToFile ( LOG_FILE );
}
catch ( const Exception &e )
{
IsStringListOK = false;
LogMessage( "ticksrv.exe : GoSendMessageToLog()>
SaveToFile()" );
}
}
}
}
catch ( const Exception &e )
{
IsStringListOK = false;
LogMessage( "ticksrv.exe : GoSendMessageToLog()>StringList = new
TStringList()" );
}
}
__finally
{
if ( StringList )
delete StringList;
}
}
This is the sample for TService Application.
Same is for ordinary Windows Forms app, just get rid of: LogMessage()
functions, which are
intended to Log also errors in the process of Ading to log file.
CONFIG_LIMITING == do you want to limit growing log file
CONFIG_LINES == how much lines are is log sopposed to be limited to.
--
Best Regards,
Vladimir Stefanovic
 

{smallsort}

Re:File Creation and contention

Hi Vladimir,
this is good. Unfortunately I meant several separate applications. So
the critical section won't do it. Do you know of the equivalent for
separate application? And if so, do you have an example?
John.
 

Re:File Creation and contention

Correction:
Those should be in CPP, of course.
__fastcall TTicketService::TTicketService(TComponent* Owner)
: TService(Owner)
{
// ...
InitializeCriticalSection( &ticksrvLogCriticalSection );
// ...
}
__fastcall TTicketService::~TTicketService()
{
// ...
DeleteCriticalSection( &ticksrvLogCriticalSection );
// ...
}
Best Regards,
Vladimir Stefanovic
 

Re:File Creation and contention

"John Grabner" < XXXX@XXXXX.COM >wrote in message
Quote
Unfortunately I meant several separate applications. So the critical
section won't do it. Do you know of the equivalent for separate
application? And if so, do you have an example?
Have a look at the Win32 reference help for the function CreateFile. HTH,
--
Mark Jacobs
www.jacobsm.com
 

Re:File Creation and contention

If you're going to give each application the same rights to create and
write to the file, you may want to use a mutex to lock the file for
writing. I assume each app can test for the existence of the file to
determine whether or not it should be created.
John Grabner wrote:
Quote
Hi Vladimir,
this is good. Unfortunately I meant several separate applications. So
the critical section won't do it. Do you know of the equivalent for
separate application? And if so, do you have an example?

John.
 

Re:File Creation and contention

Quote
Unfortunately I meant several separate applications.
I cannot say for sure, but why don't you make some contention
algorithm.
Accessing a Log file should be short-timed operation, so some
kind of loop-retry mechanism could help.
Every process/app should open exclusively the log file, write
a line or few, then exit.
In the meanwhile, other processes could try to open exclusively the
same file, but that could be possible only when ther processes release
the file.
Also some type of temp buffering can be considered also. In other
words, if a process could not access a file after say 10 retries, let it
write the log in a tem file. Other file when the access to a file is
gained write temp + current items.
--
Best Regards,
Vladimir Stefanovic
 

Re:File Creation and contention

Correction:
Quote
Other file
Other time,
--
Best Regards,
Vladimir Stefanovic