• Welcome to Valhalla Legends Archive.
 

Log time with milliseconds

Started by Grok, December 15, 2003, 11:18 AM

Previous topic - Next topic

Grok

I use a single procedure to write log files.
My old WriteLog used to produce:

10:28:09  DatabaseFile::ConnectToDatabase() entered.
10:28:09  DatabaseFile::ConnectToDatabase() successful, connected.
10:28:09  ERROR DatabaseFile::SetupForValidation() - -2147211402-The requested item is not in the collection.
10:28:15  DatabaseFile::ConnectToDatabase() entered.
10:28:15  DatabaseFile::ConnectToDatabase() successful, connected.
10:28:15  ERROR DatabaseFile::SetupForValidation() - -2147211402-The requested item is not in the collection.

When just HH:MM:SS was not sufficient, I had to
upgrade WriteLog().  Now it reports milliseconds.

12:13:59.0281  *********
12:13:59.0571  
12:14:00.0152  *********
12:14:00.0312  
12:14:00.0633  *********

Here's the VB code for whoever cares.


Private Declare Sub GetLocalTime Lib "Kernel32" (lpSystemTime As SYSTEMTIME)

Private Type SYSTEMTIME
   wYear As Integer
   wMonth As Integer
   wDayOfWeek As Integer
   wDay As Integer
   wHour As Integer
   wMinute As Integer
   wSecond As Integer
   wMilliseconds As Integer
End Type
Private st As SYSTEMTIME

Public Sub WriteLog(ByVal msg As String)
   
   On Error GoTo WriteLogErr
   
   Dim F As Integer
   Dim fn As String
   Dim sTime As String
   GetLocalTime st
   sTime = Format(st.wHour, "00") & ":" & Format(st.wMinute, "00") & ":" & _
           Format(st.wSecond, "00") & "." & Format(st.wMilliseconds, "0000")
   fn = "C:\TEMP\" & Format$(Now, "YYYYMMDD") & ".txt"
   F = FreeFile
   Open fn For Append As #F
   Print #F, sTime & "  " & msg
   Close #F
'    Timer1.Interval = CInt(Right(CStr(st.wMilliseconds), 3)) + 1
   DoEvents
   
WriteLogExit:
   Exit Sub
   
WriteLogErr:
   Debug.Print Err.Description
   Resume WriteLogExit
   
End Sub


Spht

#1
Quote from: Grok on December 15, 2003, 11:18 AM    sTime = Format(st.wHour, "0") & ":" & Format(st.wMinute, "0") & ":" & _
           Format(st.wSecond, "0") & "." & Format(st.wMilliseconds, "00")

Removed unneeded zero's since st structure contains all numbers (so can never return an empty string).

Edit - Took off another zero for wMilliseconds since it'll never be >999.

Grok

Quote from: Spht on December 15, 2003, 11:25 AM
Quote from: Grok on December 15, 2003, 11:18 AM    sTime = Format(st.wHour, "0") & ":" & Format(st.wMinute, "0") & ":" & _
           Format(st.wSecond, "0") & "." & Format(st.wMilliseconds, "00")

Removed unneeded zero's since st structure contains all numbers (so can never return an empty string).

Edit - Took off another zero for wMilliseconds since it'll never be >999.

Before correcting my code, please test your corrections.  I specifically used those format strings in order to pad the hour, minute, second, and milliseconds appropriately.  I did have one extra 0 in milliseconds, but everything else was correct.

Spht

#3
Quote from: Grok on December 15, 2003, 01:01 PM
Quote from: Spht on December 15, 2003, 11:25 AM
Quote from: Grok on December 15, 2003, 11:18 AM    sTime = Format(st.wHour, "0") & ":" & Format(st.wMinute, "0") & ":" & _
           Format(st.wSecond, "0") & "." & Format(st.wMilliseconds, "00")

Removed unneeded zero's since st structure contains all numbers (so can never return an empty string).

Edit - Took off another zero for wMilliseconds since it'll never be >999.

Before correcting my code, please test your corrections.  I specifically used those format strings in order to pad the hour, minute, second, and milliseconds appropriately.  I did have one extra 0 in milliseconds, but everything else was correct.

Sorry about that, I thought you were using Right() for some reason...

Dim a As Long
a = 0
Debug.Print Right("00" & a, 2)
Debug.Print Right("0" & a, 2)
a = 10
Debug.Print Right("00" & a, 2)
Debug.Print Right("0" & a, 2)


Extra zero unneeded.

UserLoser.

#4
Quote from: Spht on December 15, 2003, 01:36 PM
Sorry about that, I thought you were using Right() for some reason...

Sounds like what I did when you yelled at me (right & extra 0's) :P

Also, sometimes wHour returns 0 when it's 6:00 PM on my system, why is that?

Grok

Moved Andreas Strobl's code for VB.NET to the .NET forum.