-# Copyright (c) 2011, 2012, 2013 Nicira, Inc.
+# Copyright (c) 2011, 2012, 2013, 2015, 2016 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
import sys
import threading
+import six
+from six.moves import range
+
import ovs.dirs
import ovs.unixctl
import ovs.util
"emer": logging.CRITICAL,
"off": logging.CRITICAL
}
+FACILITIES = ['auth', 'authpriv', 'cron', 'daemon', 'ftp', 'kern', 'lpr',
+ 'mail', 'news', 'syslog', 'user', 'uucp', 'local0', 'local1',
+ 'local2', 'local3', 'local4', 'local5', 'local6', 'local7']
+syslog_facility = "daemon"
+syslog_handler = ''
def get_level(level_str):
return LEVELS.get(level_str.lower())
-class Vlog:
+class Vlog(object):
__inited = False
__msg_num = 0
__start_time = 0
msg_num = Vlog.__msg_num
Vlog.__msg_num += 1
- for f, f_level in Vlog.__mfl[self.name].iteritems():
+ for f, f_level in six.iteritems(Vlog.__mfl[self.name]):
f_level = LEVELS.get(f_level, logging.CRITICAL)
if level_num >= f_level:
msg = self._build_message(message, f, level, msg_num)
matches = formatting.match(match)
# Do we need to apply padding?
if not matches.group(1) and replace != "":
- replace = replace.center(len(replace)+2)
+ replace = replace.center(len(replace) + 2)
# Does the field have a minimum width
if matches.group(2):
min_width = int(matches.group(2))
def __is_enabled(self, level):
level = LEVELS.get(level.lower(), logging.DEBUG)
- for f, f_level in Vlog.__mfl[self.name].iteritems():
+ for f, f_level in six.iteritems(Vlog.__mfl[self.name]):
f_level = LEVELS.get(f_level, logging.CRITICAL)
if level >= f_level:
return True
if f == "console":
logger.addHandler(logging.StreamHandler(sys.stderr))
elif f == "syslog":
- logger.addHandler(logging.handlers.SysLogHandler(
- address="/dev/log",
- facility=logging.handlers.SysLogHandler.LOG_DAEMON))
+ Vlog.add_syslog_handler()
elif f == "file" and Vlog.__log_file:
Vlog.__file_handler = logging.FileHandler(Vlog.__log_file)
logger.addHandler(Vlog.__file_handler)
ovs.unixctl.command_register("vlog/reopen", "", 0, 0,
Vlog._unixctl_vlog_reopen, None)
- ovs.unixctl.command_register("vlog/set", "spec", 1, sys.maxint,
+ ovs.unixctl.command_register("vlog/close", "", 0, 0,
+ Vlog._unixctl_vlog_close, None)
+ ovs.unixctl.command_register("vlog/set", "spec", 1, sys.maxsize,
Vlog._unixctl_vlog_set, None)
ovs.unixctl.command_register("vlog/list", "", 0, 0,
Vlog._unixctl_vlog_list, None)
return
if module == "any":
- modules = Vlog.__mfl.keys()
+ modules = list(Vlog.__mfl.keys())
else:
modules = [module]
if destination == "any":
- destinations = DESTINATIONS.keys()
+ destinations = list(DESTINATIONS.keys())
else:
destinations = [destination]
destination = destination.lower()
Vlog.__log_patterns[destination] = pattern
+ @staticmethod
+ def add_syslog_handler(facility=None):
+ global syslog_facility, syslog_handler
+
+ # If handler is already added and there is no change in 'facility',
+ # there is nothing to do.
+ if (not facility or facility == syslog_facility) and syslog_handler:
+ return
+
+ logger = logging.getLogger('syslog')
+ # If there is no infrastructure to support python syslog, increase
+ # the logging severity level to avoid repeated errors.
+ if not os.path.exists("/dev/log"):
+ logger.setLevel(logging.CRITICAL)
+ return
+
+ if syslog_handler:
+ logger.removeHandler(syslog_handler)
+
+ if facility:
+ syslog_facility = facility
+
+ syslog_handler = logging.handlers.SysLogHandler(address="/dev/log",
+ facility=syslog_facility)
+ logger.addHandler(syslog_handler)
+ return
+
@staticmethod
def set_levels_from_string(s):
module = None
return "Destination %s does not exist" % words[1]
except IndexError:
return "Please supply a valid pattern and destination"
+ elif words[0] == "FACILITY":
+ if words[1] in FACILITIES:
+ Vlog.add_syslog_handler(words[1])
+ return
+ else:
+ return "Facility %s is invalid" % words[1]
for word in [w.lower() for w in words]:
if word == "any":
else:
conn.reply("Logging to file not configured")
+ @staticmethod
+ def _unixctl_vlog_close(conn, unused_argv, unused_aux):
+ if Vlog.__log_file:
+ logger = logging.getLogger("file")
+ logger.removeHandler(Vlog.__file_handler)
+ conn.reply(None)
+
@staticmethod
def _unixctl_vlog_set(conn, argv, unused_aux):
for arg in argv: