| OLD | NEW |
| 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions | 4 # modification, are permitted provided that the following conditions |
| 5 # are met: | 5 # are met: |
| 6 # 1. Redistributions of source code must retain the above copyright | 6 # 1. Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # 2. Redistributions in binary form must reproduce the above copyright | 8 # 2. Redistributions in binary form must reproduce the above copyright |
| 9 # notice, this list of conditions and the following disclaimer in the | 9 # notice, this list of conditions and the following disclaimer in the |
| 10 # documentation and/or other materials provided with the distribution. | 10 # documentation and/or other materials provided with the distribution. |
| 11 # | 11 # |
| 12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND | 12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
| 13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR | 15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR |
| 16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 22 | 22 |
| 23 """Supports webkitpy logging.""" | 23 """Supports webkitpy logging.""" |
| 24 | 24 |
| 25 # FIXME: Move this file to webkitpy/python24 since logging needs to | |
| 26 # be configured prior to running version-checking code. | |
| 27 | |
| 28 import logging | 25 import logging |
| 29 import os | |
| 30 import sys | 26 import sys |
| 31 | 27 |
| 32 import webkitpy | |
| 33 | |
| 34 | |
| 35 _log = logging.getLogger(__name__) | 28 _log = logging.getLogger(__name__) |
| 36 | 29 |
| 37 # We set these directory paths lazily in get_logger() below. | |
| 38 _scripts_dir = "" | |
| 39 """The normalized, absolute path to the ...Scripts directory.""" | |
| 40 | |
| 41 _webkitpy_dir = "" | |
| 42 """The normalized, absolute path to the ...Scripts/webkitpy directory.""" | |
| 43 | |
| 44 | |
| 45 def _normalize_path(path): | |
| 46 """Return the given path normalized. | |
| 47 | |
| 48 Converts a path to an absolute path, removes any trailing slashes, | |
| 49 removes any extension, and lower-cases it. | |
| 50 """ | |
| 51 path = os.path.abspath(path) | |
| 52 path = os.path.normpath(path) | |
| 53 path = os.path.splitext(path)[0] # Remove the extension, if any. | |
| 54 path = path.lower() | |
| 55 | |
| 56 return path | |
| 57 | |
| 58 | |
| 59 # Observe that the implementation of this function does not require | |
| 60 # the use of any hard-coded strings like "webkitpy", etc. | |
| 61 # | |
| 62 # The main benefit this function has over using-- | |
| 63 # | |
| 64 # _log = logging.getLogger(__name__) | |
| 65 # | |
| 66 # is that get_logger() returns the same value even if __name__ is | |
| 67 # "__main__" -- i.e. even if the module is the script being executed | |
| 68 # from the command-line. | |
| 69 def get_logger(path): | |
| 70 """Return a logging.logger for the given path. | |
| 71 | |
| 72 Returns: | |
| 73 A logger whose name is the name of the module corresponding to | |
| 74 the given path. If the module is in webkitpy, the name is | |
| 75 the fully-qualified dotted module name beginning with webkitpy.... | |
| 76 Otherwise, the name is the base name of the module (i.e. without | |
| 77 any dotted module name prefix). | |
| 78 | |
| 79 Args: | |
| 80 path: The path of the module. Normally, this parameter should be | |
| 81 the __file__ variable of the module. | |
| 82 | |
| 83 Sample usage: | |
| 84 | |
| 85 from webkitpy.common.system import logutils | |
| 86 | |
| 87 _log = logutils.get_logger(__file__) | |
| 88 """ | |
| 89 # Since we assign to _scripts_dir and _webkitpy_dir in this function, | |
| 90 # we need to declare them global. | |
| 91 global _scripts_dir | |
| 92 global _webkitpy_dir | |
| 93 | |
| 94 path = _normalize_path(path) | |
| 95 | |
| 96 # Lazily evaluate _webkitpy_dir and _scripts_dir. | |
| 97 if not _scripts_dir: | |
| 98 # The normalized, absolute path to ...Scripts/webkitpy/__init__. | |
| 99 webkitpy_path = _normalize_path(webkitpy.__file__) | |
| 100 | |
| 101 _webkitpy_dir = os.path.split(webkitpy_path)[0] | |
| 102 _scripts_dir = os.path.split(_webkitpy_dir)[0] | |
| 103 | |
| 104 if path.startswith(_webkitpy_dir): | |
| 105 # Remove the initial Scripts directory portion, so the path | |
| 106 # starts with /webkitpy, for example "/webkitpy/init/logutils". | |
| 107 path = path[len(_scripts_dir):] | |
| 108 | |
| 109 parts = [] | |
| 110 while True: | |
| 111 (path, tail) = os.path.split(path) | |
| 112 if not tail: | |
| 113 break | |
| 114 parts.insert(0, tail) | |
| 115 | |
| 116 logger_name = ".".join(parts) # For example, webkitpy.common.system.log
utils. | |
| 117 else: | |
| 118 # The path is outside of webkitpy. Default to the basename | |
| 119 # without the extension. | |
| 120 basename = os.path.basename(path) | |
| 121 logger_name = os.path.splitext(basename)[0] | |
| 122 | |
| 123 return logging.getLogger(logger_name) | |
| 124 | |
| 125 | 30 |
| 126 def _default_handlers(stream, logging_level): | 31 def _default_handlers(stream, logging_level): |
| 127 """Return a list of the default logging handlers to use. | 32 """Return a list of the default logging handlers to use. |
| 128 | 33 |
| 129 Args: | 34 Args: |
| 130 stream: See the configure_logging() docstring. | 35 stream: See the configure_logging() docstring. |
| 131 """ | 36 """ |
| 132 # Create the filter. | 37 # Create the filter. |
| 133 def should_log(record): | 38 def should_log(record): |
| 134 """Return whether a logging.LogRecord should be logged.""" | 39 """Return whether a logging.LogRecord should be logged.""" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 handlers = _default_handlers(stream, logging_level) | 99 handlers = _default_handlers(stream, logging_level) |
| 195 | 100 |
| 196 logger.setLevel(logging_level) | 101 logger.setLevel(logging_level) |
| 197 | 102 |
| 198 for handler in handlers: | 103 for handler in handlers: |
| 199 logger.addHandler(handler) | 104 logger.addHandler(handler) |
| 200 | 105 |
| 201 _log.debug("Debug logging enabled.") | 106 _log.debug("Debug logging enabled.") |
| 202 | 107 |
| 203 return handlers | 108 return handlers |
| OLD | NEW |