[pygccxml-commit] SF.net SVN: pygccxml: [341] pyplusplus_dev/pyplusplus/_logging_
Brought to you by:
mbaas,
roman_yakovenko
From: <mb...@us...> - 2006-07-24 15:49:38
|
Revision: 341 Author: mbaas Date: 2006-07-24 08:49:30 -0700 (Mon, 24 Jul 2006) ViewCVS: http://svn.sourceforge.net/pygccxml/?rev=341&view=rev Log Message: ----------- Implemented a new formatter that reformats a message so that long messages are split into several lines. This new 'multi line formatter' is now used for the default stream handler that writes the messages to stdout. Modified Paths: -------------- pyplusplus_dev/pyplusplus/_logging_/__init__.py Added Paths: ----------- pyplusplus_dev/pyplusplus/_logging_/multilineformatter.py Modified: pyplusplus_dev/pyplusplus/_logging_/__init__.py =================================================================== --- pyplusplus_dev/pyplusplus/_logging_/__init__.py 2006-07-24 15:48:04 UTC (rev 340) +++ pyplusplus_dev/pyplusplus/_logging_/__init__.py 2006-07-24 15:49:30 UTC (rev 341) @@ -8,23 +8,25 @@ This package contains logging configuration for pyplusplus. Default log level is DEBUG. Default log messages destination is sys.stdout. """ - + import os import sys import logging - -def _create_logger_( name ): - logger = logging.getLogger(name) - __handler = logging.StreamHandler(sys.stdout) - __handler.setFormatter( logging.Formatter( os.linesep + '%(levelname)s %(message)s' ) ) - logger.addHandler(__handler) - logger.setLevel(logging.INFO) - return logger - -class loggers: - file_writer = _create_logger_( 'pyplusplus.file_writer' ) - declarations = _create_logger_( 'pyplusplus.declarations' ) - module_builder = _create_logger_( 'pyplusplus.module_builder' ) - #root logger exists for configuration purpose only - root = logging.getLogger( 'pyplusplus' ) - all = [ root, file_writer, module_builder ] +from multilineformatter import multi_line_formatter_t + +def _create_logger_( name ): + logger = logging.getLogger(name) + __handler = logging.StreamHandler(sys.stdout) +# __handler.setFormatter( logging.Formatter( os.linesep + '%(levelname)s %(message)s' ) ) + __handler.setFormatter( multi_line_formatter_t( os.linesep + '%(levelname)s: %(message)s' ) ) + logger.addHandler(__handler) + logger.setLevel(logging.INFO) + return logger + +class loggers: + file_writer = _create_logger_( 'pyplusplus.file_writer' ) + declarations = _create_logger_( 'pyplusplus.declarations' ) + module_builder = _create_logger_( 'pyplusplus.module_builder' ) + #root logger exists for configuration purpose only + root = logging.getLogger( 'pyplusplus' ) + all = [ root, file_writer, module_builder ] Added: pyplusplus_dev/pyplusplus/_logging_/multilineformatter.py =================================================================== --- pyplusplus_dev/pyplusplus/_logging_/multilineformatter.py (rev 0) +++ pyplusplus_dev/pyplusplus/_logging_/multilineformatter.py 2006-07-24 15:49:30 UTC (rev 341) @@ -0,0 +1,89 @@ +# Copyright 2004 Roman Yakovenko. +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +# Initial version by Matthias Baas (ba...@ir...). + +import os, logging, textwrap + +class multi_line_formatter_t(logging.Formatter): + """Custom log formatter to split long message into several lines. + + This formatter is used for the default stream handler that outputs + its messages to stdout. + """ + + def __init__(self, fmt=None, datefmt=None, width=70): + """Constructor. + + See the Python standard library reference for a documentation + of fmt and datefmt. + width is the maximum width of the generated text blocks. + """ + logging.Formatter.__init__(self, fmt, datefmt) + self._width = width + + def format(self, record): + """This method overwrites the original one. + + The first thing that is done in the original format() method + is the creation of the record.message attribute: + + record.message = record.getMessage() + + Now this method temporarily replaces the getMessage() method of + the record by a version that returns a pregenerated message that + spans several lines. Then the original format() method is called + which will invoke the 'fake' method. + """ + # Get the original single-line message + message = record.getMessage() + # Distribute the message among several lines... + message = self.formatMessage(message, width=self._width) + # ...and temporarily replace the getMessage() method so that the + # reformatted message is used + mth = record.getMessage + record.getMessage = lambda x=message: x + # Invoke the inherited format() method + res = logging.Formatter.format(self, record) + # Restore the original method + record.getMessage = mth + return res + + @staticmethod + def formatMessage(msgline, width=70): + """Format a long single line message so that it is easier to read. + + msgline is a string containing a single message. It can either be + a plain message string which is reformatted using the textwrap + module or it can be of the form <decl>;<msg> where <decl> is the + declaration string and <msg> an arbitrary message. Lines of this + form will be separated so that the declaration and the message + appear in individual text blocks separated by the string '->'. + + In any case the return string will be indented except for the first + line. + + width is the maximum width of any text blocks (without indendation). + """ + txts = msgline.split(";") + # Ensure that there are no more than two items in txts + if len(txts)>2: + txts = [txts[0], ";".join(txts[1:])] + + # Insert a separator if there are two parts (=decl and msg) + if len(txts)==2: + txts.insert(1, "->") + + # Apply the text wrapper to shorten the maximum line length + lines = [] + for txt in txts: + txt = txt.strip().replace(os.linesep, " ") + lines.extend(textwrap.wrap(txt, width)) + + # Indent the text (except for the first line) + lines[1:] = map(lambda s: (2*" ")+s, lines[1:]) + return os.linesep.join(lines) + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |