Module: check_mk
Branch: master
Commit: 3ba9ec9e68e1791048cc5319cc4b397718980673
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3ba9ec9e68e179…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Aug 2 08:11:38 2017 +0200
Parse timestamp without modifying underlying string.
Change-Id: I9a9ca4b55004d25756cce3cf9959aca9bbff5162
---
livestatus/src/LogEntry.cc | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/livestatus/src/LogEntry.cc b/livestatus/src/LogEntry.cc
index 4201aa4..3b4d84d 100644
--- a/livestatus/src/LogEntry.cc
+++ b/livestatus/src/LogEntry.cc
@@ -25,6 +25,7 @@
#include "LogEntry.h"
#include <cstdlib>
#include <cstring>
+#include <stdexcept>
#include <unordered_map>
#include <utility>
#include "MonitoringCore.h"
@@ -50,8 +51,6 @@ LogEntry::LogEntry(MonitoringCore *mc, unsigned lineno, char *line) {
line[--linelen] = '\0';
}
- // keep unsplit copy of the message (needs lots of memory, maybe we could
- // optimize that one day...)
_complete = line;
// pointer to options (everything after ':')
@@ -64,17 +63,22 @@ LogEntry::LogEntry(MonitoringCore *mc, unsigned lineno, char *line) {
}
_options = &_complete[pos];
- // [1260722267] xxx - extract timestamp, validate message
- if (linelen < 13 || line[0] != '[' || line[11] != ']') {
+ // 0123456789012345678901234567890
+ // [1234567890] FOO BAR: blah blah
+ constexpr size_t timestamp_prefix_length = 13;
+ try {
+ if (_complete.size() < timestamp_prefix_length || _complete[0] != '['
||
+ _complete[11] != ']' || _complete[12] != ' ') {
+ throw std::invalid_argument("timestamp delimiter");
+ }
+ _time = std::stoi(_complete.substr(1, 10));
+ } catch (const std::logic_error &e) {
_logclass = Class::invalid;
_type = LogEntryType::none;
return; // ignore invalid lines silently
}
- line[11] = 0; // zero-terminate time stamp
- _time = atoi(line + 1);
- // also skip space after timestamp
- classifyLogMessage(line + 13);
+ classifyLogMessage(line + timestamp_prefix_length);
applyWorkarounds();
updateReferences(mc);
}