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>
13 #include "jimautoconf.h"
24 # define LOG_AUTHPRIV LOG_AUTH
27 static const char * const facilities
[] = {
28 [LOG_AUTHPRIV
] = "authpriv",
30 [LOG_DAEMON
] = "daemon",
31 [LOG_KERN
] = "kernel",
35 [LOG_SYSLOG
] = "syslog",
38 [LOG_LOCAL0
] = "local0",
39 [LOG_LOCAL1
] = "local1",
40 [LOG_LOCAL2
] = "local2",
41 [LOG_LOCAL3
] = "local3",
42 [LOG_LOCAL4
] = "local4",
43 [LOG_LOCAL5
] = "local5",
44 [LOG_LOCAL6
] = "local6",
45 [LOG_LOCAL7
] = "local7",
48 static const char * const priorities
[] = {
49 [LOG_EMERG
] = "emerg",
50 [LOG_ALERT
] = "alert",
53 [LOG_WARNING
] = "warning",
54 [LOG_NOTICE
] = "notice",
56 [LOG_DEBUG
] = "debug",
60 * Deletes the syslog command.
62 static void Jim_SyslogCmdDelete(Jim_Interp
*interp
, void *privData
)
64 SyslogInfo
*info
= (SyslogInfo
*) privData
;
66 if (info
->logOpened
) {
73 * implements syslog tcl command. General format: syslog ?options? level text
74 * options -facility -ident -options
76 * syslog ?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? text
78 int Jim_SyslogCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
80 int priority
= LOG_INFO
;
82 SyslogInfo
*info
= Jim_CmdPrivData(interp
);
86 Jim_WrongNumArgs(interp
, 1, argv
,
87 "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
90 while (i
< argc
- 1) {
91 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-facility")) {
93 Jim_FindByName(Jim_String(argv
[i
+ 1]), facilities
,
94 sizeof(facilities
) / sizeof(*facilities
));
96 Jim_SetResultString(interp
, "Unknown facility", -1);
99 if (info
->facility
!= entry
) {
100 info
->facility
= entry
;
101 if (info
->logOpened
) {
107 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-options")) {
110 if (Jim_GetLong(interp
, argv
[i
+ 1], &tmp
) == JIM_ERR
) {
114 if (info
->logOpened
) {
119 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-ident")) {
120 strncpy(info
->ident
, Jim_String(argv
[i
+ 1]), sizeof(info
->ident
));
121 info
->ident
[sizeof(info
->ident
) - 1] = 0;
122 if (info
->logOpened
) {
133 /* There should be either 0, 1 or 2 args left */
135 /* No args, but they have set some options, so OK */
141 Jim_FindByName(Jim_String(argv
[i
]), priorities
,
142 sizeof(priorities
) / sizeof(*priorities
));
144 Jim_SetResultString(interp
, "Unknown priority", -1);
153 if (!info
->logOpened
) {
154 if (!info
->ident
[0]) {
155 Jim_Obj
*argv0
= Jim_GetGlobalVariableStr(interp
, "argv0", JIM_NONE
);
158 strncpy(info
->ident
, Jim_String(argv0
), sizeof(info
->ident
));
161 strcpy(info
->ident
, "Tcl script");
163 info
->ident
[sizeof(info
->ident
) - 1] = 0;
165 openlog(info
->ident
, info
->options
, info
->facility
);
168 syslog(priority
, "%s", Jim_String(argv
[i
]));
173 int Jim_syslogInit(Jim_Interp
*interp
)
177 if (Jim_PackageProvide(interp
, "syslog", "1.0", JIM_ERRMSG
))
180 info
= Jim_Alloc(sizeof(*info
));
184 info
->facility
= LOG_USER
;
187 Jim_CreateCommand(interp
, "syslog", Jim_SyslogCmd
, info
, Jim_SyslogCmdDelete
);