/**************************************************************************/ /* */ /* ________ */ /* ()_______) */ /* \:::::::\ Make your mark on open source, let people know */ /* \:::::::\ what you can do, profit on reputation. */ /* @:::::::/ */ /* ~~~~~~~ */ /* ---------------------------------------------------------------------- */ /* 2009 Aug 07 Scott Auge sauge@amduus.com scott_auge@yahoo.com */ /* Amduus Information Works, Inc. */ /* http://www.amduus.com */ /* */ /* Initial development */ /* */ /* ---------------------------------------------------------------------- */ /* */ /**************************************************************************/ /**************************************************************************/ /* PLEASE READ */ /* ______ */ /* .-" "-. */ /* / \ */ /* _ | | _ */ /* ( \ |, .-. .-. ,| / ) */ /* > "=._ | )(__/ \__)( | _.=" < */ /* (_/"=._"=._ |/ /\ \| _.="_.="\_) */ /* "=._ (_ ^^ _)"_.=" */ /* "=\__|IIIIII|__/=" */ /* _.="| \IIIIII/ |"=._ */ /* _ _.="_.="\ /"=._"=._ _ */ /* ( \_.="_.=" `--------` "=._"=._/ ) */ /* > _.=" "=._ < */ /* (_/ \_) */ /**************************************************************************/ /* REQUIRES 10.1C OR BETTER */ /**************************************************************************/ /* The properties are remembered on each run, but stream closes auto- */ /* matically when the top level program exits. */ /**************************************************************************/ /* If you nest deeper, be sure program-name() in PrintPreamble() is set */ /* so the actual program's name is being shown and not this one's. */ /**************************************************************************/ class TroubleShooter: define public static property IsFileOpen as logical init false no-undo get. private set. define public static property LogFile as character init "" no-undo get. set. define public static property LogLevel as integer init 0 no-undo get. set. define public static property LogKeywords as character init "" no-undo get. set. define public static property DateFormat as character init "99-99-9999" no-undo get. set. define public static property TimeFormat as character init "hh:mm:ss" no-undo get. set. define public static property ShowDate as logical init true no-undo get. set. define public static property ShowTime as logical init true no-undo get. set. define public static property ShowProgramName as logical init true no-undo get. set. define public static property DoAppend as logical init true no-undo get. private set. define private stream LogFile. /**************************************************************************/ /* Open the file up and determine if appending or not. */ /**************************************************************************/ method public static void OpenFile(input DoAppend as logical): if IsFileOpen then CloseFile(). if DoAppend then output stream LogFile to value(LogFile) append. else output stream LogFile to value(LogFile). IsFileOpen = true. end. /* OpenFile () */ /**************************************************************************/ /* We are done with this file! */ /**************************************************************************/ method public static void CloseFile(): output stream LogFile close. IsFileOpen = false. end. /* CloseFile () */ /**************************************************************************/ /* Print the message without a crlf (useful for combinations on the same */ /* line. */ /**************************************************************************/ method public static void Print (input MsgText as character): PrintPreamble(). put stream LogFile unformatted MsgText. end. /* Print () */ method public static void Print (input RequestKeywords as character, input RequestLogLevel as integer, input MsgText as character): if not LogLevelOK (RequestLogLevel) then return. if not KeywordOK (RequestKeywords) then return. PrintPreamble(). put stream LogFile unformatted MsgText. end. /* Print () */ /**************************************************************************/ /* Print the log entry with a crlf at the end. */ /**************************************************************************/ method public static void PrintLn (input MsgText as character): Print(MsgText). put stream LogFile skip. end. /* PrintLn () */ method public static void PrintLn (input RequestKeywords as character, input RequestLogLevel as integer, input MsgText as character): if not LogLevelOK (RequestLogLevel) then return. if not KeywordOK (RequestKeywords) then return. Print(MsgText). put stream LogFile skip. end. /* PrintLn () */ /**************************************************************************/ /* Convenient seperator. */ /**************************************************************************/ method public static void Seperater (): PrintLn ("------------------------------------"). end. /* Seperator */ /**************************************************************************/ /* What kind of information should be placed before the message. */ /**************************************************************************/ method private static void PrintPreamble (): if ShowDate then put stream LogFile unformatted string(today, DateFormat) " ". if ShowTime then put stream LogFile unformatted string (time, TimeFormat) " ". if ShowProgramName then put stream LogFile unformatted program-name(4) " ". end. /* PrintPreamble () */ /**************************************************************************/ /* Does the log entry requested match the keywords to allow entries for. */ /**************************************************************************/ method private static logical KeywordOK (input KeyWordsToCheck as character): define variable CurrentKeyword as character no-undo. define variable i as integer no-undo. do i = 1 to num-entries (KeywordsToCheck): CurrentKeyword = entry(i, KeywordsToCheck). if can-do (LogKeywords, CurrentKeyword) then return true. end. /* do */ end. /* KeywordMatch () */ /**************************************************************************/ /* Determine if the log level of the requested log entry is appropriate */ /* to the log level requested to track at. */ /**************************************************************************/ method private static logical LogLevelOK (input RequestLogLevel as integer): return RequestLogLevel < LogLevel. end. /* LogLevelOK */ end. /* class TroubleShooter */ /********************************************************************* Unit Test TroubleShooter:LogFile = "c:\tmp\a.log". TroubleShooter:OpenFile(TroubleShooter:DoAppend). TroubleShooter:PrintLn ("Test"). TroubleShooter:CloseFile(). TroubleShooter:LogFile = "c:\tmp\a.log". TroubleShooter:OpenFile(not TroubleShooter:DoAppend). TroubleShooter:PrintLn ("not doing append"). TroubleShooter:ShowDate = false. TroubleShooter:PrintLn ("No date"). TroubleShooter:CloseFile(). display TroubleShooter:LogFile. TroubleShooter:LogFile = "c:\tmp\a.log". TroubleShooter:OpenFile(not TroubleShooter:DoAppend). TroubleShooter:PrintLn ("not doing append"). TroubleShooter:ShowDate = false. TroubleShooter:PrintLn ("No date"). TroubleShooter:Seperater(). run TestSubRoutineForTroubleShooter.p. TroubleShooter:Seperater(). TroubleShooter:CloseFile(). TroubleShooter:LogFile = "c:\tmp\a.log". TroubleShooter:LogLevel = 1. TroubleShooter:LogKeywords = "credit,sales". TroubleShooter:OpenFile(not TroubleShooter:DoAppend). TroubleShooter:PrintLn ("Test"). TroubleShooter:PrintLn ("web", 1, "Should not appear 1"). TroubleShooter:PrintLn ("credit", 1, "Should not appear 2"). TroubleShooter:LogLevel = 3. TroubleShooter:PrintLn ("sales", 1, "Should appear 1"). TroubleShooter:PrintLn ("credit", 1, "Should appear 2"). TroubleShooter:PrintLn ("sales", 4, "Should not appear 3"). TroubleShooter:CloseFile(). *********************************************************************/