🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
9 messages
9 total messages Started by John Grabner Fri, 23 Mar 2007 05:15
File Creation and contention
#2207
Author: John Grabner
Date: Fri, 23 Mar 2007 05:15
5 lines
1056 bytes
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
#2208
Author: DreamChaser
Date: Fri, 23 Mar 2007 05:33
25 lines
1853 bytes
John Grabner wrote:
> 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
#2209
Author: Vladimir Stefanovic
Date: Fri, 23 Mar 2007 09:14
145 lines
4527 bytes
>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() 
 > 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 
Re: File Creation and contention
#2210
Author: John Grabner
Date: Fri, 23 Mar 2007 09:35
6 lines
1352 bytes
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
#2211
Author: Vladimir Stefanovic
Date: Fri, 23 Mar 2007 09:36
22 lines
1366 bytes
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
#2212
Author: Mark Jacobs
Date: Fri, 23 Mar 2007 12:25
10 lines
1438 bytes
"John Grabner" <grabnerj@ihug.co.nz> wrote in message 
news:46039f9b$1@newsgroups.borland.com...
> 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
#2213
Author: Mike K
Date: Fri, 23 Mar 2007 17:03
14 lines
1464 bytes
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:
> 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
#2214
Author: Vladimir Stefanovic
Date: Fri, 23 Mar 2007 19:42
25 lines
1772 bytes
> 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
#2215
Author: Vladimir Stefanovic
Date: Fri, 23 Mar 2007 19:45
10 lines
1113 bytes
Correction:

> Other file

Other time,


-- 
Best Regards,
Vladimir Stefanovic 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads