Re: [tkined] Syslog ident string

Juergen Schoenwaelder (schoenw@ibr.cs.tu-bs.de)
Wed, 21 Apr 1999 16:01:28 +0200

>>>>> Bill Harris writes:

Bill> Is there (simple) way to change the ident string on the syslog
Bill> call. I'm writing some scripts and having "scotty" show up on
Bill> every entry is annoying when I'd rather it be configurable as an
Bill> arguement to the call.

I think this is a reasonable question. The string "scotty" is
currently hard-wired into the Tnm extension. I have written code to
make the identification string configurable. You can now write:

syslog -ident foobar warning "oops - take care"

You can also set the default in case you don't want to give the
identification string every time:

syslog -ident barfoo
syslog warning "oops - take care"

You can retrieve the current default with:

syslog -ident

This scheme is consistent with the way the Tnm extension manages
defaults/option for the Tnm::dns or the Tnm::icmp command. The
implementation is based on the Tnm 3.0.0 snapshot. The patch is
appended below. Porting it back to 2.1.X requires some editing since
the internal interfaces have changed quite a bit.
Juergen

Index: tnm/generic/tnmSyslog.c
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/tnm/generic/tnmSyslog.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 tnmSyslog.c
--- tnmSyslog.c 1997/08/30 19:59:51 1.1.1.1
+++ tnmSyslog.c 1999/04/21 13:25:50
@@ -5,6 +5,7 @@
*
* Copyright (c) 1993-1996 Technical University of Braunschweig.
* Copyright (c) 1996-1997 University of Twente.
+ * Copyright (c) 1997-1999 Technical University of Braunschweig.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
@@ -25,6 +26,56 @@
{ 0, NULL }
};

+/*
+ * Every Tcl interpreter has an associated DnsControl record. It
+ * keeps track of the default settings for this interpreter.
+ */
+
+static char tnmSyslogControl[] = "tnmSyslogControl";
+
+typedef struct SyslogControl {
+ char *ident; /* Identification of the event source. */
+} SyslogControl;
+
+/*
+ * Forward declarations for procedures defined later in this file:
+ */
+
+static void
+AssocDeleteProc _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp));
+
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * AssocDeleteProc --
+ *
+ * This procedure is called when a Tcl interpreter gets destroyed
+ * so that we can clean up the data associated with this interpreter.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+AssocDeleteProc(clientData, interp)
+ ClientData clientData;
+ Tcl_Interp *interp;
+{
+ SyslogControl *control = (SyslogControl *) clientData;
+
+ if (control) {
+ if (control->ident) {
+ ckfree(control->ident);
+ }
+ ckfree((char *) control);
+ }
+}

/*
*----------------------------------------------------------------------
@@ -50,24 +101,77 @@
int argc;
char **argv;
{
+ char *argv0 = argv[0];
int level, code;
+ char *ident = NULL;

- if (argc != 3) {
- Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
- " level message\"", (char *) NULL);
+ SyslogControl *control = (SyslogControl *)
+ Tcl_GetAssocData(interp, tnmSyslogControl, NULL);
+
+ if (! control) {
+ control = (SyslogControl *) ckalloc(sizeof(SyslogControl));
+ control->ident = ckstrdup("scotty");
+ Tcl_SetAssocData(interp, tnmSyslogControl, AssocDeleteProc,
+ (ClientData) control);
+ }
+
+ argc--; argv++;
+
+ if (argc == 0) {
+ wrongArgs:
+ Tcl_AppendResult(interp, "wrong # args: should be \"", argv0,
+ " ?-ident string? level message\"", (char *) NULL);
return TCL_ERROR;
}

- level = TnmGetTableKey(tnmLogTable, argv[1]);
+ /*
+ * Parse optional parameters:
+ */
+
+ while ((argc > 0) && (*argv[0] == '-')) {
+ if (strcmp(argv[0], "-ident") == 0) {
+ argc--, argv++;
+ if (argc <= 0) {
+ Tcl_SetResult(interp, control->ident, TCL_STATIC);
+ return TCL_OK;
+ }
+ ident = argv[0];
+ argc--, argv++;
+ } else {
+ Tcl_AppendResult(interp, "unknown option \"", argv[0], "\"",
+ (char *) NULL);
+ return TCL_ERROR;
+ }
+ }
+
+ if (argc == 0) {
+ if (ident) {
+ if (control->ident) {
+ ckfree(control->ident);
+ }
+ control->ident = ckstrdup(ident);
+ }
+ return TCL_OK;
+ }
+
+ if (argc != 2) {
+ goto wrongArgs;
+ }
+
+ if (! ident) {
+ ident = control->ident;
+ }
+
+ level = TnmGetTableKey(tnmLogTable, argv[0]);

if (level < 0) {
- Tcl_AppendResult(interp, "bad level \"", argv[1],
+ Tcl_AppendResult(interp, "bad level \"", argv[0],
"\": should be ", TnmGetTableValues(tnmLogTable),
(char *) NULL);
return TCL_ERROR;
}

- code = TnmWriteLogMessage(level, argv[2]);
+ code = TnmWriteLogMessage(ident, level, argv[1]);
if (code != 0) {
Tcl_SetResult(interp, "error while accessing system logging facility",
TCL_STATIC);
Index: tnm/generic/tnmInt.h
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/tnm/generic/tnmInt.h,v
retrieving revision 1.13
diff -u -r1.13 tnmInt.h
--- tnmInt.h 1998/10/30 09:57:07 1.13
+++ tnmInt.h 1999/04/21 13:50:42
@@ -5,7 +5,7 @@
*
* Copyright (c) 1993-1996 Technical University of Braunschweig.
* Copyright (c) 1996-1997 University of Twente.
- * Copyright (c) 1997-1998 Technical University of Braunschweig.
+ * Copyright (c) 1997-1999 Technical University of Braunschweig.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
@@ -351,7 +351,7 @@
EXTERN TnmTable tnmLogTable[];

EXTERN int
-TnmWriteLogMessage _ANSI_ARGS_((int level, char *message));
+TnmWriteLogMessage _ANSI_ARGS_((char *ident, int level, char *message));

EXTERN void
TnmWriteMessage _ANSI_ARGS_((char *msg));
Index: tnm/tests/syslog.test
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/tnm/tests/syslog.test,v
retrieving revision 1.3
diff -u -r1.3 syslog.test
--- syslog.test 1998/01/15 16:39:17 1.3
+++ syslog.test 1999/04/21 13:25:37
@@ -6,7 +6,7 @@
#
# Copyright (c) 1994-1996 Technical University of Braunschweig.
# Copyright (c) 1996-1997 University of Twente.
-# Copyright (c) 1997-1998 Technical University of Braunschweig.
+# Copyright (c) 1997-1999 Technical University of Braunschweig.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
@@ -23,12 +23,27 @@

# set VERBOSE 1

+# save default settings...
+set syslogIdent [syslog -ident]
+
test syslog-1.1 {syslog} {
syslog debug "scotty test suite"
} {}
test syslog-1.2 {syslog} {
list [catch {syslog debug} msg] $msg
-} {1 {wrong # args: should be "syslog level message"}}
+} {1 {wrong # args: should be "syslog ?-ident string? level message"}}
test syslog-1.3 {syslog} {
list [catch {syslog 5 "scotty test suite"} msg] $msg
} {1 {bad level "5": should be emergency, alert, critical, error, warning, notice, info, or debug}}
+test syslog-1.4 {syslog ident option} {
+ syslog -ident foo
+} {}
+test syslog-1.5 {syslog ident option} {
+ syslog -ident
+} {foo}
+test syslog-1.6 {syslog unknown option} {
+ list [catch {syslog -foobar} msg] $msg
+} {1 {unknown option "-foobar"}}
+
+# restore default settings...
+syslog -ident $syslogIdent
\ No newline at end of file
Index: unix/tnmUnixLog.c
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/unix/tnmUnixLog.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 tnmUnixLog.c
--- tnmUnixLog.c 1997/08/30 20:00:54 1.1.1.1
+++ tnmUnixLog.c 1999/04/21 13:28:26
@@ -5,6 +5,7 @@
*
* Copyright (c) 1993-1996 Technical University of Braunschweig.
* Copyright (c) 1996-1997 University of Twente.
+ * Copyright (c) 1997-1999 Technical University of Braunschweig.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
@@ -34,7 +35,8 @@
*/

int
-TnmWriteLogMessage(level, message)
+TnmWriteLogMessage(ident, level, message)
+ char *ident;
int level;
char *message;
{
@@ -67,11 +69,15 @@
return -1;
}

+ if (! ident) {
+ ident = "scotty";
+ }
+
if (message != NULL) {
#ifdef ultrix
- openlog("scotty", LOG_PID);
+ openlog(ident, LOG_PID);
#else
- openlog("scotty", LOG_PID, LOG_USER);
+ openlog(ident, LOG_PID, LOG_USER);
#endif
syslog(level, message);
closelog();
Index: win/tnmWinLog.c
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/win/tnmWinLog.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 tnmWinLog.c
--- tnmWinLog.c 1997/08/30 20:00:58 1.1.1.1
+++ tnmWinLog.c 1999/04/21 13:28:34
@@ -4,6 +4,7 @@
* Windows specific functions to write to the system logging facility.
*
* Copyright (c) 1996-1997 University of Twente.
+ * Copyright (c) 1997-1999 Technical University of Braunschweig.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
@@ -40,7 +41,8 @@
*/

int
-TnmWriteLogMessage(level, message)
+TnmWriteLogMessage(ident, level, message)
+ char *ident;
int level;
char *message;
{
@@ -69,7 +71,11 @@
return -1;
}

- ed = RegisterEventSource(NULL, "scotty");
+ if (! ident) {
+ ident = "scotty";
+ }
+
+ ed = RegisterEventSource(NULL, ident);
if (ed != NULL) {
msgList[0] = message;
ReportEvent(ed, type, category, id, NULL, 1, 0, msgList, NULL);
Index: tnm/snmp/tnmMibFrozen.c
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/tnm/snmp/tnmMibFrozen.c,v
retrieving revision 1.3
diff -u -r1.3 tnmMibFrozen.c
--- tnmMibFrozen.c 1998/09/17 09:26:55 1.3
+++ tnmMibFrozen.c 1999/04/21 12:58:02
@@ -565,19 +565,19 @@
*/

if (1 != fread((char *) &poolSize, sizeof(int), 1, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading string pool size...\n");
return NULL;
}

pool = ckalloc(poolSize);
if (poolSize != fread(pool, 1, poolSize, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading string pool...\n");
return NULL;
}
if (strcmp(pool, TNM_VERSION) != 0) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"wrong .idy file version...\n");
return NULL;
}
@@ -587,7 +587,7 @@
*/

if (1 != fread((char *) &numEnums, sizeof(numEnums), 1, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading enum counter...\n");
return NULL;
}
@@ -597,7 +597,8 @@
int i;
enums = (TnmMibRest *) ckalloc(numEnums * sizeof(TnmMibRest));
if (numEnums != fread(enums, sizeof(TnmMibRest), numEnums, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG, "error reading enums...\n");
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
+ "error reading enums...\n");
ckfree((char *) enums);
return NULL;
}
@@ -616,7 +617,7 @@
*/

if (1 != fread((char *) &numTcs, sizeof(numTcs), 1, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading tc counter...\n");
return NULL;
}
@@ -626,7 +627,7 @@

tcs = (TnmMibType *) ckalloc(numTcs * sizeof(TnmMibType));
if (numTcs != fread(tcs, sizeof(TnmMibType), numTcs, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading tcs...\n");
ckfree((char *) tcs);
return NULL;
@@ -671,7 +672,7 @@
*/

if (1 != fread((char *) &numNodes, sizeof(numNodes), 1, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading node counter...\n");
return NULL;
}
@@ -682,7 +683,7 @@

nodes = (TnmMibNode *) ckalloc(numNodes * sizeof(TnmMibNode));
if (numNodes != fread(nodes, sizeof(TnmMibNode), numNodes, fp)) {
- TnmWriteLogMessage(TNM_LOG_DEBUG,
+ TnmWriteLogMessage(NULL, TNM_LOG_DEBUG,
"error reading nodes...\n");
ckfree((char *) nodes);
return NULL;
Index: doc/syslog.n
===================================================================
RCS file: /usr/home/schoenw/CVS/scotty/doc/syslog.n,v
retrieving revision 1.3
diff -u -r1.3 syslog.n
--- syslog.n 1998/03/26 15:58:39 1.3
+++ syslog.n 1999/04/21 13:40:23
@@ -6,20 +6,20 @@
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.so man.macros
-.TH Tnm::syslog n "December 1997" Tnm "Tnm Tcl Extension"
+.TH Tnm::syslog n "April 1999" Tnm "Tnm Tcl Extension"
.BS
'\" Note: do not modify the .SH NAME line immediately below!
.SH NAME
Tnm::syslog \- Write messages to the system logger.
-.SH SYNOPSIS
-.BI Tnm::syslog " level message"
-.BE

.SH DESCRIPTION
-The \fBTnm::syslog\fR command allows a Tcl script to write a \fImessage\fR
-to the system logger. Further processing of the message depends on the
-priority \fIlevel\fR of the message and the configuration of the
-system message logging facility. Allowed priority levels are:
+The Tnm::syslog command writes messages to the local system logging
+facility. On Unix platforms, the message is written to the syslog
+daemon. On Windows platforms, the message is written to the Windows
+event logging facility.
+
+Every message has an associated priority level. Allowed
+priority levels are:
.TP 16
.SB emergency
A panic condition. This is normally broadcast to all users.
@@ -48,10 +48,31 @@
Messages that contain information
normally of use only when debugging a program.
.PP
-Not all system message logging facilities support all priority
-levels. The Tnm extension will automatically convert these priorities
-into levels understood by the local system message logging facility
-if needed.
+Not all system logging facilities support all priority levels. The Tnm
+extension will automatically convert the priorities listed above into
+levels understood by the local system logging facility if needed.
+
+.SH DNS COMMAND
+.TP
+\fBTnm::syslog\fR [\fIoptions\fR]
+Invoking the \fBTnm::syslog\fR command with options but without any
+command arguments allows to retrieve and change the default
+values. See the description of supported options below. Default values
+are bound to a Tcl interpreter which allows to have multiple Tcl
+interpreter with different defaults.
+
+.TP
+\fBTnm::syslog\fR [\fIoptions\fR] \fIlevel\fR \fImessage\fR
+The \fBTnm::syslog\fR command writes a \fImessage\fR to the system
+logging facility. Further processing of the message depends on the
+priority \fIlevel\fR of the message and the configuration of the
+system logging facility.
+
+.SH SYSLOG OPTIONS
+.TP
+.BI "-ident " string
+The \fB-ident\fR option defines the identification \fIstring\fR of the
+event source. The default value is the string \fIscotty\fR.

.SH SEE ALSO
scotty(1), Tnm(n), Tcl(n)

--
!! This message is brought to you via the `tkined & scotty' mailing list.
!! Please do not reply to this message to unsubscribe. To subscribe or
!! unsubscribe, send a mail message to <tkined-request@ibr.cs.tu-bs.de>.
!! See http://wwwsnmp.cs.utwente.nl/~schoenw/scotty/ for more information.