VirtualBox

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

Last change on this file was 101918, checked in by vboxsync, 6 months ago

libs/xpcom/python: Get rid of the log to mozilla console service code because it isn't used (and even if there is no listener ever registered for the console service anywhere so it would just go straight to /dev/null anyway), bugref:10545

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
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 <MarkH@activestate.com>
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.
40from __future__ import print_function
41import sys
42if sys.version_info[0] <= 2:
43 import exceptions
44 XPCOMBaseException = exceptions.Exception
45else:
46 XPCOMBaseException = Exception
47
48# A global "verbose" flag - currently used by the
49# server package to print trace messages
50verbose = 0
51# Map of nsresult -> constant_name.
52hr_map = {}
53
54# The standard XPCOM exception object.
55# Instances of this class are raised by the XPCOM extension module.
56class Exception(XPCOMBaseException):
57 def __init__(self, errno, message = None):
58 assert int(errno) == errno, "The errno param must be an integer"
59 self.errno = errno
60 self.msg = message
61 XPCOMBaseException.__init__(self, errno)
62 def __str__(self):
63 if not hr_map:
64 from . import nsError
65 for name, val in list(nsError.__dict__.items()):
66 if type(val)==type(0):
67 hr_map[val] = name
68 message = self.msg
69 if message is None:
70 message = hr_map.get(self.errno)
71 if message is None:
72 message = ""
73 return "0x%x (%s)" % (self.errno & 0xFFFFFFFF, message)
74
75# An alias for Exception - allows code to say "from xpcom import COMException"
76# rather than "Exception", preventing clashes with the builtin Exception
77COMException = Exception
78
79# Exceptions thrown by servers. It can be good for diagnostics to
80# differentiate between a ServerException (which was presumably explicitly thrown)
81# and a normal exception which may simply be propagating down.
82# (When ServerException objects are thrown across the XPConnect
83# gateway they will be converted back to normal client exceptions if
84# subsequently re-caught by Python)
85class ServerException(Exception):
86 def __init__(self, errno=None, *args, **kw):
87 if errno is None:
88 from . import nsError
89 errno = nsError.NS_ERROR_FAILURE
90 Exception.__init__(self, errno, *args, **kw)
91
92# Logging support - setup the 'xpcom' logger to write to sys.stderr,
93# or optionally a file.
94# Environment variables supports:
95# PYXPCOM_LOG_FILE=filename - if set, used instead of sys.stderr.
96# PYXPCOM_LOG_LEVEL=level - level may be a number or a logging level
97# constant (eg, 'debug', 'error')
98# Later it may make sense to allow a different log level to be set for
99# the file.
100import logging
101def setupLogging():
102 import os
103 if sys.version_info[0] <= 2:
104 import threading, thread
105
106 # Add a handler to print to stderr, or optionally a file
107 # PYXPCOM_LOG_FILE can specify a filename
108 filename = os.environ.get("PYXPCOM_LOG_FILE")
109 stream = sys.stderr # this is what logging uses as default
110 if filename:
111 try:
112 # open without buffering so never pending output
113 stream = open(filename, "wU", 0)
114 except IOError as why:
115 print("pyxpcom failed to open log file '%s': %s" % (filename, why), file=sys.stderr)
116 # stream remains default
117
118 hdlr = logging.StreamHandler(stream)
119 # see above - fix a deadlock problem on this handler too.
120 if sys.version_info[0] <= 2:
121 if type(hdlr.lock) == thread.LockType:
122 hdlr.lock = threading.RLock()
123
124 fmt = logging.Formatter(logging.BASIC_FORMAT)
125 hdlr.setFormatter(fmt)
126 logger.addHandler(hdlr)
127 # Allow PYXPCOM_LOG_LEVEL to set the level
128 level = os.environ.get("PYXPCOM_LOG_LEVEL")
129 if level:
130 try:
131 level = int(level)
132 except ValueError:
133 try:
134 # might be a symbolic name - all are upper-case
135 level = int(getattr(logging, level.upper()))
136 except (AttributeError, ValueError):
137 logger.warning("The PYXPCOM_LOG_LEVEL variable specifies an "
138 "invalid level")
139 level = None
140 if level:
141 logger.setLevel(level)
142
143logger = logging.getLogger('xpcom')
144# If someone else has already setup this logger, leave things alone.
145if len(logger.handlers) == 0:
146 setupLogging()
147
148# Cleanup namespace - but leave 'logger' there for people to use, so they
149# don't need to know the exact name of the logger.
150del logging, setupLogging
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use