Skip to content

Commit

Permalink
Merge pull request #76 from sy-c/master
Browse files Browse the repository at this point in the history
v2.4.0
  • Loading branch information
sy-c authored Oct 19, 2021
2 parents 3d28557 + 15ce4a9 commit fd0c035
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/releaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ This file describes the main feature changes for each InfoLogger released versio

## v2.3.0 - 15/10/2021
- Added possibility to use an AutoMuteToken in the c++ stream operator.

## v2.4.0 - 19/10/2021
- Added getMessageCount() / resetMessageCount() to keep track of count of messages, by severity.
7 changes: 7 additions & 0 deletions include/InfoLogger/InfoLogger.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ class InfoLogger
/// Reset defined filters.
/// All messages are then kept and handled normally.
void filterReset();

/// Get current messages count for a given severity (use 'Undefined' for any)
/// Messages are counted after filter, and before flood protection.
unsigned long getMessageCount(InfoLogger::Severity severity);

/// Reset counters of messages
void resetMessageCount();

///////////////////////
/// internals
Expand Down
48 changes: 48 additions & 0 deletions src/InfoLogger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,41 @@ class InfoLogger::Impl
floodFile_msg_drop=0;
}

// message statistics
static const unsigned int numberOfSeverities = 5;
std::atomic<unsigned long> messageCountPerSeverity[numberOfSeverities + 1] = {};

int getIndexFromSeverity(InfoLogger::Severity s) {
switch(s) {
case InfoLogger::Severity::Undefined: // undefined counted as 'info', as in defaultMsg
case InfoLogger::Severity::Info:
return 1;
case InfoLogger::Severity::Error:
return 2;
case InfoLogger::Severity::Fatal:
return 3;
case InfoLogger::Severity::Warning:
return 4;
case InfoLogger::Severity::Debug:
return 5;
}
return 0;
}

unsigned long getMessageCount(InfoLogger::Severity severity) {
// when severity "undefined", return the grand total
if (severity == InfoLogger::Severity::Undefined) {
return messageCountPerSeverity[0].load();
}
return messageCountPerSeverity[getIndexFromSeverity(severity)].load();
}

void resetMessageCount() {
for (int i=0; i<=numberOfSeverities; i++) {
messageCountPerSeverity[i] = 0;
}
}

};

void InfoLogger::Impl::refreshDefaultMsg()
Expand Down Expand Up @@ -595,6 +630,11 @@ int InfoLogger::Impl::pushMessage(const InfoLoggerMessageOption& options, const
InfoLoggerMessageHelperSetValue(msg, msgHelper.ix_username, String, context.userName.c_str());
}

// message stats: after filter, before flood protection
// NB: "Undefined" to be counted as "Info", as in defaultMsg. Translated correctly by getIndexFromSeverity.
messageCountPerSeverity[0]++;
messageCountPerSeverity[getIndexFromSeverity(options.severity)]++;

if (flood_protection) {

// update message statistics */
Expand Down Expand Up @@ -1188,6 +1228,14 @@ int InfoLogger::log(AutoMuteToken &limit, const char* message, ...) {
return err;
}

unsigned long InfoLogger::getMessageCount(InfoLogger::Severity severity) {
return mPimpl->getMessageCount(severity);
}

void InfoLogger::resetMessageCount() {
mPimpl->resetMessageCount();
}

// end of namespace
} // namespace InfoLogger
} // namespace AliceO2
Expand Down
6 changes: 4 additions & 2 deletions test/testInfoLogger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ int main()
theLog.log(InfoLogger::Severity::Fatal, "infoLogger FATAL message test");
theLog.log(InfoLogger::Severity::Debug, "infoLogger DEBUG message test");

theLog.log(LogInfoDevel, "Number of messages logged so far: %lu I=%lu", theLog.getMessageCount(InfoLogger::Severity::Undefined), theLog.getMessageCount(InfoLogger::Severity::Info));

InfoLogger::InfoLoggerMessageOption options = InfoLogger::undefinedMessageOption;
options.sourceLine = __LINE__;
options.sourceFile = __FILE__;
Expand All @@ -50,10 +52,10 @@ int main()

// example use of context
InfoLoggerContext ctxt({{InfoLoggerContext::FieldName::Facility, std::string("test1")}});
theLog.log({{}},ctxt,"infoLogger message - facility test1");
theLog.log({{InfoLogger::Severity::Info}},ctxt,"infoLogger message - facility test1");

// reuse a context and overwrite some fields
theLog.log({{}},InfoLoggerContext(ctxt,{{InfoLoggerContext::FieldName::Facility, std::string("test2")}}),"infoLogger message - facility test2");
theLog.log({{InfoLogger::Severity::Info}},InfoLoggerContext(ctxt,{{InfoLoggerContext::FieldName::Facility, std::string("test2")}}),"infoLogger message - facility test2");

// c++ style
theLog << "another test message " << InfoLogger::endm;
Expand Down

0 comments on commit fd0c035

Please sign in to comment.