Jump to content

Write app events and errors into a file and change file name each time


Korom
 Share

Recommended Posts

Hello everyone

I am new in autoit

What I want to do is ,  writing my application events and erros and save them in a '.txt' file

then change filename each time when the file size become bigger than (1 MB ) ( I do not want a lot of lines in the same file)

for example , my app is writing into "My_app_events0.txt" then when it's size become 1MB or more it start writing into "My_app_events1.txt"  without deleting the first file , and then

"My_app_events2.txt" ,"My_app_events3.txt" then "My_app_events4.txt"  ...ect

I am using this function to get file size

 

Func GetFileSize($inputSize, $inputUnit = 1, $outputPlaces = 2, $outputString = True, $inputBits = False, $outputBits = False, $outputUnit = -4)
    Local $unitNames[9] = ["","K","M","G","T","P","E","Z","Y"]
    Local $bytes = $inputSize * 1024 ^ $inputUnit
    Local $b = "B"
    If $inputBits Then $bytes /= 8
    If $outputBits Then
        $bytes *= 8
        $b = "b"
    EndIf
    If $outputUnit < 0 Then
        Local $outputMax = Abs($outputUnit)
        $outputUnit = Int(Log($bytes)/Log(1024))
        If $outputUnit > $outputMax Then $outputUnit = $outputMax
    EndIf
    If $outputString Then
        Return String(Round($bytes / 1024 ^ $outputUnit, $outputPlaces)) & $unitNames[$outputUnit] & $b
    Else
        Return Round($bytes / 1024 ^ $outputUnit, $outputPlaces)
    EndIf
 EndFunc

I've Tried to write this methode but it doesn't work I do't know why it consist in getting the file number from a registry key and add ( +1 ) each time we change the file to write into .

$Key = "HKEY_CURRENT_USER\Software\My_app_events"
$Val = 'Number'
$Read_Number = RegRead($Key,$Val)
;$LogC = FileOpen($LogN,2)
If RegRead($Key,$Val) <> "" Then ; a number already exists So this is not my first run
$Fname =  "My_app_events" & $Read_Number & ".txt" ; Read our filename

Else ; So this is my 1st Run
RegWrite($Key,$Val,"REG_SZ","0") ;  Write First number = 0
$Fname = "My_app_events" & $Read_Number & ".txt" ; Read our filename
EndIf

while 1
$KeySize = GetFileSize($LogF)

If $KeySize >= 1024 Then ; If file Size > 1 MB (1000 KB )
$OlD_Num = RegRead($Key,$Val) ; Getting old Number
RegWrite($Key,$Val,"REG_SZ","") ; Empty the number
RegWrite($Key,$Val,"REG_SZ",$OlD_Num +1) ; Writing the next Number

Restart()

EndIf
    
WriteEvents()   

Sleep(1000)
 Wend
 Func WriteEvents($What_to_write)
    FileWrite($Fname,$What_to_write)
    endfunc
 Func Restart()
Run(@Autoitexe) ; Restart
Exit
    Endfunc

I tried also the same way but with another methode which read file name from another '.txt' file but it didn't work to I removed it after .

I wish I will get help , thanks in advance and Sorry for my bad English :)

Edited by Korom
correct mistake
Link to comment
Share on other sites

Or you could add the date to your filename and so automatically create a new file every day. Much easier to debug when the user tells you: "The program crashed yesterday in the afternoon".
And easier to code ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Your code is wrong. I'm seeing that you need to debug it a little. You need to put FileGetSize not GetFileSize.

https://www.autoitscript.com/autoit3/docs/functions/FileGetSize.htm

no bro code is correct "GetFileSize" is an UDF that I found somewhere in this forums it retuurns the size in kilobyte and it woks perfectly 

, normal FileGetSize retuurns it in bytes .

 

 

Func GetFileSize($inputSize, $inputUnit = 1, $outputPlaces = 2, $outputString = True, $inputBits = False, $outputBits = False, $outputUnit = -4)
    Local $unitNames[9] = ["","K","M","G","T","P","E","Z","Y"]
    Local $bytes = $inputSize * 1024 ^ $inputUnit
    Local $b = "B"
    If $inputBits Then $bytes /= 8
    If $outputBits Then
        $bytes *= 8
        $b = "b"
    EndIf
    If $outputUnit < 0 Then
        Local $outputMax = Abs($outputUnit)
        $outputUnit = Int(Log($bytes)/Log(1024))
        If $outputUnit > $outputMax Then $outputUnit = $outputMax
    EndIf
    If $outputString Then
        Return String(Round($bytes / 1024 ^ $outputUnit, $outputPlaces)) & $unitNames[$outputUnit] & $b
    Else
        Return Round($bytes / 1024 ^ $outputUnit, $outputPlaces)
    EndIf
 EndFunc

Or you could add the date to your filename and so automatically create a new file every day. Much easier to debug when the user tells you: "The program crashed yesterday in the afternoon".
And easier to code ;)

no mate I want files names to have the same radical (same name) exept last number (name0.txt ,name1.txt ,name2.txt ...) I need it like that fo the rest of my app

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