ExpandVars uses characters that are invalid in filenames

delphi package - automated exception handling

ExpandVars uses characters that are invalid in filenames

Postby obones » Tue May 09, 2017 8:51 am

Hello,

As I'm using madExcept in services, I want the bugreport to be automatically saved.
This can be easily configured, but this also means bugreports all end in the same file, or a unique file gets overwritten (which is my case).
To make things clearer and add a bit of uniqueness, I wanted to add a date/time part in the filename. The documentation wasn't clear about that but the source told me that the filename gets parsed by ExpandVars before being used.
So, looking inside that method, I see that there is %date%, %time% and %datetime% that are supported here.
Unfortunately, the time marker uses colons to separate the various time parts, but the colon character is forbidden in filenames, which leads to a bogus filename being generated on disc. In my case, it only keeps the millisecond part.
Short of modifying the source, is there a way for me to intervene on the bugreport final filename just before it gets used?

Regards,
Olivier
obones
 
Posts: 44
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Postby obones » Tue May 09, 2017 1:13 pm

Well, here is my workaround:

Code: Select all
procedure AdjustFileNameExceptHandler(const exceptIntf: IMEException; var handled: boolean);
var
  sysTime: TSystemTime;
  date, time: string;
begin
  GetLocalTime(sysTime);
  date := IntToStrExW(dword(sysTime.wYear        ), 4) + '-'  +
          IntToStrExW(dword(sysTime.wMonth       ), 2) + '-'  +
          IntToStrExW(dword(sysTime.wDay         ), 2);
  time := IntToStrExW(dword(sysTime.wHour        ), 2) + '.'  +
          IntToStrExW(dword(sysTime.wMinute      ), 2) + '.'  +
          IntToStrExW(dword(sysTime.wSecond      ), 2) + '.' +
          IntToStrExW(dword(sysTime.wMilliseconds), 1);

  exceptIntf.BugReportFile := ChangeFileExt(exceptIntf.BugReportFile, ' - '+ date + ' ' + time + ExtractFileExt(exceptIntf.BugReportFile));
end;

initialization
  RegisterExceptionHandler(AdjustFileNameExceptHandler, stDontSync, epQuickFiltering);


Basically, the same code as in madExcept itself, but I use dots instead of colons for every time elements.
This works but has the drawback that it must be done in every project, whereas I have a centralized configuration file for my services.
obones
 
Posts: 44
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Postby madshi » Wed May 10, 2017 5:46 pm

Will fix this in the next build by replacing ":" with "." after ExpandVars, when used for saving bug reports.
madshi
Site Admin
 
Posts: 9477
Joined: Sun Mar 21, 2004 5:25 pm

Re: ExpandVars uses characters that are invalid in filenames

Postby obones » Thu May 11, 2017 7:49 am

Thanks, that will do it for time.
However, there are quite a few other variables that get expanded and their content may also contain other forbidden characters. Here is a reference:
https://msdn.microsoft.com/en-us/librar ... onventions

As this is "undocumented" territory, maybe it could be made to use something other than ExpandVars, with a subset of available variables and tailored for filenames.
obones
 
Posts: 44
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Postby madshi » Thu May 11, 2017 7:56 am

There are not a whole lot of %vars% supported, and most of them are compatible with file names, so I think replacing : with . should do. In all the years of madExcept you're the first one who had a problem with this, anyway, so I don't think it's a wide spread problem.
madshi
Site Admin
 
Posts: 9477
Joined: Sun Mar 21, 2004 5:25 pm

Re: ExpandVars uses characters that are invalid in filenames

Postby obones » Thu May 11, 2017 12:12 pm

I believe I'm the first to report this because it does not seem to be documented that one can use %xx% variables in the bugreport filename.
And I must insist, to me there are other variables that can contain more than the colon, here is a list:

exceptMsg
bugReport
time
datetime
errorDetails

Hence my suggestion to have a special function just for filenames, but I agree this may lead to duplicated code.
obones
 
Posts: 44
Joined: Fri May 15, 2009 11:47 am

Re: ExpandVars uses characters that are invalid in filenames

Postby madshi » Thu May 11, 2017 3:07 pm

time and dateTime are already covered. All other 3 can be very long (longer than MAX_PATH, and that's just for the file name!) and as such are totally unsuitable to be used as file names.
madshi
Site Admin
 
Posts: 9477
Joined: Sun Mar 21, 2004 5:25 pm


Return to madExcept

Who is online

Users browsing this forum: No registered users and 3 guests