🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2209

Re: File Creation and contention

#2209
From: 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 
Technical Details
Message-ID: <46039a75@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4603628f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,5d20743a8be4a735
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Vladimir Stefanovic" <antivari@sbb.co.yu>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4603628f$1@newsgroups.borland.com>
Subject: Re: File Creation and contention
Date: Fri, 23 Mar 2007 10:14:31 +0100
Lines: 147
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 87.116.181.111
Message-ID: <46039a75@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1174641269 87.116.181.111 (23 Mar 2007 02:14:29 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail