| # |
| # |
| |
| # Copyright (C) 2014 Google Inc. |
| # |
| # This program is free software; you can redistribute it and/or modify |
| # it under the terms of the GNU General Public License as published by |
| # the Free Software Foundation; either version 2 of the License, or |
| # (at your option) any later version. |
| # |
| # This program is distributed in the hope that it will be useful, but |
| # WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| # General Public License for more details. |
| # |
| # You should have received a copy of the GNU General Public License |
| # along with this program; if not, write to the Free Software |
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| # 02110-1301, USA. |
| |
| """ Handles the logging of messages with appropriate coloring. |
| |
| """ |
| |
| import sys |
| |
| |
| _INFO_SEQ = None |
| _WARNING_SEQ = None |
| _ERROR_SEQ = None |
| _RESET_SEQ = None |
| |
| |
| def _SetupColours(): |
| """Initializes the colour constants. |
| |
| """ |
| # pylint: disable=W0603 |
| # due to global usage |
| global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ |
| |
| # Don't use colours if stdout isn't a terminal |
| if not sys.stdout.isatty(): |
| return |
| |
| try: |
| import curses |
| except ImportError: |
| # Don't use colours if curses module can't be imported |
| return |
| |
| curses.setupterm() |
| |
| _RESET_SEQ = curses.tigetstr("op") |
| |
| setaf = curses.tigetstr("setaf") |
| _INFO_SEQ = curses.tparm(setaf, curses.COLOR_GREEN) |
| _WARNING_SEQ = curses.tparm(setaf, curses.COLOR_YELLOW) |
| _ERROR_SEQ = curses.tparm(setaf, curses.COLOR_RED) |
| |
| |
| _SetupColours() |
| |
| |
| def _FormatWithColor(text, seq): |
| if not seq: |
| return text |
| return "%s%s%s" % (seq, text, _RESET_SEQ) |
| |
| |
| FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ) |
| FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ) |
| FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ) |