2 # vim: ai ts=4 sts=4 et sw=4
4 # Copyright 2009, 2010, 2011 Intel, Inc.
6 # This copyrighted material is made available to anyone wishing to use, modify,
7 # copy, or redistribute it subject to the terms and conditions of the GNU
8 # General Public License v.2. This program is distributed in the hope that it
9 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
10 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
16 # trademarks that are incorporated in the source code or documentation are not
17 # subject to the GNU General Public License and may only be used or replicated
18 # with the express permission of Red Hat, Inc.
23 from datetime import datetime
25 __ALL__ = ['set_mode',
40 INFO_COLOR = 32 # green
41 WARN_COLOR = 33 # yellow
46 PREFIX_RE = re.compile('^<(.*?)>\s*(.*)')
62 def _general_print(head, color, msg = None, stream = sys.stdout, level = 'normal'):
64 if LOG_LEVELS[level] > LOG_LEVEL:
69 save_msg = msg.strip()
71 timestr = datetime.now().strftime('[%m/%d %H:%M:%S] ')
72 LOG_CONTENT += timestr + save_msg + '\n'
74 _color_print(head, color, msg, stream, level)
76 def _color_print(head, color, msg, stream, level):
78 if color == NO_COLOR or \
79 not stream.isatty() or \
80 os.getenv('ANSI_COLORS_DISABLED') is not None:
83 if head.startswith('\r'):
90 head = '\033[%dm%s:\033[0m ' %(color, head)
92 # ESC cmd to clear line
93 head = '\033[2K' + head
97 if head.startswith('\r'):
102 stream.write('%s%s' % (head, msg))
108 def _color_perror(head, color, msg, level = 'normal'):
109 _general_print(head, color, msg, sys.stderr, level)
111 def _split_msg(head, msg):
112 if isinstance(msg, list):
113 msg = '\n'.join(map(str, msg))
115 if msg.startswith('\n'):
116 # means print \n at first
120 elif msg.startswith('\r'):
121 # means print \r at first
125 m = PREFIX_RE.match(msg)
127 head += ' <%s>' % m.group(1)
133 return (k for k,v in LOG_LEVELS.items() if v==LOG_LEVEL).next()
135 def set_loglevel(level):
137 if level not in LOG_LEVELS:
141 LOG_LEVEL = LOG_LEVELS[level]
143 def set_interactive(mode=True):
151 _general_print('', NO_COLOR, msg)
154 head, msg = _split_msg('Info', msg)
155 _general_print(head, INFO_COLOR, msg)
158 head, msg = _split_msg('Verbose', msg)
159 _general_print(head, INFO_COLOR, msg, level = 'verbose')
162 head, msg = _split_msg('Warning', msg)
163 _color_perror(head, WARN_COLOR, msg)
166 head, msg = _split_msg('Debug', msg)
167 _color_perror(head, ERR_COLOR, msg, level = 'debug')
170 head, msg = _split_msg('Error', msg)
171 _color_perror(head, ERR_COLOR, msg)
174 def ask(msg, default=True):
175 _general_print('\rQ', ASK_COLOR, '')
183 repl = raw_input(msg)
184 if repl.lower() == 'y':
186 elif repl.lower() == 'n':
188 elif not repl.strip():
198 _general_print('', NO_COLOR, msg)
201 except KeyboardInterrupt:
202 sys.stdout.write('\n')
207 _general_print('\rQ', ASK_COLOR, '')
209 msg = 'press <ENTER> to continue ...'
212 def set_logfile(fpath):
217 fp = open(LOG_FILE_FP, 'w')
218 fp.write(LOG_CONTENT)
221 if LOG_FILE_FP is not None:
222 warning('duplicate log file configuration')
224 LOG_FILE_FP = os.path.abspath(os.path.expanduser(fpath))
227 atexit.register(_savelogf)