Zerobug

Generate texte with date & time

5 posts in this topic

Hi Guys,

For testing an analysis log software, I need to create an autoit script to generate "fake log".

I have tryied, some things, but unfortunnately, it does'nt works fine, for exemple, in the script below, the time don't update, always the same

The goal is to have a script which loop on a 4 messages (info, warning, error and fatal error)

#include <Date.au3>

$File = "logfile.log"
$Text = @MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & ":" & @MIN & " [ERROR] This is an Error Message" & @CRLF & _
@MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & ":" & @MIN & " [INFO] This is an Information Message" & @CRLF & _
@MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & ":" & @MIN & " [WARNING]This is a Warning Message" & @CRLF & _
@MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & ":" & @MIN & " [FATAL]This is a Message with Fatal Error" & @CRLF

HotKeySet("{ESC}","Quit") 

While True    
Sleep(5000)
    FileWrite($File, $Text)
    Sleep(2000)
        FileWrite($File, $Text)
    Sleep(2000)

    FileWrite($File, $Text)
    Sleep(2000)
        FileWrite($File, $Text)
    Sleep(2000)
WEnd

Func Quit()
    Exit
EndFunc

Can you help me ?

Many thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this (more randomized):

#include <Date.au3>

$File = "logfile.log"
Local $aText = [ _
    "[ERROR] This is an Error Message", _
    "[INFO] This is an Information Message", _
    "[WARNING] This is a Warning Message", _
    "[FATAL] This is a Message with Fatal Error" _
], $sText, $sDate = _NowCalc()

HotKeySet("{ESC}", _Quit)

While True
    $sDate = _DateAdd("s", Random(1, 86400, 1), $sDate)
    $sText = $sDate & " " & $aText[Random(0, UBound($aText) - 1, 1)] & @CRLF
    FileWrite($File, $sText)
WEnd

Func _Quit()
    Exit
EndFunc

 

Edited by jchd
Fixed typo in variable name

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@Zerobug

The time should be updated inside the While loop  :)

#include <Date.au3>

$File = "logfile.log"
HotKeySet("{ESC}","Quit") 

While True    
Sleep(5000)
    FileWrite($File, _MyCurrentTime() & " [ERROR] This is an Error Message" & @crlf)
    Sleep(2000)
    FileWrite($File, _MyCurrentTime() & " [INFO] This is an Information Message" & @crlf)
    Sleep(2000)
    FileWrite($File, _MyCurrentTime() & " [WARNING] This is a Warning Message" & @crlf)
    Sleep(2000)
    FileWrite($File, _MyCurrentTime() & " [FATAL] This is a Message with Fatal Error" & @crlf)
    Sleep(2000)
WEnd

Func _MyCurrentTime()
   Return @MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & ":" & @SEC
EndFunc

Func Quit()
    Exit
EndFunc

 

Share this post


Link to post
Share on other sites

I've juste replaced 

FileWrite($File, $Text

by

FileWrite($File, $sText

and, i's perfect (also awesome).

 

Many thanks for your help.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ah, sorry for the typo (now fixed).

Feel free to make the random progression in seconds smaller (< 86400) or bigger (> 86400) to make the output more realistic for your use case.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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