public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] logger: add support for --loglevel-prefix when logging stdin
@ 2013-05-13 21:57 Dennis H Jensen
  2013-05-14  8:07 ` Karel Zak
  0 siblings, 1 reply; 3+ messages in thread
From: Dennis H Jensen @ 2013-05-13 21:57 UTC (permalink / raw)
  To: util-linux; +Cc: Dennis H Jensen

This patch adds a new option to logger that will make it look for a
loglevel prefix <[0-7]> at the beginning of every line.

If a loglevel is found logger will log the message using the found
loglevel combined with the facility specified by --priority. When no
loglevel is found the default level provided by the --priority option
will be used.

Signed-off-by: Dennis H Jensen <dennis.h.jensen@siemens.com>
---
 misc-utils/logger.1 |  9 +++++++++
 misc-utils/logger.c | 33 +++++++++++++++++++++++++++++----
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/misc-utils/logger.1 b/misc-utils/logger.1
index 09ecdc4..af6f4f5 100644
--- a/misc-utils/logger.1
+++ b/misc-utils/logger.1
@@ -109,6 +109,15 @@ Write to the specified
 .I socket
 instead of to the builtin syslog routines.
 .TP
+\fB\-\-loglevel\-prefix\fR
+Look for a log level prefix on every line read from standard input.
+The log level prefix must have the format \fI<0...7>\fR; logger will
+use the facility specified by the \fB\-p\fR option and the specified level
+as the default when no log level prefix is found. For example \fB\-p\fR
+\fIuser.debug\fR will log messages in the user facility and default to
+the debug level.
+This option doesn't affect a command-line message.
+.TP
 \fB\-V\fR, \fB\-\-version\fR
 Display version information and exit.
 .TP
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c83c0b8..36371be 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -64,6 +64,10 @@ enum {
 	ALL_TYPES = TYPE_UDP | TYPE_TCP
 };
 
+enum {
+	ARG_LOGLEVEL_PREFIX = 0x100
+};
+
 static int decode(char *name, CODE *codetab)
 {
 	register CODE *c;
@@ -240,7 +244,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
  */
 int
 main(int argc, char **argv) {
-	int ch, logflags, pri;
+	int ch, logflags, pri, loglevel_prefix;
 	char *tag, buf[1024];
 	char *usock = NULL;
 	char *server = NULL;
@@ -260,6 +264,7 @@ main(int argc, char **argv) {
 		{ "port",	required_argument,  0, 'P' },
 		{ "version",	no_argument,	    0, 'V' },
 		{ "help",	no_argument,	    0, 'h' },
+		{ "loglevel-prefix", no_argument,   0, ARG_LOGLEVEL_PREFIX },
 		{ NULL,		0, 0, 0 }
 	};
 
@@ -271,9 +276,10 @@ main(int argc, char **argv) {
 	tag = NULL;
 	pri = LOG_NOTICE;
 	logflags = 0;
+	loglevel_prefix = 0;
 	while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
 					    longopts, NULL)) != -1) {
-		switch((char)ch) {
+		switch (ch) {
 		case 'f':		/* file to log */
 			if (freopen(optarg, "r", stdin) == NULL)
 				err(EXIT_FAILURE, _("file %s"),
@@ -311,6 +317,9 @@ main(int argc, char **argv) {
 			exit(EXIT_SUCCESS);
 		case 'h':
 			usage(stdout);
+		case ARG_LOGLEVEL_PREFIX:
+			loglevel_prefix = 1;
+			break;
 		case '?':
 		default:
 			usage(stderr);
@@ -360,6 +369,9 @@ main(int argc, char **argv) {
 			mysyslog(LogSock, logflags, pri, tag, buf);
 		}
 	} else {
+		char *msg = buf;
+		int default_priority = pri;
+		int facility = default_priority & LOG_FACMASK;
 		while (fgets(buf, sizeof(buf), stdin) != NULL) {
 		    /* glibc is buggy and adds an additional newline,
 		       so we have to remove it here until glibc is fixed */
@@ -368,10 +380,23 @@ main(int argc, char **argv) {
 		    if (len > 0 && buf[len - 1] == '\n')
 			    buf[len - 1] = '\0';
 
+			msg = buf;
+			if (loglevel_prefix) {
+				int level = default_priority & LOG_PRIMASK;
+				if (msg[0] == '<' && msg[1] && msg[2] == '>') {
+					switch (msg[1]) {
+					case '0' ... '7':
+						level = msg[1] - '0';
+						msg += 3;
+					}
+				}
+				pri = facility | level;
+			}
+
 		    if (!usock && !server)
-			syslog(pri, "%s", buf);
+			syslog(pri, "%s", msg);
 		    else
-			mysyslog(LogSock, logflags, pri, tag, buf);
+			mysyslog(LogSock, logflags, pri, tag, msg);
 		}
 	}
 	if (!usock && !server)
-- 
1.8.1.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH RFC] logger: add support for --loglevel-prefix when logging stdin
  2013-05-13 21:57 [PATCH RFC] logger: add support for --loglevel-prefix when logging stdin Dennis H Jensen
@ 2013-05-14  8:07 ` Karel Zak
  2013-05-14  8:38   ` Dennis H Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Karel Zak @ 2013-05-14  8:07 UTC (permalink / raw)
  To: Dennis H Jensen; +Cc: util-linux


 Hi Dennis,

On Mon, May 13, 2013 at 11:57:18PM +0200, Dennis H Jensen wrote:
> This patch adds a new option to logger that will make it look for a
> loglevel prefix <[0-7]> at the beginning of every line.
> 
> If a loglevel is found logger will log the message using the found
> loglevel combined with the facility specified by --priority. When no
> loglevel is found the default level provided by the --priority option
> will be used.

I have doubts if this is a good idea. 

It seems that you're trying to introduce a new meaning of the <n>
convention.  The <n> in the syslog API is not only log level, it's
((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)) -- bottom 3 bits are the
priority and the top 28 bits are the facility). 

It would be better to follow this already existing convention. It means 
to use something like

    --prio-prefix

and use the <n> number as whole priority number. The result will be
command line interface with pretty simple semantic.

The problem is that <n> encoding is not user friendly, but I guess we
should be able to explain all in the logger man page (for example in
an EXAMPLE section). For example user.debug:

    LOG_USER=$((1<<3))
    LOG_DEBUG=7
    echo $(( $LOG_USER & 0x03f8 | $LOG_DEBUG & 0x07 ))

to get the number in shell.

> --- a/misc-utils/logger.c
> +++ b/misc-utils/logger.c
> @@ -64,6 +64,10 @@ enum {
>  	ALL_TYPES = TYPE_UDP | TYPE_TCP
>  };
>  
> +enum {
> +	ARG_LOGLEVEL_PREFIX = 0x100

 We usually use OPT_* and CHAR_MAX, for example:

        OPT_PRIO_PREFIX = CHAR_MAX + 1


    Karel


-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH RFC] logger: add support for --loglevel-prefix when logging stdin
  2013-05-14  8:07 ` Karel Zak
@ 2013-05-14  8:38   ` Dennis H Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Dennis H Jensen @ 2013-05-14  8:38 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux@vger.kernel.org

On Tue, 2013-05-14 at 10:07 +0200, Karel Zak wrote:
> Hi Dennis,
> 
> On Mon, May 13, 2013 at 11:57:18PM +0200, Dennis H Jensen wrote:
> > This patch adds a new option to logger that will make it look for a
> > loglevel prefix <[0-7]> at the beginning of every line.
> > 
> > If a loglevel is found logger will log the message using the found
> > loglevel combined with the facility specified by --priority. When no
> > loglevel is found the default level provided by the --priority option
> > will be used.
> 
> I have doubts if this is a good idea. 
> 
> It seems that you're trying to introduce a new meaning of the <n>
> convention.  The <n> in the syslog API is not only log level, it's
> ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)) -- bottom 3 bits are the
> priority and the top 28 bits are the facility).
>  
> 
> It would be better to follow this already existing convention. It means 
> to use something like
> 
>     --prio-prefix
> 
> and use the <n> number as whole priority number. The result will be
> command line interface with pretty simple semantic.

I wanted the <n> convention but only provide the level, but you are
right, it would be best to provide the whole API.

> 
> The problem is that <n> encoding is not user friendly, but I guess we
> should be able to explain all in the logger man page (for example in
> an EXAMPLE section). For example user.debug:
> 
>     LOG_USER=$((1<<3))
>     LOG_DEBUG=7
>     echo $(( $LOG_USER & 0x03f8 | $LOG_DEBUG & 0x07 ))
> 
> to get the number in shell.

maybe I will add a shiny example to the man page :)

> 
> > --- a/misc-utils/logger.c
> > +++ b/misc-utils/logger.c
> > @@ -64,6 +64,10 @@ enum {
> >  	ALL_TYPES = TYPE_UDP | TYPE_TCP
> >  };
> >  
> > +enum {
> > +	ARG_LOGLEVEL_PREFIX = 0x100
> 
>  We usually use OPT_* and CHAR_MAX, for example:
> 
>         OPT_PRIO_PREFIX = CHAR_MAX + 1
> 
> 

I will resend a new version of the patch..

//Dennis



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-05-14  8:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-13 21:57 [PATCH RFC] logger: add support for --loglevel-prefix when logging stdin Dennis H Jensen
2013-05-14  8:07 ` Karel Zak
2013-05-14  8:38   ` Dennis H Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox