ThirdParty/Jasy/jasy/core/Console.py

changeset 6650
1dd52aa8897c
parent 6645
ad476851d7e0
equal deleted inserted replaced
6649:f1b3a73831c9 6650:1dd52aa8897c
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # Copyright (c) 2013 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2013 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 # Jasy - Web Tooling Framework
6 # Copyright 2010-2012 Zynga Inc.
7 # Copyright 2013-2014 Sebastian Werner
8 #
9
10 """
11 Centralized logging for complete Jasy environment.
12 """
5 13
6 from __future__ import unicode_literals 14 from __future__ import unicode_literals
7 15
8 import logging 16 import logging, sys
17
18 __all__ = ["colorize", "header", "error", "warn", "info", "debug", "indent", "outdent"]
19
20
21
22 # ---------------------------------------------
23 # Colorized Output
24 # ---------------------------------------------
25
26 __colors = {
27 'bold' : ['\033[1m', '\033[22m'],
28 'italic' : ['\033[3m', '\033[23m'],
29 'underline' : ['\033[4m', '\033[24m'],
30 'inverse' : ['\033[7m', '\033[27m'],
31
32 'white' : ['\033[37m', '\033[39m'],
33 'grey' : ['\033[90m', '\033[39m'],
34 'black' : ['\033[30m', '\033[39m'],
35
36 'blue' : ['\033[34m', '\033[39m'],
37 'cyan' : ['\033[36m', '\033[39m'],
38 'green' : ['\033[32m', '\033[39m'],
39 'magenta' : ['\033[35m', '\033[39m'],
40 'red' : ['\033[31m', '\033[39m'],
41 'yellow' : ['\033[33m', '\033[39m']
42 }
43
44 def colorize(text, color="red"):
45 """Uses to colorize the given text for output on Unix terminals"""
46
47 # Not supported on console on Windows native
48 # Note: Cygwin has a different platform value
49 if sys.platform == "win32":
50 return text
51
52 entry = __colors[color]
53 return "%s%s%s" % (entry[0], text, entry[1])
54
55
56
57 # ---------------------------------------------
58 # Logging API
59 # ---------------------------------------------
60
61 __level = 0
62
63 def __format(text):
64 global __level
65
66 if __level == 0 or text == "":
67 return text
68 elif __level == 1:
69 return "- %s" % text
70 else:
71 return "%s- %s" % (" " * (__level-1), text)
72
73 def indent():
74 """
75 Increments global indenting level. Prepends spaces to the next
76 logging messages until outdent() is called.
77
78 Should be called whenever leaving a structural logging section.
79 """
80
81 global __level
82 __level += 1
83
84 def outdent(all=False):
85 """
86 Decrements global indenting level.
87 Should be called whenever leaving a structural logging section.
88 """
89
90 global __level
91
92 if all:
93 __level = 0
94 else:
95 __level -= 1
9 96
10 def error(text, *argv): 97 def error(text, *argv):
11 """Outputs an error message""" 98 """Outputs an error message (visible by default)"""
12 99
13 logging.error(text, *argv) 100 logging.warn(__format(colorize(colorize(text, "red"), "bold")), *argv)
14 101
15 def warn(text, *argv): 102 def warn(text, *argv):
16 """Outputs an warning""" 103 """Outputs an warning (visible by default)"""
17 104
18 logging.warn(text, *argv) 105 logging.warn(__format(colorize(text, "red")), *argv)
19 106
20 def info(text, *argv): 107 def info(text, *argv):
21 """Outputs an info message""" 108 """Outputs an info message (visible by default, disable via --quiet option)"""
22 109
23 logging.info(text, *argv) 110 logging.info(__format(text), *argv)
24 111
25 def debug(text, *argv): 112 def debug(text, *argv):
26 """Output a debug message""" 113 """Output a debug message (hidden by default, enable via --verbose option)"""
27 114
28 logging.debug(text, *argv) 115 logging.debug(__format(text), *argv)
116
117 def header(title):
118 """Outputs the given title with prominent formatting"""
119
120 global __level
121 __level = 0
122
123 logging.info("")
124 logging.info(colorize(colorize(">>> %s" % title.upper(), "blue"), "bold"))
125 logging.info(colorize("-------------------------------------------------------------------------------", "blue"))

eric ide

mercurial