Module: check_mk
Branch: master
Commit: 91b46126bf7f0e521738042b129e27dc72c38097
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=91b46126bf7f0e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Aug 2 08:22:52 2017 +0200
No need to modify a log line during parsing.
Change-Id: Id12240802f830eb593f604a0c3105ccb917da42c
---
livestatus/src/LogEntry.cc | 10 +---------
livestatus/src/LogEntry.h | 2 +-
livestatus/src/Logfile.cc | 12 ++++++++++--
livestatus/src/Logfile.h | 3 +--
4 files changed, 13 insertions(+), 14 deletions(-)
diff --git a/livestatus/src/LogEntry.cc b/livestatus/src/LogEntry.cc
index 3b4d84d..a4624f1 100644
--- a/livestatus/src/LogEntry.cc
+++ b/livestatus/src/LogEntry.cc
@@ -24,7 +24,6 @@
#include "LogEntry.h"
#include <cstdlib>
-#include <cstring>
#include <stdexcept>
#include <unordered_map>
#include <utility>
@@ -35,7 +34,7 @@ using mk::starts_with;
using std::string;
using std::unordered_map;
-LogEntry::LogEntry(MonitoringCore *mc, unsigned lineno, char *line) {
+LogEntry::LogEntry(MonitoringCore *mc, unsigned lineno, const char *line) {
// TODO(sp) Fix classifyLogMessage() below to always set all fields and
// remove this set-me-to-zero-to-be-sure-block.
_state = 0;
@@ -44,13 +43,6 @@ LogEntry::LogEntry(MonitoringCore *mc, unsigned lineno, char *line) {
_service = nullptr;
_contact = nullptr;
_lineno = lineno;
-
- // make a copy of the message and strip trailing newline
- size_t linelen = strlen(line);
- while (linelen > 0 && line[linelen - 1] == '\n') {
- line[--linelen] = '\0';
- }
-
_complete = line;
// pointer to options (everything after ':')
diff --git a/livestatus/src/LogEntry.h b/livestatus/src/LogEntry.h
index 473d993..24771a7 100644
--- a/livestatus/src/LogEntry.h
+++ b/livestatus/src/LogEntry.h
@@ -99,7 +99,7 @@ public:
Command _command;
// NOTE: line gets modified!
- LogEntry(MonitoringCore *mc, unsigned lineno, char *line);
+ LogEntry(MonitoringCore *mc, unsigned lineno, const char *line);
unsigned updateReferences(MonitoringCore *mc);
static ServiceState parseServiceState(const std::string &str);
static HostState parseHostState(const std::string &str);
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index 8b228ed..66a596e 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -158,6 +158,7 @@ void Logfile::load(LogCache *logcache, time_t since, time_t until,
void Logfile::loadRange(FILE *file, unsigned missing_types, LogCache *logcache,
time_t since, time_t until, unsigned logclasses) {
vector<char> linebuffer(65536);
+ // TODO(sp) We should really use C++ I/O here...
while (fgets(&linebuffer[0], linebuffer.size(), file) != nullptr) {
if (_lineno >= _mc->maxLinesPerLogFile()) {
Error(logger()) << "more than " <<
_mc->maxLinesPerLogFile()
@@ -165,6 +166,13 @@ void Logfile::loadRange(FILE *file, unsigned missing_types, LogCache
*logcache,
return;
}
_lineno++;
+ // remove trailing newline (should be nuked, see above)
+ for (auto &ch : linebuffer) {
+ if (ch == '\0' || ch == '\n') {
+ ch = '\0';
+ break;
+ }
+ }
if (processLogLine(_lineno, &linebuffer[0], missing_types)) {
logcache->handleNewMessage(this, since, until,
logclasses); // memory management
@@ -192,9 +200,9 @@ long Logfile::freeMessages(unsigned logclasses) {
return freed;
}
-bool Logfile::processLogLine(uint32_t lineno, char *linebuffer,
+bool Logfile::processLogLine(uint32_t lineno, const char *line,
unsigned logclasses) {
- auto entry = make_unique<LogEntry>(_mc, lineno, linebuffer);
+ auto entry = make_unique<LogEntry>(_mc, lineno, line);
// ignored invalid lines
if (entry->_logclass == LogEntry::Class::invalid) {
return false;
diff --git a/livestatus/src/Logfile.h b/livestatus/src/Logfile.h
index 9ffd68b..0b1567d 100644
--- a/livestatus/src/Logfile.h
+++ b/livestatus/src/Logfile.h
@@ -93,8 +93,7 @@ public:
private:
void loadRange(FILE *file, unsigned missing_types, LogCache *, time_t since,
time_t until, unsigned logclasses);
- // NOTE: linebuffer gets modified!
- bool processLogLine(uint32_t, char *linebuffer, unsigned);
+ bool processLogLine(uint32_t lineno, const char *line, unsigned logclasses);
uint64_t makeKey(time_t, unsigned);
Logger *logger() const;
};