Skip to contentSkip to conversation

Implement an easy parsable log output that allows access to flags of the log message

PATCH In review Arne Schwabe 2 participants 3 messages Dec 17, 2013 – Mar 21, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement an easy parsable log output that allows access to flags of the log message

---
 doc/openvpn.8         |  6 ++++++
 src/openvpn/error.c   | 28 +++++++++++++++++++++++++++-
 src/openvpn/error.h   |  2 ++
 src/openvpn/options.c |  8 ++++++++
 src/openvpn/options.h |  1 +
 5 files changed, 44 insertions(+), 1 deletion(-)
doc/openvpn.8 +6 -0
index f06d536..48a8864 100644
@@ -2273,6 +2273,12 @@ otherwise would be prepended. In particular, this applies to
 log messages sent to stdout.
 .\"*********************************************************
 .TP
+.B \-\-machine-readable-output
+Always write timestamps and message flags to log messages, even when they
+otherwise would not be prefixed. In particular, this applies to
+log messages sent to stdout.
+.\"*********************************************************
+.TP
 .B \-\-writepid file
 Write OpenVPN's main process ID to
 .B file.
src/openvpn/error.c +27 -1
index 98611a1..af865f3 100644
@@ -86,6 +86,10 @@ static bool std_redir;      /* GLOBAL */
 /* Should messages be written to the syslog? */
 static bool use_syslog;     /* GLOBAL */
 
+/* Should stdout/stderr be be parsable and always be prefixed with time
+ * and message flags */
+static bool machine_readable_output;   /* GLOBAL */
+
 /* Should timestamps be included on messages to stdout/stderr? */
 static bool suppress_timestamps; /* GLOBAL */
 
@@ -159,10 +163,17 @@ set_suppress_timestamps (bool suppressed)
 }
 
 void
+set_machine_readable_output (bool parsable)
+{
+  machine_readable_output = parsable;
+}
+
+void
 error_reset ()
 {
   use_syslog = std_redir = false;
   suppress_timestamps = false;
+  machine_readable_output = false;
   x_debug_level = 1;
   mute_cutoff = 0;
   mute_count = 0;
@@ -334,7 +345,22 @@ void x_msg_va (const unsigned int flags, const char *format, va_list arglist)
 	  FILE *fp = msg_fp(flags);
 	  const bool show_usec = check_debug_level (DEBUG_LEVEL_USEC_TIME);
 
-	  if ((flags & M_NOPREFIX) || suppress_timestamps)
+	  if (machine_readable_output)
+	    {
+	      struct timeval tv;
+	      gettimeofday (&tv, NULL);
+
+	      fprintf (fp, "%lu.%06lu %x %s%s%s%s",
+		       tv.tv_sec,
+		       tv.tv_usec,
+		       flags,
+		       prefix,
+		       prefix_sep,
+		       m1,
+		       "\n");
+
+	    }
+	  else if ((flags & M_NOPREFIX) || suppress_timestamps)
 	    {
 	      fprintf (fp, "%s%s%s%s",
 		       prefix,
src/openvpn/error.h +2 -0
index 27c48b6..5571bfd 100644
@@ -194,6 +194,8 @@ void error_reset (void);
 void errors_to_stderr (void);
 
 void set_suppress_timestamps (bool suppressed);
+void set_parsable_output (bool parsable);
+
 
 #define SDL_CONSTRAIN (1<<0)
 bool set_debug_level (const int level, const unsigned int flags);
src/openvpn/options.c +8 -0
index e8704fe..d5101ce 100644
@@ -342,6 +342,7 @@ static const char usage_message[] =
   "--log file      : Output log to file which is created/truncated on open.\n"
   "--log-append file : Append log to file, or create file if nonexistent.\n"
   "--suppress-timestamps : Don't log timestamps to stdout/stderr.\n"
+  "--machine-readable-output : Always log timestamp, message flags to stdout/stderr.\n"
   "--writepid file : Write main process ID to file.\n"
   "--nice n        : Change process priority (>0 = lower, <0 = higher).\n"
   "--echo [parms ...] : Echo parameters to log output.\n"
@@ -1511,6 +1512,7 @@ show_settings (const struct options *o)
   SHOW_INT (inetd);
   SHOW_BOOL (log);
   SHOW_BOOL (suppress_timestamps);
+  SHOW_BOOL (machine_readable_output);
   SHOW_INT (nice);
   SHOW_INT (verbosity);
   SHOW_INT (mute);
@@ -4662,6 +4664,12 @@ add_option (struct options *options,
       options->suppress_timestamps = true;
       set_suppress_timestamps(true);
     }
+  else if (streq (p[0], "machine-readable-output"))
+    {
+      VERIFY_PERMISSION (OPT_P_GENERAL);
+      options->machine_readable_output = true;
+      set_machine_readable_output(true);
+    }
   else if (streq (p[0], "log-append") && p[1])
     {
       VERIFY_PERMISSION (OPT_P_GENERAL);
src/openvpn/options.h +1 -0
index 95e67df..dda9658 100644
@@ -305,6 +305,7 @@ struct options
 
   bool log;
   bool suppress_timestamps;
+  bool machine_readable_output;
   int nice;
   int verbosity;
   int mute;
Signature · 1 line
1.8.3.4 (Apple Git-47)
#1 lore raw

Hi,

doc/openvpn.8 otherwise would be prepended. In particular, this applies to · line 2281

Quoted 10 lines · Arne Schwabe
doc/openvpn.8 +6 -0
@@ -2273,6 +2273,12 @@ otherwise would be prepended. In particular, this applies to
 log messages sent to stdout.
 .\"*********************************************************
 .TP
+.B \-\-machine-readable-output
+Always write timestamps and message flags to log messages, even when they
+otherwise would not be prefixed. In particular, this applies to
+log messages sent to stdout.
+.\"*********************************************************
+.TP

I'm fine with it, but I'd like a bit more review by others - is this good enough? Does it cover all corner cases?

It certainly looks nice on the "OpenVPN for Android" App if you can use the slider to increase/decrease the log level for an already-happened openvpn connection, in real time :-)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#2 lore raw

Hi,

Quoted 15 lines · Gert Doering

doc/openvpn.8 otherwise would be prepended. In particular, this applies to · line 2281

Quoted 10 lines · Arne Schwabe
doc/openvpn.8 +6 -0
@@ -2273,6 +2273,12 @@ otherwise would be prepended. In particular, this applies to
 log messages sent to stdout.
 .\"*********************************************************
 .TP
+.B \-\-machine-readable-output
+Always write timestamps and message flags to log messages, even when they
+otherwise would not be prefixed. In particular, this applies to
+log messages sent to stdout.
+.\"*********************************************************
+.TP

I'm fine with it, but I'd like a bit more review by others - is this good enough? Does it cover all corner cases?

Quoted 3 lines

It certainly looks nice on the "OpenVPN for Android" App if you can use the slider to increase/decrease the log level for an already-happened openvpn connection, in real time :-)

Just FTR, as nobody has complained about this, I'm ACKing it now and merging it.

We *could* use a bit more of your attention in reviewing code, not only in lengthy what-if discussions... :-)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...