Jump to content
Sign in to follow this  
SnArF

Console write and Logfiles

Recommended Posts

SnArF

A simple UDF to write to the console and or to log files.

Updated the UDF, no you can set the logile with _CwlSetLogFile()

#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 by SnArF

My scripts: _ConsoleWriteLog | _FileArray2D

 

 

 

Share this post


Link to post
Share on other sites
wakillon

Hi SnArf

i get an error

D: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.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
guinness

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 by guinness
  • Like 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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
guinness

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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
guinness

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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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  

×