iTech Logging Source Code Snippets
The 2nd generation logging solution
Source code snippets for iTech Logging (Microsoft Visual Basic)
ITLogLib
Initializing and deinitializing your application without main form |
|
ITLogMsg
Service functions
Opening ITLogBook out of your application with current logfiles |
|
Opening ITConfigManager out of your application with the current configuration file |
ITLogLib - Initializing your Windows application
ITLogLib must be initialized with a configuration file. This file can be created and edited by ITConfigManager, a special editor shipped with iTech Logging. The parameter application name is user-defined and identifies your application. Initialization fails if the configuration file could not be found.
After successful initialization the standard output channel StdLog is opened and ready to receive logmessages. Additional output channels should be opened within Form_Load.
See also: ITLogLib - Opening an additional logical output channel.
Private Sub Form_Load()
' Initialize the global ITLogLib2 object (for
evaluation use empty LicenseKey string)
If Not
(ITLogLib2.Initialize("MyApplication",
"ConfigFileName.icf", "LicenseKey"))
Then
MsgBox ITLogLib2.LastErrorDescription, vbOKOnly, "Error"
End If
End Sub
ITLogLib - Deinitializing your Windows application
Before your application exits, ITLogLib should be deinitialized in order to write out all buffered logmessages and close all opened output channels.
Private Sub Form_Unload(Cancel
As Integer)
' Deinitialize the global ITLogLib2 object (forces
writing out buffered logmessages)
ITLogLib2.Deinitialize
End Sub
ITLogLib - Initializing and deinitializing your application without main form
See also:
ITLogLib - Initializing your Windows application
ITLogLib - Deinitializing your Windows application
Sub Main()
' Initialize the global ITLogLib2 object (for evaluation
use empty LicenseKey string)
If Not
(ITLogLib2.Initialize("MyApplication",
"ConfigFileName.icf", "LicenseKey"))
Then
MsgBox ITLogLib2.LastErrorDescription, vbOKOnly, "Error"
End If
...
' Deinitialize the global ITLogLib2 object (forces
writing out buffered logmessages)
ITLogLib2.Deinitialize
End Sub
ITLogLib - Opening an additional logical output channel
Besides the standard output channel StdLog additional application specific logical output channels can be opened. For each output channel an appropriate set of configuration parameters must exist in the configuration file, otherwise OpenLogChannel() fails. Use ITConfigManager to create or edit the parameter set. (See also: ITLogLib - Initializing your Windows application).
It's recommended to open all used output channel within Form_Load() for window applications or Main() for applications without a main forms.
' Open
additional logchannel 'DeveloperLog'
If
OpenLogChannel("DeveloperLog")
Is Nothing Then
Dim LogMsg As New
ITLogMsg
LogMsg.Create "Application", "Initialize", LEVEL_ERROR
LogMsg.Add "Can't open logchannel 'DeveloperLog'"
LogMsg.Add vbCrLf
LogMsg.Add ITLogLib2.LastErrorDescription
End If
ITLogLib - Closing a logical output channel
Closing a logical output channel forces writing out its buffered logmessages. If you deinitialize ITLogLib all opened output channels will be closed automatically.
The standard output channel StdLog can’t be closed explicitly by user.
' Close
additional logchannel 'DeveloperLog'
If
LogChannel("DeveloperLog")
Is Nothing Then
QuickLog "Application", "Deinitialize", "Can't close logchannel 'DeveloperLog'", LEVEL_WARNING
Else
CloseLogChannel
"DeveloperLog"
End If
ITLogMsg - The simplest way to protocol
The easiest way to protocol is using QuickLog.
' Use default
loglevel (LEVEL_INFO) and standard logchannel (StdLog)
QuickLog "Application",
"Deinitialize", "Program terminated"
' Use
loglevel WARNING
QuickLog "Application", "Deinitialize", "Program terminated", LEVEL_WARNING
' Use
loglevel WARNING and logchannel 'DeveloperLog'
QuickLog "Application", "Deinitialize", "Program terminated", LEVEL_WARNING, "DeveloperLog"
ITLogMsg - The standard way to protocol
Logmessages can be created anywhere in your applications code and passed as parameters to subsequent functions calls. Its interface provides different stream operators for C++ base types, which allow a comfortable extension of their logmessage texts. The loglevel (info, warning, error and alarm) can be supplemented during the life cycle of a logmessage as well.
A logmessage will be send automatically to its output channel(s), as far as you didn’t explicitly send it or suppress its output.
Dim LogMsg As New
ITLogMsg
Dim bVal As Boolean
Dim iVal As Integer
Dim dVal As Double
Dim sVal As String
bVal = False
iVal = 42
dVal = 99.99
sVal = "logmessage text string"
' Create the logmessage
LogMsg.Create "MyModule", "MyCase", LEVEL_INFO, "DeveloperLog"
' Loglevel and logchannel are optional
' Add logmessage text
LogMsg.Add "boolean: " & bVal & " "
LogMsg.Add "integer: " & iVal & " double: " & dVal
LogMsg.Add vbCrLf & sVal
' Add loglevel
LogMsg.AddLogLevel LEVEL_ERROR
' LogMsg will be sent automatically to its logchannel
at the end of its lifecycle
ITLogMsg - Writing to a variable output channel
You can create logmessages dynamically using the create function of its output channel.
' Logging to variable logchannel
Private Sub LogState(OutputChannel As ITOutputLogChannel)
Dim LogMsg As ITLogMsg
Set LogMsg =
OutputChannel
.CreateLogMsg("Statistics", "State")
LogMsg.Add "State=" & 42
End Sub
'
Call the function
Dim OutputChannel
As ITOutputLogChannel
Set
OutputChannel = LogChannel("DeveloperLog")
If Not (OutputChannel
Is
Nothing) Then
LogState
OutputChannel
End If
ITLogMsg - Conditional creation of logmessages
As soon as a logmessage variable is declared, the complete ITLogMsg interface can be used, without producing runtime errors. Subsequent calls to its interface functions simply will be ignored as long as the object is not created. Thus you have the possibility of conditional creation of logmessages.
' Conditional creation of logmessages
Dim ErrorLogMsg As New ITLogMsg
' The ITLogMsg interface can be used at this stage, but changes to ErrorLogMsg will be ignored.
If Not CheckState() Then
ErrorLogMsg.Create "Statistics", "State", LEVEL_ERROR
ErrorLogMsg.Add "Check failed "
End If
' ErrorLogMsg will only be sent, if above CheckState function failed.
ErrorLogMsg.Send
ITLogMsg - Explicit sending of logmessages
Logmessages can be sent explicitly, e.g. depending on a condition within your program code. Subsequent calls to its interface don’t result in a runtime error but will simply be ignored.
' Explicit sending of logmessages
Dim LogMsg As New ITLogMsg
LogMsg.Create "Statistics", "State"
If Not CheckState() Then
LogMsg.Add "Check failed"
LogMsg.AddLogLevel LEVEL_ERROR
LogMsg.Send
End If
' Additional changes to LogMsg will be ignored, if above CheckState function failed.
ITLogMsg - Explicit cancelling of logmessages
Logmessages can be suppressed explicitly, e.g. depending on a condition within your program code. Subsequent calls to its interface don’t result in a runtime error but will simply be ignored.
' Cancel logmessages
Dim LogMsg As ITLogMsg
LogMsg.Create "Statistics", "State"
If Not
CheckState()
Then
LogMsg.Cancel '
Send only in case of success
End If
' Additional changes to LogMsg will be ignored, if above CheckState function failed.
LogMsg.Send
ITLogMsg - Optimizing performance with active output filters
Using the property WillBeLogged avoids expensive calls to protocol functions if the logmessage will not be written out later, e.g. if you did set up an appropriate write-out filter, or did explicitly cancel (LogMsg.Cancel) or send (LogMsg.Send) the logmessage before. (‘Expensive’ means collecting lots of information and append it to the logmessage text.)
' Using the property WillBeLogged
If LogMsg.WillBeLogged Then
LogMsg.Add Me.FullDump '
expensive function
End If
Service function - Opening ITLogBook out of your application with current logfiles
You may define a (hidden) button in your applications interface, that opens ITLogBook with the current logfile of a given output channel, thus your developer or end user always have comfortable access to the most recent log information. (Therefore iTech logging must be set up on the target system, in order to attach the “ilf” file extension to the ITLogBook application.
'
Open ITLogBook with current logfile
Dim OutputChannel
As ITOutputLogChannel
Set OutputChannel =
LogChannel("DeveloperLog")
If OutputChannel Is
Nothing Then
MsgBox "Can't open
ITLogBook - Logchannel 'DeveloperLog' not open", vbOKOnly, "Warning"
ElseIf OutputChannel.CurrentLogFile = "" Then
MsgBox "Can't open ITLogBook - Logchannel 'DeveloperLog' still empty", vbOKOnly, "Warning"
Else
Shell "cmd /C start " & OutputChannel.CurrentLogFile, vbNormalFocus
End If
You may define a (hidden) button in your applications interface, that opens ITConfigManager with the current configuration file, thus your developer or end user always have comfortable access to the current parameters, defining the output behavior. (Therefore iTech logging must be set up on the target system, in order to attach the “icf” file extension to the ITConfigManager application.
' Open ITConfigManager with application's current configfile
If ITLogLib2.IsInitialized
Then
Shell "cmd /C start " & ITLogLib2.ConfigFile, vbNormalFocus
End If
Copyright © 1999 - 2004 iTech Software GmbH