jim.c: Fix Jim_ReplaceHashEntry() for ref counted objects
[jimtcl.git] / jim-syslog.c
blobbd373b84f0fc39fcb39e4c315158e435db5daf5a
2 /* Syslog interface for tcl
3 * Copyright Victor Wagner <vitus@ice.ru> at
4 * http://www.ice.ru/~vitus/works/tcl.html#syslog
6 * Slightly modified by Steve Bennett <steveb@snapgear.com>
7 * Ported to Jim by Steve Bennett <steveb@workware.net.au>
8 */
9 #include <syslog.h>
10 #include <string.h>
12 #include <jim.h>
14 typedef struct
16 int logOpened;
17 int facility;
18 int options;
19 char ident[32];
20 } SyslogInfo;
22 #ifndef LOG_AUTHPRIV
23 # define LOG_AUTHPRIV LOG_AUTH
24 #endif
26 static const char * const facilities[] = {
27 [LOG_AUTHPRIV] = "authpriv",
28 [LOG_CRON] = "cron",
29 [LOG_DAEMON] = "daemon",
30 [LOG_KERN] = "kernel",
31 [LOG_LPR] = "lpr",
32 [LOG_MAIL] = "mail",
33 [LOG_NEWS] = "news",
34 [LOG_SYSLOG] = "syslog",
35 [LOG_USER] = "user",
36 [LOG_UUCP] = "uucp",
37 [LOG_LOCAL0] = "local0",
38 [LOG_LOCAL1] = "local1",
39 [LOG_LOCAL2] = "local2",
40 [LOG_LOCAL3] = "local3",
41 [LOG_LOCAL4] = "local4",
42 [LOG_LOCAL5] = "local5",
43 [LOG_LOCAL6] = "local6",
44 [LOG_LOCAL7] = "local7",
47 static const char * const priorities[] = {
48 [LOG_EMERG] = "emerg",
49 [LOG_ALERT] = "alert",
50 [LOG_CRIT] = "crit",
51 [LOG_ERR] = "error",
52 [LOG_WARNING] = "warning",
53 [LOG_NOTICE] = "notice",
54 [LOG_INFO] = "info",
55 [LOG_DEBUG] = "debug",
58 /**
59 * Deletes the syslog command.
61 static void Jim_SyslogCmdDelete(Jim_Interp *interp, void *privData)
63 SyslogInfo *info = (SyslogInfo *) privData;
65 if (info->logOpened) {
66 closelog();
68 Jim_Free(info);
71 /* Syslog_Log -
72 * implements syslog tcl command. General format: syslog ?options? level text
73 * options -facility -ident -options
75 * syslog ?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? text
77 int Jim_SyslogCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
79 int priority = LOG_INFO;
80 int i = 1;
81 SyslogInfo *info = Jim_CmdPrivData(interp);
83 if (argc <= 1) {
84 wrongargs:
85 Jim_WrongNumArgs(interp, 1, argv,
86 "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
87 return JIM_ERR;
89 while (i < argc - 1) {
90 if (Jim_CompareStringImmediate(interp, argv[i], "-facility")) {
91 int entry =
92 Jim_FindByName(Jim_String(argv[i + 1]), facilities,
93 sizeof(facilities) / sizeof(*facilities));
94 if (entry < 0) {
95 Jim_SetResultString(interp, "Unknown facility", -1);
96 return JIM_ERR;
98 if (info->facility != entry) {
99 info->facility = entry;
100 if (info->logOpened) {
101 closelog();
102 info->logOpened = 0;
106 else if (Jim_CompareStringImmediate(interp, argv[i], "-options")) {
107 long tmp;
109 if (Jim_GetLong(interp, argv[i + 1], &tmp) == JIM_ERR) {
110 return JIM_ERR;
112 info->options = tmp;
113 if (info->logOpened) {
114 closelog();
115 info->logOpened = 0;
118 else if (Jim_CompareStringImmediate(interp, argv[i], "-ident")) {
119 strncpy(info->ident, Jim_String(argv[i + 1]), sizeof(info->ident));
120 info->ident[sizeof(info->ident) - 1] = 0;
121 if (info->logOpened) {
122 closelog();
123 info->logOpened = 0;
126 else {
127 break;
129 i += 2;
132 /* There should be either 0, 1 or 2 args left */
133 if (i == argc) {
134 /* No args, but they have set some options, so OK */
135 return JIM_OK;
138 if (i < argc - 1) {
139 priority =
140 Jim_FindByName(Jim_String(argv[i]), priorities,
141 sizeof(priorities) / sizeof(*priorities));
142 if (priority < 0) {
143 Jim_SetResultString(interp, "Unknown priority", -1);
144 return JIM_ERR;
146 i++;
149 if (i != argc - 1) {
150 goto wrongargs;
152 if (!info->logOpened) {
153 if (!info->ident[0]) {
154 Jim_Obj *argv0 = Jim_GetGlobalVariableStr(interp, "argv0", JIM_NONE);
156 if (argv0) {
157 strncpy(info->ident, Jim_String(argv0), sizeof(info->ident));
159 else {
160 strcpy(info->ident, "Tcl script");
162 info->ident[sizeof(info->ident) - 1] = 0;
164 openlog(info->ident, info->options, info->facility);
165 info->logOpened = 1;
167 syslog(priority, "%s", Jim_String(argv[i]));
169 return JIM_OK;
172 int Jim_syslogInit(Jim_Interp *interp)
174 SyslogInfo *info;
176 if (Jim_PackageProvide(interp, "syslog", "1.0", JIM_ERRMSG))
177 return JIM_ERR;
179 info = Jim_Alloc(sizeof(*info));
181 info->logOpened = 0;
182 info->options = 0;
183 info->facility = LOG_USER;
184 info->ident[0] = 0;
186 Jim_CreateCommand(interp, "syslog", Jim_SyslogCmd, info, Jim_SyslogCmdDelete);
188 return JIM_OK;