public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 13/19] dmesg: make time format parsing to use enum bit field
Date: Sun,  2 Jun 2013 18:51:19 +0100	[thread overview]
Message-ID: <1370195485-27907-14-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1370195485-27907-1-git-send-email-kerolasa@iki.fi>

This will pave way to add single option to define all time formats, such
as --time-format=[ctime|reltime|delta|none].

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/dmesg.c | 59 +++++++++++++++++++++++++++++++------------------------
 1 file changed, 33 insertions(+), 26 deletions(-)

diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index ae8e938..b882120 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -126,6 +126,13 @@ enum {
 	DMESG_METHOD_MMAP	/* mmap file with records (see --file) */
 };
 
+enum {
+	TIMEFTM_NONE	= (1 << 0),
+	TIMEFTM_CTIME	= (1 << 1),
+	TIMEFTM_DELTA	= (1 << 2),
+	TIMEFTM_RELTIME = (1 << 3),
+};
+
 struct dmesg_control {
 	/* bit arrays -- see include/bitops.h */
 	char levels[ARRAY_SIZE(level_names) / NBBY + 1];
@@ -152,16 +159,13 @@ struct dmesg_control {
 	char		*filename;
 	char		*mmap_buff;
 	size_t		pagesize;
+	unsigned int	time_fmt;	/* time format */
 
 	unsigned int	follow:1,	/* wait for new messages */
 			raw:1,		/* raw mode */
 			fltr_lev:1,	/* filter out by levels[] */
 			fltr_fac:1,	/* filter out by facilities[] */
 			decode:1,	/* use "facility: level: " prefix */
-			notime:1,	/* don't print timestamp */
-			delta:1,	/* show time deltas */
-			reltime:1,	/* show human readable relative times */
-			ctime:1,	/* show human readable time */
 			pager:1,	/* pipe output into a pager */
 			color:1;	/* colorize messages */
 };
@@ -689,9 +693,10 @@ static int get_next_syslog_record(struct dmesg_control *ctl,
 
 		if (*begin == '[' && (*(begin + 1) == ' ' ||
 				      isdigit(*(begin + 1)))) {
-			if (ctl->delta || ctl->ctime || ctl->reltime)
+			if (ctl->time_fmt & (TIMEFTM_DELTA | TIMEFTM_CTIME | TIMEFTM_RELTIME))
 				begin = parse_syslog_timestamp(begin + 1, &rec->tv);
-			else if (ctl->notime)
+
+			else if (ctl->time_fmt & TIMEFTM_NONE)
 				begin = skip_item(begin, end, "]");
 
 			if (begin < end && *begin == ' ')
@@ -855,12 +860,12 @@ static void print_record(struct dmesg_control *ctl,
 	/*
 	 * [sec.usec <delta>] or [ctime <delta>]
 	 */
-	if (ctl->delta) {
+	if (ctl->time_fmt & TIMEFTM_DELTA) {
 		if (ctl->color)
 			color_enable(DMESG_COLOR_TIME);
-		if (ctl->ctime)
+		if (ctl->time_fmt & TIMEFTM_CTIME)
 			printf("[%s ", record_ctime(ctl, rec, buf, sizeof(buf)));
-		else if (ctl->notime)
+		else if (ctl->time_fmt == TIMEFTM_NONE)
 			putchar('[');
 		else
 			printf("[%5d.%06d ", (int) rec->tv.tv_sec,
@@ -872,7 +877,7 @@ static void print_record(struct dmesg_control *ctl,
 	/*
 	 * [ctime]
 	 */
-	} else if (ctl->ctime) {
+	} else if (ctl->time_fmt & TIMEFTM_CTIME) {
 		if (ctl->color)
 			color_enable(DMESG_COLOR_TIME);
 		printf("[%s] ", record_ctime(ctl, rec, buf, sizeof(buf)));
@@ -883,7 +888,7 @@ static void print_record(struct dmesg_control *ctl,
 	/*
 	 * [reltime]
 	 */
-	else if (ctl->reltime) {
+	else if (ctl->time_fmt & TIMEFTM_RELTIME) {
 		double delta;
 		struct tm cur;
 
@@ -918,14 +923,15 @@ static void print_record(struct dmesg_control *ctl,
 	 * the [sec.usec] string.
 	 */
 	if (ctl->method == DMESG_METHOD_KMSG &&
-	    !ctl->notime && !ctl->delta && !ctl->ctime && !ctl->reltime) {
+	    !(ctl->time_fmt & TIMEFTM_NONE)
+	    && !(ctl->time_fmt & (TIMEFTM_CTIME | TIMEFTM_DELTA | TIMEFTM_RELTIME)))
+	{
 		if (ctl->color)
 			color_enable(DMESG_COLOR_TIME);
 		printf("[%5d.%06d] ", (int) rec->tv.tv_sec, (int) rec->tv.tv_usec);
 		if (ctl->color)
 			color_disable();
 	}
-
 mesg:
 	mesg = rec->mesg;
 	mesg_size = rec->mesg_size;
@@ -1065,7 +1071,7 @@ static int parse_kmsg_record(struct dmesg_control *ctl,
 		goto mesg;
 
 	/* C) timestamp */
-	if (ctl->notime)
+	if (ctl->time_fmt & TIMEFTM_NONE)
 		p = skip_item(p, end, ",;");
 	else
 		p = parse_kmsg_timestamp(p, &rec->tv);
@@ -1148,6 +1154,7 @@ int main(int argc, char *argv[])
 		.action = SYSLOG_ACTION_READ_ALL,
 		.method = DMESG_METHOD_KMSG,
 		.kmsg = -1,
+		.time_fmt = 0,
 	};
 	int colormode = UL_COLORMODE_NEVER;
 
@@ -1209,13 +1216,13 @@ int main(int argc, char *argv[])
 			ctl.action = SYSLOG_ACTION_CONSOLE_OFF;
 			break;
 		case 'd':
-			ctl.delta = 1;
+			ctl.time_fmt |= TIMEFTM_DELTA;
 			break;
 		case 'E':
 			ctl.action = SYSLOG_ACTION_CONSOLE_ON;
 			break;
 		case 'e':
-			ctl.reltime = 1;
+			ctl.time_fmt |= TIMEFTM_RELTIME;
 			break;
 		case 'F':
 			ctl.filename = optarg;
@@ -1228,7 +1235,7 @@ int main(int argc, char *argv[])
 				return EXIT_FAILURE;
 			break;
 		case 'H':
-			ctl.reltime = 1;
+			ctl.time_fmt |= TIMEFTM_RELTIME;
 			ctl.color = 1;
 			ctl.pager = 1;
 			break;
@@ -1273,10 +1280,10 @@ int main(int argc, char *argv[])
 		case 'T':
 			ctl.boot_time = get_boot_time();
 			if (ctl.boot_time)
-				ctl.ctime = 1;
+				ctl.time_fmt |= TIMEFTM_CTIME;
 			break;
 		case 't':
-			ctl.notime = 1;
+			ctl.time_fmt |= TIMEFTM_NONE;
 			break;
 		case 'u':
 			ctl.fltr_fac = 1;
@@ -1304,20 +1311,20 @@ int main(int argc, char *argv[])
 	if (argc > 1)
 		usage(stderr);
 
-	if (ctl.raw && (ctl.fltr_lev || ctl.fltr_fac || ctl.delta ||
-			ctl.notime || ctl.ctime || ctl.decode))
+	if (ctl.raw && (ctl.fltr_lev || ctl.fltr_fac || ctl.decode ||
+			ctl.time_fmt & (TIMEFTM_NONE | TIMEFTM_DELTA | TIMEFTM_CTIME)))
 		errx(EXIT_FAILURE, _("--raw can't be used together with level, "
-		     "facility, decode, delta, ctime or notime options"));
+				     "facility, decode, delta, ctime or notime options"));
 
-	if (ctl.notime && (ctl.ctime || ctl.reltime))
+	if (ctl.time_fmt & TIMEFTM_NONE && (ctl.time_fmt & (TIMEFTM_CTIME | TIMEFTM_RELTIME)))
 		errx(EXIT_FAILURE, _("--notime can't be used together with --ctime or --reltime"));
-	if (ctl.reltime && ctl.ctime)
+	if ((ctl.time_fmt & TIMEFTM_RELTIME) && (ctl.time_fmt & TIMEFTM_CTIME))
 		errx(EXIT_FAILURE, _("--reltime can't be used together with --ctime "));
 
-	if (ctl.reltime) {
+	if (ctl.time_fmt & TIMEFTM_RELTIME) {
 		ctl.boot_time = get_boot_time();
 		if (!ctl.boot_time)
-			ctl.reltime = 0;
+			ctl.time_fmt &= TIMEFTM_RELTIME;
 	}
 
 	ctl.color = colors_init(colormode) ? 1 : 0;
-- 
1.8.3


  parent reply	other threads:[~2013-06-02 17:52 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-02 17:51 [PATCH 00/19] pull: various clean ups and couple bug fixes Sami Kerola
2013-06-02 17:51 ` [PATCH 01/19] lib: remove unused code Sami Kerola
2013-06-02 17:51 ` [PATCH 02/19] lscpu: add max MHz value to make cpu governor effects more visible Sami Kerola
2013-06-02 17:51 ` [PATCH 03/19] docs: add lscpu max mhz to manual and bash completion Sami Kerola
2013-06-02 17:51 ` [PATCH 04/19] sfdisk: use libc error printing function, and symbolic exit values Sami Kerola
2013-06-07 10:20   ` Karel Zak
2013-06-02 17:51 ` [PATCH 05/19] sfdisk: clean up usage() functions Sami Kerola
2013-06-02 17:51 ` [PATCH 06/19] sfdisk: use program_invocation_short_name to determine program name Sami Kerola
2013-06-02 17:51 ` [PATCH 07/19] docs: correct sfdisk --activate instructions Sami Kerola
2013-06-02 17:51 ` [PATCH 08/19] sfdisk: remove --unhide and related functions Sami Kerola
2013-06-02 17:51 ` [PATCH 09/19] sfdisk: replace my_warn() with warnx() Sami Kerola
2013-06-02 17:51 ` [PATCH 10/19] rev: stop adding new line at the end when input does not have it Sami Kerola
2013-06-02 17:51 ` [PATCH 11/19] rev: simplify new line detection and impossible test Sami Kerola
2013-06-02 17:51 ` [PATCH 12/19] rev: reduce stream checking when closing read-only file descriptor Sami Kerola
2013-06-02 17:51 ` Sami Kerola [this message]
2013-06-03  8:45   ` [PATCH 13/19] dmesg: make time format parsing to use enum bit field Karel Zak
2013-06-02 17:51 ` [PATCH 14/19] dmesg: add --time-format option Sami Kerola
2013-06-02 17:51 ` [PATCH 15/19] dmesg: add iso-8601 time format Sami Kerola
2013-06-02 17:51 ` [PATCH 16/19] docs: add --time-format option and ISO-8601 format to manual Sami Kerola
2013-06-02 17:51 ` [PATCH 17/19] dmesg: make usage() a little bit shorter Sami Kerola
2013-06-03  8:56   ` Karel Zak
2013-06-02 17:51 ` [PATCH 18/19] dmesg: more deterministic boot time detection Sami Kerola
2013-06-03  9:35   ` Karel Zak
2013-06-02 17:51 ` [PATCH 19/19] cal: fix few type mismatches Sami Kerola
2013-06-07 10:28 ` [PATCH 00/19] pull: various clean ups and couple bug fixes Karel Zak
2013-06-07 15:45   ` Sami Kerola

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1370195485-27907-14-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox