Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from log import Handler
3c"""source-before-namespace
4#include <syslog.h>
6static inline void openlog_wrapper(const char *ident, int option, int facility)
7{
8 openlog(ident, option, facility);
9}
11static inline void syslog_wrapper(int priority, const char *message_p)
12{
13 syslog(priority, "%s", message_p);
14}
16static inline void closelog_wrapper()
17{
18 closelog();
19}
21static inline int setlogmask_wrapper(int mask)
22{
23 return setlogmask(mask);
24}
25"""
27# Levels.
28LEVEL_EMERG: i64 = c"LOG_EMERG"
29LEVEL_ALERT: i64 = c"LOG_ALERT"
30LEVEL_CRIT: i64 = c"LOG_CRIT"
31LEVEL_ERR: i64 = c"LOG_ERR"
32LEVEL_WARNING: i64 = c"LOG_WARNING"
33LEVEL_NOTICE: i64 = c"LOG_NOTICE"
34LEVEL_INFO: i64 = c"LOG_INFO"
35LEVEL_DEBUG: i64 = c"LOG_DEBUG"
37# Facilities.
38FACILITY_KERN: i64 = c"LOG_KERN"
39FACILITY_USER: i64 = c"LOG_USER"
40FACILITY_MAIL: i64 = c"LOG_MAIL"
41FACILITY_DAEMON: i64 = c"LOG_DAEMON"
42FACILITY_AUTH: i64 = c"LOG_AUTH"
43FACILITY_LPR: i64 = c"LOG_LPR"
44FACILITY_NEWS: i64 = c"LOG_NEWS"
45FACILITY_UUCP: i64 = c"LOG_UUCP"
46FACILITY_CRON: i64 = c"LOG_CRON"
47FACILITY_SYSLOG: i64 = c"LOG_SYSLOG"
48FACILITY_LOCAL0: i64 = c"LOG_LOCAL0"
49FACILITY_LOCAL1: i64 = c"LOG_LOCAL1"
50FACILITY_LOCAL2: i64 = c"LOG_LOCAL2"
51FACILITY_LOCAL3: i64 = c"LOG_LOCAL3"
52FACILITY_LOCAL4: i64 = c"LOG_LOCAL4"
53FACILITY_LOCAL5: i64 = c"LOG_LOCAL5"
54FACILITY_LOCAL6: i64 = c"LOG_LOCAL6"
55FACILITY_LOCAL7: i64 = c"LOG_LOCAL7"
56FACILITY_AUTHPRIV: i64 = c"LOG_AUTHPRIV"
58# Options.
59OPTION_PID: i64 = c"LOG_PID"
60OPTION_CONS: i64 = c"LOG_CONS"
61OPTION_NDELAY: i64 = c"LOG_NDELAY"
62OPTION_PERROR: i64 = c"LOG_PERROR"
64func openlog(ident: string, option: i64, facility: i64):
65 """Opens a connection to the system logger for a program.
67 """
69 utf8_ident = ident.to_utf8()
70 utf8_ident += 0
72 c"openlog_wrapper((const char *)utf8_ident.m_bytes->data(), option, facility);"
74macro SYSLOG(priority: i64, message: string):
75 """Generates a log message.
77 """
79 if (setlogmask(0) & priority) == 0:
80 return
82 utf8_message = message.to_utf8()
83 utf8_message += 0
85 c"syslog_wrapper(priority, (const char *)utf8_message.m_bytes->data());"
87func closelog():
88 """Closes the file descriptor being used to write to the system
89 logger.
91 """
93 c"closelog_wrapper();"
95func setlogmask(mask: i64) -> i64:
96 """Sets the logmask for the calling process, and returns the previous
97 mask. If the mask argument is 0, the current logmask is not
98 modified.
100 """
102 res: i64 = 0
104 c"res = setlogmask_wrapper(mask);"
106 return res
108func log_upto(level: i64) -> i64:
109 """Calculates the mask for all levels up to and including given level.
111 """
113 mask: i64 = 0
115 c"""
116 mask = LOG_UPTO(level);
117 """
119 return mask
121class SyslogHandler(Handler):
122 """Writes log messages to the system log.
124 """
126 func write(self, message: string):
127 """Writes given log message to the system log.
129 """
131 SYSLOG(LEVEL_DEBUG, message)
133test syslog():
134 openlog("mys-syslog", 0, FACILITY_USER)
135 SYSLOG(LEVEL_WARNING, "Mys testing syslog")
136 setlogmask(log_upto(LEVEL_ERR))
137 SYSLOG(LEVEL_DEBUG, "Mys testing syslog 2")
138 closelog()