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.
Logger Control | Logging API |
Add | Add Logger control to the GUI. |
Error | Add error message |
Warning | Add warning message. |
Info | Add info message. |
Auto | Add error, warning or info message. |
Clear | Clears the Logger control. |
OpenInBrowser | Opens the content of the logger control in the default system browser. |
Save | Saves the content of the logger control in the file. |
SetSaveFile | Set up the real time save file. |
Example | |
About |
Log_Add( hGui, x, y, w, h, Handler = "" )
Add Logger control to the GUI.
GUI | Handle of the parent. |
X-H | Control coordinates. |
Handler | Notification function that fires when user clicks on the type icon (i.e. properties). Function accepts two parameters - Type and Id. |
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.
bg | Global background, by default “white”. |
fg | Global foreground, by default “black”. |
fsize | Global font size, 1-7 for predefined sizes or number with ‘pt’ suffix (points). |
isize | Global icon size, by default 16. |
separator | HTML 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. |
icongroup | Icon 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. |
style | Space separated list of styles for the control, can include ‘border’ and ‘transparent’. None by default. |
catwidth | Category width or if no category is used, separator between icons and text. |
catalign | Category align, by default “left”. |
tcategory | If 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%_bg | Type background. |
%type%_fg | Type foreground. |
%type%_fsize | Type font size. |
%type%_isize | Type icon size. |
%type%_catfg | Type category foreground color. |
Behavioral options :
saveHour | N, 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. |
logDir | Directory in which to save log files. Has no meaning if saveHour is disabled. |
timeFormat | Time format used for log file names. See FormatTime for details about the format. Has no meaning if saveHour is disabled. |
error_kw, warning_kw | Coma separated list of keywords for Auto function. |
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.
Log_Error( txt, category = "", link = "" )
Add error message
txt | Text of the message. It can contain references to AHK variables. |
category | Optional category. |
link | Optional 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)”. |
Error number.
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.
Log_Auto( txt, category = "", link = "" )
Add error, warning or info message.
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.
#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 }
Add Logger control to the GUI.
Log_Add( hGui, x, y, w, h, Handler = "" )
Add error message
Log_Error( txt, category = "", link = "" )
Add warning message.
Log_Warning( txt, category = "", link = "" )
Add info message.
Log_Info( txt, category = "", link = "" )
Add error, warning or info message.
Log_Auto( txt, category = "", link = "" )
Clears the Logger control.
Log_Clear()
Opens the content of the logger control in the default system browser.
Log_OpenInBrowser()
Saves the content of the logger control in the file.
Log_Save( FileName, bAppend = false )
Set up the real time save file.
Log_SetSaveFile( FileName = "" )