Results 1 to 5 of 5

Thread: How to write a log to a .txt file!

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default How to write a log to a .txt file!

    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!

  2. #2
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    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

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Le Jingle View Post
    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

  4. #4
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    I suggest using a TStringList over these methods. But that might be more of a personal opinion.
    Working on: Tithe Farmer

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Just a little bump, "apppath" can be used instead of "C:simba"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •