PDA

View Full Version : How to write a log to a .txt file!



rj
03-30-2013, 09:54 PM
Hello, welcome to my guide on how to write a log to a .txt file!


Table of contents:

- Uses
- Open file and save file
- Procedure to add text
- Closing


Uses

Writing text into a file is very useful for debugging the bot if you are gone for long periods of time, and it is also helpful incase you don't want your writeln to be spammed.



Open file and save file

In order to write the file, we need to have a file with the name in C:Simba, luckily you can make the script automatically do this for you. So first we will make the Function:

function OpenReportFile(): integer;
var
fileName,Bot_SavePath: string;

fileName is what you want to call the file, Bot_SavePath is the path you want to save it at.

Next we will make the function that will make the file if it does not already exist

begin

Bot_SavePath := 'C:/Simba/' // path
if (not directoryExists(Bot_SavePath)) then // if it does not exist it will save else were
forceDirectories(Bot_SavePath);

fileName := 'Test.txt'; // file name
fileName := Bot_SavePath + fileName;

try
if (fileExists(fileName)) then // checks if the file exists
result := appendFile(fileName)
else
result := createFile(fileName); // if it does not exist it creates it
except
writeln('OpenSRLLogFile: '+exceptionToString(exceptionType, exceptionParam)); // if there is an error it will report it
end;
end;


So far you should have:

function OpenReportFile(): integer;
var
fileName,Bot_SavePath: string;
begin

Bot_SavePath := 'C:/Simba/' // path
if (not directoryExists(Bot_SavePath)) then // if it does not exist it will save else were
forceDirectories(Bot_SavePath);

fileName := 'Test.txt'; // file name
fileName := Bot_SavePath + fileName;

try
if (fileExists(fileName)) then // checks if the file exists
result := appendFile(fileName)
else
result := createFile(fileName); // if it does not exist it creates it
except
writeln('OpenSRLLogFile: '+exceptionToString(exceptionType, exceptionParam)); // if there is an error it will report it
end;
end;


Procedure to add text


Next, we want to create a procedure that will write to the file, I will call my procedure "AddReport"

procedure AddReport(s: string);


As you can see, the parameters are AddReport('Enter string here')

Now we will add the meat that writes the text to the file:


var
tmpFile: integer;
begin
writeln(s); // writeln the text

tmpFile := OpenReportFile(); // refers to the function above and opens the file

if (tmpFile >= 0) then
try
writeFileString(tmpFile, '['+msToTime(getTimeRunning(), TIME_BARE)+']: '+s+#13+#10); // writes the text and the time stamp
except
Writeln('Error writing File'); // if there is an error then this occurs
finally
closeFile(tmpFile); // closes the file
end;
end;

Closing


So to write any text to the file, just call:

AddReport('Test, test!');


Test script:

{$I SRL/SRL.Simba}
function OpenReportFile(): integer;
var
fileName,Bot_SavePath: string;
begin

Bot_SavePath := 'C:/Simba/' // path
if (not directoryExists(Bot_SavePath)) then // if it does not exist it will save else were
forceDirectories(Bot_SavePath);

fileName := 'Test.txt'; // file name
fileName := Bot_SavePath + fileName;

try
if (fileExists(fileName)) then // checks if the file exists
result := appendFile(fileName)
else
result := createFile(fileName); // if it does not exist it creates it
except
writeln('OpenSRLLogFile: '+exceptionToString(exceptionType, exceptionParam)); // if there is an error it will report it
end;
end;
procedure AddReport(s:string);
var
tmpFile: integer;
begin
writeln(s); // writeln the text

tmpFile := OpenReportFile(); // refers to the function above and opens the file

if (tmpFile >= 0) then
try
writeFileString(tmpFile, '['+msToTime(getTimeRunning(), TIME_BARE)+']: '+s+#13+#10); // writes the text and the time stamp
except
Writeln('Error writing File'); // if there is an error then this occurs
finally
closeFile(tmpFile); // closes the file
end;
end;
Begin
Addreport('F')
End.

I hope this helped, it is very useful for debugging scripts after a long runtime!

Le Jingle
03-30-2013, 09:57 PM
Do note that you may also find AddToSRLLog(s: string); useful too, granted you include the SRLlog.simba file or include srl/srl.simba Other than that, this is a decent custom function, nice work

rj
03-30-2013, 10:07 PM
Do note that you may also find AddToSRLLog(s: string); useful too, granted you include the SRLlog.simba file or include srl/srl.simba Other than that, this is a decent custom function, nice work

Meh I ripped it off somewere in the SRL include and edited it a lot for my own purposes

masterBB
04-25-2013, 03:38 PM
I suggest using a TStringList over these methods. But that might be more of a personal opinion.

rj
10-08-2013, 11:38 PM
Just a little bump, "apppath" can be used instead of "C:simba"