TheCuz Posted June 20, 2011 Share Posted June 20, 2011 (edited) I mostly created this UDF because I needed something good for logging to either console or file, or both. Most of the defaults in the file are what I commonly use for most of my projects, but you can set them by calling the setting functions. The UDF file is documented as to what the functions do. Enjoy.... p.s. - to make it more Vista/7 friendly, you can use the _SetLogPath function to another folder if you are running your application from the program files directory without elevated privileges. expandcollapse popup#include <file.au3> #include <array.au3> ;~ ConsoleWrite("Init LogUTil" & @CRLF) Global $sLogName = "" #cs Log level is set in the following manner, default is INFO: TRACE = 5 DEBUG = 4 WARN = 3 INFO = 2 ERROR = 1 OFF = 0 #ce Local $iLogLevel = 2 Const $TRACE = 5 Const $DEBUG = 4 Const $INFO = 3 Const $WARN = 2 Const $ERROR = 1 Const $OFF = 0 Local $bLogToConsole = False Local $bLogToFile = True ;~ used to set part of the log name to the programs name If @Compiled Then $sLogName = StringReplace(@ScriptName, ".exe", "") Else $sLogName = StringReplace(@ScriptName, ".au3", "") EndIf Local $sLogPath = @ScriptDir Global $log = $sLogPath & "\" & $sLogName & "_1.log" ;=============================================================================== ; Function Name: _SetLogName ; Description:: used to set part of the log name if you don't want to use the application name ; Parameter(s): $s - string name to use as part of the log name ; Author(s): TheCuz ;=============================================================================== Func _SetLogName($s) $sLogName = $s EndFunc ;=============================================================================== ; Function Name: _SetLogPath ; Description:: sets the folder where the log file should be ; Parameter(s): $s - (string)folder path without trailing slash. i.e.- c:\path\to\log ; Author(s): TheCuz ;=============================================================================== Func _SetLogPath($s) $sLogPath = $s EndFunc ;=============================================================================== ; Function Name: _SetLogToConsole ; Description:: will set whether or not to print to a console output ; Parameter(s): $b - (boolean)true to output to console, false to not output to console ; Author(s): TheCuz ;=============================================================================== Func _SetLogToConsole($b) $bLogToConsole = $b EndFunc ;=============================================================================== ; Function Name: _SetLogToFile ; Description:: will set whether or not to log to a file ; Parameter(s): $b - (boolean)true to print to file, false to not print ; Author(s): TheCuz ;=============================================================================== Func _SetLogToFile($b) $bLogToFile = $b EndFunc ;=============================================================================== ; Function Name: _SetLogLevel ; Description:: sets the log level to use when logging. i.e. - trace, debug, info, etc. ; Parameter(s): uses one of the constants to set the logging level. i.e. - $iTRACE, $iDEBUG, etc. ; Author(s): TheCuz ;=============================================================================== Func _SetLogLevel($iLevel) $iLogLevel = $iLevel EndFunc ;=============================================================================== ; Function Name: _LogCheck ; Description:: used to check the size of the log file. If it is larger than $iMaxLogSize, it will remove the ; oldest log file, rename the other, then start a new one ; Parameter(s): $iMaxLogCount = (int)how many log files to keep ; $iMaxLogSize = (int)how large the log file must be(in bytes) before performing actions ; Author(s): TheCuz ;=============================================================================== Func _LogCheck($iMaxLogCount = 5, $iMaxLogSize = 5000000) ;get log file list Local $aLogList = _FileListToArray($sLogPath, "*.log") If Not IsArray($aLogList) Then _FileWriteLog($log, "ERROR: could not create log list array") Return EndIf If FileGetSize($log) > $iMaxLogSize Then For $i = $iMaxLogCount To 1 Step -1 If $i <> $iMaxLogCount Then If FileExists($sLogPath & "\" & $sLogName & "_" & $i & ".log") Then FileMove($sLogPath & "\" & $sLogName & "_" & $i & ".log", $sLogPath & "\" & $sLogName & "_" & ($i + 1) & ".log") EndIf Else If FileExists($sLogPath & "\" & $sLogName & "_" & $i & ".log") Then FileDelete($sLogPath & "\" & $sLogName & "_" & $i & ".log") EndIf EndIf Next EndIf EndFunc ;=============================================================================== ; Function Name: _LogTrace ; Description:: used to print trace messges to file or console ; Parameter(s): $sTag - (string)used to mark the message with a subject/title ; $sMsg - (string)message to print ; Author(s): TheCuz ;=============================================================================== Func _LogTrace($sTag, $sMsg) If $iLogLevel >= $TRACE Then If $bLogToFile Then _FileWriteLog($log, "TRACE[" & $sTag & "] " & $sMsg) If $bLogToConsole Then ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "TRACE[" & $sTag & "] " & $sMsg & @CRLF) EndIf EndIf EndFunc ;=============================================================================== ; Function Name: _LogDebug ; Description:: used to print trace messges to file or console ; Parameter(s): $sTag - (string)used to mark the message with a subject/title ; $sMsg - (string)message to print ; Author(s): TheCuz ;=============================================================================== Func _LogDebug($sTag, $sMsg) If $iLogLevel >= $DEBUG Then If $bLogToFile Then _FileWriteLog($log, "DEBUG[" & $sTag & "] " & $sMsg) If $bLogToConsole Then ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "DEBUG[" & $sTag & "] " & $sMsg & @CRLF) EndIf EndIf EndFunc ;=============================================================================== ; Function Name: _LogWarn ; Description:: used to print trace messges to file or console ; Parameter(s): $sTag - (string)used to mark the message with a subject/title ; $sMsg - (string)message to print ; Author(s): TheCuz ;=============================================================================== Func _LogWarn($sTag, $sMsg) If $iLogLevel >= $WARN Then If $bLogToFile Then _FileWriteLog($log, "WARN[" & $sTag & "] " & $sMsg) If $bLogToConsole Then ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "WARN[" & $sTag & "] " & $sMsg & @CRLF) EndIf EndIf EndFunc ;=============================================================================== ; Function Name: _LogInfo ; Description:: used to print trace messges to file or console ; Parameter(s): $sTag - (string)used to mark the message with a subject/title ; $sMsg - (string)message to print ; Author(s): TheCuz ;=============================================================================== Func _LogInfo($sTag, $sMsg) If $iLogLevel >= $INFO Then If $bLogToFile Then _FileWriteLog($log, "INFO[" & $sTag & "] " & $sMsg) If $bLogToConsole Then ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "INFO[" & $sTag & "] " & $sMsg & @CRLF) EndIf EndIf EndFunc ;=============================================================================== ; Function Name: _LogError ; Description:: used to print trace messges to file or console ; Parameter(s): $sTag - (string)used to mark the message with a subject/title ; $sMsg - (string)message to print ; Author(s): TheCuz ;=============================================================================== Func _LogError($sTag, $sMsg) If $iLogLevel >= $ERROR Then If $bLogToFile Then _FileWriteLog($log, "ERROR[" & $sTag & "] " & $sMsg) If $bLogToConsole Then ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "ERROR[" & $sTag & "] " & $sMsg & @CRLF) EndIf EndIf EndFunc ;=============================================================================== ; Function Name: _p ; Description:: prints output to log file and console ; Parameter(s): msg: message to print ;=============================================================================== Func _p($msg) _FileWriteLog($log, $msg) ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $msg & @CRLF) EndFunc Edit: added comment about changing where to save logs if using Vista/7 Edit: noticed that the _LogCheck function will still look for log in application folder. Added a _SetLogPath function to change the folder where log file will be saved to and updated the _LogCheck function to use that folder. Default is @ScriptDir. Edited June 21, 2011 by TheCuz [font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font] Link to comment Share on other sites More sharing options...
TheCuz Posted June 21, 2011 Author Share Posted June 21, 2011 Updated a couple of things. See edit comments from first post. Any constructive feedback is welcomed! [font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font] Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now