zlib: Don't use PASTE for INTMAX error messages
[jimtcl.git] / jim-syslog.c
blob9e279ef09d1a7dfbb80cef969b68059bf50dd4d1
1 /* Syslog interface for tcl
2 * Copyright Victor Wagner <vitus@ice.ru> at
3 * http://www.ice.ru/~vitus/works/tcl.html#syslog
5 * Slightly modified by Steve Bennett <steveb@snapgear.com>
6 * Ported to Jim by Steve Bennett <steveb@workware.net.au>
7 */
8 #include <syslog.h>
9 #include <string.h>
11 #include <jim.h>
13 typedef struct
15 int logOpened;
16 int facility;
17 int options;
18 char ident[32];
19 } SyslogInfo;
21 #ifndef LOG_AUTHPRIV
22 # define LOG_AUTHPRIV LOG_AUTH
23 #endif
25 static const char * const facilities[] = {
26 [LOG_AUTHPRIV] = "authpriv",
27 [LOG_CRON] = "cron",
28 [LOG_DAEMON] = "daemon",
29 [LOG_KERN] = "kernel",
30 [LOG_LPR] = "lpr",
31 [LOG_MAIL] = "mail",
32 [LOG_NEWS] = "news",
33 [LOG_SYSLOG] = "syslog",
34 [LOG_USER] = "user",
35 [LOG_UUCP] = "uucp",
36 [LOG_LOCAL0] = "local0",
37 [LOG_LOCAL1] = "local1",
38 [LOG_LOCAL2] = "local2",
39 [LOG_LOCAL3] = "local3",
40 [LOG_LOCAL4] = "local4",
41 [LOG_LOCAL5] = "local5",
42 [LOG_LOCAL6] = "local6",
43 [LOG_LOCAL7] = "local7",
46 static const char * const priorities[] = {
47 [LOG_EMERG] = "emerg",
48 [LOG_ALERT] = "alert",
49 [LOG_CRIT] = "crit",
50 [LOG_ERR] = "error",
51 [LOG_WARNING] = "warning",
52 [LOG_NOTICE] = "notice",
53 [LOG_INFO] = "info",
54 [LOG_DEBUG] = "debug",
57 /**
58 * Deletes the syslog command.
60 static void Jim_SyslogCmdDelete(Jim_Interp *interp, void *privData)
62 SyslogInfo *info = (SyslogInfo *) privData;
64 if (info->logOpened) {
65 closelog();
67 Jim_Free(info);
70 /* Syslog_Log -
71 * implements syslog tcl command. General format: syslog ?options? level text
72 * options -facility -ident -options
74 * syslog ?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? text
76 int Jim_SyslogCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
78 int priority = LOG_INFO;
79 int i = 1;
80 SyslogInfo *info = Jim_CmdPrivData(interp);
82 if (argc <= 1) {
83 wrongargs:
84 Jim_WrongNumArgs(interp, 1, argv,
85 "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
86 return JIM_ERR;
88 while (i < argc - 1) {
89 if (Jim_CompareStringImmediate(interp, argv[i], "-facility")) {
90 int entry =
91 Jim_FindByName(Jim_String(argv[i + 1]), facilities,
92 sizeof(facilities) / sizeof(*facilities));
93 if (entry < 0) {
94 Jim_SetResultString(interp, "Unknown facility", -1);
95 return JIM_ERR;
97 if (info->facility != entry) {
98 info->facility = entry;
99 if (info->logOpened) {
100 closelog();
101 info->logOpened = 0;
105 else if (Jim_CompareStringImmediate(interp, argv[i], "-options")) {
106 long tmp;
108 if (Jim_GetLong(interp, argv[i + 1], &tmp) == JIM_ERR) {
109 return JIM_ERR;
111 info->options = tmp;
112 if (info->logOpened) {
113 closelog();
114 info->logOpened = 0;
117 else if (Jim_CompareStringImmediate(interp, argv[i], "-ident")) {
118 strncpy(info->ident, Jim_String(argv[i + 1]), sizeof(info->ident));
119 info->ident[sizeof(info->ident) - 1] = 0;
120 if (info->logOpened) {
121 closelog();
122 info->logOpened = 0;
125 else {
126 break;
128 i += 2;
131 /* There should be either 0, 1 or 2 args left */
132 if (i == argc) {
133 /* No args, but they have set some options, so OK */
134 return JIM_OK;
137 if (i < argc - 1) {
138 priority =
139 Jim_FindByName(Jim_String(argv[i]), priorities,
140 sizeof(priorities) / sizeof(*priorities));
141 if (priority < 0) {
142 Jim_SetResultString(interp, "Unknown priority", -1);
143 return JIM_ERR;
145 i++;
148 if (i != argc - 1) {
149 goto wrongargs;
151 if (!info->logOpened) {
152 if (!info->ident[0]) {
153 Jim_Obj *argv0 = Jim_GetGlobalVariableStr(interp, "argv0", JIM_NONE);
155 if (argv0) {
156 strncpy(info->ident, Jim_String(argv0), sizeof(info->ident));
158 else {
159 strcpy(info->ident, "Tcl script");
161 info->ident[sizeof(info->ident) - 1] = 0;
163 openlog(info->ident, info->options, info->facility);
164 info->logOpened = 1;
166 syslog(priority, "%s", Jim_String(argv[i]));
168 return JIM_OK;
171 int Jim_syslogInit(Jim_Interp *interp)
173 SyslogInfo *info;
175 if (Jim_PackageProvide(interp, "syslog", "1.0", JIM_ERRMSG))
176 return JIM_ERR;
178 info = Jim_Alloc(sizeof(*info));
180 info->logOpened = 0;
181 info->options = 0;
182 info->facility = LOG_USER;
183 info->ident[0] = 0;
185 Jim_CreateCommand(interp, "syslog", Jim_SyslogCmd, info, Jim_SyslogCmdDelete);
187 return JIM_OK;