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>
23 # define LOG_AUTHPRIV LOG_AUTH
26 static const char * const facilities
[] = {
27 [LOG_AUTHPRIV
] = "authpriv",
29 [LOG_DAEMON
] = "daemon",
30 [LOG_KERN
] = "kernel",
34 [LOG_SYSLOG
] = "syslog",
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",
52 [LOG_WARNING
] = "warning",
53 [LOG_NOTICE
] = "notice",
55 [LOG_DEBUG
] = "debug",
59 * Deletes the syslog command.
61 static void Jim_SyslogCmdDelete(Jim_Interp
*interp
, void *privData
)
63 SyslogInfo
*info
= (SyslogInfo
*) privData
;
65 if (info
->logOpened
) {
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
;
81 SyslogInfo
*info
= Jim_CmdPrivData(interp
);
85 Jim_WrongNumArgs(interp
, 1, argv
,
86 "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
89 while (i
< argc
- 1) {
90 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-facility")) {
92 Jim_FindByName(Jim_String(argv
[i
+ 1]), facilities
,
93 sizeof(facilities
) / sizeof(*facilities
));
95 Jim_SetResultString(interp
, "Unknown facility", -1);
98 if (info
->facility
!= entry
) {
99 info
->facility
= entry
;
100 if (info
->logOpened
) {
106 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-options")) {
109 if (Jim_GetLong(interp
, argv
[i
+ 1], &tmp
) == JIM_ERR
) {
113 if (info
->logOpened
) {
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
) {
132 /* There should be either 0, 1 or 2 args left */
134 /* No args, but they have set some options, so OK */
140 Jim_FindByName(Jim_String(argv
[i
]), priorities
,
141 sizeof(priorities
) / sizeof(*priorities
));
143 Jim_SetResultString(interp
, "Unknown priority", -1);
152 if (!info
->logOpened
) {
153 if (!info
->ident
[0]) {
154 Jim_Obj
*argv0
= Jim_GetGlobalVariableStr(interp
, "argv0", JIM_NONE
);
157 strncpy(info
->ident
, Jim_String(argv0
), sizeof(info
->ident
));
160 strcpy(info
->ident
, "Tcl script");
162 info
->ident
[sizeof(info
->ident
) - 1] = 0;
164 openlog(info
->ident
, info
->options
, info
->facility
);
167 syslog(priority
, "%s", Jim_String(argv
[i
]));
172 int Jim_syslogInit(Jim_Interp
*interp
)
176 if (Jim_PackageProvide(interp
, "syslog", "1.0", JIM_ERRMSG
))
179 info
= Jim_Alloc(sizeof(*info
));
183 info
->facility
= LOG_USER
;
186 Jim_CreateCommand(interp
, "syslog", Jim_SyslogCmd
, info
, Jim_SyslogCmdDelete
);