SnArF Posted January 21, 2013 Share Posted January 21, 2013 (edited) A simple UDF to write to the console and or to log files. Updated the UDF, no you can set the logile with _CwlSetLogFile() expandcollapse popup#include <date.au3> $LogFile = @ScriptDir & "\logfile.log" MsgBox(0,"Example 1", 'Write "test" to Console') _cw("test") MsgBox(0,"Example 2", 'Write "test" "test" with CR to Console') _cw("test") _cw("test") MsgBox(0,"Example 3", 'Write "test" "test" without CR to Console') _cw("test") _cw("test",0) MsgBox(0,"Example 4", 'Write "Start Script" to Console and Logfile, including date and time (logfile only)') _cwl("Start Script",1,"s") MsgBox(0,"Example 5", 'Write "dots" to console and logfile and adding 10 rows with "."') _cwl("dots") For $i = 1 to 10 _cwl(".") Next MsgBox(0,"Example 6", 'Write "dots" to console and logfile and adding 10 "." after dots') _cwl("dots") For $i = 1 to 10 _cwl(".",0) Next MsgBox(0,"Example 7", 'Write "numbers" to console and logfile and 123456789 with CR') _cwl("numbers") For $i = 1 to 10 _cwl($i) Next MsgBox(0,"Example 8", 'Write "numbers" to console and logfile and 123456789 without CR') _cwl("numbers") For $i = 1 to 10 _cwl(" ",0) _cwl($i,0) Next MsgBox(0,"Example 9", 'Write "End Script" to Console and Logfile, including date and time (logfile only)') _cwl("End Script",1,"e") ; Func Console Write and Write to Logfile Func _CW($CWtext, $CWlf = 1); _CW("text"[, flag]) If $CWlf = 1 Then ConsoleWrite(@CR & $CWtext) Else ConsoleWrite($CWtext) EndIf EndFunc ; Func Console Write and Write to Logfile Func _CWL($CWLtext, $CWLlf = 1, $CWLtype = "n"); _CW("text"[, flag][, type]) If $CWLlf = 1 Then ConsoleWrite(@CR & $CWLtext) Else ConsoleWrite($CWLtext) EndIf $CWLfile = FileOpen($LogFile, 1) If $CWLtype = "s" Then FileWrite($CWLfile, "--- " & $CWLtext & " - " & _NowCalc()) If $CWLtype = "e" Then FileWrite($CWLfile, @CRLF & "--- " & $CWLtext & " - " & _NowCalc() & @CRLF & "----------------------------------------" & @CRLF & @CRLF ) If $CWLtype = "n" And $CWLlf = 1 Then $CWLtext = @CR & "- " & $CWLtext $CWLtext=StringReplace($CWLtext,@CRLF,@CR) $CWLtext=StringReplace($CWLtext,@CR,@CRLF) FileWrite($CWLfile, $CWLtext) EndIf If $CWLtype = "n" And $CWLlf = 0 Then FileWrite($CWLfile, $CWLtext) FileClose($CWLfile) EndFunc EndFunc_ConsoleWriteLog.au3 Edited January 21, 2013 by SnArF My scripts: _ConsoleWriteLog | _FileArray2D Link to comment Share on other sites More sharing options...
wakillon Posted January 21, 2013 Share Posted January 21, 2013 Hi SnArf i get an errorD:Bureau_ConsoleWriteLog.au3(69,30) : WARNING: $LogFile: possibly used before declaration.$CWLfile = FileOpen($LogFile,~~~~~~~~~~~~~~~~~~~~~~~~~~~~^D:Bureau_ConsoleWriteLog.au3(69,30) : ERROR: $LogFile: undeclared global variable.$CWLfile = FileOpen($LogFile,~~~~~~~~~~~~~~~~~~~~~~~~~~~~^D:Bureau_ConsoleWriteLog.au3 - 1 error(s), 1 warning(s) AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
SnArF Posted January 21, 2013 Author Share Posted January 21, 2013 Hi wakillon, did you copy the code or do you use the UDF? If you use the UDF you have to declare the logfile My scripts: _ConsoleWriteLog | _FileArray2D Link to comment Share on other sites More sharing options...
guinness Posted January 21, 2013 Share Posted January 21, 2013 (edited) Why not pass it as a parameter instead? UDFs work in a way that all variable should be declared prior to the user implementing it in their script. If you don't want to "clog" up the _CW parameters, then you could something like this >> (though not recommended) #include-once #include <FileConstants.au3> Global $__sCWLogFile = @ScriptDir & '\LogFile.log' ; Default log file variable placed at the top of the UDF. ; A user just has to call the relevant function. Func _CW_GetLogFile() Return $__sCWLogFile EndFunc ;==>_CW_GetLogFile Func _CW_SetLogFile($sLogFile) If FileExists($sLogFile) = 0 Then FileClose(FileOpen($sLogFile, $FO_OVERWRITE)) ; Create the file if it doesn't exist. EndIf $__sCWLogFile = $sLogFile EndFunc ;==>_CW_SetLogFile Edited January 21, 2013 by guinness SnArF 1 UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
SnArF Posted January 21, 2013 Author Share Posted January 21, 2013 Thanks for the suggestion guinness, i've modified the UDF. added _CwlLogFile() working fine now My scripts: _ConsoleWriteLog | _FileArray2D Link to comment Share on other sites More sharing options...
guinness Posted January 21, 2013 Share Posted January 21, 2013 Also use $FO_APPEND in FileConstants rather than a 'magic number.' UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
SnArF Posted January 21, 2013 Author Share Posted January 21, 2013 Guinness, could you explain why I should use $FO_APPEND instead of 1? My scripts: _ConsoleWriteLog | _FileArray2D Link to comment Share on other sites More sharing options...
guinness Posted January 21, 2013 Share Posted January 21, 2013 Guinness, could you explain why I should use $FO_APPEND instead of 1?I can, it improves readability of the code and when you consider AutoIt is constantly changing a Dev at any moment could decide the value of 1 for FileOpen is to delete the file (unlikely of course, but you never know.)Source: https://en.wikipedia.org/wiki/Magic_number_(programming) UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 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