rtvlog.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Geoffrey Biggs
00009     RT-Synthesis Research Group
00010     Intelligent Systems Research Institute,
00011     National Institute of Advanced Industrial Science and Technology (AIST),
00012     Japan
00013     All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016 
00017 Implementation of the command to make a component exit.
00018 
00019 '''
00020 
00021 
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.exceptions
00026 import rtctree.tree
00027 import rtctree.path
00028 import RTC
00029 import sys
00030 import traceback
00031 
00032 import path
00033 import rts_exceptions
00034 import rtshell
00035 
00036 
00037 def log_cb(rtc, time, source, level, msg):
00038     print '[{0} {1}] {2} <{3}> {4}'.format(rtc, time, level, source, msg)
00039 
00040 
00041 def print_logs(paths, options, tree=None):
00042     for p in paths:
00043         path, port = rtctree.path.parse_path(p[1])
00044         if port:
00045             raise rts_exceptions.NotAComponentError(p[0])
00046         if not path[-1]:
00047             raise rts_exceptions.NotAComponentError(p[0])
00048         p.append(path)
00049 
00050     if not tree:
00051         parsed = [p[2] for p in paths]
00052         tree = rtctree.tree.RTCTree(paths=parsed, filter=parsed)
00053 
00054     filters = ','.join(options.filters)
00055     ids = []
00056     try:
00057         for p in paths:
00058             if not tree.has_path(p[2]):
00059                 raise rts_exceptions.NoSuchObjectError(p[0])
00060             rtc = tree.get_node(p[2])
00061             if rtc.is_zombie:
00062                 raise rts_exceptions.ZombieObjectError(p[0])
00063             if not rtc.is_component:
00064                 raise rts_exceptions.NotAComponentError(p[0])
00065 
00066             id = rtc.add_logger(log_cb, level=options.level,
00067                     filters=filters)
00068             ids.append((rtc, id))
00069     except rtctree.exceptions.AddLoggerError, e:
00070         # Remove all the loggers that were added
00071         for i in ids:
00072             i[0].remove_logger(i[1])
00073         # Re-raise
00074         raise e
00075 
00076     # Wait for a keyboard interrupt
00077     try:
00078         while True:
00079             raw_input('')
00080     except KeyboardInterrupt:
00081         pass
00082     # Remove all the loggers that were added
00083     for i in ids:
00084         i[0].remove_logger(i[1])
00085 
00086 
00087 def main(argv=None, tree=None):
00088     usage = '''Usage: %prog [options] <path 1> [path 2...]
00089 View a component logs.'''
00090     version = rtshell.RTSH_VERSION
00091     parser = optparse.OptionParser(usage=usage, version=version)
00092     parser.add_option('-f', '--filter', dest='filters', action='append',
00093             type='string', default='ALL',
00094             help='Event source filters. [Default: %default]')
00095     parser.add_option('-l', '--level', dest='level', action='store',
00096             type='string', default='NORMAL',
00097             help='Log level. [Default: %default]')
00098     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00099             default=False,
00100             help='Output verbose information. [Default: %default]')
00101 
00102     if argv:
00103         sys.argv = [sys.argv[0]] + argv
00104     try:
00105         options, args = parser.parse_args()
00106     except optparse.OptionError, e:
00107         print >>sys.stderr, 'OptionError:', e
00108         return 1
00109 
00110     if not args:
00111         # If no paths given then can't do anything.
00112         print >>sys.stderr, '{0}: No component specified.'.format(
00113                 os.path.basename(sys.argv[0]))
00114         return 1
00115     paths = [[p, path.cmd_path_to_full_path(p)] for p in args]
00116 
00117     try:
00118         print_logs(paths, options, tree)
00119     except Exception, e:
00120         if options.verbose:
00121             traceback.print_exc()
00122         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00123         return 1
00124     return 0
00125 
00126 
00127 # vim: tw=79
00128 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12