Jump to content

Write LOG file


DaLiMan
 Share

Recommended Posts

Hi There,

I have a little problem writing to a LOG file.

Not the writing itself is the problem but it can be so that multiple users write at the same time.

I checked this for a while and now and then the LOG file isn't added.

(only 1 or 2 instead of 4 lines are written)

My (test) script is as below.

Is there anyone who can solve this problem?

;~ =========================================Timer for starting 4 simultaniously =================================
Dim $Time = InputBox("","tijd?")
Dim $TimeChk = 0

While $TimeChk = 0
   $Time2 = @HOUR & ":" & @MIN
   If $Time = $Time2 Then
      $TimeChk = 1
   EndIf
Wend


;~ =================================================DIM all variables =================================
Dim $Versie = "3.2.1.1"   ; This could be your script version
Dim $Today = @MDAY & "-" & @MON & "-" & @YEAR
Dim $Time = @HOUR & ":" & @MIN & ":" & @SEC

Dim $Naam = "NAME1"        ; Accually was - StringUpper(RegRead("HKEY_CURRENT_USER\SOFTWARE\CQR", "Naam"))
Dim $Plaats = "PLAATS1"    ; Accually was - StringUpper(RegRead("HKEY_CURRENT_USER\SOFTWARE\CQR", "Plaats"))
Dim $Log = "\\NLF18S01\NLF18$\CQR\User\" & $Plaats & "-" & $Naam & ".TXT"

Dim $Naam2 = "ALLUSERS"         
Dim $Plaats2 = "SOLAR"     
Dim $Log2 = "\\NLF18S01\NLF18$\CQR\User\" & $Plaats2 & "-" & $Naam2 & ".TXT"

Dim $RunChk = IniRead("\\NLF18S01\NLF18$\CQR\User\CQR.ini", "Run","Solar", "")
Dim $RunChk2 = IniRead("\\NLF18S01\NLF18$\CQR\User\CQR.ini", "Daniel","CQR", "")
Dim $ScreenChk
Dim $WriteChk = 0

;~ ==============================INI-filecheck for USING===================================
;~ =============================no INI-file or no right data is [NO USE]==================================
If $RunChk <> "Y" Or $RunChk2 <> "Y" Then
   MsgBox(16,"CQRinvuller ©","Life could be so beautifull !!!" & $RunChk & " " & $RunChk2, 10)
   Exit
EndIf
;~ ====================================================================================================

============

$ScreenChk = "OVWR01"     ; Accually was - StringReplace(ClipGet(), " ","")

If $ScreenChk = "OVWR01" Then
   While $WriteChk = 0
      $file = FileOpen($Log , 1)
      $file2 = FileOpen($Log2 , 1)
     ; Check if file opened for writing OK
      If $file = -1 or $file2 = -1 Then
          $WriteChk = 0
      Else
         $WriteChk = 1
      EndIf
   Wend
   FileWrite($file, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "ALL_OK_!" & @TAB & $Versie)
   FileWrite($file2, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "ALL_OK_!" & @TAB & $Versie)
   FileClose($file)
   $WriteChk = 0
ElseIf $ScreenChk = "OVWI02R" Then
      While $WriteChk = 0
      $file = FileOpen($Log , 1)
      $file2 = FileOpen($Log2 , 1)
     ; Check if file opened for writing OK
      If $file = -1 or $file2 = -1 Then
          $WriteChk = 0
      Else
         $WriteChk = 1
      EndIf
   Wend
   FileWrite($file, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "OVWIO2R!" & @TAB & $Versie)
   FileWrite($file2, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "OVWIO2R!" & @TAB & $Versie)
   FileClose($file)
   $WriteChk = 0
   Exit
Else
   While $WriteChk = 0
      $file = FileOpen($Log , 1)
      $file2 = FileOpen($Log2 , 1)
     ; Check if file opened for writing OK
      If $file = -1 or $file2 = -1 Then
          $WriteChk = 0
      Else
         $WriteChk = 1
      EndIf
   Wend
   FileWrite($file, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "VKSCHRM!" & @TAB & $Versie)
   FileWrite($file2, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "VKSCHRM!" & @TAB & $Versie)
   FileClose($file)
   $WriteChk = 0
   Exit
EndIf
Link to comment
Share on other sites

The file is just locked during the open-write-close operation of one user, so the second trying to update the file at the same time will fail.

Use either different files or retry the FileWrite on error e.g.:

Do not use FileOpen and replace all Your FileWrite-calls with LogFileWrite and submit the filename instead of a filehadle as parameter and add:

Func LogFileWrite($filename, $text)
   While 1
      If 1 = FileWrite($filename, $text) Then  ExitLoop 
      Sleep(300)
   Wend
EndFunc
Edited by kanumi
Link to comment
Share on other sites

You mean like below?

$ScreenChk = "OVWR01"     ; Accually was - StringReplace(ClipGet(), " ","")

If $ScreenChk = "OVWR01" Then
   LogFileWrite($Log, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "ALL_OK_!" & @TAB & $Versie)
   LogFileWrite($Log2, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "ALL_OK_!" & @TAB & $Versie)
    
ElseIf $ScreenChk = "OVWI02R" Then
   LogFileWrite($Log, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "OVWIO2R!" & @TAB & $Versie)
   LogFileWrite($Log2, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "OVWIO2R!" & @TAB & $Versie)
   Exit
Else
   LogFileWrite($Log, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "VKSCHRM!" & @TAB & $Versie)
   LogFileWrite($Log2, @CRLF & $Today & @TAB & $Time & @TAB & $Naam & @TAB & $Plaats & @TAB & "VKSCHRM!" & @TAB & $Versie)
EndIf

Func LogFileWrite($filename, $text)
  While 1
     If 1 = FileWrite($filename, $text) Then  ExitLoop 
     Sleep(300)
  Wend
EndFunc

I'm having a little trouble understanding what this function exactly does.

Can U explain it to me?

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