Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:48

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 
0022 import logging
0023 log = logging.getLogger(__name__) 
0024 
0025 # py2->py3  unicode->str str->bytes
0026 try:
0027     unicode
0028     ansi_ = lambda msg, codes:unicode("\x1b[%sm%s\x1b[0m" % (";".join(map(str, codes)), msg))
0029 except NameError:
0030     ansi_ = lambda msg, codes:str("\x1b[%sm%s\x1b[0m" % (";".join(map(str, codes)), msg))
0031 pass
0032 
0033 code = {
0034     "white_on_red_bright":(41,37,1),
0035     "blink_white_on_red_bright":(5,41,37,1),
0036     "blink_white_on_red":(5,41,37),
0037     "blink_red":(5,31,),
0038     "red":(31,),
0039     "yellow":(33,),
0040     "green":(32,),
0041     "pink":(35,),
0042     "normal":(0,),
0043     "underline":(4,),
0044     "bold":(1,),
0045     "reverse":(7,),
0046     "blink":(5,),
0047 }  
0048 
0049 red_ = lambda msg:ansi_(msg, code["red"])
0050 underline_ = lambda msg:ansi_(msg, code["underline"])
0051 bold_ = lambda msg:ansi_(msg, code["bold"])
0052 reverse_ = lambda msg:ansi_(msg, code["reverse"])
0053 blink_ = lambda msg:ansi_(msg, code["blink"])
0054 
0055 fatal_ = lambda msg:ansi_(msg, code["white_on_red_bright"])
0056 error_ = lambda msg:ansi_(msg, code["red"])
0057 warning_ = lambda msg:ansi_(msg, code["yellow"])
0058 info_ = lambda msg:ansi_(msg, code["green"])
0059 debug_ = lambda msg:ansi_(msg, code["pink"])
0060 
0061 
0062 enum2func = {
0063      logging.FATAL:fatal_,
0064      logging.ERROR:error_,
0065      logging.WARNING:warning_,
0066      logging.INFO:info_,
0067      logging.DEBUG:debug_,
0068 }
0069 
0070 def emit_ansi(fn):
0071     """ 
0072     Based on https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
0073     """
0074     def new(*args):
0075         levelno = args[1].levelno
0076         args[1].msg = enum2func[levelno](args[1].msg)
0077         return fn(*args)
0078     return new
0079 
0080 def init_logging(level="info", color=True, cflog=False):
0081     """
0082     """
0083     if color:
0084         logging.StreamHandler.emit = emit_ansi(logging.StreamHandler.emit)  
0085     pass
0086     #fmt = '[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)-8s - %(message)s'
0087     #fmt = '[%(asctime)s] p%(process)s {%(filename)-10s:%(lineno)d} %(levelname)-8s - %(message)s'
0088 
0089     if cflog:
0090         fmt = '[{%(funcName)-20s:%(filename)-10s:%(lineno)d} %(levelname)-8s - %(message)s'
0091     else:
0092         fmt = '[%(asctime)s] p%(process)s {%(funcName)-20s:%(filename)-10s:%(lineno)d} %(levelname)-8s - %(message)s'
0093     pass
0094     logging.basicConfig(level=getattr(logging,level.upper()), format=fmt)
0095 pass
0096 
0097 
0098 if __name__ == '__main__':
0099     init_logging(color=True, level="debug")
0100 
0101     names = "debug info warning error critical fatal".split()
0102     for name in names: 
0103         uname = name.upper()
0104         func = getattr(log, name)
0105         level = getattr(logging, uname)
0106         msg = "%20s : %20s : %d " % ( name, uname, level )
0107         func( msg ) 
0108     pass   
0109 
0110 
0111