Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-09

TEventLog.FileName location: Change default file location for filename without change unit code.

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

phoebus phoebus

Hi,

I'm using FPC 2.2.4 with Lazarus.

The unit eventlog is great for logging events. SO i use it.  
My question is how to change the default location for the filename for Linxu ssytem which is harcoded in the code.
The documentation for TEventLog.FileName wrote:
" FileName is the name of the log file used to log messages (...). The file is then located in the "/tmp" directory on unix-like systems, or in the application directory for Dos/Windows like systems
 
Line 56 to 60 from eventlog.inc (fpc\2.2.4\source\packages\fcl-base\src\unix)     Function TEventLog.DefaultFileName : String;     begin
      Result:='/tmp/'+ChangeFileExt(ExtractFileName(Paramstr(0)),'.log');     end;
I can of course change the function code like Windows to have the log in the same directory thtat the application.:
Line 18 to 22 from eventlog.inc (fpc\2.2.4\source\packages\fcl-base\src\windows)     Function TEventLog.DefaultFileName : String;     begin
      Result:=ChangeFileExt(Paramstr(0),'.log');     end;

Could you indicate a nice solution to indicate log file location in using the unit eventlog without change fpc code?
 
FYI: this is the code i use:
    program project1;
    {$mode objfpc}{$H+}
    uses  Sysutils, eventlog,
      {$IFDEF UNIX}  BaseUnix,{$IFDEF UseCThreads}     cthreads,
      {$ENDIF}{$ENDIF}
    Classes
      { you can add units after this };
    {$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
     var
      A : TEventLog;
    begin
      A := TEventLog.Create(nil);
      A.LogType := ltFile;
      A.FileName := 'log';
      A.Active := True;
      A.Info('Program started.');
      A.Info('Info from A.');
      A.Warning('Info from A.');
      A.Error('Info from A.');
      A.Debug('Info from A.');
      A.Info('Program stopped.');
      FreeAndNil(A);
     end.
 
Thanks for your advice.
Regards,
Phoebus

Reply by email

Michael Van Canneyt
to phoebus phoebus
phoebus phoebus wrote 28 lines
Hi, I'm using FPC 2.2.4 with Lazarus. The unit eventlog is great for logging events. SO i use it.   My question is how to change the default location for the filename for Linxu ssytem which is harcoded in the code. The documentation for TEventLog.FileName wrote: " FileName is the name of the log file used to log messages (...). The file is then located in the "/tmp" directory on unix-like systems, or in the application directory for Dos/Windows like systems   Line 56 to 60 from eventlog.inc (fpc\2.2.4\source\packages\fcl-base\src\unix)     Function TEventLog.DefaultFileName : String;     begin       Result:='/tmp/'+ChangeFileExt(ExtractFileName(Paramstr(0)),'.log');     end; I can of course change the function code like Windows to have the log in the same directory thtat the application.: Line 18 to 22 from eventlog.inc (fpc\2.2.4\source\packages\fcl-base\src\windows)     Function TEventLog.DefaultFileName : String;     begin       Result:=ChangeFileExt(Paramstr(0),'.log');     end; Could you indicate a nice solution to indicate log file location in using the unit eventlog without change fpc code?

You can just set the filename property, with full path: /my/path/mylog.txt

It will be created in the path you specify, as you can see in ActivateFileLog.

Procedure TEventLog.ActivateFileLog;
begin
   If (FFileName='') then
     FFileName:=DefaultFileName;
   // This will raise an exception if the file cannot be opened for writing !
   FStream:=TFileStream.Create(FFileName,fmCreate or fmShareDenyWrite);
end;

The default is just that, the default. If you specify a filename, then that is used. What more do you need ?

Michael.

Reply by email

DaWorm
to Michael Van Canneyt

On Tue, Sep 21, 2010 at 9:39 AM, Michael Van Canneyt <

Show quoted text 8 lines
wrote: begin > Result:=ChangeFileExt(Paramstr(0),'.log'); > end; > Function TEventLog.DefaultFileName : String;

On Windows, this can be problematic. Win7 for sure, usually Vista, and any version of Windows that has had its security tightened won't let you write files into the application folder.

Jeff.

Reply by email

phoebus phoebus
to DaWorm

Hi,

Thanks Michael for the update.

> The default is just that, the default. If you specify a filename, then that is >used.

Yes, that's right.

Thanks also jeff for the advise on Windows platform.

Regards,
Frphoebus

Reply by email