ia64/xen-unstable

changeset 14668:9981a42c8054

Made tracing thread safe, and also print thread name.
author Magnus Carlsson <magnus@galois.com>
date Thu Mar 29 14:27:46 2007 -0700 (2007-03-29)
parents dc1654ada984
children a5a2c49eda68
files tools/python/xen/xend/server/SrvDaemon.py
line diff
     1.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Thu Mar 29 23:31:58 2007 +0100
     1.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Thu Mar 29 14:27:46 2007 -0700
     1.3 @@ -38,7 +38,8 @@ class Daemon:
     1.4          self.traceon = False
     1.5          self.tracefile = None
     1.6          self.traceindent = 0
     1.7 -        self.child = 0 
     1.8 +        self.child = 0
     1.9 +        self.traceLock = threading.Lock()
    1.10  
    1.11  
    1.12      def cleanup_xend(self, kill):
    1.13 @@ -253,6 +254,7 @@ class Daemon:
    1.14                  pass
    1.15  
    1.16      def print_trace(self, string):
    1.17 +        self.tracefile.write("%s: "% threading.currentThread().getName())
    1.18          for i in range(self.traceindent):
    1.19              ch = " "
    1.20              if (i % 5):
    1.21 @@ -263,50 +265,54 @@ class Daemon:
    1.22          self.tracefile.write(string)
    1.23              
    1.24      def trace(self, frame, event, arg):
    1.25 -        if not self.traceon:
    1.26 -            print >>self.tracefile
    1.27 -            print >>self.tracefile, '-' * 20, 'TRACE OFF', '-' * 20
    1.28 -            self.tracefile.close()
    1.29 -            self.tracefile = None
    1.30 -            return None
    1.31 -        if event == 'call':
    1.32 -            code = frame.f_code
    1.33 -            filename = code.co_filename
    1.34 -            m = re.search('.*xend/(.*)', filename)
    1.35 -            if not m:
    1.36 -                return None
    1.37 -            modulename = m.group(1)
    1.38 -            if modulename.endswith('.pyc'):
    1.39 -                modulename = modulename[:-1]
    1.40 -            if modulename == 'sxp.py' or \
    1.41 -               modulename == 'XendLogging.py' or \
    1.42 -               modulename == 'XendMonitor.py' or \
    1.43 -               modulename == 'server/SrvServer.py':
    1.44 +        self.traceLock.acquire()
    1.45 +        try:
    1.46 +            if not self.traceon:
    1.47 +                print >>self.tracefile
    1.48 +                print >>self.tracefile, '-' * 20, 'TRACE OFF', '-' * 20
    1.49 +                self.tracefile.close()
    1.50 +                self.tracefile = None
    1.51                  return None
    1.52 -            self.traceindent += 1
    1.53 -            self.print_trace("> %s:%s\n"
    1.54 -                             % (modulename, code.co_name))
    1.55 -        elif event == 'line':
    1.56 -            filename = frame.f_code.co_filename
    1.57 -            lineno = frame.f_lineno
    1.58 -            self.print_trace("%4d %s" %
    1.59 -                             (lineno, linecache.getline(filename, lineno)))
    1.60 -        elif event == 'return':
    1.61 -            code = frame.f_code
    1.62 -            filename = code.co_filename
    1.63 -            m = re.search('.*xend/(.*)', filename)
    1.64 -            if not m:
    1.65 -                return None
    1.66 -            modulename = m.group(1)
    1.67 -            self.print_trace("< %s:%s\n"
    1.68 -                             % (modulename, code.co_name))
    1.69 -            self.traceindent -= 1
    1.70 -        elif event == 'exception':
    1.71 -            self.print_trace("! Exception:\n")
    1.72 -            (ex, val, tb) = arg
    1.73 -            traceback.print_exception(ex, val, tb, 10, self.tracefile)
    1.74 -            #del tb
    1.75 -        return self.trace
    1.76 +            if event == 'call':
    1.77 +                code = frame.f_code
    1.78 +                filename = code.co_filename
    1.79 +                m = re.search('.*xend/(.*)', filename)
    1.80 +                if not m:
    1.81 +                    return None
    1.82 +                modulename = m.group(1)
    1.83 +                if modulename.endswith('.pyc'):
    1.84 +                    modulename = modulename[:-1]
    1.85 +                if modulename == 'sxp.py' or \
    1.86 +                   modulename == 'XendLogging.py' or \
    1.87 +                   modulename == 'XendMonitor.py' or \
    1.88 +                   modulename == 'server/SrvServer.py':
    1.89 +                    return None
    1.90 +                self.traceindent += 1
    1.91 +                self.print_trace("> %s:%s\n"
    1.92 +                                 % (modulename, code.co_name))
    1.93 +            elif event == 'line':
    1.94 +                filename = frame.f_code.co_filename
    1.95 +                lineno = frame.f_lineno
    1.96 +                self.print_trace("%4d %s" %
    1.97 +                                 (lineno, linecache.getline(filename, lineno)))
    1.98 +            elif event == 'return':
    1.99 +                code = frame.f_code
   1.100 +                filename = code.co_filename
   1.101 +                m = re.search('.*xend/(.*)', filename)
   1.102 +                if not m:
   1.103 +                    return None
   1.104 +                modulename = m.group(1)
   1.105 +                self.print_trace("< %s:%s\n"
   1.106 +                                 % (modulename, code.co_name))
   1.107 +                self.traceindent -= 1
   1.108 +            elif event == 'exception':
   1.109 +                self.print_trace("! Exception:\n")
   1.110 +                (ex, val, tb) = arg
   1.111 +                traceback.print_exception(ex, val, tb, 10, self.tracefile)
   1.112 +                #del tb
   1.113 +            return self.trace
   1.114 +        finally:
   1.115 +            self.traceLock.release()
   1.116  
   1.117      def set_user(self):
   1.118          # Set the UID.