WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Rolling Logfile UDF


I thought I'd post this UDF, just in case any one else finds it useful. Feel free to edit/modify it etc.

I use it for keeping logs of certain scripts etc, while keeping the log size to a minimum by deleting old entries

;***************************************************************************
;** UDF for creating a rolling log file
;** Log file will be created and new entries appended until maximum size is reached
;** then oldest entries will be discarded and new entries appended
;** Created by D. Bailey
;** Version 1
;** Inputs: Locallogfile=filename for log, a new file will be created if it doesn't exist, otherwise it will be appended
;** LogSubject=Identifier in the log file (maximum 20 characters)
;** LogMsg=Subject to write in the log
;** onoff=values 0=off 1=0n, enables logging to be switched on/off if you have multiple log calls in a script
;** returns=always 0
;***************************************************************************


#DefineFunction F_ROLLING_LOGFILE(LocalLogFile,LogSubject,LogMsg, onoff)
If onoff==0 then return 0;Don't write log = enables log calls to be switched on/off
MaxLogfileSize=4000 ;maximum size of log file(in bytes) -will be approximated
a = TimeDate()
Log_time = strcat(a,": ")
subject = Strfix(LogSubject," ",20) ; maximum of 20 characters for label in logfile
final_Msg = strcat(Log_time," ", subject, LogMsg)
MsgSize=StrLen(final_Msg )

;allocate buffer and read in existing log
IF fileexist(locallogfile)>=1
Logfilesize=FileSize (LocalLogFile)
binbuf=Binaryalloc (logfilesize+MsgSize+2);account for addition of @CRLF 
BinaryRead(binbuf, locallogfile)
Else 
Logfilesize=0
binbuf=Binaryalloc (logfilesize+MsgSize+2)
Endif

;when log = maxmimum size discard old log data
;equivalent to the length of input message
;plus the rest of the remaining text line
If logfilesize+ MsgSize >= MaxLogfileSize
position=BinaryIndexEx(binbuf, MsgSize,@CRLF, @FWDSCAN, 0)
if position ==-1 then position=0 ;use start of binbuf
Else position=position+2
binbuf2=Binaryalloc (logfilesize+MsgSize)
BinaryCopy(binbuf2,0, binbuf, position, logfilesize-position)
Binarypokestr(binbuf2, BinaryEodGet(binbuf2), @CRLF)
Binarypokestr(binbuf2, BinaryEodGet(binbuf2), final_Msg )
BinaryWrite(binbuf2, locallogfile)
Binaryfree(binbuf2)
Else;for new logs, or logs below maximum size limit just append data to existing file
Binarypokestr(binbuf, BinaryEodGet(binbuf), @CRLF)
Binarypokestr(binbuf, BinaryEodGet(binbuf), final_Msg)
BinaryWrite(binbuf, locallogfile)
EndIF
Binaryfree(binbuf)
return 0
#EndFunction
;*******************************************************************
;***** End of function F_ROLLING_LOGFILE 
;*******************************************************************



;Test the UDF
;define the logfile
wbtdir = DirScript()
LOG=STRCAT(wbtdir, "TESTLOG.TXT")
logfileon=1 ;switch on log, change values to 0 to switch off logging

for a =1 to 50 ;populate log file with test entries
F_ROLLING_LOGFILE(Log,"Log Entry","Test entry %a%", logfileon)
Next

exit

Article ID:   W16701
File Created: 2005:02:18:12:21:54
Last Updated: 2005:02:18:12:21:54