Sign in to follow this  
Followers 0
chalup

problems with FileWriteFile()

4 posts in this topic

I've written recently huge script - the bot for the online game. I use the function:

Func Debug ( $msg )
    If ( $debuglog <> 0 ) Then
        FileWriteLine ( $debuglog, @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $msg )
    EndIf
EndFunc

to log the information about what the bot is doing. Recently one of the bot users reported that the bot is locking his computer, but the debug.log file he sent me is cut in half of the sentence. It wasn't one-time issue - it happenes every time the bot locks up the computer. I suppose the FileWriteLine saves the information in "blocks" and if the bot locks the computer, some of the "buffered" information is not saved. Is there any way to fix this issue, other than closing and reopening the file every time i save something? Or maybe i'm completely wrong?

Share this post


Link to post
Share on other sites



I've written recently huge script - the bot for the online game. I use the function:

Func Debug ( $msg )
    If ( $debuglog <> 0 ) Then
        FileWriteLine ( $debuglog, @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $msg )
    EndIf
EndFunc

to log the information about what the bot is doing. Recently one of the bot users reported that the bot is locking his computer, but the debug.log file he sent me is cut in half of the sentence. It wasn't one-time issue - it happenes every time the bot locks up the computer. I suppose the FileWriteLine saves the information in "blocks" and if the bot locks the computer, some of the "buffered" information is not saved. Is there any way to fix this issue, other than closing and reopening the file every time i save something? Or maybe i'm completely wrong?

It appears you are holding the log file open the whole time, using the file handle $debuglog to test if there is a log file open. This is not ideal for a log file. #include <file.au3> and use _FileWriteLog($LogFile, "Log text...") with a string file path for $LogFile. The function adds the timestamp automatically, too.

:rolleyes:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

It appears you are holding the log file open the whole time, using the file handle $debuglog to test if there is a log file open. This is not ideal for a log file. #include <file.au3> and use _FileWriteLog($LogFile, "Log text...") with a string file path for $LogFile. The function adds the timestamp automatically, too.

:rolleyes:

Yes, that's exactly what mine function does, I wrote it, because I didn't know about _FileWriteLog.

I've written two scripts:

HotKeySet ( "{ESC}", "Terminate" )
$debuglog = FileOpen ("debug.log", 2); those two lines clear the contents of the debug.log file. 
FileClose ( $debuglog )

Global $i = 0

Global $ti = TimerInit()


While ( TimerDiff($ti) < 10000 )
    Debug ( $i & "========================================================================" )
    $i = $i + 1
WEnd
Terminate()

Func Terminate ()
    Local $x = FileOpen ("end.log", 2)
    FileWriteLine ( $x, $i )
    FileClose ( $x )
    MsgBox ( 0, "TEST", "DONE!" )
    While ( 1 == 1 )
        Sleep ( 1000 )
    WEnd
EndFunc


Func Debug ( $msg )
    Local $debuglog = FileOpen ("debug.log", 1)
    FileWriteLine ( $debuglog, @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $msg )
    FileClose ( $debuglog )
EndFunc

and

#include <File.au3>

HotKeySet ( "{ESC}", "Terminate" )
$debuglog = FileOpen ("debug.log", 2); those two lines clear the contents of the debug.log file. 
FileClose ( $debuglog )

Global $i = 0

Global $ti = TimerInit()


While ( TimerDiff($ti) < 10000 )
    _FileWriteLog ( "debug.log", $i & "========================================================================" )
    $i = $i + 1
WEnd
Terminate()

Func Terminate ()
    Local $x = FileOpen ("end.log", 2)
    FileWriteLine ( $x, $i )
    FileClose ( $x )
    MsgBox ( 0, "TEST", "DONE!" )
    While ( 1 == 1 )
        Sleep ( 1000 )
    WEnd
EndFunc

I let them run, clicked OK in the message box and then closed the process by using task manager (similar script using old Debug function was of course much faster, but the debug.log output was cut in the middle of the "=" signs). The first one is faster about 50% ( difference between $i being ~46k and ~68k on my computer). I guess the _FileWriteLog is doing something more than my function, maybe additional checks if file exists and so on?

Thank you for your help and i hope this can help someone.

Share this post


Link to post
Share on other sites

_FileWriteLog() acts just like FileWrite(), if used with a string path instead of a file handle. It creates/opens the file, writes to the file, then closes the file, then returns. This is sure to be slower than keeping the file open the whole time, but logging is not usually a performance issue. Sounds like your task might be an exception.

:rolleyes:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0