PDA

View Full Version : How to Store Logfile In AppData Directory


David Maw
20-Nov-2005, 05:15 AM
Using the program directory as a default location to store error log files
typically only works if the user running the program is an administrator,
because they need the ability to write to that directory. Requiring users of
an application to be administrators is usually a bad thing.

Writing to the program directory can also cause trouble if multiple users
could be using it at the same time (as in a program on a network share). It
is probably better to store files in the user's local application data
directory:
I've attached a procedure to locate the directory; I use it with EurekaLog
by doing this:

CurrentEurekaLogOptions.OutputPath := GetAppdataDirectory;

When creating my main form.

It would be helpful to EurekaLog offered this capability as an option.

The code for GetAppdataDirectory in a sample command line program is below
for anyone who wants it.

David
mailto: dee gee em (3 letters) at surfcity dot net

================================================== ===================
program AppDirConsole;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils,
ShFolder;

function GetAppdataDirectory : string;
var SHGetFolderPath : TSHGetFolderPath;
hFolderDLL : THandle;
var
Buf: PChar;
begin
// Finds (or creates) a directory the user can use for Application state
storage
// Handles older operating systems which may be missing SHFolder.DLL
// Compiles and runs in either a GUI or a console application
Buf := StrAlloc(MAX_PATH);
hFolderDLL := LoadLibrary('SHFolder.dll');
try
SHGetFolderPath := nil;
if hFolderDLL <> 0 then
@SHGetFolderPath := GetProcAddress(hFolderDLL, 'SHGetFolderPathA');
if Assigned(SHGetFolderPath)
and (S_OK = SHGetFolderPath(0, CSIDL_APPDATA or CSIDL_FLAG_CREATE, 0,
0, Buf))
then
// This is the normal case, we got a path, just continue
else // Probably an ancient NT4 O/S with old IE, just use the temp
directory
GetTempPath(Max_path, Buf);
finally
if hFolderDLL <> 0 then
FreeLibrary(hFolderDLL);
result := Buf;
StrDispose(Buf);
end;
// Now append the name of the program itself
result := IncludeTrailingPathDelimiter(result)
+ ChangeFileExt(ExtractFileName(ParamStr(0)),'');
if not DirectoryExists(result) then
CreateDir(result);
end; // GetAppdataDirectory

begin
WriteLn(GetAppdataDirectory);
WriteLn('');
WriteLn('press enter to continue');
ReadLn;
end.

admin
21-Nov-2005, 10:29 AM
Hi,

David Maw wrote:
> Using the program directory as a default location to store error log files
> typically only works if the user running the program is an administrator,
> because they need the ability to write to that directory. Requiring users of
> an application to be administrators is usually a bad thing.
>
> Writing to the program directory can also cause trouble if multiple users
> could be using it at the same time (as in a program on a network share). It
> is probably better to store files in the user's local application data
> directory:
> I've attached a procedure to locate the directory; I use it with EurekaLog
> by doing this:
>
> CurrentEurekaLogOptions.OutputPath := GetAppdataDirectory;
>
> [...]

Thank you very much, but I have just solved this problem in the 5.0.4
version.

If the users haven't the write right, then EurekaLog use as new path:
%UserProfile%/EurekaLog/ProjectName/File


--
Best regards...

Fabio Dell'Aria.
----------------
http://www.eurekalog.com
Catch every BUG showing line n.

briangochnauer
16-Dec-2005, 01:16 AM
I have enterprise version. So I changed the source and added
-----------------
function ExpandEnvironmentVar(var Value: string): Boolean;
var
R: Integer;
Expanded: string;
begin
SetLength(Expanded, 1);
R := ExpandEnvironmentStrings(PChar(Value), PChar(Expanded), 0);
SetLength(Expanded, R);
Result := ExpandEnvironmentStrings(PChar(Value), PChar(Expanded), R) <> 0;
if Result then
begin
StrResetLength(Expanded);
Value := Expanded;
end;
end;
-----------------
then added

-----------------
function CheckForEnvVars(s:string):string;
var ws :string;
begin
ws := s;
if ExpandEnvironmentVar(ws) then
Result := ws
else Result := s;
end;

-----------------
and changed
function GetUserPath: string;

-so I could use any environment variable in the logpath.

I think it would be useful to others... :)

Brian

admin
16-Jan-2006, 09:59 AM
briangochnauer wrote:
> I have enterprise version. So I changed the source and added
>
> [...]
>
> -so I could use any environment variable in the logpath.
>
> I think it would be useful to others... :)
>
> Brian

Thank you for your feedback Brian! :)

--
Best regards...

Fabio Dell'Aria.
----------------
http://www.eurekalog.com
Catch every BUG, every time!