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:
Simba Code:
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
Simba Code:
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:
Simba Code:
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"
Simba Code:
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:
Simba Code:
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:
Simba Code:
AddReport('Test, test!');
Test script:
Simba Code:
{$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!