Skip to content

Commit

Permalink
libflux/flog: Support output to stderr
Browse files Browse the repository at this point in the history
Make flux handle optional in logging functions.  If flux handle
is NULL, output to stderr.

Add unit tests appropriately.

Fixes flux-framework#1191
  • Loading branch information
chu11 committed Sep 14, 2017
1 parent 06a2ec8 commit 7fd9a3b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/common/libflux/flog.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static int log_rpc (flux_t *h, const char *buf, int len, int flags)

int flux_vlog (flux_t *h, int level, const char *fmt, va_list ap)
{
logctx_t *ctx = getctx (h);
logctx_t *ctx;
int saved_errno = errno;
uint32_t rank;
int len;
Expand All @@ -143,7 +143,15 @@ int flux_vlog (flux_t *h, int level, const char *fmt, va_list ap)
struct stdlog_header hdr;
int rpc_flags = FLUX_RPC_NORESPONSE;

if (!ctx) {
if (!h) {
char buf[FLUX_MAX_LOGBUF + 1];
const char *lstr = stdlog_severity_to_string (LOG_PRI (level));

(void)vsnprintf (buf, sizeof (buf), fmt, ap);
return fprintf (stderr, "%s: %s\n", lstr, buf);
}

if (!(ctx = getctx (h))) {
errno = ENOMEM;
goto fatal;
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/libflux/flog.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void flux_log_set_appname (flux_t *h, const char *s);
void flux_log_set_procid (flux_t *h, const char *s);

/* Log a message at the specified level, as defined for syslog(3).
*
* Flux handle is optional, if set to NULL output to stderr.
*/
int flux_vlog (flux_t *h, int level, const char *fmt, va_list ap);
int flux_log (flux_t *h, int level, const char *fmt, ...)
Expand All @@ -37,6 +39,8 @@ int flux_log (flux_t *h, int level, const char *fmt, ...)
/* Log a message at LOG_ERR level, appending a colon, space, and error string.
* The system 'errno' is assumed to be valid and contain an error code
* that can be decoded with zmq_strerror(3).
*
* Flux handle is optional, if set to NULL output to stderr.
*/
void flux_log_verror (flux_t *h, const char *fmt, va_list ap);
void flux_log_error (flux_t *h, const char *fmt, ...)
Expand Down
6 changes: 6 additions & 0 deletions t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ check_PROGRAMS = \
loop/reactor.t \
loop/reduce.t \
loop/log.t \
loop/logstderr \
rpc/rpc.t \
rpc/mrpc.t \
rolemask/loop.t \
Expand Down Expand Up @@ -237,6 +238,11 @@ loop_log_t_SOURCES = loop/log.c
loop_log_t_CPPFLAGS = $(test_cppflags)
loop_log_t_LDADD = $(test_ldadd) $(LIBDL)

loop_logstderr_SOURCES = loop/logstderr.c
loop_logstderr_CPPFLAGS = $(test_cppflags)
loop_logstderr_LDADD = \
$(test_ldadd) $(LIBDL) $(LIBUTIL)

loop_reactor_t_SOURCES = loop/reactor.c
loop_reactor_t_CPPFLAGS = $(test_cppflags)
loop_reactor_t_LDADD = $(test_ldadd) $(LIBDL)
Expand Down
19 changes: 19 additions & 0 deletions t/loop/logstderr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <errno.h>
#include <syslog.h>
#include <flux/core.h>

int main (int argc, char *argv[])
{
errno = EPERM;
flux_log (NULL, LOG_WARNING, "hello");

errno = ENOENT;
flux_log_error (NULL, "world");

return (0);
}

/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/

7 changes: 7 additions & 0 deletions t/t0001-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,11 @@ test_expect_success 'instance can stop cleanly with subscribers (#1025)' '
flux start ${ARGS} -s2 --bootstrap=selfpmi bash -c "nohup flux event sub hb &"
'

# test for issue #1191
test_expect_success 'passing NULL to flux_log functions logs to stderr (#1191)' '
${FLUX_BUILD_DIR}/t/loop/logstderr > std.out 2> std.err
grep "warning: hello" std.err &&
grep "err: world: No such file or directory" std.err
'

test_done

0 comments on commit 7fd9a3b

Please sign in to comment.