1 /* Syslog interface for tcl
11 Tcl_HashTable *priorities;
12 Tcl_HashTable *facilities;
15 void Syslog_ListHash(Tcl_Interp *interp,Tcl_HashTable *table);
16 /* SyslogHelp - puts usage message into interp->result
21 void SyslogHelp(Tcl_Interp *interp,char *cmdname)
22 { Tcl_AppendResult(interp,"Wrong # of args. should be ",cmdname,
23 " ?option value? priority message",NULL);
27 * implements syslog tcl command. General format: syslog ?options? level text
28 * options -facility -ident -options
33 int Syslog_Log(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
34 { SyslogInfo *info=(SyslogInfo *)data;
35 Tcl_DString *message = NULL;
39 SyslogHelp(interp,Tcl_GetString(objv[0]));
43 if (!strncmp(Tcl_GetString(objv[i]),"-facility",10)) {
44 char *facility_name = Tcl_GetString(objv[i+1]);
45 Tcl_HashEntry * entry=Tcl_FindHashEntry(info->facilities,facility_name);
47 Tcl_AppendResult(interp,"Invalid facility name: \"",Tcl_GetString(objv[i+1]),"\"",
48 " available facilities: ",
50 Syslog_ListHash(interp,info->facilities);
53 info->facility=(int)Tcl_GetHashValue(entry);
54 if (info-> logOpened) {
58 } else if (!strncmp(Tcl_GetString(objv[i]),"-options",9)) {
60 if (Tcl_GetLongFromObj(interp,objv[i+1],&tmp)==TCL_ERROR)
63 if (info->logOpened) {
67 } else if (!strncmp(Tcl_GetStringFromObj(objv[i],NULL),"-ident",7)) {
68 char *ident_name=Tcl_GetString(objv[i+1]);
69 Tcl_DString *dstring=(Tcl_DString *)Tcl_Alloc(sizeof(Tcl_DString));
70 Tcl_DStringInit(dstring);
71 Tcl_UtfToExternalDString(NULL,ident_name,strlen(ident_name),
73 strncpy(info->ident,Tcl_DStringValue(dstring),32);
74 Tcl_DStringFree(dstring);
75 Tcl_Free((char *)dstring);
77 if (info->logOpened) {
83 Tcl_HashEntry *entry=Tcl_FindHashEntry(info->priorities,Tcl_GetString(objv[i]));
85 Tcl_AppendResult(interp,"Invalid syslog level \"",Tcl_GetString(objv[i]),"\"",
86 " available levels: ",
88 Syslog_ListHash(interp,info->priorities);
91 priority=(int)Tcl_GetHashValue(entry);
92 message=(Tcl_DString *)Tcl_Alloc(sizeof(Tcl_DString));
93 Tcl_DStringInit(message);
94 messageutf=Tcl_GetString(objv[i+1]);
95 Tcl_UtfToExternalDString(NULL,messageutf,strlen(messageutf),
100 SyslogHelp(interp,Tcl_GetString(objv[0]));
107 SyslogHelp(interp,Tcl_GetString(objv[0]));
111 if (!info->logOpened) {
112 openlog(info->ident,info->options,info->facility);
115 syslog(priority,"%s",Tcl_DStringValue(message));
116 Tcl_DStringFree(message);
117 Tcl_Free((char *)message);
122 * Syslog_Delete - Tcl_CmdDeleteProc for syslog command.
123 * Frees all hash tables and closes log if it was opened.
125 void Syslog_Delete(ClientData data)
126 { SyslogInfo *info=(SyslogInfo *)data;
127 Tcl_DeleteHashTable(info->facilities);
128 Tcl_Free((char *)info->facilities);
129 Tcl_DeleteHashTable(info->priorities);
130 Tcl_Free((char *)info->priorities);
131 if (info->logOpened) {
134 Tcl_Free((char *)info);
138 * Syslog_ListHash - appends to interp result all the values of given
141 void Syslog_ListHash(Tcl_Interp *interp,Tcl_HashTable *table)
143 Tcl_HashSearch *searchPtr=(Tcl_HashSearch *)
144 Tcl_Alloc(sizeof(Tcl_HashSearch));
145 Tcl_HashEntry *entry;
146 char separator[3]={' ',' ',0};
147 entry=Tcl_FirstHashEntry(table,searchPtr);
149 Tcl_AppendResult(interp,separator,Tcl_GetHashKey(table,entry),NULL);
151 entry=Tcl_NextHashEntry(searchPtr);
153 Tcl_Free((char *)searchPtr);
156 * My simplified wrapper for add values into hash
159 void AddEntry(Tcl_HashTable *table,char *key,int value)
161 Tcl_HashEntry *entry=Tcl_CreateHashEntry(table,key,&new);
162 Tcl_SetHashValue(entry,(ClientData)value);
166 * Package initialization procedure for Syslog package.
167 * Creates command 'syslog', fills hash tables to map symbolic prioriry
168 * and facility names to system constants.
170 int Syslog_Init(Tcl_Interp *interp)
173 if (Tcl_InitStubs(interp,"8.1",0)==NULL) {
176 info=(SyslogInfo *)Tcl_Alloc(sizeof(SyslogInfo));
179 info->facility=LOG_USER;
180 argv0=Tcl_GetVar(interp,"argv0",TCL_GLOBAL_ONLY);
182 strncpy(info->ident,argv0,32);
184 strcpy(info->ident,"Tcl script");
187 info->facilities =(Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
188 Tcl_InitHashTable(info->facilities,TCL_STRING_KEYS);
189 AddEntry(info->facilities,"auth",LOG_AUTH);
191 # define LOG_AUTHPRIV LOG_AUTH
193 AddEntry(info->facilities,"authpriv",LOG_AUTHPRIV);
194 AddEntry(info->facilities,"cron",LOG_CRON);
195 AddEntry(info->facilities,"daemon",LOG_DAEMON);
196 AddEntry(info->facilities,"kernel",LOG_KERN);
197 AddEntry(info->facilities,"lpr",LOG_LPR);
198 AddEntry(info->facilities,"mail",LOG_MAIL);
199 AddEntry(info->facilities,"news",LOG_NEWS);
200 AddEntry(info->facilities,"syslog",LOG_SYSLOG);
201 AddEntry(info->facilities,"user",LOG_USER);
202 AddEntry(info->facilities,"uucp",LOG_UUCP);
203 AddEntry(info->facilities,"local0",LOG_LOCAL0);
204 AddEntry(info->facilities,"local1",LOG_LOCAL1);
205 AddEntry(info->facilities,"local2",LOG_LOCAL2);
206 AddEntry(info->facilities,"local3",LOG_LOCAL3);
207 AddEntry(info->facilities,"local4",LOG_LOCAL4);
208 AddEntry(info->facilities,"local5",LOG_LOCAL5);
209 AddEntry(info->facilities,"local6",LOG_LOCAL6);
210 AddEntry(info->facilities,"local7",LOG_LOCAL7);
211 info->priorities = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
212 Tcl_InitHashTable(info->priorities,TCL_STRING_KEYS);
213 AddEntry(info->priorities,"emerg",LOG_EMERG);
214 AddEntry(info->priorities,"alert",LOG_ALERT);
215 AddEntry(info->priorities,"crit",LOG_CRIT);
216 AddEntry(info->priorities,"err",LOG_ERR);
217 AddEntry(info->priorities,"error",LOG_ERR);
218 AddEntry(info->priorities,"warning",LOG_WARNING);
219 AddEntry(info->priorities,"notice",LOG_NOTICE);
220 AddEntry(info->priorities,"info",LOG_INFO);
221 AddEntry(info->priorities,"debug",LOG_DEBUG);
222 Tcl_CreateObjCommand(interp,"syslog",Syslog_Log,(ClientData) info,
224 return Tcl_PkgProvide(interp,"Syslog",VERSION);