Sign in to follow this  
Followers 0
vortexed

Help with Log to file logic

24 posts in this topic

Hello Everyone,

I wanted to accomplish logging to a file accross the network only if my ping replies would match be less than 200 MS. With that said I tried creating something that I wanted to share with the community before I use it to make sure that it's correct. This is for testing purposes so I know I need to correct the paths if I want to log to a network share etc...

If you notice, inside my While 1 ... Wend I am invoking _LogTofile() 3 times. What I want to accomplish is to only perform _FileWrite() once but capture every message. Is my code doing this - It tests fine to me just that I am having second doubts.

#include <Date.au3>
Global $FlushLogs
While 1
Sleep(5000)   
   
If _OnDBU() Then ConsoleWrite("ON DIALUP " & @CRLF)
If Not _OnDBU() Then ConsoleWrite("NOT ON DIALUP " & @CRLF)

_LogToFile(@ScriptDir & "\test.log", _Now() & " Test1")
Sleep(2000)
_LogToFile(@ScriptDir & "\test.log", _Now() & " Test2")
Sleep(2000)
_LogToFile(@ScriptDir & "\test2.log", _Now() & " Test3")

$FlushLogs = True
WEnd

Func _LogToFile($LogFile, $message)
$message = $message & @CRLF
If Not _OnDBU() And $FlushLogs Then FileWrite($LogFile, @ComputerName & " " & @IPAddress1 & " " & FileGetVersion(@ScriptFullPath) & " " & $message)
EndFunc

Func _OnDBU()
Local $Bool
Local $PingServer = Ping("10.10.10.1",250)
If $PingServer > 200 Then; also possible:  If @error = 0 Then ...
   Return $Bool = False
    ConsoleWrite("Online, roundtrip was:" & $PingServer & " ERROR " & @error & @CRLF)
 Else
    Return $Bool = True
EndIf
EndFunc

This is part of my output...

 

S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:56:45 PM Test1
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:56:47 PM Test2
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:56:54 PM Test1
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:56:56 PM Test2
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:57:03 PM Test1
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:57:05 PM Test2
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:57:12 PM Test1
S00503R001 10.10.10.12 0.0.0.0 6/14/2013 10:57:14 PM Test2

Share this post


Link to post
Share on other sites



A few enhancements (maked with <==)

#include <Date.au3>
Global $FlushLogs = False  ; <== Set an initial value
While 1
    Sleep(5000)
    If _OnDBU() Then ; <== Only eone function call needed
        ConsoleWrite("ON DIALUP " & @CRLF)
    Else
        ConsoleWrite("NOT ON DIALUP " & @CRLF)
    EndIf
    _LogToFile(@ScriptDir & "\test.log", _Now() & " Test1")
    Sleep(2000)
    _LogToFile(@ScriptDir & "\test.log", _Now() & " Test2")
    Sleep(2000)
    _LogToFile(@ScriptDir & "\test2.log", _Now() & " Test3")
    $FlushLogs = True
WEnd

Func _LogToFile($LogFile, $message)
    $message = $message & @CRLF
    If Not _OnDBU() And $FlushLogs Then FileWrite($LogFile, @ComputerName & " " & @IPAddress1 & " " & FileGetVersion(@ScriptFullPath) & " " & $message)
EndFunc

Func _OnDBU()
    Local $Bool
    Local $PingServer = Ping("10.10.10.1",250)
    If $PingServer > 200 Then; also possible:  If @error = 0 Then ...
       ConsoleWrite("Online, roundtrip was:" & $PingServer & " ERROR " & @error & @CRLF) ; <== Write to Console before Return
       Return $Bool = False
     Else
        Return $Bool = True
    EndIf
EndFunc

Why do you write to 3 log files and wait 2 seconds before writing to the next log file?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water - just for testing

I am going to be implementing this in my main program and I intend it to have many _LogToFile() calls all at different times. I just wanted to make sure that there was a clear gap of time between the two calls to _LogToFile() so that I could see them in the log file.

Thanks for the recommendation! This will work!

Share this post


Link to post
Share on other sites

I see.

In addition: You could use function _FileWriteLog to write the log entries. The function adds the date at the beginning of each line so you do not have to code it yourself.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water - thanks for the recommendation I will use that going forward.

Additionally I noticed something that I wasn't noticing late last night.

I did additional tests to make sure that it was working as I intended it and it is NOT.

I simplified it more and added your suggestions...see code below

What it's not doing is my first call to _LogToFile() is writing to my filesystem when it shouldn't be. I do not want it to write to my file system every time I invoke to _LogToFile(). Instead I want it to write to my log file at the end of my While loop.

Thanks for taking the time and reading this I appreciate it.

; Script Start - Add your code below here
#include <Date.au3>
Global $FlushLogs = False
While 1
ConsoleWrite("STARTING..." & @CRLF)
_LogToFile(@ScriptDir & "\test.log", " Test1")
Sleep(1000*60*1)
_LogToFile(@ScriptDir & "\test.log", " Test2")
Sleep(1000*60*2)
WEnd

Func _LogToFile($LogFile, $message)
$message = $message & @CRLF
If Not _OnDBU() Then FileWrite($LogFile, _Now() & " " & @ComputerName & " " & @IPAddress1 & " " & FileGetVersion(@ScriptFullPath) & " " & $message)
EndFunc

Func _OnDBU()
Local $Bool
Local $PingServer = Ping("10.10.10.1",250)
If $PingServer > 200 Then; also possible:  If @error = 0 Then ...
   ConsoleWrite("Online, roundtrip was:" & $PingServer & " ERROR " & @error & @CRLF)
   Return $Bool = False
     Else
    Return $Bool = True
EndIf
EndFunc

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

In the first version of the script you were setting $FlushLogs, now I can only see the line where the variable is defined.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

BTW: I would modify function _OnDBU:

Func _OnDBU()
    Local $PingServer = Ping("10.10.10.1", 250)
    If $PingServer > 200 Then; also possible:  If @error = 0 Then ...
       ConsoleWrite("Online, roundtrip was:" & $PingServer & " ERROR " & @error & @CRLF)
       Return False
    EndIf
    Return True
EndFunc

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Brain fart on my part - I have changed and re-tested and it still behaves the same way.

#include <Date.au3>
Global $FlushLogs = False
While 1
ConsoleWrite(_Now() & "STARTING..." & @CRLF)
_LogToFile(@ScriptDir & "\test.log", " Test1")
Sleep(1000*60*1)
_LogToFile(@ScriptDir & "\test.log", " Test2")
Sleep(1000*60*2)
$FlushLogs = True
WEnd

Func _LogToFile($LogFile, $message)
   $message = $message & @CRLF  
   If Not _OnDBU() And $FlushLogs Then FileWrite($LogFile, _Now() & " " & @ComputerName & " " & @IPAddress1 & " " & FileGetVersion(@ScriptFullPath) & " " & $message)
EndFunc

Func _OnDBU()     
   Local $PingServer = Ping("10.10.10.1", 250)     
   If $PingServer > 200 Then        
      ConsoleWrite("Online, roundtrip was:" & $PingServer & " ERROR " & @error & @CRLF)        
      Return False     
   EndIf     
   Return True 
EndFunc

Share this post


Link to post
Share on other sites

See no reason why the first log entry is being written.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

test.png

Share this post


Link to post
Share on other sites

It should only be writing to my file system every 3 minutes. Instead it's writing to it every time the _LogToFile() function is called. Last time it wrote was at 11:39 next time it should write is at 11:42. Instead it wrote again at 11:40

Share this post


Link to post
Share on other sites

Sorry for multiple posts - I will keep it to lesser posts. Just posting the information as I find it. I'll try and calm myself.

Notice the gap between Test1 and Test2 - it's 1 minute apart which accurately reflects the time-stamp but the problem is that  it's writing to the test.log every minute after Test1. I don't want it to do that. I want it to only write all the messages at once while capturing the exact time the messages occurred at. Hopefully I am explaining this correctly.

6/15/2013 11:33:19 AM S00503R001 10.10.10.12 0.0.0.0  Test1
6/15/2013 11:34:19 AM S00503R001 10.10.10.12 0.0.0.0  Test2
6/15/2013 11:36:19 AM S00503R001 10.10.10.12 0.0.0.0  Test1
6/15/2013 11:37:19 AM S00503R001 10.10.10.12 0.0.0.0  Test2
6/15/2013 11:39:19 AM S00503R001 10.10.10.12 0.0.0.0  Test1
6/15/2013 11:40:19 AM S00503R001 10.10.10.12 0.0.0.0  Test2
6/15/2013 11:42:19 AM S00503R001 10.10.10.12 0.0.0.0  Test1
6/15/2013 11:43:19 AM S00503R001 10.10.10.12 0.0.0.0  Test2

Share this post


Link to post
Share on other sites

But do you think it is sensible not to write log records as soon as something is to be logged? If the script crashes you loose all log records not committed to the log file.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Regarding it being sensible to write log records as soon as something needs to be logged. No - I want to do this more effectively - instead of invoking FileWrite multiple times I only want to FileWrite() once. Because FileWrite() opens the file system and locks the file I want to do this one time only with all the data that I want to capture in it. Hopefully this makes sense.

Thanks once again for taking the time to respond - I really appreciate your time.

--Also regarding app crashing - I do not count on my script crashing if it does I have checks in place to know about it. I am more concerned about FileWrite() being done once than with the data I am writing. The data I am collecting is informational only to help identify certain things - it's not a necessity, however I do want it to be accurate.

Edited by vortexed

Share this post


Link to post
Share on other sites

To write to a file more efficiently use FileOpen to open the file and then pass the file handle to FileWrite. To close the file use FileClose.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

What is the difference between FileOpen and FileWrite? And how is it more efficient?

Share this post


Link to post
Share on other sites

Help file pretty much explains the difference.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

FileWrite without a handle opens and closes the file after each write. FileWrite with a handle (returned by FileOpen) keeps the file open until it is closed with FileClose.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You guys know that _FileWriteLog can have a handle passed from FileOpen instead of the file path?


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

Yes, we do. At the moment the question is the difference between FileWrite with a path and a handle.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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