diff options
author | Henning Baldersheim <balder@oath.com> | 2018-09-28 09:39:29 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@oath.com> | 2018-09-28 09:39:29 +0200 |
commit | 5bce87ebb2f1abd2570a0e2cb6d545d5d557cfe9 (patch) | |
tree | 89b65c6dff5d6f238cd77225079476e29b2432d1 /logd | |
parent | bbfc9ef04a31a6f57f5c17316f13dbcf5daaa247 (diff) |
Use std::string to simplify ownership and unify on one type.
Diffstat (limited to 'logd')
-rw-r--r-- | logd/src/logd/conf.cpp | 20 | ||||
-rw-r--r-- | logd/src/logd/conf.h | 2 | ||||
-rw-r--r-- | logd/src/logd/errhandle.h | 8 | ||||
-rw-r--r-- | logd/src/logd/forward.cpp | 10 | ||||
-rw-r--r-- | logd/src/logd/perform.cpp | 8 | ||||
-rw-r--r-- | logd/src/logd/service.cpp | 39 | ||||
-rw-r--r-- | logd/src/logd/service.h | 34 |
7 files changed, 57 insertions, 64 deletions
diff --git a/logd/src/logd/conf.cpp b/logd/src/logd/conf.cpp index f2622ac8020..7b5ed0c88b7 100644 --- a/logd/src/logd/conf.cpp +++ b/logd/src/logd/conf.cpp @@ -22,11 +22,7 @@ ConfSub::configure(std::unique_ptr<LogdConfig> cfg) { const LogdConfig &newconf(*cfg); if (newconf.logserver.host != _logServer) { - if (newconf.logserver.host.size() > 255) { - LOG(warning, "too long logserver hostname: %s", newconf.logserver.host.c_str()); - return; - } - strcpy(_logServer, newconf.logserver.host.c_str()); + _logServer = newconf.logserver.host; _needToConnect = true; } if (newconf.logserver.use != _use_logserver) { @@ -100,12 +96,12 @@ ConfSub::latch() void ConfSub::connectToLogserver() { - int newfd = makeconn(_logServer, _logPort); + int newfd = makeconn(_logServer.c_str(), _logPort); if (newfd >= 0) { resetFileDescriptor(newfd); - LOG(debug, "connected to logserver at %s:%d", _logServer, _logPort); + LOG(debug, "connected to logserver at %s:%d", _logServer.c_str(), _logPort); } else { - LOG(debug, "could not connect to %s:%d", _logServer, _logPort); + LOG(debug, "could not connect to %s:%d", _logServer.c_str(), _logPort); } } @@ -141,7 +137,8 @@ ConfSub::closeConn() } ConfSub::ConfSub(Forwarder &fw, const config::ConfigUri & configUri) - : _logPort(0), + : _logServer(), + _logPort(0), _logserverfd(-1), _statePort(0), _rotate_size(INT_MAX), @@ -155,18 +152,17 @@ ConfSub::ConfSub(Forwarder &fw, const config::ConfigUri & configUri) _hasAvailable(false), _needToConnect(true) { - _logServer[0] = '\0'; _handle = _subscriber.subscribe<LogdConfig>(configUri.getConfigId()); _subscriber.nextConfig(0); configure(_handle->getConfig()); - LOG(debug, "got logServer %s", _logServer); + LOG(debug, "got logServer %s", _logServer.c_str()); LOG(debug, "got handle %p", _handle.get()); } ConfSub::~ConfSub() { - LOG(debug, "forget logServer %s", _logServer); + LOG(debug, "forget logServer %s", _logServer.c_str()); LOG(debug, "done ~ConfSub()"); } diff --git a/logd/src/logd/conf.h b/logd/src/logd/conf.h index cf163d2dcfd..894070fff43 100644 --- a/logd/src/logd/conf.h +++ b/logd/src/logd/conf.h @@ -10,7 +10,7 @@ class Forwarder; class ConfSub { private: - char _logServer[256]; + std::string _logServer; int _logPort; int _logserverfd; int _statePort; diff --git a/logd/src/logd/errhandle.h b/logd/src/logd/errhandle.h index 6798cac3cd9..b2ff3516b69 100644 --- a/logd/src/logd/errhandle.h +++ b/logd/src/logd/errhandle.h @@ -2,15 +2,15 @@ #pragma once #include <stdexcept> -#include <vespa/vespalib/stllike/string.h> +#include <string> namespace logdemon { class MsgException : public std::exception { private: - vespalib::string _string; + std::string _string; public: - MsgException(const char *s) : _string(s) {} + MsgException(const std::string & s) : _string(s) {} ~MsgException() override {} const char *what() const throw() override { return _string.c_str(); } }; @@ -33,4 +33,4 @@ public: SomethingBad(const char *s) : MsgException(s) {} }; -} // namespace +} diff --git a/logd/src/logd/forward.cpp b/logd/src/logd/forward.cpp index 85fd6f3aba1..1abf6bffe58 100644 --- a/logd/src/logd/forward.cpp +++ b/logd/src/logd/forward.cpp @@ -69,8 +69,6 @@ Forwarder::forwardLine(const char *line, const char *eol) bool Forwarder::parseline(const char *linestart, const char *lineend) { - using vespalib::string; - int llength = lineend - linestart; const char *fieldstart = linestart; @@ -133,7 +131,7 @@ Forwarder::parseline(const char *linestart, const char *lineend) if (tab == fieldstart) { LOG(spam, "empty service in logline: %.*s", llength, linestart); } - string service(fieldstart, tab-fieldstart); + std::string service(fieldstart, tab-fieldstart); // component fieldstart = tab + 1; @@ -143,7 +141,7 @@ Forwarder::parseline(const char *linestart, const char *lineend) ++_badLines; return false; } - string component(fieldstart, tab-fieldstart); + std::string component(fieldstart, tab-fieldstart); // level fieldstart = tab + 1; @@ -153,7 +151,7 @@ Forwarder::parseline(const char *linestart, const char *lineend) ++_badLines; return false; } - string level(fieldstart, tab-fieldstart); + std::string level(fieldstart, tab-fieldstart); LogLevel l = _levelparser.parseLevel(level.c_str()); // rest is freeform message, must be on this line: @@ -171,7 +169,7 @@ Forwarder::parseline(const char *linestart, const char *lineend) return found->second; } - Service *svcp = knownServices.getService(service.c_str()); + Service *svcp = knownServices.getService(service); Component *cp = svcp->getComponent(component); cp->remember(logtime, pid); bool retval = cp->shouldForward(l); diff --git a/logd/src/logd/perform.cpp b/logd/src/logd/perform.cpp index e991e143f27..07afac88841 100644 --- a/logd/src/logd/perform.cpp +++ b/logd/src/logd/perform.cpp @@ -69,7 +69,7 @@ ExternalPerformer::doCmd(char *line) if (isPrefix("list components ", line)) { const char *servstr = line+5+11; Service *svc = _services.getService(servstr); - for (const auto & entry : svc->_components) { + for (const auto & entry : svc->components()) { const char * key = entry.first.c_str(); char buf[1024]; snprintf(buf, 1024, "component %s %s\n", servstr, key); @@ -87,7 +87,7 @@ ExternalPerformer::doCmd(char *line) char *compstr = strchr(servstr, ' '); if (compstr == nullptr) { Service *svc = _services.getService(servstr); - for (const auto & entry : svc->_components) { + for (const auto & entry : svc->components()) { listStates(servstr, entry.first.c_str()); } return; @@ -101,7 +101,7 @@ ExternalPerformer::doCmd(char *line) if (levmods == nullptr) { LOG(error, "bad command: %s", line); } else { - vespalib::string orig(line); + std::string orig(line); *levmods++ = '\0'; doSetAllStates(levmods, orig.c_str()); } @@ -176,7 +176,7 @@ ExternalPerformer::doSetAllStates(char *levmods, const char *origline) { if (nextlev) *nextlev++ = '\0'; for (const auto & serviceEntry : _services._services) { Service *svc = _services.getService(serviceEntry.first); - for (const auto & entry : svc->_components) { + for (const auto & entry : svc->components()) { Component *cmp = svc->getComponent(entry.first); assert(cmp != 0); diff --git a/logd/src/logd/service.cpp b/logd/src/logd/service.cpp index 976c7fa6ea4..88d17a93b44 100644 --- a/logd/src/logd/service.cpp +++ b/logd/src/logd/service.cpp @@ -12,16 +12,12 @@ namespace logdemon { unsigned long Component::defFwd = (unsigned long)-1; -Component::Component(const char *servicename, const char *name) +Component::Component(const std::string & servicename, const std::string & name) : _isforwarding(defFwd), _lastseen(0.0), _lastpid(0), _myservice(servicename), _myname(name), - _logctlname() + _logctlname(name.substr(name.find('.'))) { assert(ns_log::Logger::NUM_LOGLEVELS < 32); - const char *withoutprefix = strchr(name, '.'); - if (withoutprefix != nullptr) { - _logctlname = withoutprefix; - } } Component::~Component() = default; @@ -32,8 +28,8 @@ Component::doLogAtAll(LogLevel level) using ns_log::ControlFile; char lcfn[FILENAME_MAX]; - if (! ControlFile::makeName(_myservice, lcfn, FILENAME_MAX)) { - LOG(debug, "no logcontrol file for service '%s'", _myservice); + if (! ControlFile::makeName(_myservice.c_str(), lcfn, FILENAME_MAX)) { + LOG(debug, "no logcontrol file for service '%s'", _myservice.c_str()); return; } try { @@ -41,7 +37,7 @@ Component::doLogAtAll(LogLevel level) unsigned int *lstring = foo.getLevels(_logctlname.c_str()); lstring[level] = CHARS_TO_UINT(' ', ' ', 'O', 'N'); } catch (...) { - LOG(debug, "exception changing logcontrol for %s", _myservice); + LOG(debug, "exception changing logcontrol for %s", _myservice.c_str()); } } @@ -51,8 +47,8 @@ Component::dontLogAtAll(LogLevel level) using ns_log::ControlFile; char lcfn[FILENAME_MAX]; - if (! ControlFile::makeName(_myservice, lcfn, FILENAME_MAX)) { - LOG(debug, "no logcontrol file for service '%s'", _myservice); + if (! ControlFile::makeName(_myservice.c_str(), lcfn, FILENAME_MAX)) { + LOG(debug, "no logcontrol file for service '%s'", _myservice.c_str()); return; } try { @@ -60,7 +56,7 @@ Component::dontLogAtAll(LogLevel level) unsigned int *lstring = foo.getLevels(_logctlname.c_str()); lstring[level] = CHARS_TO_UINT(' ', 'O', 'F', 'F'); } catch (...) { - LOG(debug, "exception changing logcontrol for %s", _myservice); + LOG(debug, "exception changing logcontrol for %s", _myservice.c_str()); } } @@ -70,8 +66,8 @@ Component::shouldLogAtAll(LogLevel level) const using ns_log::ControlFile; char lcfn[FILENAME_MAX]; - if (! ControlFile::makeName(_myservice, lcfn, FILENAME_MAX)) { - LOG(spam, "no logcontrol file for service '%s'", _myservice); + if (! ControlFile::makeName(_myservice.c_str(), lcfn, FILENAME_MAX)) { + LOG(spam, "no logcontrol file for service '%s'", _myservice.c_str()); return true; } try { @@ -79,12 +75,12 @@ Component::shouldLogAtAll(LogLevel level) const unsigned int *lstring = foo.getLevels(_logctlname.c_str()); return (lstring[level] == CHARS_TO_UINT(' ', ' ', 'O', 'N')); } catch (...) { - LOG(debug, "exception checking logcontrol for %s", _myservice); + LOG(debug, "exception checking logcontrol for %s", _myservice.c_str()); } return true; } -Service::Service(const char *name) +Service::Service(const std::string & name) : _myname(name), _components() { @@ -93,25 +89,26 @@ Service::Service(const char *name) Service::~Service() = default; Component * -Service::getComponent(const vespalib::string & comp) { +Service::getComponent(const std::string & comp) { auto found = _components.find(comp); if (found == _components.end()) { - _components[comp] = std::make_unique<Component>(_myname.c_str(), comp.c_str()); + _components[comp] = std::make_unique<Component>(_myname, comp); found = _components.find(comp); } return found->second.get(); } Service * -Services::getService(const vespalib::string & serv) { +Services::getService(const std::string & serv) { auto found = _services.find(serv); if (found == _services.end()) { - _services[serv] = std::make_unique<Service>(serv.c_str()); + _services[serv] = std::make_unique<Service>(serv); found = _services.find(serv); } return found->second.get(); } +Services::Services() = default; Services::~Services() = default; void @@ -122,7 +119,7 @@ Services::dumpState(int fildesc) for (const auto & serviceEntry : _services) { const Service & svc = *serviceEntry.second; const char * service = serviceEntry.first.c_str(); - for (const auto & entry : svc._components) { + for (const auto & entry : svc.components()) { const Component & cmp = *entry.second; const char * key = entry.first.c_str(); char buf[1024]; diff --git a/logd/src/logd/service.h b/logd/src/logd/service.h index 3dd613008b6..65f580ee54e 100644 --- a/logd/src/logd/service.h +++ b/logd/src/logd/service.h @@ -11,12 +11,12 @@ typedef ns_log::Logger::LogLevel LogLevel; class Component { - unsigned long _isforwarding; - double _lastseen; - int _lastpid; - const char *_myservice; - vespalib::string _myname; - vespalib::string _logctlname; + unsigned long _isforwarding; + double _lastseen; + int _lastpid; + std::string _myservice; + std::string _myname; + std::string _logctlname; static unsigned long defFwd; public: @@ -31,7 +31,7 @@ public: void doLogAtAll(LogLevel level); void dontLogAtAll(LogLevel level); bool shouldLogAtAll(LogLevel level) const; - Component(const char *servicename, const char *name); + Component(const std::string & servicename, const std::string & name); ~Component(); void remember(double t, int p) { _lastseen = t; _lastpid = p; } double lastSeen() const { return _lastseen; } @@ -40,23 +40,25 @@ public: class Service { -private: - vespalib::string _myname; - Service(const Service& other); - Service& operator= (const Service& other); public: - std::unordered_map<std::string, std::unique_ptr<Component>> _components; - Component *getComponent(const vespalib::string & comp); - Service(const char *name); + using ComponentMap = std::unordered_map<std::string, std::unique_ptr<Component>>; + Service(const Service& other) = delete; + Service& operator= (const Service& other) = delete; + Service(const std::string & name); ~Service(); + Component *getComponent(const std::string & comp); + const ComponentMap & components() const { return _components; } +private: + std::string _myname; + ComponentMap _components; }; class Services { public: std::unordered_map<std::string, std::unique_ptr<Service>> _services; - Service *getService(const vespalib::string & serv); - Services() : _services() {} + Service *getService(const std::string & serv); + Services(); ~Services(); void dumpState(int fildesc); }; |