@@ -70,7 +70,10 @@ class DPLRawParser
7070 using buffer_type = typename rawparser_type::buffer_type;
7171
7272 DPLRawParser () = delete ;
73- DPLRawParser (InputRecord& inputs, std::vector<InputSpec> filterSpecs = {}) : mInputs (inputs), mFilterSpecs (filterSpecs) {}
73+ DPLRawParser (InputRecord& inputs, std::vector<InputSpec> filterSpecs = {}, fair::Severity sev = fair::Severity::alarm) : mInputs (inputs), mFilterSpecs (filterSpecs), mSeverity (sev) {}
74+
75+ void setMaxFailureMessages (size_t n) { mMaxFailureMessages = n; }
76+ void setExtFailureCounter (size_t * cnt) { mExtFailureCounter = cnt; }
7477
7578 // this is a dummy default buffer used to initialize the RawParser in the iterator
7679 // constructor
@@ -98,8 +101,8 @@ class DPLRawParser
98101
99102 Iterator () = delete ;
100103
101- Iterator (InputRecord& parent, input_iterator it, input_iterator end, std::vector<InputSpec> const & filterSpecs)
102- : mParent (parent), mInputIterator (it), mEnd (end), mPartIterator (mInputIterator .begin()), mParser (std::make_unique<parser_type>(reinterpret_cast <const char *>(&initializer), sizeof (initializer))), mCurrent (mParser ->begin ()), mFilterSpecs(filterSpecs)
104+ Iterator (InputRecord& parent, input_iterator it, input_iterator end, std::vector<InputSpec> const & filterSpecs, fair::Severity sev = fair::Severity::alarm, size_t maxErrMsg = - 1 , size_t * cntErrMsg = nullptr )
105+ : mParent (parent), mInputIterator (it), mEnd (end), mPartIterator (mInputIterator .begin()), mParser (std::make_unique<parser_type>(reinterpret_cast <const char *>(&initializer), sizeof (initializer))), mCurrent (mParser ->begin ()), mFilterSpecs(filterSpecs), mMaxFailureMessages(maxErrMsg), mExtFailureCounter(cntErrMsg), mSeverity(sev)
103106 {
104107 mParser .reset ();
105108 next ();
@@ -231,6 +234,22 @@ class DPLRawParser
231234
232235 bool next ()
233236 {
237+
238+ auto logFailure = [this ](const std::string& msg, const std::runtime_error& e) {
239+ if (!this ->mExtFailureCounter || (*this ->mExtFailureCounter )++ < this ->mMaxFailureMessages ) {
240+ if (this ->mSeverity == fair::Severity::alarm) {
241+ LOG (alarm) << msg << (*this ->mInputIterator ).spec ->binding << " : " << e.what ();
242+ } else if (this ->mSeverity == fair::Severity::warn) {
243+ LOG (warn) << msg << (*this ->mInputIterator ).spec ->binding << " : " << e.what ();
244+ } else if (this ->mSeverity == fair::Severity::fatal) {
245+ LOG (fatal) << msg << (*this ->mInputIterator ).spec ->binding << " : " << e.what ();
246+ } else if (this ->mSeverity == fair::Severity::info) {
247+ LOG (info) << msg << (*this ->mInputIterator ).spec ->binding << " : " << e.what ();
248+ } else {
249+ LOG (debug) << msg << (*this ->mInputIterator ).spec ->binding << " : " << e.what ();
250+ }
251+ } };
252+
234253 while (mInputIterator != mEnd ) {
235254 bool isInitial = mParser == nullptr ;
236255 while (mPartIterator != mInputIterator .end ()) {
@@ -262,9 +281,7 @@ class DPLRawParser
262281 try {
263282 raw = mParent .get <gsl::span<char >>(*mPartIterator );
264283 } catch (const std::runtime_error& e) {
265- // TODO: need some better handling to avoid to be spammed by error messages
266- LOG (error) << " failed to read data from " << (*mInputIterator ).spec ->binding ;
267- LOG (error) << e.what ();
284+ logFailure (" failed to read data from " , e);
268285 }
269286 if (raw.size () == 0 ) {
270287 continue ;
@@ -273,8 +290,7 @@ class DPLRawParser
273290 try {
274291 mParser = std::make_unique<parser_type>(raw.data (), raw.size ());
275292 } catch (const std::runtime_error& e) {
276- LOG (alarm) << " can not create raw parser form input data" ;
277- LOG (alarm) << e.what ();
293+ logFailure (" can not create raw parser from " , e);
278294 }
279295
280296 if (mParser != nullptr ) {
@@ -295,18 +311,21 @@ class DPLRawParser
295311 std::unique_ptr<parser_type> mParser ;
296312 parser_iterator mCurrent ;
297313 std::vector<InputSpec> const & mFilterSpecs ;
314+ size_t mMaxFailureMessages = -1 ;
315+ size_t * mExtFailureCounter = nullptr ; // external optionally provided counter to throttle error messages
316+ fair::Severity mSeverity = fair::Severity::alarm;
298317 };
299318
300319 using const_iterator = Iterator<DataRef const >;
301320
302321 const_iterator begin () const
303322 {
304- return const_iterator (mInputs , mInputs .begin (), mInputs .end (), mFilterSpecs );
323+ return const_iterator (mInputs , mInputs .begin (), mInputs .end (), mFilterSpecs , mSeverity , mMaxFailureMessages , mExtFailureCounter );
305324 }
306325
307326 const_iterator end () const
308327 {
309- return const_iterator (mInputs , mInputs .end (), mInputs .end (), mFilterSpecs );
328+ return const_iterator (mInputs , mInputs .end (), mInputs .end (), mFilterSpecs , mSeverity , mMaxFailureMessages , mExtFailureCounter );
310329 }
311330
312331 // / Format helper for stream output of the iterator content,
@@ -316,6 +335,9 @@ class DPLRawParser
316335 private:
317336 InputRecord& mInputs ;
318337 std::vector<InputSpec> mFilterSpecs ;
338+ size_t mMaxFailureMessages = -1 ;
339+ size_t * mExtFailureCounter = nullptr ; // external optionally provided counter to throttle error messages
340+ fair::Severity mSeverity = fair::Severity::alarm;
319341};
320342
321343} // namespace o2::framework
0 commit comments