Skip to content

Instantly share code, notes, and snippets.

@jbratu
Last active March 22, 2024 03:40
Show Gist options
  • Select an option

  • Save jbratu/00efcd51f2f0d946303d058f22dd6065 to your computer and use it in GitHub Desktop.

Select an option

Save jbratu/00efcd51f2f0d946303d058f22dd6065 to your computer and use it in GitHub Desktop.
Simple OpenInsight logging utility for recording log files from your code.
Function CS_Logger(instance, inMethod, Param1, Param2, Param3, Param4, outValue,outStat)
/*
Function: CS_Logger
>CS_Logger(instance, Param1, Param2, Param3, Param4, outValue,outStat)
Logging Utility.
Parameters:
instance - A previously created log object instance created With the 'Setup' method
inMethod - Varies depending On action.
Param1...Param4 - Varies depending On inMethod.
outValue - Unused. Optional Return value by reference.
outStat - Unused. Optional Return status by reference.
Example simple:
>LogObj = CS_Logger('','Setup')
>CS_Logger(LogObj, 'SetLogParams', 'C:\Revsoft\Temp', 'test log.txt')
>CS_Logger(LogObj, 'WriteEntry', 'First log entry!', 'INFO')
>CS_Logger(LogObj, 'Destroy')
Example Medium:
>LogObj = CS_Logger('','Setup')
>CS_Logger(LogObj, 'SetLogParams', '%TEMP%\your-tests', 'test log.txt', 0) ;* Write Logs Immediately
>CS_Logger(LogObj, 'WriteEntry', 'First log entry!', 'INFO')
>CS_Logger(LogObj, 'Destroy')
Example full:
>LogObj = CS_Logger('','Setup')
>LogPrefix = CS_Logger(LogObj, 'getDefaultFilePrefix')
>CS_Logger(LogObj, 'SetLogParams', 'C:\Revsoft\Temp', LogPrefix : ' test log.txt')
>CS_Logger(LogObj, 'SetLogType', 'STARTUP')
>CS_Logger(LogObj, 'WriteEntry', 'First log entry!', 'INFO')
>CS_Logger(LogObj, 'WriteEntry', 'Second log entry.', 'INFO')
>CS_Logger(LogObj, 'SetLogType', 'JOB')
>CS_Logger(LogObj, 'WriteEntry', 'First job task.', 'INFO')
>CS_Logger(LogObj, 'WriteEntry', 'Second job task.', 'INFO')
>CS_Logger(LogObj, 'WriteEntry', 'Job malfunction', 'ERROR')
>CS_Logger(LogObj, 'SetLogType', 'TERM')
>CS_Logger(LogObj, 'WriteEntry', 'Job error triggered exit', 'INFO')
>CS_Logger(LogObj, 'WriteEntry', 'Process ended.', 'ERROR')
>CS_Logger(LogObj, 'Destroy')
Revisions:
- History (Date, Initials, Notes)
2024-03-21 JAB-CS Add support For %TEMP% as a log destination.
2017-01-18 JAB-CS Default To file append instead of overwrite
2017-01-11 JAB-CS First release With documentation examples
*/
Declare Function Assigned, SendMessage, DCOUNT, Get_Status, RTI_OS_Directory, GetTickCount, CS_Logger
Declare Subroutine RTI_OS_Directory, Set_Status, Delete
$Insert Logical
instance = If Assigned(instance) Then instance Else ''
method = If Assigned(inMethod) Then inMethod Else ''
p1 = If Assigned(param1) Then param1 Else ''
p2 = If Assigned(param2) Then param2 Else ''
p3 = If Assigned(param3) Then param3 Else ''
p4 = If Assigned(param4) Then param4 Else ''
isNewInstance = False$
If Method EQ 'Setup' Then
*On setup method only assign an ID and return it to the calling program.
Gosub Setup_
isNewInstance = True$
End
Gosub initInstanceID_
*If this instance wasn't setup and we didn't call Setup then don't do anything
If instance EQ '' Then Return
CS_Logger_common = "CS_Logger_Instance_":instance
common //CS_Logger_common// init@, wlfeType@, logdir@, logfile@, wlfeWriteOffset@ ,wlfeOutputFileHandle@ ,DEBUG_LOG_BUFFER@, DEBUG_LOG_BUFFER_SIZE@, wlfeAppend@, wlfeUnused1@, wlfeUnused2@, wlfeUnused3@
Gosub postInitInstanceID_
outValue = ''
outStat = ''
retval = ''
DELIM = \09\ ;* TAB
Begin Case
Case Method _eqc "Setup" ; Gosub Setup_
Case Method _eqc "Destroy" ; Gosub Destroy_
Case Method _eqc 'getDefaultFilePrefix' ; Gosub getDefaultFilePrefix_
Case Method _eqc 'Flush' ; Gosub Flush
Case method _eqc "SetLogParams" ; Gosub SetLogParams_
Case method _eqc "setLogType" ; Gosub setLogType_
Case Method _eqc 'WriteEntry' ; Gosub WriteLogFileEntry_
Case Method _eqc "PostCriticalError" ; Gosub PostCriticalError
Case Otherwise$
Set_Status(1, "", "No method named " : inmethod)
Goto End
End Case
End:
Return retval
*
*
*
PostCriticalError:
error = p1
*Until I get something else to work
debug
Return
*
*
*
/*
interface: SetLogParams
Required parameters of log file such as where To log And what To name the file.
>CS_Logger(Instance, 'SetLogParams', LogDir, LogFileName, BufferSize)
Parameters:
Instance - A previously created log object instance created With the 'Setup' method
'SetLogParams' -
LogDir - Path To Write the log file
LogFileName - Name of the log file
BufferSize - If 0 logs are immediately written To disk, otherwise they are buffered And purged Until
size of log entries exceeds this buffer size.
Returns:
None
Example:
CS_Logger(LogObj, 'SetLogParams', 'C:\Revsoft\Temp', 'test log.txt', 0)
*/
SetLogParams_:
*p1 = Output Directory
*p2 = FileName
*p3 = Buffer
If p1 EQ "" Or p2 EQ "" Then
*
*Not optional parameters
*
debug
End
If p1[1,6] EQ '%TEMP%' Then
tempPath = RTI_OS_Directory( "GetWindowsTempPath" )
p1 = temppath : p1[7,Len(p1)]
End
If RTI_OS_Directory('EXISTS', p1) EQ '-1' Then
RTI_OS_Directory('CREATE', p1, True$)
*
* Folder must exist
*
If RTI_OS_Directory('EXISTS', p1) EQ '-1' Then
Set_Status("1", "", "Folder should exist by now, couldn't create")
Return
End
End
logdir@ = p1
logfile@ = p2
DEBUG_LOG_BUFFER_SIZE@ = p3
*assign a file name is none specified
If logfile@ EQ "" Then
Gosub getDefaultFilePrefix_
logfile@ = getDefaultFilePrefix_RetVal : "_log " : instance : ".txt"
End
wlfeAppend@ = True$ ;* Default to append mode
Return
Flush:
tmp = DEBUG_LOG_BUFFER_SIZE@
DEBUG_LOG_BUFFER_SIZE@ = 0
Call CS_Logger(instance, "WriteEntry", "Flushing Log...")
DEBUG_LOG_BUFFER_SIZE@ = tmp
Return
/*
interface: Setup
Create an instance of a logging object.
>CS_Logger('','Setup')
Parameters:
Instance - Empty string.
Method - 'Setup'
Returns:
Handle To a logging object.
Example:
LogObj = CS_Logger('','Setup')
*/
Setup_:
common /csRnd/ WScript, init%
If Assigned(init%) Else init% = ''
If init% else
WScript = oleCreateInstance("ScriptControl")
OlePutProperty(wscript, "Language", "JScript")
s = 'function getrandom() {return Math.random();}'
x = WScript->AddCode(s)
init% = 1
end
rndSeed = WScript->Eval("getrandom();")
thisInstance = rndSeed[3,Len(rndSeed)] : '_' : GetTickCount()
instance = thisInstance
retval = thisInstance
return
/*
interface: Destroy
Stop logging And close the log file.
>CS_Logger(Instance, 'Destroy')
Parameters:
Instance - A previously created log object instance created With the 'Setup' method
'Destroy'-
Returns:
None
Notes:
Any pending log entries In the buffer are purged.
Example:
CS_Logger(LogObj, 'Destroy')
*/
Destroy_:
Gosub Flush
hnd = wlfeOutputFileHandle@
OSClose hnd
init@ = ''
wlfeType@ = ''
logdir@ = ''
logfile@ = ''
wlfeWriteOffset@ = ''
wlfeOutputFileHandle@ = ''
DEBUG_LOG_BUFFER@ = ''
DEBUG_LOG_BUFFER_SIZE@ = ''
Return
**
**end: Destroy
**
/*
interface: WriteEntry
Write an entry To the log file linked To the current log instance.
>CS_Logger(Instance, 'WriteEntry', Msg, Severity)
Parameters:
Instance - A previously created log object instance created With the 'Setup' method
'WriteEntry' -
Msg - Log file message To Write
Severity - Optional importance indicator of log message such as:
EMERGENCY
ALERT
CRITICAL
ERROR
WARNING
NOTICE
INFO
DEBUG
Returns:
None
Example:
CS_Logger(LogObj, 'WriteEntry', 'First log entry!', 'INFO')
*/
WriteLogFileEntry_:
*p1 logentry
wlfeLogEntry = p1
If p2 EQ '' Then
Severity = 'INFO'
End Else
Severity = p2
End
If p3 EQ '' Then
Type = wlfeType@
End Else
Type = p3
End
wlfeFileName = logfile@
If logfile@ EQ "" Or wlfeLogEntry EQ "" Then
Return
End
If wlfeWriteOffset@ = "" Then
*We haven't written to the log before so create a new file or figure
*the append position
logDir = logdir@
If logDir[-1,1] EQ '\' Then
hnd_fname = logDir : wlfeFileName
End Else
hnd_fname = logDir : '\' : wlfeFileName
End
hnd_fname_data = Dir(hnd_fname)
hnd_fname_size = hnd_fname_data<1>
If wlfeAppend@ = True$ And hnd_fname_size > 0 Then
*Don't overwrite file because we are in append mode and file has data
wlfeWriteOffset@ = hnd_fname_size
End Else
*Log file is in overwrite mode or didn't have any data
oswrite "" To hnd_fname ; * create the new file
error = status()
If error NE 0 Then
debug
End
wlfeWriteOffset@ = 0
End
End
wlfeLogEntry = TimeDate() : DELIM : Type : DELIM : Severity : DELIM : wlfeLogEntry : \0D0A\
hnd = wlfeOutputFileHandle@
If hnd EQ "" Then
*File hasn't been opened either so open it now
logDir = logdir@
If logDir[-1,1] EQ '\' Then
hnd_fname = logDir : wlfeFileName
End Else
hnd_fname = logDir : '\' : wlfeFileName
End
OSOpen hnd_fname To hnd Else
error = status()
debug
End
wlfeOutputFileHandle@ = hnd
End
tmpLogEntry = DEBUG_LOG_BUFFER@ : wlfeLogEntry
tmpLogEntryLen = Len(tmpLogEntry)
If (tmpLogEntryLen) > DEBUG_LOG_BUFFER_SIZE@ Then
hnd = wlfeOutputFileHandle@
OSBWrite tmpLogEntry On hnd At wlfeWriteOffset@
DEBUG_LOG_BUFFER@ = ""
wlfeWriteOffset@ = wlfeWriteOffset@ + tmpLogEntryLen
End Else
DEBUG_LOG_BUFFER@ = DEBUG_LOG_BUFFER@ : wlfeLogEntry
End
Return
initInstanceID_:
If instance EQ '' Then
*This instance isn't setup, don't do anything
instance = ''
End
Return
postInitInstanceID_:
If isNewInstance EQ True$ Then
wlfeType@ = 'GENERAL'
init@ = true$
End
Return
/*
interface: SetLogType
Assign all entries written To this log To be a certain type. This can be changed throughout the course of the
use of the logging objects use.
>CS_Logger(Instance, 'SetLogType', Type)
Parameters:
Instance - A previously created log object instance created With the 'Setup' method
'SetLogType' -
Type - A string containing the type of log entries.
Returns:
None
Example:
Indicate all subsequent log entries are type 'SCHED'
CS_Logger(LogObj, 'SetLogType', 'SCHED')
*/
setLogType_:
wlfeType@ = p1
Return
/*
interface: getDefaultFilePrefix
Get a file prefix For all log files. Defaults To date time suitable For
a file name.
>CS_Logger(LogObj,'getDefaultFilePrefix')
Parameters:
Instance - Empty string.
Method - 'getDefaultFilePrefix'
Returns:
A string representing the current Month Day Year And Time
Example:
LogPrefix = CS_Logger(LogObj, 'getDefaultFilePrefix')
*/
getDefaultFilePrefix_:
TD = DATE()
T = time()
* get day of the week to name report
DAY=MOD(TD,7)
TDAY=OCONV(TD,'D2-')[4,2]
YR=OCONV(TD,'D2-')[7,2]
MO=OCONV(TD,'D2-')[1,2]
TM = Oconv(T, 'MTH')
Convert ":" To "" In TM
*
RetVal = MO:TDAY:YR:'_':TM
getDefaultFilePrefix_RetVal = RetVal
Return
/*
#AUTO_GENERATE_DOCS
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment