Jump to content

Time Stamp?


Recommended Posts

i am making a function that creates a log of all the things my script does. i want it to inculde a time stamp for every line that it writes. does autoit have a timefunction? all i could find in the help file was a timer, which can only start then stop to give a time diffrence.

i would like to have something like this:

logfile.txt:

line of file happened @ 00:01:00

line of file happened @ 00:01:01

---or--

line of file happened @ 60000 milliseconds from start

line2 of file happened @ 61000 milliseconds from start

either a time stamp or a timer would work.

perhaps i need to make a time stamp function of my own? make a function that creates a file, closes it. then reads the year/day/time/second the file was made and saves that as the start time. then starts a adlib function that adds a global counter var ever 100 milliseconds. then print out that global var on each string that i write to the file. is there not an easier way?

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

ok, thanks. i think that will work excatly how i need it to.

heres another quick Q about my log maker:

does leaving a file open throught the script slow it down? or would it be better to open and close it ever time i write a line [ i will be writing MANY 100+ lines into this log file]

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

here is my log making script. right now it only puts in 2 lines with a time stamp, in the real application it will be adding lines every few seconds.

Global $file
Global $DebugText

 FileOpen("test.txt", 2)
FileClose("test.txt")

MsgBox(4096,"","Start",1)
sleep(1000)

$DebugText=("hi" & @HOUR &":"& @MIN & ":" & @SEC)
RecLog($DebugText)
sleep(1000)
$DebugText=("Bye" & @HOUR &":"& @MIN & ":" & @SEC)
RecLog($DebugText)

MsgBox(4096,"","Done",1)

FileClose($file)

Func RecLog($DebugText)
    $file = FileOpen("test.txt", 1)
    If $file = -1 Then
      MsgBox(4096, "Error", "Unable to open file.",1)
      EndIf
    FileWriteLine($file, $DebugText)
EndFunc

so you are saying dont use $file for the handle. just use

FileWriteLine(test.txt,$DebugText)
?

i noticed it writes each line before the other lines so when i read the log it will be in reverse. can i switch that?

and do i need to set $file and $DebugText as globals? or is that overkill?

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

wait a sec. i was mistaken. the lines are written in correct order EXCEPT the last line. here is my code:

Global $file
Global $DebugText

 FileOpen("test.txt", 2)
FileClose("test.txt")

MsgBox(4096,"","Start",1)

$DebugText=("Hi            [" & @HOUR & ":" & @MIN & ":" & @SEC & "]" )
RecLog($DebugText)
sleep(1000)
$DebugText=("line1        [" & @HOUR & ":" & @MIN & ":" & @SEC & "]" )
RecLog($DebugText)
sleep(1000)
$DebugText=("line2        [" & @HOUR & ":" & @MIN & ":" & @SEC & "]" )
RecLog($DebugText)
sleep(1000)
$DebugText=("line3        [" & @HOUR & ":" & @MIN & ":" & @SEC & "]" )
RecLog($DebugText)
sleep(1000)
$DebugText=("Bye         [" & @HOUR & ":" & @MIN & ":" & @SEC & "]" )
RecLog($DebugText)

FileClose($file)

MsgBox(4096,"","Done",1)

Func RecLog($DebugText)
    $file = FileOpen("test.txt", 1)
    If $file = -1 Then
      MsgBox(4096, "Error", "Unable to open file.",1)
      EndIf
    FileWriteLine($file, $DebugText)
EndFunc

and here is what it outputs:

Bye      [15:22:49]Hi            [15:22:45]

line1        [15:22:46]

line2        [15:22:47]

line3        [15:22:48]

hi though line3 are in proper order. but why is the "bye" line up top?

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

yea, you can eliminate a few lines of code.

Global $file
Global $DebugText
$file="test.txt"

MsgBox(4096,"","Start",1)
sleep(1000)

$DebugText=("hi" & @HOUR &":"& @MIN & ":" & @SEC)
RecLog($DebugText)
sleep(1000)
$DebugText=("Bye" & @HOUR &":"& @MIN & ":" & @SEC)
RecLog($DebugText)
MsgBox(4096,"","Done",1)

Func RecLog($DebugText)
if FileWriteLine($file, $DebugText)=-1 then MsgBox(4096, "Error", "Unable to open file.",1)
EndFunc

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

I would add tab instead, and mine works out well. You can also eliminate a few more lines if you don't need debug text for anything else.

Global $file
Global $DebugText
$timestamp=@tab&"[" & @HOUR & ":" & @MIN & ":" & @SEC & "]" 
$file="test.txt"

MsgBox(4096,"","Start",1)
sleep(1000)

$DebugText="Hi"&$timestamp
RecLog($DebugText)
sleep(1000)
RecLog("line1"&$timestamp); less lines
sleep(1000)
RecLog("line2"&$timestamp); less lines
sleep(1000)
$DebugText="line3"&$timestamp
RecLog($DebugText)
sleep(1000)
$DebugText="Bye"&$timestamp
RecLog($DebugText)

Func RecLog($DebugText)
if FileWriteLine($file, $DebugText)=-1 then MsgBox(4096, "Error", "Unable to open file.",1)
EndFunc

Tabs don't show up well in HTML though

Hi      [12:42:01]line1  [12:42:01]

line2  [12:42:01]

line3  [12:42:01]

Bye      [12:42:01]

you might consider

$timestamp="[" & @HOUR & ":" & @MIN & ":" & @SEC & "]   " 
;....
RecLog($timestamp&"line2"); I like the time first personally
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

My smaller version:

$timestamp="[" & @HOUR & ":" & @MIN & ":" & @SEC & "]   " 
$file="test.txt"
FileDelete ( $file) 

MsgBox(4096,"","Start",1)
sleep(1000)

$DebugText=$timestamp&"Hi"
RecLog($DebugText); if you need debug text.
sleep(1000)
RecLog($timestamp&"line1"); less lines if not
sleep(1000)
RecLog($timestamp&"line2")
sleep(1000)
RecLog($timestamp&"line3")
sleep(1000)
RecLog($timestamp&"Bye")

Func RecLog($DebugText)
if FileWriteLine($file, $DebugText)=-1 then MsgBox(4096, "Error", "Unable to open file.",1)
EndFunc

[12:52:19]Hi

[12:52:19] line1

[12:52:19] line2

[12:52:19] line3

[12:52:19] Bye

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

thanks evveryone. scriptkittys script works great except i need the time stamp to be based off of each line. not just when you set it. i can solve this by re-defining $timestamp right before i write the line inthe txt file.

the order is correct tho! any thoughts on why mine had the bye line messed up?

Edited by Wallfire

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

not sure exactly why, but it has to do with opening it 4 times.

FileOpen("test.txt", 2)
FileClose("test.txt")
;...
Func RecLog($DebugText)
$file = FileOpen("test.txt", 1)
If $file = -1 Then
     MsgBox(4096, "Error", "Unable to open file.",1)
     EndIf
FileWriteLine($file, $DebugText)
EndFunc

should have been

FileOpen("test.txt", 2)
FileClose("test.txt")
$file = FileOpen("test.txt", 1)
If $file = -1 Then
     MsgBox(4096, "Error", "Unable to open file.",1)
     EndIf
;...

Func RecLog($DebugText)
FileWriteLine($file, $DebugText)
EndFunc

or close the file each function.

This could be a cool feature, if I can get it to work consistantly, so that the first line is the final outcome.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

You never close the file before you re-open it. Every time you call RecLog, you open it again. That's what's causing problems. It's probably a bug as it shouldn't do that, but the main fault lies in your code not properly taking care of any open files or making sure the file isn't already open before trying to open it again.

Link to comment
Share on other sites

oh yea, I forgot my error codes. filewriteline should be:

If FileWriteLine($file, $DebugText)=0 then MsgBox(4096, @error, "Unable to open file.",1)

not

If FileWriteLine($file, $DebugText)=-1 then MsgBox(4096, @error, "Unable to open file.",1)

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

speaking of errors in my code....

can someone identify what is wrong with this one?

Line 77 (file"C:\"):

BotLog($timestamp$"text..."&"other suff...")

BotLog(^ ERROR

DRROR: Error in expresion.

i have the same $timestamp var in other lines and they all work fine. for some reason i keep getting an error here, cant figure out why....

Edited by Wallfire

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

you are a genius! thanks alot

lol, your still a genius but that wasnt it. i just typoed that in there. :whistle:

here is the exact line of code copied:

BotLog($timestamp&" Picked up. coord: ("&$x&","&$y&")")

and if still gives me an error pointing to the $ in timestamp like before.

Edited by Wallfire

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

BotLog( instead of RecLog( maybe?

This works fine:

$timestamp="[" & @HOUR & ":" & @MIN & ":" & @SEC & "]   "
$file="test.txt"
FileDelete ( $file)

MsgBox(4096,"","Start",1)
sleep(1000)

$DebugText=$timestamp&"Hi"
RecLog($DebugText); if you need debug text.
sleep(1000)
RecLog($timestamp&"line1"); less lines if not
sleep(1000)
RecLog($timestamp&"line2")
sleep(1000)
RecLog($timestamp&"line3")
sleep(1000)
RecLog($timestamp&"Bye")
$x=1
$y=2
recLog($timestamp&" Picked up. coord: ("&$x&","&$y&")")

Func RecLog($DebugText)
if FileWriteLine($file, $DebugText)=-1 then MsgBox(4096, "Error", "Unable to open file.",1)
EndFunc

output:

[18:38:25]Hi

[18:38:25] line1

[18:38:25] line2

[18:38:25] line3

[18:38:25] Bye

[18:38:25]    Picked up. coord: (1,2)

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

ya, i found my error. thanks for double checking. it turns out there was 2 errors on 2 diffrent lines but i kept looking back at the first line trying to find the mistake. i am using notepad to write these scripts ATM and i have no line# refrenses so i easily get lost.

so i made a script that will automaticly take me to a certain line :whistle:

:cheer: awww...wheres the chick?

Link to comment
Share on other sites

We have a full array of nice editors that are great to use with autoit, they have regular expressions, syntax highlighting, and most can test the script right in the program.

Makes debugging a brease and many are free.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...