X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Flog.c;h=6ac941064b1b80e91c466600dc05a0cbe8933441;hb=1ac31d623afa6873c1cbe691bf8429c03d289ac2;hp=df6eae0531ecdef5f5e3352acd49157ec418aa14;hpb=2f022a488e18b369820934c2ba54525e0c4469ee;p=libucw.git diff --git a/ucw/log.c b/ucw/log.c index df6eae05..6ac94106 100644 --- a/ucw/log.c +++ b/ucw/log.c @@ -3,6 +3,7 @@ * * (c) 1997--2009 Martin Mares * (c) 2008 Tomas Gavenciak + * (c) 2014 Tomas Valla * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -33,7 +34,9 @@ static void NONRET do_die(void); static int default_log_handler(struct log_stream *ls UNUSED, struct log_msg *m) { // This is a completely bare version of the log-file module. Errors are ignored. - write(2, m->m, m->m_len); + if (write(2, m->m, m->m_len) < 0) + { + } return 0; } @@ -161,10 +164,11 @@ vmsg(uns cat, const char *fmt, va_list args) } /* Pass the message to the log_stream */ - if (log_pass_msg(0, ls, &m)) + log_pass_msg(ls, &m); + if (m.error) { /* Error (such as infinite loop) occurred */ - log_pass_msg(0, &log_stream_default, &m); + log_pass_msg(&log_stream_default, &m); } if (m.raw_msg != msgbuf) @@ -194,7 +198,7 @@ log_report_err(struct log_stream *ls, struct log_msg *m, int err) errno = err; snprintf(errbuf, sizeof(errbuf), "Error logging to %s: %m", name); } - log_pass_msg(0, &log_stream_default, &errm); + log_pass_msg(&log_stream_default, &errm); if (ls->stream_flags & LSFLAG_ERR_IS_FATAL) do_die(); @@ -203,32 +207,20 @@ log_report_err(struct log_stream *ls, struct log_msg *m, int err) /* Maximal depth of log_pass_msg recursion */ #define LS_MAX_DEPTH 64 -int -log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m) +void +log_pass_filtered(struct log_stream *ls, struct log_msg *m) { - ASSERT(ls); - - /* Check recursion depth */ - if (depth > LS_MAX_DEPTH) - { - log_report_err(ls, m, EDEADLK); - return 1; - } - - /* Filter by level, type and hook function */ - if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels) || - !((1 << LS_GET_TYPE(m->flags)) & ls->types) || - ls->filter && ls->filter(ls, m)) - return 0; - /* Pass the message to substreams */ CLIST_FOR_EACH(simp_node *, s, ls->substreams) - if (log_pass_msg(depth+1, s->p, m)) - return 1; + { + log_pass_msg(s->p, m); + if (m->error) + return; + } /* Will pass to the handler of this stream... is there any? */ if (!ls->handler) - return 0; + return; /* Will print a message type? */ char *type = NULL; @@ -316,7 +308,36 @@ log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m) if (free_buf) xfree(free_buf); - return 0; +} + +void +log_pass_msg(struct log_stream *ls, struct log_msg *m) +{ + ASSERT(ls); + + /* Check recursion depth */ + if (m->depth > LS_MAX_DEPTH) + { + log_report_err(ls, m, EDEADLK); + m->error = 1; + return; + } + + /* Filter by level and type */ + if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels) || + !((1 << LS_GET_TYPE(m->flags)) & ls->types)) + return; + + m->depth++; + + if (ls->filter && ls->filter(ls, m)) + { + // The filter might have called log_pass_filtered() + } + else + log_pass_filtered(ls, m); + + m->depth--; } /*** Utility functions ***/ @@ -368,6 +389,16 @@ assert_failed(const char *assertion, const char *file, int line) abort(); } +void +assert_failed_msg(const char *assertion, const char *file, int line, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + vmsg(L_DEBUG, fmt, args); + msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line); + abort(); +} + void assert_failed_noinfo(void) {