Re: [Toybox] [PATCH] Implement dmesg -T.

2017-04-05 Thread enh
On Tue, Apr 4, 2017 at 4:42 PM, Rob Landley  wrote:

> On 04/04/2017 12:32 PM, enh wrote:
> > On Mon, Apr 3, 2017 at 2:43 PM, Rob Landley  > > wrote:
> >
> > On 04/03/2017 03:03 PM, enh wrote:
> > > since i have to maintain a parallel build system,
> promotion/demotion
> > > just mean extra work for me, so i'm always happier with the status
> quo :-)
> >
> > Trying to converge the build systems is on my todo list. :)
> >
> > (But today I'm playing with GPS signal tracking heuristics for
> $DAYJOB.
> > I'm finally seeing 

Re: [Toybox] [PATCH] Implement dmesg -T.

2017-04-04 Thread Rob Landley
On 04/04/2017 12:32 PM, enh wrote:
> On Mon, Apr 3, 2017 at 2:43 PM, Rob Landley  > wrote:
> 
> On 04/03/2017 03:03 PM, enh wrote:
> > since i have to maintain a parallel build system, promotion/demotion
> > just mean extra work for me, so i'm always happier with the status quo 
> :-)
> 
> Trying to converge the build systems is on my todo list. :)
> 
> (But today I'm playing with GPS signal tracking heuristics for $DAYJOB.
> I'm finally seeing 

Re: [Toybox] [PATCH] Implement dmesg -T.

2017-04-04 Thread enh
On Mon, Apr 3, 2017 at 2:43 PM, Rob Landley  wrote:

> On 04/03/2017 03:03 PM, enh wrote:
> > since i have to maintain a parallel build system, promotion/demotion
> > just mean extra work for me, so i'm always happier with the status quo
> :-)
>
> Trying to converge the build systems is on my todo list. :)
>
> (But today I'm playing with GPS signal tracking heuristics for $DAYJOB.
> I'm finally seeing #%*(&%

Re: [Toybox] [PATCH] Implement dmesg -T.

2017-04-03 Thread Rob Landley
On 04/03/2017 03:03 PM, enh wrote:
> since i have to maintain a parallel build system, promotion/demotion
> just mean extra work for me, so i'm always happier with the status quo :-)

Trying to converge the build systems is on my todo list. :)

(But today I'm playing with GPS signal tracking heuristics for $DAYJOB.
I'm finally seeing #%*(&%

Re: [Toybox] [PATCH] Implement dmesg -T.

2017-04-03 Thread enh
since i have to maintain a parallel build system, promotion/demotion just
mean extra work for me, so i'm always happier with the status quo :-)

as for the more recent patches i've been sending... it's not that i hate
awk -- i'd actually love to have the time to sit down and write toybox awk
-- but i'm trying to get to where we pass all the toybox tests [for the
toys we're using] on Android.

...and one reason i'm [finally] paying that the attention it deserves is
that i have a libz-based gzip that i'll add to _tool_box in the short term
and then work out how to get that into toybox (since you'll presumably want
your no-dependencies version too, but you presumably won't want to
reimplement the libz interface). but i may as well commit the tests to
toybox right away. not least because some of the existing tests (like the
tar tests) implicitly test gzip/gunzip/zcat.

On Mon, Apr 3, 2017 at 1:32 PM, Rob Landley  wrote:

> On 03/31/2017 11:27 AM, enh wrote:
> > ping.
>
> Sorry, had a window open to review whether to promote it back to lsb and
> a week went by...
>
> Applied, still need to review it and re-promote.
>
> Rob
>



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] Implement dmesg -T.

2017-04-03 Thread Rob Landley
On 03/31/2017 11:27 AM, enh wrote:
> ping.

Sorry, had a window open to review whether to promote it back to lsb and
a week went by...

Applied, still need to review it and re-promote.

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [PATCH] Implement dmesg -T.

2017-03-25 Thread enh
Also refactor so that legacy mode gets all the new functionality too, and
add -S to make it easier to test legacy mode on 3.5 or newer kernels (at least
on the host; on Android the syslog(2) system call is no longer allowed).

Bug: http://b/36090178
---
 toys/pending/dmesg.c | 153 +--
 1 file changed, 88 insertions(+), 65 deletions(-)
From 82e4585c6841a990cb0389324923d9a7d725de17 Mon Sep 17 00:00:00 2001
From: Elliott Hughes 
Date: Sat, 25 Mar 2017 13:08:20 -0700
Subject: [PATCH] Implement dmesg -T.

Also refactor so that legacy mode gets all the new functionality too, and
add -S to make it easier to test legacy mode on 3.5 or newer kernels (at least
on the host; on Android the syslog(2) system call is no longer allowed).

Bug: http://b/36090178
---
 toys/pending/dmesg.c | 153 +--
 1 file changed, 88 insertions(+), 65 deletions(-)

diff --git a/toys/pending/dmesg.c b/toys/pending/dmesg.c
index 40834b0..198f64a 100644
--- a/toys/pending/dmesg.c
+++ b/toys/pending/dmesg.c
@@ -5,13 +5,13 @@
  * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/dmesg.html
 
 // We care that FLAG_c is 1, so keep c at the end.
-USE_DMESG(NEWTOY(dmesg, "w(follow)Ctrs#<1n#c[!tr][!Cc]", TOYFLAG_BIN))
+USE_DMESG(NEWTOY(dmesg, "w(follow)CSTtrs#<1n#c[!Ttr][!Cc]", TOYFLAG_BIN))
 
 config DMESG
   bool "dmesg"
   default n
   help
-usage: dmesg [-Cc] [-r|-t] [-n LEVEL] [-s SIZE] [-w]
+usage: dmesg [-Cc] [-r|-t|-T] [-n LEVEL] [-s SIZE] [-w]
 
 Print or control the kernel ring buffer.
 
@@ -19,8 +19,10 @@ config DMESG
 -c	Clear ring buffer after printing
 -n	Set kernel logging LEVEL (1-9)
 -r	Raw output (with )
+-S	Use syslog(2) rather than /dev/kmsg
 -s	Show the last SIZE many bytes
--t	Don't print kernel's timestamps
+-T	Show human-readable timestamps
+-t	Don't print timestamps
 -w	Keep waiting for more output (aka --follow)
 */
 
@@ -32,9 +34,77 @@ GLOBALS(
   long level;
   long size;
 
-  int color;
+  int use_color;
+  struct sysinfo info;
 )
 
+static void color(int c)
+{
+  if (TT.use_color) printf("\033[%dm", c);
+}
+
+static void format_message(char *msg, int new) {
+  unsigned long long time_s;
+  unsigned long long time_us;
+  int facpri, subsystem, pos;
+  char *p, *text;
+
+  // The new /dev/kmsg and the old syslog(2) formats differ slightly.
+  if (new) {
+if (sscanf(msg, "%u,%*u,%llu,%*[^;];%n", , _us, ) != 2)
+  return;
+
+time_s = time_us/100;
+time_us %= 100;
+  } else {
+if (sscanf(msg, "<%u>[%llu.%llu] %n",
+   , _s, _us, ) != 3)
+  return;
+  }
+
+  // Drop extras after end of message text.
+  text = msg + pos;
+  if ((p = strchr(text, '\n'))) *p = 0;
+
+  // Is there a subsystem? (The ": " is just a convention.)
+  p = strstr(text, ": ");
+  subsystem = p ? (p - text) : 0;
+
+  // "Raw" is a lie for /dev/kmsg. In practice, it just means we show the
+  // syslog facility/priority at the start of each line to emulate the
+  // historical syslog(2) format.
+  if (toys.optflags_r) printf("<%d>", facpri);
+
+  // Format the time.
+  if (!(toys.optflags_t)) {
+color(32);
+if (toys.optflags_T) {
+  time_t t = (time(NULL) - TT.info.uptime) + time_s;
+  char *ts = ctime();
+
+  printf("[%.*s] ", (int)(strlen(ts) - 1), ts);
+} else {
+  printf("[%5lld.%06lld] ", time_s, time_us);
+}
+color(0);
+  }
+
+  // Errors (or worse) are shown in red, subsystems are shown in yellow.
+  if (subsystem) {
+color(33);
+printf("%.*s", subsystem, text);
+text += subsystem;
+color(0);
+  }
+  if (!((facpri&7) <= 3)) xputs(text);
+  else {
+color(31);
+printf("%s", text);
+color(0);
+xputc('\n');
+  }
+}
+
 static int xklogctl(int type, char *buf, int len)
 {
   int rc = klogctl(type, buf, len);
@@ -55,34 +125,25 @@ static void legacy_mode()
   data = to = from = xmalloc(size+1);
   data[size = xklogctl(3 + (toys.optflags & FLAG_c), data, size)] = 0;
 
-  // Filter out level markers and optionally time markers
-  if (!(toys.optflags & FLAG_r)) while ((from - data) < size) {
-if (from == data || from[-1] == '\n') {
-  char *to;
+  // Break into messages (one per line) and send each one to format_message.
+  to = data + size;
+  while (from < to) {
+char *msg_end = memchr(from, '\n', (to-from));
 
-  if (*from == '<' && (to = strchr(from, '>'))) from = ++to;
-  if ((toys.optflags_t) && *from == '[' && (to = strchr(from, ']')))
-from = to+1+(to[1]==' ');
-}
-*(to++) = *(from++);
-  } else to = data+size;
-
-  // Write result. The odds of somebody requesting a buffer of size 3 and
-  // getting "<1>" are remote, but don't segfault if they do.
-  if (to != data) {
-xwrite(1, data, to-data);
-if (to[-1] != '\n') xputc('\n');
+if (!msg_end) break;
+*msg_end = '\0';
+format_message(from, 0);
+