Ńņ ^e1?c@sPdZddkZddkZdZdZddd„Zddd„ƒYZdS( sŪ log.py: Error logging module for Python. $Revision: 1.7 $ $Date: 2002/06/22 04:08:54 $ Exports: Log ( fileName=None, prefix=None ): [ if (fileName is a string or None) and (prefix is a string or None) -> if Log() has never been instantiated -> return a new, singleton instance of Log(), logging to fileName if given, using prefix (prefix) if given, defaulting to DEFAULT_PREFIX else -> return the singleton instance ] .addLogFile ( fileName ): [ if fileName is a string -> if fileName names a file that can be opened for writing -> self := self with fileName added to its list of log files else -> self := self with an error logged because we can't open (fileName) raise IOError ] .setPrefix ( prefix ): [ if prefix is a string -> self := self with its prefix changed to (prefix) ] .msgKind ( kind, text, ... ): [ if self has logged no messages of type (kind) -> self := self with a count of 1 message of type (kind) else -> self := self with one more message of type (kind) In any case -> +:= (self's prefix) + (kind) + ": " + (concatenation of all (text) arguments) + " " (self's log files) +:= ] .error ( text, ... ): [ if self has logged no messages of type "Error" -> self := self with a count of 1 message of type "Error" else -> self := self with one more message of type "Error" In any case -> +:= (self's prefix) + ("Error") + ": " + (concatenation of all (text) arguments) + " " (self's log files) +:= ] .warning ( text, ... ): [ if self has logged no messages of type WARNING_KIND -> self := self with a count of 1 message of that kind else -> self := self with one more message of that kind In any case -> +:= (self's prefix) + WARNING_KIND + ": " + (concatenation of all (text) arguments) + " " (self's log files) +:= ] .count ( kind="Error" ): [ if self has logged no messages of type (kind) -> return 0 else -> return the number of messages self has logged of type (kind) ] .fatal ( text, ... ): [ +:= (self's prefix) + ("Fatal error: ") + (concatenation of all (text) arguments) + " " (self's log files) +:= stop execution ] .message ( text, ... ): [ +:= (self's prefix) + (concatenation of all (text) arguments) + " " (self's log files) +:= ] .write ( text, ... ): [ +:= (concatenation of all (text) arguments) + " " (self's log files) +:= ] .revision [ the RCS revision string ] .date [ the RCS date string ] Features: * All messages are written to sys.stderr * All messages are also written to any number of additional log files that callers want written * Messages are classified according to a ``message kind'' code (e.g., errors, warnings), and callers can ask the object how many messages of a given kind have been logged * A characteristic prefix, defaulting to "*** ", is prefixed to each output line. This module is an example of the Singleton pattern, as described in: Gamma, Erich; Richard Helm; Ralph Johnson; John Vlissides. Design patterns: elements of reusable object-oriented software. Addison-Wesley, 1994, ISBN 0-201-63361-2. In practice, this means that all instantiations are the identical object. The singleton object is class _Log; use the Log() function as the actual constructor. Written by John W. Shipman (john@nmt.edu), New Mexico Tech Computer Center, Socorro, NM 87801. i’’’’NtErrortWarningcCsWtiptƒt_n|otii|ƒn|otii|ƒntiS(sE [ if (fileName is a string or None) and (prefix) is a string or None) -> return the singleton _Log object, logging to fileName (unless fileName is None), and with prefix (prefix) (unless prefix is None, in which case the prefix is _Log.DEFAULT_PREFIX) ] (t_Logtinstancet addLogFilet setPrefix(tfileNametprefix((s(/u/www/docs/tcc/projects/pystyler/log.pytLogss RcBs”eZdZdZeZdZdZdZ ddd„Z d„Z d„Z d„Z d„Zd „Zed „Zd „Zd „Zd „Zd„Zd„Z RS(sk Singleton log object. Do not directly instantiate _Log(); use the Log() constructor function above. State: .instance: [ a member which is None if self has never been instantiated, else self ] .kindMap: [ a dictionary such that .kindMap[k] == the count of all messages of type (k) logged so far ] .logList: [ all fileNames passed to constructors, as a list of writeable file objects ] .prefix: [ the last (prefix) argument passed to a constructor, defaulting to DEFAULT_PREFIX ] s*** s$Revision: 1.7 $s$Date: 2002/06/22 04:08:54 $cCs"h|_g|_|i|_dS(s) Constructor for the Log object. N(tkindMaptlogListtDEFAULT_PREFIXR(tselfRR((s(/u/www/docs/tcc/projects/pystyler/log.pyt__init__©s  cCs ||_dS(sO [ if prefix is a string -> self.prefix := prefix ] N(R(R R((s(/u/www/docs/tcc/projects/pystyler/log.pyR³scCs#t|dƒ}|ii|ƒdS(sļ [ if fileName is a string -> if fileName names a file that can be opened for writing -> self.logList +:= (fileName), opened for writing else -> raise IOError ] twN(topenR tappend(R Rtf((s(/u/www/docs/tcc/projects/pystyler/log.pyR½scGsg|ii|ƒpd|i| self.kindMap[kind] := 1 else -> self.kindMap[kind] +:= 1 In any case -> +:= (self's prefix) + (kind) + ": " + (concatenation of all (text) arguments) + " " (self's log files) +:= ] is: tN(R thas_keytstringtjointmessage(R tkindttexttoutput((s(/u/www/docs/tcc/projects/pystyler/log.pytmsgKindŹs cGs)ti|dƒ}|i|i|ƒdS(sÖ [ if self.kindMap has key (DEFAULT_KIND) -> self.kindMap[DEFAULT_KIND] = self.kindMap[DEFAULT_KIND] + 1 else -> self.kindMap[DEFAULT_KIND] = 1 In any case -> +:= (self's prefix) + ("Error") + ": " + (concatenation of all (text) arguments) + " " (self's log files) +:= ] RN(RRRt DEFAULT_KIND(R RtallText((s(/u/www/docs/tcc/projects/pystyler/log.pyterrorės cGs&ti|dƒ}|it|ƒdS(sŹ [ if self.kindMap has key (DEFAULT_KIND) -> self.kindMap[DEFAULT_KIND] = self.kindMap[DEFAULT_KIND] + 1 else -> self.kindMap[DEFAULT_KIND] = 1 In any case -> +:= (self's prefix) + ("Error") + ": " + (concatenation of all (text) arguments) + " " (self's log files) +:= ] RN(RRRt WARNING_KIND(R RR((s(/u/www/docs/tcc/projects/pystyler/log.pytwarningżs cCs'|ii|ƒo |i|SdSdS(s3 Returns number of messages of type (kind) iN(R R(R R((s(/u/www/docs/tcc/projects/pystyler/log.pytcounts cGs,ti|dƒ}|id|ƒt‚dS(s, Write a message and stop execution Rs Fatal errorN(RRRt SystemExit(R RR((s(/u/www/docs/tcc/projects/pystyler/log.pytfatalscGs*|iti|dƒ}|i|ƒdS(s2 Write a message with the standard prefix RN(RRRtwrite(R RR((s(/u/www/docs/tcc/projects/pystyler/log.pyR#scGsKti|dƒd}tii|ƒx|iD]}|i|ƒq0WdS(s< Write the given text to and all log files Rs N(RRtsyststderrR#R (R RRR((s(/u/www/docs/tcc/projects/pystyler/log.pyR#,s  cCsdS(Ns$Revision: 1.7 $((R ((s(/u/www/docs/tcc/projects/pystyler/log.pytrevision7scCsdS(Ns$Date: 2002/06/22 04:08:54 $((R ((s(/u/www/docs/tcc/projects/pystyler/log.pytdate=sN(t__name__t __module__t__doc__R t ERROR_KINDRtNoneRR&R'R RRRRRR R"RR#(((s(/u/www/docs/tcc/projects/pystyler/log.pyRŽs$ !   ((R*R$RR+RR,RR(((s(/u/www/docs/tcc/projects/pystyler/log.pytds