Jump to content

Logfile


 Share

Recommended Posts

I was looking for a way to log events in a several AutoIt scripts. In addition I found out that while debugging with the tray icon and the msgbox is perfectly doable, sometimes you need more information than what they can display, and there are times where you want to copy that text, which is hard from a msgbox or a tray icon.

I created this very basic logfile system consisting of two functions, a _openLogFile() that must be called in the beginning of your script, and a _addToLog() that can be called at any time during your script. The system is extremely simple, but does the job. A new logfile is created if the old one is getting too large. You can set your own filesize limits.

Enjoy!

BR,

Anders

$logFilePath="c:\scripts\"
$logFileName="autoItDebugLog"

dim $logFileHandle  
_openLogFile()
_addToLog("info","Application started")

;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;This function open the latest logfile for use. If the file is too large, a new logfile is created and opened.
;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func _openLogFile()
    ;Find the file with the latest date/time stamp, while matchine the filename
    FileChangeDir($logFilePath)
    $logFileHandle=FileFindFirstFile($logFileName & "*")
    $searchDate=0
    $searchName=$logFileName & "-" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR  & @MIN & @SEC & ".txt"
    while 1
        $tmpfName=FileFindNextFile($logFileHandle)
        if @error then ExitLoop
        $tmpfdate=FileGetTime($tmpfName,0,1) 
        if $searchDate<$tmpfdate then 
            $searchDate=$tmpfdate
            $searchName=$tmpfName
        EndIf
    WEnd

    ;If the current file is too large (more than 200kb), then create a new file in stead of appending to the old file
    if FileGetSize($searchName)>200000 then $searchName =  $logFileName & "-" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR &  @MIN &  @SEC & ".txt"
    $logFileHandle=FileOpen($searchName,1)
    if $logFileHandle=-1 then 
        MsgBox(0,"Error","Unable to open logfile: " & $logFilePath & $searchName)
        Exit
    EndIf
EndFunc

;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;This function append one line to the logfile $logFileHandle that was previosuly opened/created with _openLogFile()
;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func _addToLog($logGroup, $logStr)
    ;First adjust the $logGroup parameter to a 8 digit string, fixed length, left aligned
    if StringLen($logGroup) > 8 then 
        $logGroup=StringRight($logGroup,8)
    Else
        $logGroup=$logGroup&StringRight("        ",8-StringLen($logGroup))
    EndIf
    FileWriteLine($logFileHandle,@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":"&  @MIN & ":"& @SEC &" |" & $logGroup & "|  " & $logStr )
EndFunc

logFileFunction.au3

Link to comment
Share on other sites

Welcome to the forum :mellow:

Nice example for a first post.

I guess it would be good for you to know that there is already something similar in AutoIt - you could have a look at various functions included in "EventLog.au3" and _FileWriteLog function included in "File.au3"

Good luck,

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • 7 months later...

Hi,

that's a great tool!

Just a question: why do I see the new messages in the logfile AFTER I close the script? When the script is running, there are no messages from the current script session. How do I change this?

Thank you!

eddl

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...