Skip to content

Commit

Permalink
Limit the period over which messages are collapsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
caladri committed Apr 7, 2015
1 parent 657d666 commit 40d011e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions common/log.cc
Expand Up @@ -35,12 +35,18 @@
#include <list>
#include <sstream>

/* Collapse repeating log messages for up to 2 seconds. */
#define LOG_REPEAT_COLLAPSE_SECONDS (2)

struct LogMask {
regex_t regex_;
enum Log::Priority priority_;
};

static std::list<LogMask> log_masks;
static std::pair<std::string, std::string> last_log;
static unsigned last_log_count;
static struct timeval last_log_start;

#ifdef USE_SYSLOG
static int syslog_priority(const Log::Priority&);
Expand Down Expand Up @@ -98,6 +104,22 @@ Log::log(const Priority& priority, const LogHandle& handle,
if (rv == -1)
memset(&now, 0, sizeof now);

if (last_log.first == handle_string && last_log.second == message &&
now.tv_sec - last_log_start.tv_sec <= LOG_REPEAT_COLLAPSE_SECONDS) {
last_log_count++;
return;
} else {
if (last_log_count != 0) {
std::cerr << now << " [" << last_log.first << "] " <<
"last message repeated " << last_log_count <<
" times." << std::endl;
last_log_count = 0;
}
last_log.first = handle_string;
last_log.second = message;
last_log_start = now;
}

std::cerr << now << " [" << handle_string << "] " <<
priority << ": " <<
message <<
Expand Down

0 comments on commit 40d011e

Please sign in to comment.