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>
22 # define LOG_AUTHPRIV LOG_AUTH
25 static const char * const facilities
[] = {
26 [LOG_AUTHPRIV
] = "authpriv",
28 [LOG_DAEMON
] = "daemon",
29 [LOG_KERN
] = "kernel",
33 [LOG_SYSLOG
] = "syslog",
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",
51 [LOG_WARNING
] = "warning",
52 [LOG_NOTICE
] = "notice",
54 [LOG_DEBUG
] = "debug",
58 * Deletes the syslog command.
60 static void Jim_SyslogCmdDelete(Jim_Interp
*interp
, void *privData
)
62 SyslogInfo
*info
= (SyslogInfo
*) privData
;
64 if (info
->logOpened
) {
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
;
80 SyslogInfo
*info
= Jim_CmdPrivData(interp
);
84 Jim_WrongNumArgs(interp
, 1, argv
,
85 "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
88 while (i
< argc
- 1) {
89 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-facility")) {
91 Jim_FindByName(Jim_String(argv
[i
+ 1]), facilities
,
92 sizeof(facilities
) / sizeof(*facilities
));
94 Jim_SetResultString(interp
, "Unknown facility", -1);
97 if (info
->facility
!= entry
) {
98 info
->facility
= entry
;
99 if (info
->logOpened
) {
105 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-options")) {
108 if (Jim_GetLong(interp
, argv
[i
+ 1], &tmp
) == JIM_ERR
) {
112 if (info
->logOpened
) {
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
) {
131 /* There should be either 0, 1 or 2 args left */
133 /* No args, but they have set some options, so OK */
139 Jim_FindByName(Jim_String(argv
[i
]), priorities
,
140 sizeof(priorities
) / sizeof(*priorities
));
142 Jim_SetResultString(interp
, "Unknown priority", -1);
151 if (!info
->logOpened
) {
152 if (!info
->ident
[0]) {
153 Jim_Obj
*argv0
= Jim_GetGlobalVariableStr(interp
, "argv0", JIM_NONE
);
156 strncpy(info
->ident
, Jim_String(argv0
), sizeof(info
->ident
));
159 strcpy(info
->ident
, "Tcl script");
161 info
->ident
[sizeof(info
->ident
) - 1] = 0;
163 openlog(info
->ident
, info
->options
, info
->facility
);
166 syslog(priority
, "%s", Jim_String(argv
[i
]));
171 int Jim_syslogInit(Jim_Interp
*interp
)
175 if (Jim_PackageProvide(interp
, "syslog", "1.0", JIM_ERRMSG
))
178 info
= Jim_Alloc(sizeof(*info
));
182 info
->facility
= LOG_USER
;
185 Jim_CreateCommand(interp
, "syslog", Jim_SyslogCmd
, info
, Jim_SyslogCmdDelete
);