Jump to content

Computer Logger [SOLVED]


Revolter
 Share

Recommended Posts

I want to make a script that calculates the minimum, the maximum and the average time spent at computer si i made "clog.au3" that you'll see lower that writes to "linenumber.txt" the number that i use to know on which line to write to and to "datelog.txt" on one line the date i used the computer and on the second line the time in miliseconds. The "clog.au3" it's in the startup folder and runs each 10 minutes.

Then i made "Computer Logger.au3" that reads the number of miliseconds from each second line and calculates the min, the max and the average of the records. But it doesn't return the corect max. It is because the "datelog.txt" is always opened by "clog.au3"?

"clog.au3" :

#include 
#include 
#include 

_Singleton( "start" )

Global $tick , $filenumber
                                                                                        ;MsgBox(0,"",@YEAR & @MON & @MDAY)
If FileExists( @DocumentsCommonDir & "\Computer Log\linenumber.txt" ) = 0 Then
    $filenumber = 0
    DirCreate( @DocumentsCommonDir & "\Computer Log" )
    FileOpen( @DocumentsCommonDir & "\Computer Log\linenumber.txt" , 1 )
    FileWrite( @DocumentsCommonDir & "\Computer Log\linenumber.txt" , $filenumber + 1 )
    FileOpen( @DocumentsCommonDir & "\Computer Log\datelog.txt" , 1 )
    FileWrite( @DocumentsCommonDir & "\Computer Log\datelog.txt" , @YEAR & @MON & @MDAY )
EndIf
                                                                                        ;MsgBox(0,"",FileReadLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , FileRead( @DocumentsCommonDir & "\Computer Log\linenumber.txt" ) ))
                                                                                        ;MsgBox(0,"",FileRead( @DocumentsCommonDir & "\Computer Log\linenumber.txt" ))
$filenumber = FileRead( @DocumentsCommonDir & "\Computer Log\linenumber.txt" )

If FileReadLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber ) <> @YEAR & @MON & @MDAY Then
    FileDelete( @DocumentsCommonDir & "\Computer Log\linenumber.txt" )
    FileWrite( @DocumentsCommonDir & "\Computer Log\linenumber.txt" , $filenumber + 2 )
    $filenumber = FileRead( @DocumentsCommonDir & "\Computer Log\linenumber.txt" )
EndIf
                                                                                        ;MsgBox(0,"",FileRead( @DocumentsCommonDir & "\Computer Log\datelog.txt"))
If FileReadLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber ) = "" Then
    _FileWriteToLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber , @YEAR & @MON & @MDAY , 1 )
EndIf

While 1
    $tick = _Date_Time_GetTickCount()

    If FileReadLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber + 1 ) = "" Then
        FileWriteLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , @LF )
        _FileWriteToLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber + 1 , $tick , 1 )
    EndIf

    If FileReadLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber + 1 ) <> "" Then
        _FileWriteToLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $filenumber + 1 , $tick , 1 )
    EndIf

    Sleep( 1000 * 60 * 10 )
WEnd

"Computer Logger.au3" :

#include 
#include 

Global $filenumber , $fileread , $min = 86400000 , $max = 0 , $sum = 0 , $average , $minhours , $minminutes , $minseconds , _
$maxhours , $maxminutes , $maxseconds , $averagehours , $averageminutes , $averageseconds , $uphours , $upminutes , $upseconds

If FileExists( @StartupCommonDir & "\clog.lnk" ) = 0 Then
    FileCreateShortcut( "clog.exe" , @StartupCommonDir & "\clog.lnk" )
EndIf

$filenumber = FileRead( @DocumentsCommonDir & "\Computer Log\linenumber.txt" )

For $i = 2 To $filenumber + 1 Step 2
    $fileread = FileReadLine( @DocumentsCommonDir & "\Computer Log\datelog.txt" , $i )
    If $min > $fileread Then
        $min = $fileread
    EndIf

    If $max < $fileread Then
        $max = $fileread
    EndIf

    $sum = $sum + $fileread
Next

;$average = $sum / ( ( $filenumber + 1 ) / 2 )
$average = ( $min + $max ) / 2

_TicksToTime( $min , $minhours , $minminutes , $minseconds )
_TicksToTime( $max , $maxhours , $maxminutes , $maxseconds )
_TicksToTime( $average , $averagehours , $averageminutes , $averageseconds )
_TicksToTime( _Date_Time_GetTickCount() , $uphours , $upminutes , $upseconds )

MsgBox( 0 , "Computer Logger" , "Minimum time spent at computer until now is: " & $minhours & ":" & $minminutes & ":" & $minseconds & @LF & _
"Maximum time spent at computer until now is: " & $maxhours & ":" & $maxminutes & ":" & $maxseconds & @LF & _
"Average time spent at computer until now is: " & $averagehours & ":" & $averageminutes & ":" & $averageseconds & @LF & _
"Time past since last reboot is: " & $uphours & ":" & $upminutes & ":" & $upseconds )

"linenumber.txt" : "15"

"datelog.txt" :

"20101108

17465091

20101109

27599540

20101110

1844041

20101111

25238730

20101112

2442991

20101113

41605

20101114

3034032

20101115

4239389"

Sorry for my bad english!!

Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

It looks to me like you are making this much harder than it should be. It is definitely a way to achieve the same thing without all these text files involved.

A couple questions:

- how do you get the time to write on those text files?

- what is for you the "start time"? (time when computer was powered up? when it rebooted? when an user logged in?)

All file reads/writes can be avoided by using a registry key to read/write in.

And another thing: every FileOpen need to have a "FileClose" somewhere; also, most of the "File..." commands does not require the file to be opened - IF a filename is provided (the file is opened and closed during the function call).

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

It looks to me like you are making this much harder than it should be. It is definitely a way to achieve the same thing without all these text files involved.

A couple questions:

- how do you get the time to write on those text files?

- what is for you the "start time"? (time when computer was powered up? when it rebooted? when an user logged in?)

All file reads/writes can be avoided by using a registry key to read/write in.

And another thing: every FileOpen need to have a "FileClose" somewhere; also, most of the "File..." commands does not require the file to be opened - IF a filename is provided (the file is opened and closed during the function call).

i'm a newbie ...

-i used

_Date_Time_GetTickCount()
to find the "start time" but i'm not very sure what this means ... it says "number of milliseconds that have elapsed since Windows was started" so i think is the time elapsed since computer has powered up ...

i dond't know very well how to work with registry ... in whick key should i write the logs?

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

Ok, I see what you mean; that will return indeed the number of ms since Windows started.

So: do you want to keep a log file for every computer and every 10 minutes to update that number? then you want to be able to get the times for all days and get some average/min/max for daily use?

If so, I'll try to put together a script for you.

#Include <Array.au3>
#Include <File.au3>

Global $LogFolder = @DocumentsCommonDir & "\Computer Log"
Global $LogFile = @DocumentsCommonDir & "\Computer Log\datelog.txt"
Global $Today = @MDAY&"/"&@MON&"/"&@YEAR

If Not FileExists($LogFolder) Then DirCreate($LogFolder)
If Not FileExists($LogFile) Then FileClose(FileOpen($LogFile, 2))

Global $FileARRAY[1]
_FileReadToArray($LogFile, $FileARRAY)
Switch @error
    Case 1
        MsgBox(16, "FileOpen error", "Cannot open "&$LogFile)
        Exit
    Case 2      ;file is empty = it was just created
        FileWriteLine($LogFile, @MDAY&"/"&@MON&"/"&@YEAR&":"&_Date_Time_GetTickCount())
    Case 0      ;file is OK and has records
        If StringInStr($FileARRAY[$FileARRAY[0]], $Today) Then      ;if the last line on the file has today's date
            ;replace it with the new time value
            $FileARRAY[$FileARRAY[0] = @MDAY&"/"&@MON&"/"&@YEAR&":"&_Date_Time_GetTickCount()   
        Else        ;if the last record is not today's date
            ;add a new record
            _ArrayAdd($FileARRAY, @MDAY&"/"&@MON&"/"&@YEAR&":"&_Date_Time_GetTickCount())
        EndIf
        ;write the new file content
        _FileWriteFromArray($LogFile, $FileARRAY)
EndSwitch

You will need to compile this script, then create a scheduled task (use Win Task Scheduler) to run every 10 minutes your application.

Every time it runs, it will update the content of your log file.

An entry in the log file will be like:

17/11/2010:548799

Just test the script on your computer, get it compiled and the scheduled task running. Once you do that, we can cover the second part (reading average/min/max time)

Forget about using registry at the time. You can use the registry to keep simple values in there, not text logs.

EDIT: if it is very difficult for you to make yourself understood because of poor English - write in Romanian, however, my answer will be in English. (I don't recommend that - use it if you REALLY need)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Ok, I see what you mean; that will return indeed the number of ms since Windows started.

So: do you want to keep a log file for every computer and every 10 minutes to update that number? then you want to be able to get the times for all days and get some average/min/max for daily use?

If so, I'll try to put together a script for you.

#Include <Array.au3>
#Include <File.au3>

Global $LogFolder = @DocumentsCommonDir & "\Computer Log"
Global $LogFile = @DocumentsCommonDir & "\Computer Log\datelog.txt"
Global $Today = @MDAY&"/"&@MON&"/"&@YEAR

If Not FileExists($LogFolder) Then DirCreate($LogFolder)
If Not FileExists($LogFile) Then FileClose(FileOpen($LogFile, 2))

Global $FileARRAY[1]
_FileReadToArray($LogFile, $FileARRAY)
Switch @error
    Case 1
        MsgBox(16, "FileOpen error", "Cannot open "&$LogFile)
        Exit
    Case 2        ;file is empty = it was just created
        FileWriteLine($LogFile, @MDAY&"/"&@MON&"/"&@YEAR&":"&_Date_Time_GetTickCount())
    Case 0        ;file is OK and has records
        If StringInStr($FileARRAY[$FileARRAY[0]], $Today) Then        ;if the last line on the file has today's date
            ;replace it with the new time value
            $FileARRAY[$FileARRAY[0] = @MDAY&"/"&@MON&"/"&@YEAR&":"&_Date_Time_GetTickCount()    
        Else        ;if the last record is not today's date
            ;add a new record
            _ArrayAdd($FileARRAY, @MDAY&"/"&@MON&"/"&@YEAR&":"&_Date_Time_GetTickCount())
        EndIf
        ;write the new file content
        _FileWriteFromArray($LogFile, $FileARRAY)
EndSwitch

You will need to compile this script, then create a scheduled task (use Win Task Scheduler) to run every 10 minutes your application.

Every time it runs, it will update the content of your log file.

An entry in the log file will be like:

17/11/2010:548799

Just test the script on your computer, get it compiled and the scheduled task running. Once you do that, we can cover the second part (reading average/min/max time)

Forget about using registry at the time. You can use the registry to keep simple values in there, not text logs.

EDIT: if it is very difficult for you to make yourself understood because of poor English - write in Romanian, however, my answer will be in English. (I don't recommend that - use it if you REALLY need)

thanks a lot!!!

some questions ... :

FileClose(FileOpen($LogFile, 2))
creates the file and closes it immediately right? /:)

_FileWriteFromArray($LogFile, $FileARRAY)
this writes on a new line the array data?

now if you could make the min/max/average you'll be my god :graduated:)

P.S. you forgot

#include <Date.au3>
:( Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

FileClose(FileOpen($LogFile, 2))

- if the file does not exist, it will create the file

- if the file exists, it will wipe the content

In the script, I used that to create the file if it does not exist.

_FileWriteFromArray($LogFile, $FileARRAY)

Quote from the help file:

If a string path is provided, the file is overwritten and closed.

That will overwrite the whole file content.

Get your testing done with the current script and we'll do the other part.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

I suggest a little bit of reading for you.

Read about "_FileReadToArray" - there is something about what it is in the element [0] of the resulting array.

Have a play with the same function - make a script to read a text file you already have and use _ArrayDisplay to display the content of resulting array.

Read about arrays too - they are very important and something you will end up using everyday.

Keep the help file always open because it is your best AutoIt friend (after 4 years I still use it very often and I will do it for a long time to come)

$FileARRAY[$FileARRAY[0]] is a little bit more advanced form of writing it. If I wrote it like:

$LastArrayElementIndex = $FileARRAY[0]

$FileARRAY[$LastArrayElementIndex]

you wouldn't have been confused at all - it is all the same but in the first form it saves an extra line of script.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

hmmm ...

but where did you initialized the $FileARRAY[0]?

btw ... i didn't used at all arrays ... thnx for ur advice ... i'll start reading about arrays to understand it better :graduated:

Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

Get your testing done with the current script and we'll do the other part.

this is the "datelog.txt" so far.

is this what you've wanted?

wouldn't be easier not to write those numbers, only the date and the ticks?

datelog.txt

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

hmmm ...

but where did you initialized the $FileARRAY[0]?

btw ... i did'n used at all arrays ... thnx for ur advice ... i'll start reading about arrays to understand it better :(

Element 0 was filled by the function that read the data into an array for you. It contains a number that lets you know how many lines of data were read. It is just for convenience, and not all built-in functions that put stuff in arrays do it. It is not automatic for arrays and is something that has to be explicitly coded into the routine that fills the array, which I actually find a good reason to not use it, because it can be confusing (because not all autoit functions that fill arrays do it) and because it makes it harder to understand the logic behind 0-based arrays for scripting newbies.

Anyway, the array that the _FileReadToArray creates, starts with element 0 (in which that function keeps a counter that is increased for every read line) but the actual data that was read, begins in element 1. This means that if you ever just want to dump all the data that was read into the array, you have to remember to exclude the 0'th element. While other functions that don't put a counter in the 0'th element, give you an array where the data already starts in the 0'th element. And then you can still retrieve the number of elements with a single command (UBound) so in my opinion there is never much use for that counter, it just puts unneeded garbage into a data array.

A small example:

#RequireAdmin
#include <File.au3>
#include <Array.au3>

Dim $resultArray

_FileReadToArray(@SystemDir & "\drivers\etc\hosts", $resultArray)

$string = "Lines in your hostfile, counted by the _FileReadToArray and put into element zero: " & $resultArray[0] & @CRLF & @CRLF
$string = $string & "Actual amount of elements in your array (given by UBound): " & UBound($resultArray) & @CRLF & @CRLF
$string = $string & "The last line from your hostfile (which is automatically the highest index): " & @CRLF & $resultArray[$resultArray[0]] & @CRLF & @CRLF

MsgBox("Test", 0, $string)

I hope I explained it a bit and didn't actually complicate matters... :graduated:

By the way, your English is certainly good enough to communicate easily, especially for someone who is not a native speaker. In my honest opinion, you have nothing to apologize for!

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Element 0 was filled by the function that read the data into an array for you. It contains a number that lets you know how many lines of data were read. It is just for convenience, and not all built-in functions that put stuff in arrays do it. It is not automatic for arrays and is something that has to be explicitly coded into the routine that fills the array, which I actually find a good reason to not use it, because it can be confusing (because not all autoit functions that fill arrays do it) and because it makes it harder to understand the logic behind 0-based arrays for scripting newbies.

Anyway, the array that the _FileReadToArray creates, starts with element 0 (in which that function keeps a counter that is increased for every read line) but the actual data that was read, begins in element 1. This means that if you ever just want to dump all the data that was read into the array, you have to remember to exclude the 0'th element. While other functions that don't put a counter in the 0'th element, give you an array where the data already starts in the 0'th element. And then you can still retrieve the number of elements with a single command (UBound) so in my opinion there is never much use for that counter, it just puts unneeded garbage into a data array.

A small example:

#RequireAdmin
#include <File.au3>
#include <Array.au3>

Dim $resultArray

_FileReadToArray(@SystemDir & "\drivers\etc\hosts", $resultArray)

$string = "Lines in your hostfile, counted by the _FileReadToArray and put into element zero: " & $resultArray[0] & @CRLF & @CRLF
$string = $string & "Actual amount of elements in your array (given by UBound): " & UBound($resultArray) & @CRLF & @CRLF
$string = $string & "The last line from your hostfile (which is automatically the highest index): " & @CRLF & $resultArray[$resultArray[0]] & @CRLF & @CRLF

MsgBox("Test", 0, $string)

I hope I explained it a bit and didn't actually complicate matters... :graduated:

By the way, your English is certainly good enough to communicate easily, especially for someone who is not a native speaker. In my honest opinion, you have nothing to apologize for!

mmmm... my brain is small and it takes me longer to think :| =))

i understood it better ... i'll start using them to accommodate (the online dictionary is not working well :() )

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

Sorry - the numbers were my bad - the file was written from array but starting with element [0] which held only a counter.

Change the line with _FileWriteFromArray($LogFile, $FileARRAY, 1), that will get rid of any numbers in the future.

If you start a new log file it will work fine; if you want to use that one, just remove all the numbers and keep the date lines.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Sorry - the numbers were my bad - the file was written from array but starting with element [0] which held only a counter.

Change the line with _FileWriteFromArray($LogFile, $FileARRAY, 1), that will get rid of any numbers in the future.

If you start a new log file it will work fine; if you want to use that one, just remove all the numbers and keep the date lines.

it's working fine ... now how do i read the min,max,average? :graduated:

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

_ArrayMin and _ArrayMax should give the min and the max array values.

I guess the average can be calculated by working through the array, adding all the values together, and divide the total by the number of values...

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

- Use _FileReadToArray to read all your log file.

- Make a For/Next loop as you did previously.

- For every element in array, you will need to split the line (StringSplit) by ":"

- Keep every second elemet of the array returned by StringSplit

- Compare all of them to get the maximum, minimum, average

A lot of this is to be done in your own code, I have only given you the direction.

Chinese Proverb

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

- Use _FileReadToArray to read all your log file.

- Make a For/Next loop as you did previously.

- For every element in array, you will need to split the line (StringSplit) by ":"

- Keep every second elemet of the array returned by StringSplit

- Compare all of them to get the maximum, minimum, average

A lot of this is to be done in your own code, I have only given you the direction.

chiar lucram la el :graduated:) si am terminat ... sa-mi zici daca e corect

#include <Array.au3>
#include <Date.au3>
#include <File.au3>
#include <String.au3>

Global $i , $j , $results , $array , $LogFile = @DocumentsCommonDir & "\Computer Log\datelog.txt" , $suma = 0 , $nr = 0 , $min , $max , $average , _
$minh , $minm , $mins , $maxh , $maxm , $maxs , $averageh , $averagem , $averages , $uph , $upm , $ups

_FileReadToArray( $LogFile , $results )

Global $ticks[ $results[ 0 ] + 1 ]

For $i = 1 To UBound( $results ) - 1
    $array = _StringExplode( $results[ $i ] , ":" , 0 )
    $suma = $suma + $array[ 1 ]
    $nr = $nr + 1
    $ticks[ $i ] = $array[ 1 ]
Next

$min = _ArrayMin( $ticks , -1 , 1 )
$max = _ArrayMax( $ticks , -1 , 1 )
$average = $suma / $nr

_TicksToTime( $min , $minh , $minm , $mins )
_TicksToTime( $max , $maxh , $maxm , $maxs )
_TicksToTime( $average , $averageh , $averagem , $averages )
_TicksToTime( _Date_Time_GetTickCount() , $uph , $upm , $ups )

MsgBox( 0 , "Computer Logger" , "Minimum time spent at computer until now is: " & $minh & ":" & $minm & ":" & $mins & @LF & _
"Maximum time spent at computer until now is: " & $maxh & ":" & $maxm & ":" & $maxs & @LF & _
"Average time spent at computer until now is: " & $averageh & ":" & $averagem & ":" & $averages & @LF & _
"Time past since last reboot is: " & $uph & ":" & $upm & ":" & $ups )
Edited by Revolter

[center]Sorry for my bad english. Trying my best :Dhttp://iulianonofrei.comhttp://www.last.fm/user/Revolt666 [/center]

Link to comment
Share on other sites

You could try to make it a service (not too experienced with that but I believe there's a bunch of info about that on the forum) or just put it in the start menu's startup folder or the registry startup keys... The thing is that to not make it dependant on something like the task-scheduler, it would need to always keep running. (Because if it doesn't always run, it depends on SOMETHING to start it up, be it a user or the task scheduler or whatever). About RAM usage: does it use much RAM then? Should be only a couple of megabytes... Never worried about that, maybe someone else has more experience with autoit + RAM usage.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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...