SRL Archive - Research & Development Lounge

Little function for debugging


Master BAW

2012-07-03 08:31:59

Hey, I made this, to put in my script for if something goes wrong.
If something goes wrong, I call this function, and enter the error into it, and then it will make a screenshot from it, and write the error into a file.

It fully works, except for the first time you run it, then it can't append the file after creating it… the second time , when the file is already created, it works though… Can somebody test it and explain me why it doesn't work the first time, and maybe help me to solve that?

This is my first time working with files, so please give me some hints :D

If you like this little Procedure, you may use it yourself :) It's also quite handy for a lot of other things that you want to log I think. It is a screenshot, plus the description of the screenshot, and it's saved in your script path .

Procedure BugLogScreen(Error: String);

var
i: integer;
theFile: Integer;
thePath: string;
DirectoryToSave : string;


begin
DirectoryToSave := 'BugsLogScreens\';
i := Length(GetFiles(ScriptPath+DirectoryToSave,'jpg'));
SaveScreenshot(ScriptPath + DirectoryToSave + 'SCR' + ToStr(i) + '.jpg');

if not DirectoryExists(ScriptPath + DirectoryToSave) then
begin
try
CreateDirectory(ScriptPath + DirectoryToSave);
writeln('Created Directory');
Wait(1000);
except
writeln('Failed to create directory!');
end;

end;

if not FileExists(ScriptPath + DirectoryToSave + 'BUGS.txt') then
begin
try
CreateFile(ScriptPath + DirectoryToSave + 'BUGS.txt');
Writeln('Created File');
Wait(1000);

except
writeln('Failed to create file!');
end;

end;

thePath := ScriptPath + DirectoryToSave + 'BUGS.txt';

try
theFile := AppendFile(thePath);
except
Writeln('Error opening file!');
end;

try
WriteFileString(theFile, IntToStr(i)+': (' + TheDate(Date_Day) + ' at ' + TheTime + ') ' + Error + #13 + #10);
except
Writeln('Error writing file');
end;

try
CloseFile(theFile); // Again, don't forget this!
except
Writeln('Error closing file');
end;

end;

example:
if Something() = GoneWrong then 
BugLogScreen('SomeThing has gone wrong');


Cheers!

Edit: changed one line :)

RuneDragon

2012-07-03 10:40:03

Thanks for the function. I was looking for this :P

Runaway

2012-07-03 12:25:23

Hey there,

This seems like an interesting function :) I've got a few suggestions for you.

1) You should always check if the directory/file exists before you do anything else. You were saving the screenshot before you checked if the directory existed, and that is why it wasn't working the first time.

2) I know you're trying to spread out the file-handling procedures, but it's really not necessary. As long as you have the try..except around them, that's all that is needed.

3) Just a personal opinion, I think you should use set a boolean parameter for taking a screenshot :)

Here's the code after applying those suggestions:


procedure BugLog(Error: String; Screen: Boolean);

var
i: Integer;
theFile: Integer;
thePath: String;
DirectoryToSave : String;

begin
DirectoryToSave := 'BugLog\';

if not DirectoryExists(ScriptPath + DirectoryToSave) then
begin
try
CreateDirectory(ScriptPath + DirectoryToSave);
Writeln('Created Directory');
Wait(1000);
except
Writeln('Failed to create directory!');
end;
end;

if not FileExists(ScriptPath + DirectoryToSave + 'BUGS.txt') then
begin
try
CreateFile(ScriptPath + DirectoryToSave + 'BUGS.txt');
Writeln('Created File');
Wait(1000);
except
Writeln('Failed to create file!');
end;
end;

if Screen then
begin
i := Length(GetFiles(ScriptPath + DirectoryToSave, 'jpg'));
SaveScreenshot(ScriptPath + DirectoryToSave + 'SCR' + ToStr(i) + '.jpg');
end;

thePath := ScriptPath + DirectoryToSave + 'BUGS.txt';

try
theFile := AppendFile(thePath);
WriteFileString(theFile, IntToStr(i)+': (' + TheDate(Date_Day) + ' at ' + TheTime + ') ' + Error + #13 + #10);
CloseFile(theFile);
except
Writeln('Error handling file!');
end;

end;

It seems like you're getting the hang of coding :) It wasn't too long ago that you were asking "basic" questions, and now you're posting your own custom functions! Good job, hope to see more from you.

Master BAW

2012-07-04 05:47:40

Thank you:)
Yea I don't have many questions anymore and when i need to know sometjing, i use the search function :)

I think i'm going to improve this function a little more by making it more general.
Secondly i would like to add the last 20 (or x) rules of the debug log intoo my bugs.txt file, for Every error . I donot know if Thats possible but i hope So, Will search for it

Runaway

2012-07-04 12:43:35

From: MasterCrimez
Thank you:)
Yea I don't have many questions anymore and when i need to know sometjing, i use the search function :)

I think i'm going to improve this function a little more by making it more general.
Secondly i would like to add the last 20 (or x) rules of the debug log intoo my bugs.txt file, for Every error . I donot know if Thats possible but i hope So, Will search for it


Ah yes, that's a good idea. Something like:


procedure BugLog(Directory, Error: String; Screen: Boolean);

// ...

begin
DirectoryToSave := Directory +'\';

you mean? And unfortunately grabbing lines from the debug isn't possible with Simba :redface: If you really want to use debug text, you'd have to do something fancy with the text you debug:


procedure SaveWriteln(Text: String; MaxLen: Integer; var Save: TStringArray);
var
hSave, i: Integer;
begin
hSave := High(Save);
if (hSave + 1 < MaxLen) then
begin
SetLength(Save, hSave + 2);
Save[hSave + 1] := Text;
end else
begin
for i := 0 to (hSave - 1) do
Save[i] := Save[i + 1];
Save[hSave] := Text;
end;
end;

^ Text is the string you want to save (so whatever you're writing to the debug), MaxLen is the max number of debug lines you want to save, and Save is the TStringArray that stores all of the debug text. Here's a little example program:


program new;
{$i srl/srl.simba}

var
TSA, Save: TStringArray;
i: Integer;

procedure SaveWriteln(Text: String; MaxLen: Integer; var Save: TStringArray);
var
hSave, i: Integer;
begin
hSave := High(Save);
if (hSave + 1 < MaxLen) then
begin
SetLength(Save, hSave + 2);
Save[hSave + 1] := Text;
end else
begin
for i := 0 to (hSave - 1) do
Save[i] := Save[i + 1];
Save[hSave] := Text;
end;
end;

begin
TSA := ['h', 'e', 'l', 'l', 'o', 't', 'h', 'e', 'r', 'e'];
for i := 0 to 9 do
begin
SaveWriteln(TSA[i], 5, Save);
Writeln(Save);
end;
end.

Sorry to make what you were attempting to make, but I've thought about doing this before and I already had something in mind :p

Master BAW

2012-07-05 08:55:53

cool! will look into it when I feel like it and include such thing into my default functions/procecures include file :) thanks for sharing :D