本文整理汇总了Python中util.log.log函数的典型用法代码示例。如果您正苦于以下问题:Python log函数的具体用法?Python log怎么用?Python log使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了log函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getCorrections
def getCorrections(self):
"""
Performs validation on the loaded contents and provides back the
corrections. If validation is disabled then this won't provide any
results.
"""
self.valsLock.acquire()
if not self.isLoaded(): returnVal = None
else:
torVersion = torTools.getConn().getVersion()
skipValidation = not CONFIG["features.torrc.validate"]
skipValidation |= (torVersion is None or not torVersion.meets_requirements(stem.version.Requirement.GETINFO_CONFIG_TEXT))
if skipValidation:
log.log(log.INFO, "Skipping torrc validation (requires tor 0.2.2.7-alpha)")
returnVal = {}
else:
if self.corrections == None:
self.corrections = validate(self.contents)
returnVal = list(self.corrections)
self.valsLock.release()
return returnVal
示例2: loadConfig
def loadConfig(config):
config.update(CONFIG)
for configKey in config.getKeys():
# fetches any port.label.* values
if configKey.startswith("port.label."):
portEntry = configKey[11:]
purpose = config.get(configKey)
divIndex = portEntry.find("-")
if divIndex == -1:
# single port
if portEntry.isdigit():
PORT_USAGE[portEntry] = purpose
else:
msg = "Port value isn't numeric for entry: %s" % configKey
log.log(CONFIG["log.configEntryTypeError"], msg)
else:
try:
# range of ports (inclusive)
minPort = int(portEntry[:divIndex])
maxPort = int(portEntry[divIndex + 1:])
if minPort > maxPort: raise ValueError()
for port in range(minPort, maxPort + 1):
PORT_USAGE[str(port)] = purpose
except ValueError:
msg = "Unable to parse port range for entry: %s" % configKey
log.log(CONFIG["log.configEntryTypeError"], msg)
示例3: _initColors
def _initColors():
"""
Initializes color mappings usable by curses. This can only be done after
calling curses.initscr().
"""
global COLOR_ATTR_INITIALIZED, COLOR_IS_SUPPORTED
if not COLOR_ATTR_INITIALIZED:
COLOR_ATTR_INITIALIZED = True
COLOR_IS_SUPPORTED = False
if not CONFIG["features.colorInterface"]: return
try: COLOR_IS_SUPPORTED = curses.has_colors()
except curses.error: return # initscr hasn't been called yet
# initializes color mappings if color support is available
if COLOR_IS_SUPPORTED:
colorpair = 0
log.log(CONFIG["log.cursesColorSupport"], "Terminal color support detected and enabled")
for colorName in COLOR_LIST:
fgColor = COLOR_LIST[colorName]
bgColor = -1 # allows for default (possibly transparent) background
colorpair += 1
curses.init_pair(colorpair, fgColor, bgColor)
COLOR_ATTR[colorName] = curses.color_pair(colorpair)
else:
log.log(CONFIG["log.cursesColorSupport"], "Terminal color support unavailable")
示例4: getValue
def getValue(self, key, default=None, multiple=False):
"""
This provides the currently value associated with a given key. If no such
key exists then this provides the default.
Arguments:
key - config setting to be fetched
default - value provided if no such key exists
multiple - provides back a list of all values if true, otherwise this
returns the last loaded configuration value
"""
self.contentsLock.acquire()
if key in self.contents:
val = self.contents[key]
if not multiple: val = val[-1]
self.requestedKeys.add(key)
else:
msg = "config entry '%s' not found, defaulting to '%s'" % (key, str(default))
log.log(CONFIG["log.configEntryNotFound"], msg)
val = default
self.contentsLock.release()
return val
示例5: getStrCSV
def getStrCSV(self, key, default = None, count = None):
"""
Fetches the given key as a comma separated value. This provides back a list
with the stripped values.
Arguments:
key - config setting to be fetched
default - value provided if no such key exists or doesn't match the count
count - if set, then a TypeError is logged (and default returned) if
the number of elements doesn't match the count
"""
confValue = self.getValue(key)
if confValue == None: return default
else:
confComp = [entry.strip() for entry in confValue.split(",")]
# check if the count doesn't match
if count != None and len(confComp) != count:
msg = "Config entry '%s' is expected to be %i comma separated values" % (key, count)
if default != None and (isinstance(default, list) or isinstance(default, tuple)):
defaultStr = ", ".join([str(i) for i in default])
msg += ", defaulting to '%s'" % defaultStr
log.log(CONFIG["log.configEntryTypeError"], msg)
return default
return confComp
示例6: load
def load(self, logFailure = False):
"""
Loads or reloads the torrc contents, raising an IOError if there's a
problem.
Arguments:
logFailure - if the torrc fails to load and we've never provided a
warning for this before then logs a warning
"""
self.valsLock.acquire()
# clears contents and caches
self.contents, self.configLocation = None, None
self.displayableContents = None
self.strippedContents = None
self.corrections = None
try:
self.configLocation = getConfigLocation()
configFile = open(self.configLocation, "r")
self.contents = configFile.readlines()
configFile.close()
except IOError, exc:
if logFailure and not self.isLoadFailWarned:
msg = "Unable to load torrc (%s)" % sysTools.getFileErrorMsg(exc)
log.log(CONFIG["log.torrc.readFailed"], msg)
self.isLoadFailWarned = True
self.valsLock.release()
raise exc
示例7: __init__
def __init__(self, stdscr, loggedEvents, config=None):
panel.Panel.__init__(self, stdscr, "log", 0)
threading.Thread.__init__(self)
self.setDaemon(True)
# Make sure that the msg.* messages are loaded. Lazy loading it later is
# fine, but this way we're sure it happens before warning about unused
# config options.
loadLogMessages()
# regex filters the user has defined
self.filterOptions = []
self._config = dict(DEFAULT_CONFIG)
if config:
config.update(self._config, {
"features.log.maxLinesPerEntry": 1,
"features.log.prepopulateReadLimit": 0,
"features.log.maxRefreshRate": 10,
"cache.logPanel.size": 1000})
for filter in self._config["features.log.regex"]:
# checks if we can't have more filters
if len(self.filterOptions) >= MAX_REGEX_FILTERS: break
try:
re.compile(filter)
self.filterOptions.append(filter)
except re.error, exc:
msg = "Invalid regular expression pattern (%s): %s" % (exc, filter)
log.log(self._config["log.configEntryTypeError"], msg)
示例8: _workerLoop
def _workerLoop(self):
"""
Simple producer-consumer loop followed by worker threads. This takes
addresses from the unresolvedQueue, attempts to look up its hostname, and
adds its results or the error to the resolved cache. Resolver reference
provides shared resources used by the thread pool.
"""
while not self.halt:
# if resolver is paused then put a hold on further resolutions
if self.isPaused:
self.cond.acquire()
if not self.halt: self.cond.wait(1)
self.cond.release()
continue
# snags next available ip, timeout is because queue can't be woken up
# when 'halt' is set
try: ipAddr = self.unresolvedQueue.get_nowait()
except Queue.Empty:
# no elements ready, wait a little while and try again
self.cond.acquire()
if not self.halt: self.cond.wait(1)
self.cond.release()
continue
if self.halt: break
try:
if self.useSocketResolution: result = _resolveViaSocket(ipAddr)
else: result = _resolveViaHost(ipAddr)
except IOError, exc: result = exc # lookup failed
except ValueError, exc: result = exc # dns error
self.resolvedLock.acquire()
self.resolvedCache[ipAddr] = (result, RESOLVER_COUNTER.next())
# trim cache if excessively large (clearing out oldest entries)
if len(self.resolvedCache) > CONFIG["cache.hostnames.size"]:
# Providing for concurrent, non-blocking calls require that entries are
# never removed from the cache, so this creates a new, trimmed version
# instead.
# determines minimum age of entries to be kept
currentCount = RESOLVER_COUNTER.next()
newCacheSize = CONFIG["cache.hostnames.size"] - CONFIG["cache.hostnames.trimSize"]
threshold = currentCount - newCacheSize
newCache = {}
msg = "trimming hostname cache from %i entries to %i" % (len(self.resolvedCache), newCacheSize)
log.log(CONFIG["log.hostnameCacheTrimmed"], msg)
# checks age of each entry, adding to toDelete if too old
for ipAddr, entry in self.resolvedCache.iteritems():
if entry[1] >= threshold: newCache[ipAddr] = entry
self.resolvedCache = newCache
self.resolvedLock.release()
示例9: loadConfig
def loadConfig(config):
config.update(CONFIG)
CONFIG["features.colorOverride"] = "none"
colorOverride = config.get("features.colorOverride", "none")
if colorOverride != "none":
try: setColorOverride(colorOverride)
except ValueError, exc:
log.log(CONFIG["log.configEntryTypeError"], exc)
示例10: isExitAllowed
def isExitAllowed(ip, port, exitPolicy, isPrivateRejected):
"""
Determines if a given connection is a permissable exit with the given
policy or not (True if it's allowed to be an exit connection, False
otherwise).
NOTE: this is a little tricky and liable to need some tweaks
"""
# might not be set when first starting up
if not exitPolicy: return True
# TODO: move into a utility and craft some unit tests (this is very error
# prone...)
# TODO: currently doesn't consider ExitPolicyRejectPrivate (which prevents
# connections to private networks and local ip)
for entry in exitPolicy.split(","):
entry = entry.strip()
isAccept = entry.startswith("accept")
entry = entry[7:] # strips off "accept " or "reject "
# parses ip address (with mask if provided) and port
if ":" in entry:
entryIP = entry[:entry.find(":")]
entryPort = entry[entry.find(":") + 1:]
else:
entryIP = entry
entryPort = "*"
#raise AssertionError(str(exitPolicy) + " - " + entryIP + ":" + entryPort)
isIPMatch = entryIP == ip or entryIP[0] == "*"
if not "-" in entryPort:
# single port
isPortMatch = entryPort == str(port) or entryPort[0] == "*"
else:
# port range
minPort = int(entryPort[:entryPort.find("-")])
maxPort = int(entryPort[entryPort.find("-") + 1:])
isPortMatch = port >= minPort and port <= maxPort
# TODO: Currently being lazy and considering subnet masks or 'private'
# keyword to be equivilant to wildcard if it would reject, and none
# if it would accept (ie, being conservative with acceptance). Would be
# nice to fix at some point.
if not isAccept: isIPMatch |= "/" in entryIP or entryIP == "private"
if isIPMatch and isPortMatch: return isAccept
# we shouldn't ever fall through due to default exit policy
log.log(log.WARN, "Exit policy left connection uncategorized: %s:%i" % (ip, port))
return False
示例11: eventTick
def eventTick(self):
"""
Processes a ps event.
"""
psResults = {} # mapping of stat names to their results
if self.queryPid and self.queryParam and self.failedCount < FAILURE_THRESHOLD:
queryCmd = "ps -p %s -o %s" % (self.queryPid, ",".join(self.queryParam))
psCall = sysTools.call(queryCmd, self.cacheTime, True)
if psCall and len(psCall) == 2:
# ps provided results (first line is headers, second is stats)
stats = psCall[1].strip().split()
if len(self.queryParam) == len(stats):
# we have a result to match each stat - constructs mapping
psResults = dict([(self.queryParam[i], stats[i]) for i in range(len(stats))])
self.failedCount = 0 # had a successful call - reset failure count
if not psResults:
# ps call failed, if we fail too many times sequentially then abandon
# listing (probably due to invalid ps parameters)
self.failedCount += 1
if self.failedCount == FAILURE_THRESHOLD:
msg = "failed several attempts to query '%s', abandoning ps graph" % queryCmd
log.log(self._config["log.graph.ps.abandon"], msg)
# if something fails (no pid, ps call failed, etc) then uses last results
primary, secondary = self.lastPrimary, self.lastSecondary
for isPrimary in (True, False):
if isPrimary: statName = self._config["features.graph.ps.primaryStat"]
else: statName = self._config["features.graph.ps.secondaryStat"]
if statName in psResults:
try:
result = float(psResults[statName])
# The 'rss' and 'size' parameters provide memory usage in KB. This is
# scaled up to MB so the graph's y-high is a reasonable value.
if statName in ("rss", "size"): result /= 1024.0
if isPrimary: primary = result
else: secondary = result
except ValueError:
if self.queryParam != HEADER_PS_PARAM:
# custom stat provides non-numeric results - give a warning and stop querying it
msg = "unable to use non-numeric ps stat '%s' for graphing" % statName
log.log(self._config["log.graph.ps.invalidStat"], msg)
self.queryParam.remove(statName)
self._processEvent(primary, secondary)
示例12: __init__
def __init__(self, processName, processPid = "", resolveRate = None, handle = None):
"""
Initializes a new resolver daemon. When no longer needed it's suggested
that this is stopped.
Arguments:
processName - name of the process being resolved
processPid - pid of the process being resolved
resolveRate - time between resolving connections (in seconds, None if
chosen dynamically)
handle - name used to query this resolver, this is the processName
if undefined
"""
threading.Thread.__init__(self)
self.setDaemon(True)
self.processName = processName
self.processPid = processPid
self.resolveRate = resolveRate
self.handle = handle if handle else processName
self.defaultRate = CONFIG["queries.connections.minRate"]
self.lastLookup = -1
self.overwriteResolver = None
self.defaultResolver = Resolver.PROC
osType = os.uname()[0]
self.resolverOptions = getSystemResolvers(osType)
log.log(CONFIG["log.connResolverOptions"], "Operating System: %s, Connection Resolvers: %s" % (osType, ", ".join(self.resolverOptions)))
# sets the default resolver to be the first found in the system's PATH
# (left as netstat if none are found)
for resolver in self.resolverOptions:
# Resolver strings correspond to their command with the exception of bsd
# resolvers.
resolverCmd = resolver.replace(" (bsd)", "")
if resolver == Resolver.PROC or sysTools.isAvailable(resolverCmd):
self.defaultResolver = resolver
break
self._connections = [] # connection cache (latest results)
self._resolutionCounter = 0 # number of successful connection resolutions
self._isPaused = False
self._halt = False # terminates thread if true
self._cond = threading.Condition() # used for pausing the thread
self._subsiquentFailures = 0 # number of failed resolutions with the default in a row
self._resolverBlacklist = [] # resolvers that have failed to resolve
# Number of sequential times the threshold rate's been too low. This is to
# avoid having stray spikes up the rate.
self._rateThresholdBroken = 0
示例13: handleKey
def handleKey(self, key):
isKeystrokeConsumed = True
if key in (ord('n'), ord('N')) and torTools.getConn().isNewnymAvailable():
self.sendNewnym()
elif key in (ord('r'), ord('R')) and not self._isTorConnected:
torctlConn = None
allowPortConnection, allowSocketConnection, _ = starter.allowConnectionTypes()
if os.path.exists(self._config["startup.interface.socket"]) and allowSocketConnection:
try: torctlConn = torTools.connect_socket(self._config["startup.interface.socket"])
except IOError, exc:
if not allowPortConnection:
cli.popups.showMsg("Unable to reconnect (%s)" % exc, 3)
elif not allowPortConnection:
cli.popups.showMsg("Unable to reconnect (socket '%s' doesn't exist)" % self._config["startup.interface.socket"], 3)
if not torctlConn and allowPortConnection:
# TODO: This has diverged from starter.py's connection, for instance it
# doesn't account for relative cookie paths or multiple authentication
# methods. We can't use the starter.py's connection function directly
# due to password prompts, but we could certainly make this mess more
# manageable.
try:
ctlAddr, ctlPort = self._config["startup.interface.ipAddress"], self._config["startup.interface.port"]
tmpConn, authType, authValue = TorCtl.TorCtl.preauth_connect(ctlAddr, ctlPort)
if authType == TorCtl.TorCtl.AUTH_TYPE.PASSWORD:
authValue = cli.popups.inputPrompt("Controller Password: ")
if not authValue: raise IOError() # cancel reconnection
elif authType == TorCtl.TorCtl.AUTH_TYPE.COOKIE:
authCookieSize = os.path.getsize(authValue)
if authCookieSize != 32:
raise IOError("authentication cookie '%s' is the wrong size (%i bytes instead of 32)" % (authValue, authCookieSize))
tmpConn.authenticate(authValue)
torctlConn = tmpConn
except Exception, exc:
# attempts to use the wizard port too
try:
cli.controller.getController().getTorManager().connectManagedInstance()
log.log(log.NOTICE, "Reconnected to Tor's control port")
cli.popups.showMsg("Tor reconnected", 1)
except:
# displays notice for the first failed connection attempt
if exc.args: cli.popups.showMsg("Unable to reconnect (%s)" % exc, 3)
示例14: __init__
def __init__(self, processName, processPid = "", resolveRate = None):
"""
Initializes a new resolver daemon. When no longer needed it's suggested
that this is stopped.
Arguments:
processName - name of the process being resolved
processPid - pid of the process being resolved
resolveRate - time between resolving connections (in seconds, None if
chosen dynamically)
"""
threading.Thread.__init__(self)
self.setDaemon(True)
self.processName = processName
self.processPid = processPid
self.resolveRate = resolveRate
self.defaultRate = CONFIG["queries.connections.minRate"]
self.lastLookup = -1
self.overwriteResolver = None
self.defaultResolver = CMD_NETSTAT
osType = os.uname()[0]
self.resolverOptions = getSystemResolvers(osType)
resolverLabels = ", ".join([CMD_STR[option] for option in self.resolverOptions])
log.log(CONFIG["log.connResolverOptions"], "Operating System: %s, Connection Resolvers: %s" % (osType, resolverLabels))
# sets the default resolver to be the first found in the system's PATH
# (left as netstat if none are found)
for resolver in self.resolverOptions:
if sysTools.isAvailable(CMD_STR[resolver]):
self.defaultResolver = resolver
break
self._connections = [] # connection cache (latest results)
self._isPaused = False
self._halt = False # terminates thread if true
self._cond = threading.Condition() # used for pausing the thread
self._subsiquentFailures = 0 # number of failed resolutions with the default in a row
self._resolverBlacklist = [] # resolvers that have failed to resolve
# Number of sequential times the threshold rate's been too low. This is to
# avoid having stray spikes up the rate.
self._rateThresholdBroken = 0
示例15: _initColors
def _initColors():
"""
Initializes color mappings usable by curses. This can only be done after
calling curses.initscr().
"""
global COLOR_ATTR_INITIALIZED, COLOR_IS_SUPPORTED
if not COLOR_ATTR_INITIALIZED:
# hack to replace all ACS characters with '+' if ACS support has been
# manually disabled
if not CONFIG["features.acsSupport"]:
for item in curses.__dict__:
if item.startswith("ACS_"):
curses.__dict__[item] = ord('+')
# replace a few common border pipes that are better rendered as '|' or
# '-' instead
curses.ACS_SBSB = ord('|')
curses.ACS_VLINE = ord('|')
curses.ACS_BSBS = ord('-')
curses.ACS_HLINE = ord('-')
COLOR_ATTR_INITIALIZED = True
COLOR_IS_SUPPORTED = False
if not CONFIG["features.colorInterface"]: return
try: COLOR_IS_SUPPORTED = curses.has_colors()
except curses.error: return # initscr hasn't been called yet
# initializes color mappings if color support is available
if COLOR_IS_SUPPORTED:
colorpair = 0
log.log(CONFIG["log.cursesColorSupport"], "Terminal color support detected and enabled")
for colorName in COLOR_LIST:
fgColor = COLOR_LIST[colorName]
bgColor = -1 # allows for default (possibly transparent) background
colorpair += 1
curses.init_pair(colorpair, fgColor, bgColor)
COLOR_ATTR[colorName] = curses.color_pair(colorpair)
else:
log.log(CONFIG["log.cursesColorSupport"], "Terminal color support unavailable")