|
| 1 | + |
| 2 | +import sys |
| 3 | +import os |
| 4 | +from os import libc |
| 5 | + |
| 6 | +openlog_ = libc.func("v", "openlog", "sii") |
| 7 | +setlogmask_ = libc.func("i", "setlogmask", "i") |
| 8 | +syslog_ = libc.func("v", "syslog", "is") |
| 9 | +isatty_ = libc.func("i", "isatty", "i") |
| 10 | + |
| 11 | +# Syslog priorities |
| 12 | +CRITICAL = 2 |
| 13 | +ERROR = 3 |
| 14 | +WARNING = 4 |
| 15 | +NOTICE = 5 |
| 16 | +INFO = 6 |
| 17 | +DEBUG = 7 |
| 18 | +NOTSET = 0 |
| 19 | + |
| 20 | +# Facility codes |
| 21 | +LOG_USER = (1<<3) # random user-level messages |
| 22 | + |
| 23 | +# Option flags for openlog |
| 24 | +LOG_PID = 0x01 # log the pid with each message |
| 25 | +LOG_CONS = 0x02 # log on the console if errors in sending |
| 26 | +LOG_ODELAY = 0x04 # delay open until first syslog() (default) |
| 27 | +LOG_NDELAY = 0x08 # don't delay open |
| 28 | +LOG_NOWAIT = 0x10 # don't wait for console forks: DEPRECATED |
| 29 | +LOG_PERROR = 0x20 # log to stderr as well |
| 30 | + |
| 31 | +def _logmask_upto(pri): |
| 32 | + return ((1<<((pri)+1))-1) |
| 33 | + |
| 34 | +class Logger: |
| 35 | + |
| 36 | + def __init__(self, name): |
| 37 | + self.name = name |
| 38 | + |
| 39 | + def log(self, level, msg, *args): |
| 40 | + if self.name is not None: |
| 41 | + s = ('%s:'+ msg) % ((self.name,) + args) |
| 42 | + else: |
| 43 | + s = msg % args |
| 44 | + syslog_(level, s) |
| 45 | + |
| 46 | + def debug(self, msg, *args): |
| 47 | + self.log(DEBUG, msg, *args) |
| 48 | + |
| 49 | + def info(self, msg, *args): |
| 50 | + self.log(INFO, msg, *args) |
| 51 | + |
| 52 | + def warning(self, msg, *args): |
| 53 | + self.log(WARNING, msg, *args) |
| 54 | + |
| 55 | + def error(self, msg, *args): |
| 56 | + self.log(ERROR, msg, *args) |
| 57 | + |
| 58 | + def critical(self, msg, *args): |
| 59 | + self.log(CRITICAL, msg, *args) |
| 60 | + |
| 61 | + |
| 62 | +_level = ERROR |
| 63 | +_loggers = {} |
| 64 | + |
| 65 | +r = isatty_(sys.stdout.fileno()) |
| 66 | +os.check_error(r) |
| 67 | + |
| 68 | +flags = LOG_CONS | LOG_PID |
| 69 | +# if we are outputting to a tty log also to stderr |
| 70 | +if r > 0: |
| 71 | + flags |= LOG_PERROR |
| 72 | +ident = 'python' |
| 73 | +if len(sys.argv): |
| 74 | + ident = sys.argv[0] |
| 75 | +openlog_(ident, flags, LOG_USER) |
| 76 | + |
| 77 | +r = setlogmask_(_logmask_upto(_level)) |
| 78 | +os.check_error(r) |
| 79 | + |
| 80 | +def getLogger(name): |
| 81 | + if name in _loggers: |
| 82 | + return _loggers[name] |
| 83 | + l = Logger(name) |
| 84 | + _loggers[name] = l |
| 85 | + return l |
| 86 | + |
| 87 | +def info(msg, *args): |
| 88 | + getLogger(None).info(msg, *args) |
| 89 | + |
| 90 | +def debug(msg, *args): |
| 91 | + getLogger(None).debug(msg, *args) |
| 92 | + |
| 93 | +def basicConfig(level=INFO, filename=None, format=None): |
| 94 | + global _level |
| 95 | + _level = level |
| 96 | + if filename is not None: |
| 97 | + print("logging.basicConfig: filename arg is not supported") |
| 98 | + if format is not None: |
| 99 | + print("logging.basicConfig: format arg is not supported") |
0 commit comments