From 4136a27de180cab6ee41543b55a088d216ceedfc Mon Sep 17 00:00:00 2001 From: Andrea Terzolo Date: Wed, 13 Jul 2022 13:24:17 +0200 Subject: [PATCH] new(userspace): add exception management Signed-off-by: Andrea Terzolo --- userspace/falco/statsfilewriter.cpp | 32 ++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/userspace/falco/statsfilewriter.cpp b/userspace/falco/statsfilewriter.cpp index c9e87d4f..d1e0b102 100644 --- a/userspace/falco/statsfilewriter.cpp +++ b/userspace/falco/statsfilewriter.cpp @@ -20,6 +20,7 @@ limitations under the License. #include "statsfilewriter.h" #include "banned.h" // This raises a compilation error when certain functions are used +#include "logger.h" using namespace std; @@ -118,20 +119,27 @@ void StatsFileWriter::handle() delta.n_preemptions = cstats.n_preemptions - m_last_stats.n_preemptions; } - jmsg["sample"] = m_num_stats; - if(m_extra != "") + try { - jmsg["extra"] = m_extra; + jmsg["sample"] = m_num_stats; + if(m_extra != "") + { + jmsg["extra"] = m_extra; + } + jmsg["cur"]["events"] = cstats.n_evts; + jmsg["cur"]["drops"] = cstats.n_drops; + jmsg["cur"]["preemptions"] = cstats.n_preemptions; + jmsg["cur"]["drop_pct"] = (cstats.n_evts == 0 ? 0 : (100.0*cstats.n_drops/cstats.n_evts)); + jmsg["delta"]["events"] = delta.n_evts; + jmsg["delta"]["drops"] = delta.n_drops; + jmsg["delta"]["preemptions"] = delta.n_preemptions; + jmsg["delta"]["drop_pct"] = (delta.n_evts == 0 ? 0 : (100.0*delta.n_drops/delta.n_evts)); + m_output << jmsg.dump() << endl; + } + catch(const exception &e) + { + falco_logger::log(LOG_ERR, "StatsFileWriter (handle): " + string(e.what()) + "\n"); } - jmsg["cur"]["events"] = cstats.n_evts; - jmsg["cur"]["drops"] = cstats.n_drops; - jmsg["cur"]["preemptions"] = cstats.n_preemptions; - jmsg["cur"]["drop_pct"] = (cstats.n_evts == 0 ? 0 : (100.0*cstats.n_drops/cstats.n_evts)); - jmsg["delta"]["events"] = delta.n_evts; - jmsg["delta"]["drops"] = delta.n_drops; - jmsg["delta"]["preemptions"] = delta.n_preemptions; - jmsg["delta"]["drop_pct"] = (delta.n_evts == 0 ? 0 : (100.0*delta.n_drops/delta.n_evts)); - m_output << jmsg.dump() << endl; m_last_stats = cstats; }