From 87508ea2795d5067b2ddcaa31aa41cc14fe2bf34 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Mon, 21 Oct 2019 13:32:53 -0700 Subject: [PATCH] automatic evhelper_ev2err --- src/evhelper.cpp | 25 ------------------------- src/evhelper.h | 6 ++---- src/log.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/evhelper.cpp b/src/evhelper.cpp index 6501ca5..12f1f3b 100644 --- a/src/evhelper.cpp +++ b/src/evhelper.cpp @@ -181,31 +181,6 @@ void evbase::assertInLoop() assert(pvt->worker.isCurrentThread()); } - -static -void evlog_handler(int severity, const char *msg) -{ - const char *sevr = "<\?\?\?>"; - int lvl = PLVL_CRIT; - switch(severity) { -#define CASE(EVLVL, PLVL) case EVENT_LOG_##EVLVL : lvl = PLVL_##PLVL; sevr = #PLVL; break - CASE(DEBUG, DEBUG); - CASE(MSG, INFO); - CASE(WARN, WARN); - CASE(ERR, ERR); -#undef CASE - } - log_printf(logerr, lvl, "libevent %s: %s\n", sevr, msg); -} - -void evhelper_ev2err() -{ - event_set_log_callback(&evlog_handler); - - // Hook in to use epicsAssert() ? - // event_set_fatal_callback() -} - evevent::evevent(struct event_base *base, evutil_socket_t sock, short mask, event_callback_fn fn, void *arg) :ev(event_new(base, sock, mask, fn, arg)) { diff --git a/src/evhelper.h b/src/evhelper.h index b4a4a7f..e53af63 100644 --- a/src/evhelper.h +++ b/src/evhelper.h @@ -23,7 +23,8 @@ namespace pvxsimpl { using namespace pvxs; -// in-line string builder (eg. for exception messages) +//! in-line string builder (eg. for exception messages) +//! eg. @code throw std::runtime_error(SB()<<"Some message"<<42); @endcode struct SB { std::ostringstream strm; SB() {} @@ -35,9 +36,6 @@ struct SB { //! prepare libevent for use by multiple threads PVXS_API void evhelper_setup_thread(); -//! Setup forwarding of libevent log messages to errlog -PVXS_API void evhelper_ev2err(); - //! Block the calling thread until any callback in-progress in the //! specified loop has completed. PVXS_API void evhelper_sync(event_base *base); diff --git a/src/log.cpp b/src/log.cpp index 3d460eb..29f7539 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -17,13 +17,33 @@ #include #include +#include "evhelper.h" + typedef epicsGuard Guard; namespace { using namespace pvxs; +DEFINE_LOGGER(logerr, "evlog"); + epicsThreadOnceId logger_once = EPICS_THREAD_ONCE_INIT; +void evlog_handler(int severity, const char *msg) +{ + const char *sevr = "<\?\?\?>"; + int lvl = PLVL_CRIT; + switch(severity) { +#define CASE(EVLVL, PLVL) case EVENT_LOG_##EVLVL : lvl = PLVL_##PLVL; sevr = #PLVL; break + CASE(DEBUG, DEBUG); + CASE(MSG, INFO); + CASE(WARN, WARN); + CASE(ERR, ERR); +#undef CASE + } + log_printf(logerr, lvl, "libevent %s: %s\n", sevr, msg); +} + + int name2lvl(const std::string& name) { #define CASE(LVL) if(name==#LVL) return PLVL_##LVL @@ -41,6 +61,11 @@ struct logger_gbl_t { std::map config; std::multimap loggers; + logger_gbl_t() + { + event_set_log_callback(&evlog_handler); + } + int init(logger *logger) { std::string name(logger->name);