Logger Control

Logging API

Logger is an AHK module implementing small HTML logging API.  It allows you to use consistent logging between different projects.  Logger can display 3 types of messages - info, warning and error.  Each message can display icon, category, text and time in fully customizable manner.

In its current implementation Logger uses QHTM control and thus it depends on qhtm.ahk wrapper.  The module can be quickly changed to use other controls instead, for instance IE control.  This can be done by reimplementing 3 functions with WebControl prefix.

Summary
Logger ControlLogging API
AddAdd Logger control to the GUI.
ErrorAdd error message
WarningAdd warning message.
InfoAdd info message.
AutoAdd error, warning or info message.
ClearClears the Logger control.
OpenInBrowserOpens the content of the logger control in the default system browser.
SaveSaves the content of the logger control in the file.
SetSaveFileSet up the real time save file.
Example
About

Add

Log_Add(hGui,  
x,  
y,  
w,  
h,  
Handler = "")

Add Logger control to the GUI.

Parameters

GUIHandle of the parent.
X-HControl coordinates.
HandlerNotification function that fires when user clicks on the type icon (i.e. properties).  Function accepts two parameters - Type and Id.

Config

To setup Logger edit Logger_config or call Logger_Store(var, value) for dynamic set-up (for some values).  You can entirely delete Logger_config.ahk if you don’t need it.

You can omit any parameter you don’t use, in which case Logger will use default values.

bgGlobal background, by default “white”.
fgGlobal foreground, by default “black”.
fsizeGlobal font size, 1-7 for predefined sizes or number with ‘pt’ suffix (points).
isizeGlobal icon size, by default 16.
separatorHTML used to separate log entries, none by default.  If starts with *, it will separate only on type change, that is , messages from the same module will not be separated.
icongroupIcon suffix.  Icons are normally grabbed from the icons folder in the script directory.  Icons are named by type with eventual suffix.  Suffix can be used to quickly change icon group.
styleSpace separated list of styles for the control, can include ‘border’ and ‘transparent’.  None by default.
catwidthCategory width or if no category is used, separator between icons and text.
catalignCategory align, by default “left”.
tcategoryIf true, type will be set as category.  By default false.

The following parameters can be set for each type of message.  The possible message types are “info”, “warning” and “error” If not set, parameter will be set to the global one.

%type%_bgType background.
%type%_fgType foreground.
%type%_fsizeType font size.
%type%_isizeType icon size.
%type%_catfgType category foreground color.

Behavioral options :

saveHourN, hour at which to save the content of the Logger in an external file.  If starts with “*” content will be saved every N hours instead at Nth hour of the day.  By default empty, means that this functionality will be disabled.
logDirDirectory in which to save log files.  Has no meaning if saveHour is disabled.
timeFormatTime format used for log file names.  See FormatTime for details about the format.  Has no meaning if saveHour is disabled.
error_kw, warning_kwComa separated list of keywords for Auto function.

Remarks

The Logger will try to include ahk files it needs (Logger_config.ahk, qhtm.ahk) from the root of the script and from the “inc” folder.  This function makes one global, Log_hwnd, that keeps handle of the Logger.

Error

Log_Error(txt,  
category = "",
link = "")

Add error message

Parameters

txtText of the message.  It can contain references to AHK variables.
categoryOptional category.
linkOptional link.  Within application link handler can be set up to fire upon link clicks.  To let the user open the link with default browser return 1 from the handler function.  The primary use of this parameter is, however, in offline mode, i.e. when user is viewing the log in the local browser.  By default, link is set to “javascript:void(0)”.

Returns

Error number.

Remarks

Text can be any kind of HTML with usual meaning except for new line, tab and space characters.  Those will be transformed to their usual meaning.  Apart from mentioned, the text is normal HTML which means that you may want to additionally stylish the message.  In some occasions you may also want to replace special HTML chars - for instance ‘<’ or ‘>’ - with appropriate HTML entities.

Category parameter is optional.  Use it to the put the name of the module that posted a message.  If logger option tcategory is enabled, type will be set as category.  If you use this parameter make sure you set catwidth to appropriate value.

Warning

Log_Warning(txt,  
category = "",
link = "")

Add warning message.

Remarks

See Error function.

Info

Log_Info(txt,  
category = "",
link = "")

Add info message.

Remarks

See Error function.

Auto

Log_Auto(txt,  
category = "",
link = "")

Add error, warning or info message.

Remarks

By default, if it sees word “error” inside message it will call Error function (similarly for warnings).  You can add comma separated list of keywords in the config file.  Put error keywords in “error_kw” variable and warning fields in “warning_kw”.  If keywords are not matched, message will be posted as Info.

See Error function for other remarks.

Clear

Log_Clear()

Clears the Logger control.

OpenInBrowser

Log_OpenInBrowser()

Opens the content of the logger control in the default system browser.

Save

Log_Save(FileName,  
bAppend =  false)

Saves the content of the logger control in the file.

Parameters

FileNameFile name to save content to.
bAppendSet to true to append content to the file, by default false.

SetSaveFile

Log_SetSaveFile(FileName = "")

Set up the real time save file.

Parameters

FileNameIf non empty, this file will be used to write information as soon as it is added to the control.  To disable real time file saving, omit this parameter.  Its disabled by default.

Example

#SingleInstance force
    Gui, +LastFound
    hGui := WinExist()
    Log_Add( hGui, 0, 0, 600, 500)
    Gui, show, w600 h500

    Log_Info("This is some info"), s()
    Log_Error("Error ocured`nThis is the description of the error"), s()
    Log_Warning("This is warning"), s()

    Log_Auto("This is some info"), s()
    Log_Auto("Error ocured`nThis is the description of the error"), s()
    Log_Auto("This is warning"), s()

return

s() {
    Random, x, 1, 3
    Sleep, % x*1000
}

About

Log_Add(hGui,  
x,  
y,  
w,  
h,  
Handler = "")
Add Logger control to the GUI.
Log_Error(txt,  
category = "",
link = "")
Add error message
Log_Warning(txt,  
category = "",
link = "")
Add warning message.
Log_Info(txt,  
category = "",
link = "")
Add info message.
Log_Auto(txt,  
category = "",
link = "")
Add error, warning or info message.
Log_Clear()
Clears the Logger control.
Log_OpenInBrowser()
Opens the content of the logger control in the default system browser.
Log_Save(FileName,  
bAppend =  false)
Saves the content of the logger control in the file.
Log_SetSaveFile(FileName = "")
Set up the real time save file.
Close