Module: check_mk
Branch: master
Commit: b1bc76d996917e8af9ff72657bd9b1f3543655fd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b1bc76d996917e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Aug 4 10:42:39 2017 +0200
Made ownership explicit.
By using std::unique_ptr instead of a naked pointer, we make it blindingly
obvious that the LogEntry instances are owned by their corresponding LogFile
instance. This removes a few fragile 'delete's, too.
Change-Id: I36d7e14e3731d937a24dac8d24a7970e132cdc91
---
livestatus/src/Logfile.cc | 41 +++++++++++++++----------------------
livestatus/src/Logfile.h | 6 +++---
livestatus/src/MonitoringCore.h | 1 +
livestatus/src/TableStateHistory.cc | 15 ++++++--------
livestatus/src/TableStateHistory.h | 2 +-
5 files changed, 28 insertions(+), 37 deletions(-)
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index 71625f9..84c8e65 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -90,10 +90,6 @@ Logfile::Logfile(MonitoringCore *mc, fs::path path, bool watch)
Logfile::~Logfile() { flush(); }
void Logfile::flush() {
- for (auto &entry : _entries) {
- delete entry.second;
- }
-
_entries.clear();
_logclasses_read = 0;
}
@@ -186,9 +182,8 @@ long Logfile::freeMessages(unsigned logclasses) {
// usual post-increment idiom, see Scott Meyers' "Effective STL", item
9
// ("Choose carefully among erasing options.").
for (auto it = _entries.begin(); it != _entries.end();) {
- LogEntry *entry = it->second;
- if (((1u << static_cast<int>(entry->_logclass)) & logclasses)
!= 0u) {
- delete entry;
+ if (((1u << static_cast<int>(it->second->_logclass)) &
logclasses) !=
+ 0u) {
_entries.erase(it++);
freed++;
} else {
@@ -224,10 +219,10 @@ logfile_entries_t *Logfile::getEntriesFromQuery(Query * /*unused*/,
LogCache *logcache,
time_t since, time_t until,
unsigned logclasses) {
- updateReferences(); // Make sure existing references to objects point to
- // correct world
- load(logcache, since, until,
- logclasses); // make sure all messages are present
+ // Make sure existing references to objects point to correct world
+ updateReferences();
+ // make sure all messages are present
+ load(logcache, since, until, logclasses);
return &_entries;
}
@@ -239,10 +234,10 @@ bool Logfile::answerQuery(Query *query, LogCache *logcache, time_t
since,
load(logcache, since, until, logclasses);
uint64_t sincekey = makeKey(since, 0);
for (auto it = _entries.lower_bound(sincekey); it != _entries.end(); ++it) {
- LogEntry *entry = it->second;
// end found or limit exceeded?
- if (entry->_time >= until || !query->processDataset(Row(entry))) {
- return false; // limit exceeded
+ if (it->second->_time >= until ||
+ !query->processDataset(Row(it->second.get()))) {
+ return false;
}
}
return true;
@@ -250,20 +245,18 @@ bool Logfile::answerQuery(Query *query, LogCache *logcache, time_t
since,
bool Logfile::answerQueryReverse(Query *query, LogCache *logcache, time_t since,
time_t until, unsigned logclasses) {
- updateReferences(); // Make sure existing references to objects point to
- // correct world
- load(logcache, since, until,
- logclasses); // make sure all messages are present
+ // Make sure existing references to objects point to correct world
+ updateReferences();
+ // make sure all messages are present
+ load(logcache, since, until, logclasses);
uint64_t untilkey = makeKey(until, 999999999);
auto it = _entries.upper_bound(untilkey);
while (it != _entries.begin()) {
--it;
- LogEntry *entry = it->second;
- if (entry->_time < since) {
- return false; // end found
- }
- if (!query->processDataset(Row(entry))) {
- return false; // limit exceeded
+ // end found or limit exceeded?
+ if (it->second->_time < since ||
+ !query->processDataset(Row(it->second.get()))) {
+ return false;
}
}
return true;
diff --git a/livestatus/src/Logfile.h b/livestatus/src/Logfile.h
index 5fc5663..2099f15 100644
--- a/livestatus/src/Logfile.h
+++ b/livestatus/src/Logfile.h
@@ -30,6 +30,7 @@
#include <cstdio>
#include <ctime>
#include <map>
+#include <memory>
#include <string>
#include "FileSystem.h"
class LogCache;
@@ -39,13 +40,12 @@ class MonitoringCore;
class Query;
#ifdef CMC
-#include <memory>
#include <vector>
class World;
#endif
-typedef std::map<uint64_t, LogEntry *>
- logfile_entries_t; // key is time_t . lineno
+// key is time_t . lineno
+using logfile_entries_t = std::map<uint64_t, std::unique_ptr<LogEntry>>;
class Logfile {
private:
diff --git a/livestatus/src/MonitoringCore.h b/livestatus/src/MonitoringCore.h
index 5555b3f..964ee88 100644
--- a/livestatus/src/MonitoringCore.h
+++ b/livestatus/src/MonitoringCore.h
@@ -59,6 +59,7 @@ public:
class ContactGroup;
class Host;
class Service;
+ class TimePeriod;
virtual ~MonitoringCore() = default;
diff --git a/livestatus/src/TableStateHistory.cc b/livestatus/src/TableStateHistory.cc
index db5fffc..c636cf3 100644
--- a/livestatus/src/TableStateHistory.cc
+++ b/livestatus/src/TableStateHistory.cc
@@ -224,19 +224,18 @@ string TableStateHistory::name() const { return
"statehist"; }
string TableStateHistory::namePrefix() const { return "statehist_"; }
-LogEntry *TableStateHistory::getPreviousLogentry() {
+void TableStateHistory::getPreviousLogentry() {
while (_it_entries == _entries->begin()) {
// open previous logfile
if (_it_logs == _log_cache->logfiles()->begin()) {
- return nullptr;
+ return;
}
--_it_logs;
_entries = _it_logs->second->getEntriesFromQuery(
_query, _log_cache, _since, _until, classmask_statehist);
_it_entries = _entries->end();
}
-
- return (--_it_entries)->second;
+ --_it_entries;
}
LogEntry *TableStateHistory::getNextLogentry() {
@@ -254,7 +253,7 @@ LogEntry *TableStateHistory::getNextLogentry() {
_query, _log_cache, _since, _until, classmask_statehist);
_it_entries = _entries->begin();
}
- return _it_entries->second;
+ return _it_entries->second.get();
}
// TODO(sp) IsObjectFilter in TableCachedStatehist recurses into sub-filters,
@@ -369,15 +368,13 @@ void TableStateHistory::answerQuery(Query *query) {
}
// Determine initial logentry
- LogEntry *entry;
_entries = _it_logs->second->getEntriesFromQuery(
query, _log_cache, _since, _until, classmask_statehist);
if (!_entries->empty() && _it_logs != newest_log) {
_it_entries = _entries->end();
// Check last entry. If it's younger than _since -> use this logfile too
if (--_it_entries != _entries->begin()) {
- entry = _it_entries->second;
- if (entry->_time >= _since) {
+ if (_it_entries->second->_time >= _since) {
_it_entries = _entries->begin();
}
}
@@ -389,7 +386,7 @@ void TableStateHistory::answerQuery(Query *query) {
bool only_update = true;
bool in_nagios_initial_states = false;
- while (nullptr != (entry = getNextLogentry())) {
+ while (LogEntry *entry = getNextLogentry()) {
if (_abort_query) {
break;
}
diff --git a/livestatus/src/TableStateHistory.h b/livestatus/src/TableStateHistory.h
index 77ab097..5e5dd8d 100644
--- a/livestatus/src/TableStateHistory.h
+++ b/livestatus/src/TableStateHistory.h
@@ -69,7 +69,7 @@ private:
logfile_entries_t *_entries;
logfile_entries_t::iterator _it_entries;
- LogEntry *getPreviousLogentry();
+ void getPreviousLogentry();
LogEntry *getNextLogentry();
void process(Query *query, HostServiceState *hs_state);
int updateHostServiceState(Query *query, const LogEntry *entry,