Skip to content

Commit

Permalink
Implement an easy parsable log output that allows access to flags of …
Browse files Browse the repository at this point in the history
…the log message

Acked-by: Gert Doering <[email protected]>
Message-Id: <[email protected]>
URL: http://article.gmane.org/gmane.network.openvpn.devel/8374

Signed-off-by: Gert Doering <[email protected]>
  • Loading branch information
schwabe authored and cron2 committed Mar 22, 2014
1 parent c058cbf commit 8f7d5e6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doc/openvpn.8
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,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.
Expand Down
28 changes: 27 additions & 1 deletion src/openvpn/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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 */

Expand Down Expand Up @@ -154,11 +158,18 @@ set_suppress_timestamps (bool suppressed)
suppress_timestamps = 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;
Expand Down Expand Up @@ -330,7 +341,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,
Expand Down
2 changes: 2 additions & 0 deletions src/openvpn/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ void error_reset (void);
void errors_to_stderr (void);

void set_suppress_timestamps (bool suppressed);
void set_machine_readable_output (bool parsable);


#define SDL_CONSTRAIN (1<<0)
bool set_debug_level (const int level, const unsigned int flags);
Expand Down
8 changes: 8 additions & 0 deletions src/openvpn/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -4722,6 +4724,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);
Expand Down
1 change: 1 addition & 0 deletions src/openvpn/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ struct options

bool log;
bool suppress_timestamps;
bool machine_readable_output;
int nice;
int verbosity;
int mute;
Expand Down

0 comments on commit 8f7d5e6

Please sign in to comment.