Article View: borland.public.delphi.nativeapi.win32
Article #151Re: Logging Functionality
From: Ian Boyd
Date: Thu, 24 Jul 2008 01:51
Date: Thu, 24 Jul 2008 01:51
67 lines
3211 bytes
3211 bytes
> what is the best approach to implement a Logging Functionality within
> my application? I need this for debuging purposes for end user. So
> that when an error occur, end user can send me the log file to
> inspect.
>
> Also, is there a why to enable / disable this functionality? For
> example depending on the command line options used to start the
> application.
i have library function:
procedure LogToFile(filename: string; const s: string);
var
f: Textfile;
FilePath: string;
begin
FilePath := ExtractFilePath(filename);
if not DirectoryExists(FilePath) then
CreateDir(FilePath);
System.Assign(f, filename);
if FileExists(filename) then
System.Append(f)
else
Rewrite(f);
try
WriteLn(f, S);
finally
System.Close(f);
end;
end;
i usually then create a procedure in the application:
procedure FileLog(const LogString: string);
var
szLogFile: string;
begin
szLogFile :=
ExtractFilePath(ParamStr(0))+'Logfiles\'+FormatDateTime('yyyymmdd',
Now)+'.log';
{The zero-th parameter to an application is, by convention, the
appliation's path and filename. But since you cannot trust this
value,
you should use GetModuleFilename instead.
Fortunatly, ParamStr() detects if you want the zero-th parameter
and instead calls GetModuleFilename. As a result, it is safe and
proper
to use ParamStr(0) to get our image filename}
LogToFile(szLogFile, DateTimeToISOStr(Now)+' '+LogString);
end;
This way the logfile rolls over each day, and it handily sitting in a
Logfiles folder.
Although this is bad bad for anyone running as a standard user under XP, and
should use SHGetFolderPath to store the log files in the user's local (i.e.
non-roaming) application data folder (CSIDL_LOCAL_APPDATA). e.g.:
c:\documents and settings\Monzer\Local Settings\Application Data
c:\users\monzer\appdata\local
The call to SHGetFolderPath is left as an excercise for the reader :)
Technical Details
Message-ID:
<4887e022$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4no184p7qt0db7ug73f0uocnnh1180h527@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 14438e,682e93d1ba8d222a X-Google-Attributes: gid14438e,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Ian Boyd" <ian.borlandnews008@zunblvlda1.dyndns.org.spamsucks> Newsgroups: borland.public.delphi.nativeapi.win32 References: <4no184p7qt0db7ug73f0uocnnh1180h527@4ax.com> In-Reply-To: <4no184p7qt0db7ug73f0uocnnh1180h527@4ax.com> Subject: Re: Logging Functionality Date: Wed, 23 Jul 2008 21:51:33 -0400 Lines: 68 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Windows Mail 6.0.6001.18000 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6001.18000 NNTP-Posting-Host: 216.8.173.156 Message-ID: <4887e022$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1216864290 216.8.173.156 (23 Jul 2008 18:51:30 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail