Module: check_mk
Branch: master
Commit: 2f9e3b469e314cb1269a370d9eb30bea74a7a828
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2f9e3b469e314c…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Aug 1 12:30:27 2017 +0200
LogEntry::assign(): Do only *one* thing
Functions should do one thing. They should do it well. They should do it
only. See:
https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-…
We had to const-correct a few fields on the way, which is a good thing, too.
Change-Id: I7adba26d364525a001482961783c717acb7b6494
---
livestatus/src/LogEntry.cc | 39 +++++++++++++++++++++------------------
livestatus/src/LogEntry.h | 18 +++++++++---------
2 files changed, 30 insertions(+), 27 deletions(-)
diff --git a/livestatus/src/LogEntry.cc b/livestatus/src/LogEntry.cc
index 7be2ce9..08ac1ea 100644
--- a/livestatus/src/LogEntry.cc
+++ b/livestatus/src/LogEntry.cc
@@ -92,43 +92,42 @@ LogEntry::LogEntry(MonitoringCore *mc, unsigned lineno, const char
*line) {
LogEntry::~LogEntry() { free(_msg); }
-bool LogEntry::assign(Param par, char **scan) {
+bool LogEntry::assign(Param par, const char *field) {
switch (par) {
case Param::HostName:
- this->_host_name = next_token(scan, ';');
+ this->_host_name = field;
break;
case Param::SvcDesc:
- this->_svc_desc = next_token(scan, ';');
+ this->_svc_desc = field;
break;
case Param::HostState:
this->_state =
- static_cast<int>(parseHostState(safe_next_token(scan,
';')));
+ static_cast<int>(parseHostState(field == nullptr ? "" :
field));
break;
case Param::ServiceState:
- this->_state =
- static_cast<int>(parseServiceState(safe_next_token(scan,
';')));
+ this->_state = static_cast<int>(
+ parseServiceState(field == nullptr ? "" : field));
break;
case Param::State:
- // numeric state
- this->_state = atoi(safe_next_token(scan, ';'));
+ this->_state = field == nullptr ? 0 : atoi(field);
break;
case Param::StateType:
- this->_state_type = next_token(scan, ';');
+ this->_state_type = field;
break;
case Param::Attempt:
- this->_attempt = atoi(safe_next_token(scan, ';'));
+ this->_attempt = field == nullptr ? 0 : atoi(field);
break;
case Param::Comment:
- this->_comment = next_token(scan, ';');
+ this->_comment = field;
break;
case Param::CommandName:
- this->_command_name = next_token(scan, ';');
+ this->_command_name = field;
break;
case Param::ContactName:
- this->_contact_name = next_token(scan, ';');
+ this->_contact_name = field;
break;
case Param::CheckOutput:
- this->_check_output = next_token(scan, ';');
+ this->_check_output = field;
break;
}
@@ -294,7 +293,7 @@ void LogEntry::classifyLogMessage() {
_text = next_token(&scan, ':');
++scan;
for (Param par : def.params) {
- assign(par, &scan);
+ assign(par, next_token(&scan, ';'));
}
return;
}
@@ -409,15 +408,19 @@ HostState LogEntry::parseHostState(const string &str) {
unsigned LogEntry::updateReferences(MonitoringCore *mc) {
unsigned updated = 0;
if (_host_name != nullptr) {
- _host = find_host(_host_name);
+ // Older Nagios headers are not const-correct... :-P
+ _host = find_host(const_cast<char *>(_host_name));
updated++;
}
if (_svc_desc != nullptr) {
- _service = find_service(_host_name, _svc_desc);
+ // Older Nagios headers are not const-correct... :-P
+ _service = find_service(const_cast<char *>(_host_name),
+ const_cast<char *>(_svc_desc));
updated++;
}
if (_contact_name != nullptr) {
- _contact = find_contact(_contact_name);
+ // Older Nagios headers are not const-correct... :-P
+ _contact = find_contact(const_cast<char *>(_contact_name));
updated++;
}
if (_command_name != nullptr) {
diff --git a/livestatus/src/LogEntry.h b/livestatus/src/LogEntry.h
index 7d975c6..213d77d 100644
--- a/livestatus/src/LogEntry.h
+++ b/livestatus/src/LogEntry.h
@@ -80,18 +80,18 @@ public:
time_t _time;
Class _logclass;
LogEntryType _type;
- std::string _complete; // copy of complete unsplit message
- const char *_options; // points into _complete after ':'
- char *_text; // points into msg
- char *_host_name; // points into msg or is 0
- char *_svc_desc; // points into msg or is 0
+ std::string _complete; // copy of complete unsplit message
+ const char *_options; // points into _complete after ':'
+ char *_text; // points into msg
+ const char *_host_name; // points into msg or is 0
+ const char *_svc_desc; // points into msg or is 0
const char *_command_name;
- char *_contact_name;
+ const char *_contact_name;
int _state;
const char *_state_type;
int _attempt;
- char *_check_output;
- char *_comment;
+ const char *_check_output;
+ const char *_comment;
host *_host;
service *_service;
@@ -129,7 +129,7 @@ private:
char *_msg; // split up with binary zeroes
static std::vector<LogDef> log_definitions;
- bool assign(Param par, char **scan);
+ bool assign(Param par, const char *field);
void applyWorkarounds();
void classifyLogMessage();
};