VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/__init__.py@ 12972

最後變更 在這個檔案從12972是 12752,由 vboxsync 提交於 16 年 前

implemented much better error reporting for Python bindings

  • 屬性 svn:eol-style 設為 native
檔案大小: 6.6 KB
 
1# ***** BEGIN LICENSE BLOCK *****
2# Version: MPL 1.1/GPL 2.0/LGPL 2.1
3#
4# The contents of this file are subject to the Mozilla Public License Version
5# 1.1 (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7# http://www.mozilla.org/MPL/
8#
9# Software distributed under the License is distributed on an "AS IS" basis,
10# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11# for the specific language governing rights and limitations under the
12# License.
13#
14# The Original Code is the Python XPCOM language bindings.
15#
16# The Initial Developer of the Original Code is
17# Activestate Tool Corp.
18# Portions created by the Initial Developer are Copyright (C) 2000
19# the Initial Developer. All Rights Reserved.
20#
21# Contributor(s):
22# Mark Hammond <[email protected]>
23#
24# Alternatively, the contents of this file may be used under the terms of
25# either the GNU General Public License Version 2 or later (the "GPL"), or
26# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27# in which case the provisions of the GPL or the LGPL are applicable instead
28# of those above. If you wish to allow use of your version of this file only
29# under the terms of either the GPL or the LGPL, and not to allow others to
30# use your version of this file under the terms of the MPL, indicate your
31# decision by deleting the provisions above and replace them with the notice
32# and other provisions required by the GPL or the LGPL. If you do not delete
33# the provisions above, a recipient may use your version of this file under
34# the terms of any one of the MPL, the GPL or the LGPL.
35#
36# ***** END LICENSE BLOCK *****
37#
38
39# The XPCOM (Cross Platform COM) package.
40import exceptions
41
42# A global "verbose" flag - currently used by the
43# server package to print trace messages
44verbose = 0
45# Map of nsresult -> constant_name.
46hr_map = {}
47
48# The standard XPCOM exception object.
49# Instances of this class are raised by the XPCOM extension module.
50class Exception(exceptions.Exception):
51 def __init__(self, errno, message = None):
52 assert int(errno) == errno, "The errno param must be an integer"
53 self.errno = errno
54 self.message = message
55 exceptions.Exception.__init__(self, errno)
56 # we do this, as parent's constructor can override message
57 self.message = message
58 def __str__(self):
59 if not hr_map:
60 import nsError
61 for name, val in nsError.__dict__.items():
62 if type(val)==type(0):
63 hr_map[val] = name
64 message = self.message
65 if message is None:
66 message = hr_map.get(self.errno)
67 if message is None:
68 message = ""
69 return "0x%x (%s)" % (self.errno & 0xFFFFFFFF, message)
70
71# An alias for Exception - allows code to say "from xpcom import COMException"
72# rather than "Exception", preventing clashes with the builtin Exception
73COMException = Exception
74
75# Exceptions thrown by servers. It can be good for diagnostics to
76# differentiate between a ServerException (which was presumably explicitly thrown)
77# and a normal exception which may simply be propagating down.
78# (When ServerException objects are thrown across the XPConnect
79# gateway they will be converted back to normal client exceptions if
80# subsequently re-caught by Python)
81class ServerException(Exception):
82 def __init__(self, errno=None, *args, **kw):
83 if errno is None:
84 import nsError
85 errno = nsError.NS_ERROR_FAILURE
86 Exception.__init__(self, errno, *args, **kw)
87
88# Logging support - setup the 'xpcom' logger to write to the Mozilla
89# console service, and also to sys.stderr, or optionally a file.
90# Environment variables supports:
91# PYXPCOM_LOG_FILE=filename - if set, used instead of sys.stderr.
92# PYXPCOM_LOG_LEVEL=level - level may be a number or a logging level
93# constant (eg, 'debug', 'error')
94# Later it may make sense to allow a different log level to be set for
95# the file than for the console service.
96import logging
97class ConsoleServiceStream:
98 # enough of a stream to keep logging happy
99 def flush(self):
100 pass
101 def write(self, msg):
102 import _xpcom
103 _xpcom.LogConsoleMessage(msg)
104 def close(self):
105 pass
106
107def setupLogging():
108 import sys, os, threading, thread
109 hdlr = logging.StreamHandler(ConsoleServiceStream())
110 fmt = logging.Formatter(logging.BASIC_FORMAT)
111 hdlr.setFormatter(fmt)
112 # There is a bug in 2.3 and 2.4.x logging module in that it doesn't
113 # use an RLock, leading to deadlocks in some cases (specifically,
114 # logger.warning("ob is %r", ob), and where repr(ob) itself tries to log)
115 # Later versions of logging use an RLock, so we detect an "old" style
116 # handler and update its lock
117 if type(hdlr.lock) == thread.LockType:
118 hdlr.lock = threading.RLock()
119
120 logger.addHandler(hdlr)
121 # The console handler in mozilla does not go to the console!?
122 # Add a handler to print to stderr, or optionally a file
123 # PYXPCOM_LOG_FILE can specify a filename
124 filename = os.environ.get("PYXPCOM_LOG_FILE")
125 stream = sys.stderr # this is what logging uses as default
126 if filename:
127 try:
128 # open without buffering so never pending output
129 stream = open(filename, "wU", 0)
130 except IOError, why:
131 print >> sys.stderr, "pyxpcom failed to open log file '%s': %s" \
132 % (filename, why)
133 # stream remains default
134
135 hdlr = logging.StreamHandler(stream)
136 # see above - fix a deadlock problem on this handler too.
137 if type(hdlr.lock) == thread.LockType:
138 hdlr.lock = threading.RLock()
139
140 fmt = logging.Formatter(logging.BASIC_FORMAT)
141 hdlr.setFormatter(fmt)
142 logger.addHandler(hdlr)
143 # Allow PYXPCOM_LOG_LEVEL to set the level
144 level = os.environ.get("PYXPCOM_LOG_LEVEL")
145 if level:
146 try:
147 level = int(level)
148 except ValueError:
149 try:
150 # might be a symbolic name - all are upper-case
151 level = int(getattr(logging, level.upper()))
152 except (AttributeError, ValueError):
153 logger.warning("The PYXPCOM_LOG_LEVEL variable specifies an "
154 "invalid level")
155 level = None
156 if level:
157 logger.setLevel(level)
158
159logger = logging.getLogger('xpcom')
160# If someone else has already setup this logger, leave things alone.
161if len(logger.handlers) == 0:
162 setupLogging()
163
164# Cleanup namespace - but leave 'logger' there for people to use, so they
165# don't need to know the exact name of the logger.
166del ConsoleServiceStream, logging, setupLogging
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette