exilon/QuickLogger

Range Error when using CustomFormatOutput

epachsoft opened this issue · 6 comments

No issue here, but to report a strange behaviour.

So I decided to update to the latest version of quick logger. Once I did it I noticed that I started to get a Range Error when using customformatoutputs. I didnt know what was causing it so I basically decided to just ignore the error and let it continue.

All good until I realized that it was not logging anything. I created a separate sample and it ran without trouble. I went crazy, spent hours and hours going at it, I debug it all the way to the loop:

Result := '';
idx := 1;
st := 0;
et := 0;
while st < fCustomFormatOutput.Length - 1 do
begin
if (fCustomFormatOutput[st] = '%') and (fCustomFormatOutput[st+1] = '{') then // <- ERROR here
begin

I'll step in and noticed that all indexes are fine, but for some reason it will throw that range error. I tried replacing the LogItemToFormat with the unused LogItemToFormat2 (its not called by anyone even if the version is over delphixe7) which didnt get the error but will get stock on certain moments so probably that's why its not used.

Finally I decided to replace .Length with the standard Length(fCustomFormatOutPut) just in case something was causing trouble with the string helper class and surprise, it started to work again.

So, for complex projects if you are using customOutPutformats and suddenly you start experiencing issues. Keep this one in mind.

No idea what other unit, project, compiler improvement is causing trouble with the helper but it did.

Please, what delphi version are you using? compiling 32 or 64bits?

My apologies for such an incomplete POST. I was just on the rush of the moment and left the important points.

I'm using Delphi 10.4.2. 64 bits. I'm not expecting you to find what is causing it. I tried for hours and hours to figure it out, small projects will do it, large ones some sort of optimization will cause that exception and have no idea why.

Worst case scenario I'll just change it to use Length on every pull I do. Thank you for the great job on the libraries.

Feliz navidad!

while st < fCustomFormatOutput.Length - 1 do
begin
if (fCustomFormatOutput[st] = '%') and (fCustomFormatOutput[st+1] = '{') then // <- ERROR here
begin

assuming that valid indexes of fCustomFormatOutput are 0..Length-1 this must result in a range error. Maybe you want to change to while st < fCustomFormatOutput.Length - 2 ?

while st < fCustomFormatOutput.Length - 1 do
begin
if (fCustomFormatOutput[st] = '%') and (fCustomFormatOutput[st+1] = '{') then // <- ERROR here
begin

assuming that valid indexes of fCustomFormatOutput are 0..Length-1 this must result in a range error. Maybe you want to change to while st < fCustomFormatOutput.Length - 2 ?

CustomFormatOutput is a template format like:
CustomFormatOutput := '%{DATE} %{TIME} - [%{LEVEL}] : %{MESSAGE} (%{MYTAG1} / %{MYTAG2})';
You must define some values. Never would be an empty string.

just change
st := 0;
To
st := 1;
As strings are 1 based when reading fcustomformatoutput[0] is out of bounds.

just change st := 0; To st := 1; As strings are 1 based when reading fcustomformatoutput[0] is out of bounds.

Previous to Delphi 10.4 the mobile compilers used by default 0-based indexing for strings. Rest are 1 based strings. I don't have range error no Windows, but i've changed to Low(string) to solve both cases, old and new delphi versions.