| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
This module provides an interface to the Unix syslog library routines. |
| 5 |
Refer to the Unix manual pages for a detailed description of the |
| 6 |
syslog facility. |
| 7 |
""" |
| 8 |
|
| 9 |
import sys |
| 10 |
if sys.platform == 'win32': |
| 11 |
raise ModuleNotFoundError("No syslog on Windows", name="syslog") |
| 12 |
|
| 13 |
try: from __pypy__ import builtinify |
| 14 |
except ImportError: builtinify = lambda f: f |
| 15 |
|
| 16 |
from _syslog_cffi import ffi, lib |
| 17 |
|
| 18 |
_S_log_open = False |
| 19 |
_S_ident_o = None |
| 20 |
|
| 21 |
def _get_argv(): |
| 22 |
try: |
| 23 |
import sys |
| 24 |
script = sys.argv[0] |
| 25 |
if isinstance(script, str): |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
return script[script.rfind('/')+1:] or None |
| 30 |
except Exception: |
| 31 |
pass |
| 32 |
return None |
| 33 |
|
| 34 |
@builtinify |
| 35 |
def openlog(ident=None, logoption=0, facility=lib.LOG_USER): |
| 36 |
global _S_ident_o, _S_log_open |
| 37 |
if ident is None: |
| 38 |
ident = _get_argv() |
| 39 |
orig_ident = ident |
| 40 |
if ident is None: |
| 41 |
_S_ident_o = ffi.NULL |
| 42 |
else: |
| 43 |
if not isinstance(ident, str): |
| 44 |
msg = "openlog() argument 1 must be a str, not {!r}" |
| 45 |
raise TypeError(msg.format(type(ident).__name__)) |
| 46 |
ident = ident.encode(sys.getdefaultencoding()) |
| 47 |
_S_ident_o = ffi.new("char[]", ident) |
| 48 |
sys.audit('syslog.openlog', orig_ident, logoption, facility) |
| 49 |
lib.openlog(_S_ident_o, logoption, facility) |
| 50 |
_S_log_open = True |
| 51 |
|
| 52 |
@builtinify |
| 53 |
def syslog(arg1, arg2=None): |
| 54 |
if arg2 is not None: |
| 55 |
priority, message = arg1, arg2 |
| 56 |
else: |
| 57 |
priority, message = LOG_INFO, arg1 |
| 58 |
sys.audit("syslog.syslog", priority, message) |
| 59 |
|
| 60 |
if not _S_log_open: |
| 61 |
openlog() |
| 62 |
if not isinstance(message, str): |
| 63 |
raise TypeError("syslog() message must be a str, not {!r}".format( |
| 64 |
type(message).__name__)) |
| 65 |
message = message.encode(sys.getdefaultencoding()) |
| 66 |
lib.syslog(priority, b"%s", message) |
| 67 |
|
| 68 |
@builtinify |
| 69 |
def closelog(): |
| 70 |
global _S_log_open, S_ident_o |
| 71 |
sys.audit("syslog.closelog") |
| 72 |
if _S_log_open: |
| 73 |
lib.closelog() |
| 74 |
_S_log_open = False |
| 75 |
_S_ident_o = None |
| 76 |
|
| 77 |
@builtinify |
| 78 |
def setlogmask(mask): |
| 79 |
sys.audit("syslog.setlogmask", mask) |
| 80 |
return lib.setlogmask(mask) |
| 81 |
|
| 82 |
@builtinify |
| 83 |
def LOG_MASK(pri): |
| 84 |
return (1 << pri) |
| 85 |
|
| 86 |
@builtinify |
| 87 |
def LOG_UPTO(pri): |
| 88 |
return (1 << (pri + 1)) - 1 |
| 89 |
|
| 90 |
__all__ = [] |
| 91 |
|
| 92 |
for name in dir(lib): |
| 93 |
if name.startswith('LOG_'): |
| 94 |
value = getattr(lib, name) |
| 95 |
if value != -919919: |
| 96 |
globals()[name] = value |
| 97 |
__all__.append(name) |
| 98 |
|
| 99 |
__all__ = tuple(__all__) + ( |
| 100 |
'openlog', 'syslog', 'closelog', 'setlogmask', |
| 101 |
'LOG_MASK', 'LOG_UPTO') |