/usr/lib/python2.7/dist-packages/bloom/logging.py is in python-bloom 0.6.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | # -*- coding: utf-8 -*-
# Software License Agreement (BSD License)
#
# Copyright (c) 2013, Open Source Robotics Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Open Source Robotics Foundation, Inc. nor
# the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import print_function
from __future__ import unicode_literals
import atexit
import datetime
import os
from platform import mac_ver
try:
from pkg_resources import parse_version
except OSError:
os.chdir(os.path.expanduser('~'))
from pkg_resources import parse_version
import re
import string
import sys
import functools
_ansi = {}
_quiet = False
_debug = False
_log_prefix_stack = ['']
_log_prefix = ''
_log_indent = False
_drop_first_log_prefix = False
_emoji_check_mark = "✅ "
_emoji_cross_mark = "❌ "
_is_mac_lion_or_greater = parse_version(mac_ver()[0]) >= parse_version('10.7.0')
def ansi(key):
"""Returns the escape sequence for a given ansi color key"""
global _ansi
return _ansi[key]
_strip_ansi_re = re.compile(r'\033\[[0-9;m]*')
def strip_ansi(msg):
return _strip_ansi_re.sub('', msg)
def enable_ANSI_colors():
"""
Populates the global module dictionary `ansi` with ANSI escape sequences.
"""
global _ansi
color_order = [
'black', 'red', 'green', 'yellow', 'blue', 'purple', 'cyan', 'white'
]
short_colors = {
'black': 'k', 'red': 'r', 'green': 'g', 'yellow': 'y', 'blue': 'b',
'purple': 'p', 'cyan': 'c', 'white': 'w'
}
_ansi = {
'escape': '\033', 'reset': 0, '|': 0,
'boldon': 1, '!': 1, 'italicson': 3, '/': 3, 'ulon': 4, '_': 4,
'invon': 7, 'boldoff': 22, 'italicsoff': 23,
'uloff': 24, 'invoff': 27
}
# Convert plain numbers to escapes
for key in _ansi:
if key != 'escape':
_ansi[key] = '{0}[{1}m'.format(_ansi['escape'], _ansi[key])
# Foreground
for index, color in enumerate(color_order):
_ansi[color] = '{0}[{1}m'.format(_ansi['escape'], 30 + index)
_ansi[color + 'f'] = _ansi[color]
_ansi[short_colors[color] + 'f'] = _ansi[color + 'f']
# Background
for index, color in enumerate(color_order):
_ansi[color + 'b'] = '{0}[{1}m'.format(_ansi['escape'], 40 + index)
_ansi[short_colors[color] + 'b'] = _ansi[color + 'b']
# Fmt sanitizers
_ansi['atexclimation'] = '@!'
_ansi['atfwdslash'] = '@/'
_ansi['atunderscore'] = '@_'
_ansi['atbar'] = '@|'
def disable_ANSI_colors():
"""
Sets all the ANSI escape sequences to empty strings, effectively disabling
console colors.
"""
global _ansi
for key in _ansi:
_ansi[key] = ''
def is_mac_lion_or_greater():
global _is_mac_lion_or_greater
return _is_mac_lion_or_greater
def get_success_prefix():
return _emoji_check_mark if _is_mac_lion_or_greater else "@{gf}<== @|"
def get_error_prefix():
return _emoji_cross_mark if _is_mac_lion_or_greater else "@{rf}@!<== @|"
# Default to ansi colors on
enable_ANSI_colors()
def quiet(state=True):
global _quiet
_quiet = state
# Default to quiet off
quiet(False)
def enable_debug(state=True):
global _debug
_debug = state
def is_debug():
global _debug
return _debug
# Default to debug off
enable_debug('DEBUG' in os.environ)
def enable_debug_indent(state=True):
global _log_indent
_log_indent = state
enable_debug_indent(True)
def enable_drop_first_log_prefix(state=True):
global _drop_first_log_prefix
_drop_first_log_prefix = state
enable_drop_first_log_prefix(True)
def _get_log_prefix():
global _log_prefix_stack, _log_indent
if _log_indent:
return (' ' * (len(_log_prefix_stack) - 2)) + _log_prefix_stack[-1]
else:
return _log_prefix_stack[-1]
def push_log_prefix(prefix):
global _log_prefix, _log_prefix_stack, _drop_first_log_prefix
if _drop_first_log_prefix and len(_log_prefix_stack) <= 1:
_log_prefix_stack.append('')
else:
_log_prefix_stack.append(prefix)
_log_prefix = _get_log_prefix()
def pop_log_prefix():
global _log_prefix, _log_prefix_stack
if len(_log_prefix_stack) > 1:
_log_prefix_stack.pop()
_log_prefix = _get_log_prefix()
class ContextDecorator(object):
def __call__(self, f):
@functools.wraps(f)
def decorated(*args, **kwds):
with self:
return f(*args, **kwds)
return decorated
class log_prefix(ContextDecorator):
def __init__(self, prefix):
self.prefix = prefix
def __enter__(self):
push_log_prefix(self.prefix)
def __exit__(self, exc_type, exc_value, traceback):
pop_log_prefix()
_file_log = None
def debug(msg, file=None, end='\n', use_prefix=True):
file = file if file is not None else sys.stdout
global _quiet, _debug, _log_prefix, _file_log
msg = '{0}'.format(msg)
if use_prefix:
msg = ansi('greenf') + _log_prefix + msg + ansi('reset')
else:
msg = ansi('greenf') + msg + ansi('reset')
if not _quiet and _debug:
print(msg, file=file, end=end)
if _file_log is not None:
print(('[debug] ' + strip_ansi(msg)).encode('UTF-8'), file=_file_log, end=end)
return msg
def info(msg, file=None, end='\n', use_prefix=True):
file = file if file is not None else sys.stdout
global _quiet, _log_prefix, _file_log
msg = '{0}'.format(msg)
if use_prefix:
msg = _log_prefix + msg + ansi('reset')
if not _quiet:
print(msg, file=file, end=end)
if _file_log is not None:
print(('[info] ' + strip_ansi(msg)).encode('UTF-8'), file=_file_log, end=end)
return msg
def warning(msg, file=None, end='\n', use_prefix=True):
file = file if file is not None else sys.stdout
global _quiet, _log_prefix, _file_log
msg = '{0}'.format(msg)
if use_prefix:
msg = ansi('yellowf') + _log_prefix + msg \
+ ansi('reset')
else:
msg = ansi('yellowf') + msg + ansi('reset')
if not _quiet:
print(msg, file=file, end=end)
if _file_log is not None:
print(('[warning] ' + strip_ansi(msg)).encode('UTF-8'), file=_file_log, end=end)
return msg
def error(msg, file=None, end='\n', use_prefix=True, exit=False):
file = file if file is not None else sys.stderr
global _quiet, _log_prefix, _file_log
msg = '{0}'.format(msg)
if use_prefix:
msg = ansi('redf') + ansi('boldon') + _log_prefix + msg + ansi('reset')
else:
msg = ansi('redf') + ansi('boldon') + msg + ansi('reset')
if _file_log is not None:
print(('[error] ' + strip_ansi(msg)).encode('UTF-8'), file=_file_log, end=end)
if exit:
if _file_log is not None:
print("[error] SYS.EXIT", file=_file_log, end=end)
sys.exit(msg)
if not _quiet:
print(msg, file=file, end=end)
return msg
try:
_log_id = os.environ.get('BLOOM_LOGGING_ID', str(os.getpid()))
os.environ['BLOOM_LOGGING_ID'] = _log_id # Store in env for subprocess
_file_log_prefix = os.path.join(os.path.expanduser('~'), '.bloom_logs')
if not os.path.isdir(_file_log_prefix):
os.makedirs(_file_log_prefix)
_file_log_path = os.path.join(_file_log_prefix, _log_id + '.log')
_file_log = open(_file_log_path, 'a')
if str(os.getpid()) == _log_id:
import bloom
_file_log.write("[bloom] bloom version " + bloom.__version__ + "\n")
except Exception as exc:
_file_log = None
error("Logging is not working: {0}: {1}".format(exc.__class__.__name__, exc))
_summary_file = None
def _get_summary_file_path():
global _summary_file, _file_log_prefix, _log_id
if _summary_file is None:
_summary_file = os.path.join(_file_log_prefix, _log_id + '.summary')
return _summary_file
@atexit.register
def close_logging():
global _file_log, _summary_file
if _file_log is not None:
name = _file_log.name
_file_log.close()
_file_log = None
if str(os.getpid()) == os.path.basename(name).split('.')[0]:
new_name = str(datetime.datetime.now())
new_name = new_name.replace(' ', '_').replace(':', '.') + '.log'
new_name = os.path.basename(str(sys.argv[0])) + "_" + new_name
new_name = os.path.join(os.path.dirname(name), new_name)
os.rename(name, new_name)
if os.path.exists(name):
os.remove(name)
class ColorTemplate(string.Template):
delimiter = '@'
def sanitize(msg):
"""Sanitizes the existing msg, use before adding color annotations"""
msg = msg.replace('@', '@@')
msg = msg.replace('{', '{{')
msg = msg.replace('}', '}}')
msg = msg.replace('@@!', '@{atexclimation}')
msg = msg.replace('@@/', '@{atfwdslash}')
msg = msg.replace('@@_', '@{atunderscore}')
msg = msg.replace('@@|', '@{atbar}')
return msg
def fmt(msg):
"""Replaces color annotations with ansi escape sequences"""
global _ansi
msg = msg.replace('@!', '@{boldon}')
msg = msg.replace('@/', '@{italicson}')
msg = msg.replace('@_', '@{ulon}')
msg = msg.replace('@|', '@{reset}')
t = ColorTemplate(msg)
msg = t.substitute(_ansi) + ansi('reset')
msg = msg.replace('{{', '{')
msg = msg.replace('}}', '}')
return msg
|