Edit file File name : vdo Content :#! /usr/libexec/platform-python # # Copyright Red Hat # # 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. # """ vdo - VDO management utility $Id: //eng/vdo-releases/aluminum/src/python/vdo/vdo#5 $ """ #pylint: disable=C0302 from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import argparse import copy import gettext import logging from logging.handlers import RotatingFileHandler import locale import os import re import sys from textwrap import TextWrapper import traceback from vdo.utils import Command from vdo.vdomgmnt import * gettext.install('vdo') ######################################################################## def configureLogger(name, logfile = None, debug = False): """Configure the logging system according to the arguments.""" debugging = debug myname = os.path.basename(sys.argv[0]) formatBase = ': %(levelname)s - %(message)s' debugBase = (': %(name)s' if debugging else '') + formatBase logger = logging.getLogger() logger.setLevel(logging.NOTSET) handler = logging.StreamHandler(sys.stderr) handler.setFormatter(logging.Formatter(myname + debugBase)) handler.setLevel(logging.DEBUG if debugging else logging.WARNING) logger.addHandler(handler) if logfile is not None: try: if os.path.isfile(logfile) or not os.path.exists(logfile): # Nonexistent or file, treat it as a file handler = RotatingFileHandler(logfile, maxBytes=10*1024*1024, backupCount=5) else: # Exists but not a file, treat it as a stream logstream = open(logfile, "w") handler = logging.StreamHandler(stream=logstream) formatter = logging.Formatter('%(asctime)s %(name)s' + formatBase) handler.setFormatter(formatter) handler.setLevel(logging.DEBUG if debugging else logging.INFO) logger.addHandler(handler) except Exception as ex: logger.error("Unable to configure logging to '{logfile}' {ex}" .format(logfile=logfile, ex=ex)) sys.exit(1) # syslog logger try: handler = logging.handlers.SysLogHandler(address='/dev/log') handler.setFormatter(logging.Formatter(myname + formatBase)) handler.setLevel(logging.WARNING) logger.addHandler(handler) except Exception as ex: logger.warn('Unable to configure logging for rsyslog: {0}'.format(ex)) ######################################################################## def main(): """The main program. Exit codes: 0 SUCCESS success 1 ERROR error(s) occurred """ SUCCESS = 0 ERROR = 1 try: import vdoInstrumentation except ImportError: pass try: locale.setlocale(locale.LC_ALL, '') except locale.Error: pass arguments = VDOArgumentParser().parse_args() if arguments.command is None: # There does not appear to be a convenient way to get a command list. VDOArgumentParser().print_usage() sys.exit(ERROR) configureLogger(os.path.basename(sys.argv[0]), arguments.logfile, arguments.debug) mainLogger = logging.getLogger(os.path.basename(sys.argv[0])) mainLogger.info(' '.join(['running'] + sys.argv)) Command.setDefaults(arguments.verbose) operation = None exitval = ERROR try: operation = vdoOperations[arguments.command] except KeyError: mainLogger.error(_('Unknown command "{0}"'.format(arguments.command))) try: if operation is not None: operation.run(arguments) exitval = SUCCESS except Exception as ex: if isinstance(ex, ExitStatus): # pylint: disable=E1101 # If it's an instance of ExitStatus we know it has exitStatus. exitval = ex.exitStatus traceInfo = sys.exc_info()[2] mainLogger.error(str(ex)) # By default (i.e., without --debug) this will log to the # specified log file, if any, but not to stderr, because of the # priority thresholds we set for the different logging # destinations. We can't use mainLogger.exception because that # logs at level ERROR which would go everywhere. traceText = 'Traceback:\n' + ''.join(traceback.format_tb(traceInfo)) mainLogger.info(traceText.rstrip()) logging.shutdown() sys.exit(exitval) ######################################################################## if __name__ == "__main__": main() Save