Tools: Fix various clang-tidy warnings

This commit is contained in:
Dennis Klein 2019-07-05 19:10:16 +02:00 committed by Dennis Klein
parent 4351b98d85
commit 69268eecfb
3 changed files with 35 additions and 38 deletions

View File

@ -12,24 +12,21 @@
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
#endif #endif
#include <algorithm>
#include <array>
#include <boost/algorithm/string.hpp> // trim
#include <boost/asio.hpp>
#include <cstdio>
#include <exception>
#include <fairlogger/Logger.h> #include <fairlogger/Logger.h>
#include <ifaddrs.h>
#include <iostream>
#include <map>
#include <netdb.h>
#include <stdexcept>
#include <string>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <boost/algorithm/string.hpp> // trim
#include <boost/asio.hpp>
#include <map>
#include <string>
#include <iostream>
#include <array>
#include <exception>
#include <stdexcept>
#include <algorithm>
using namespace std; using namespace std;
@ -44,9 +41,10 @@ namespace tools
map<string, string> getHostIPs() map<string, string> getHostIPs()
{ {
map<string, string> addressMap; map<string, string> addressMap;
struct ifaddrs *ifaddr, *ifa; ifaddrs* ifaddr;
ifaddrs* ifa;
int s; int s;
char host[NI_MAXHOST]; array<char, NI_MAXHOST> host{};
if (getifaddrs(&ifaddr) == -1) { if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs"); perror("getifaddrs");
@ -59,13 +57,13 @@ map<string, string> getHostIPs()
} }
if (ifa->ifa_addr->sa_family == AF_INET) { if (ifa->ifa_addr->sa_family == AF_INET) {
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); s = getnameinfo(ifa->ifa_addr, sizeof(sockaddr_in), host.data(), NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
if (s != 0) { if (s != 0) {
cout << "getnameinfo() failed: " << gai_strerror(s) << endl; cout << "getnameinfo() failed: " << gai_strerror(s) << endl;
throw runtime_error("getnameinfo() failed"); throw runtime_error("getnameinfo() failed");
} }
addressMap.insert(pair<string, string>(ifa->ifa_name, host)); addressMap.insert({ifa->ifa_name, host.data()});
} }
} }
@ -79,12 +77,11 @@ string getInterfaceIP(const string& interface)
{ {
try { try {
auto IPs = getHostIPs(); auto IPs = getHostIPs();
if (IPs.count(interface)) { if (IPs.count(interface) > 0) {
return IPs[interface]; return IPs[interface];
} else {
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
return "";
} }
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
return "";
} catch (runtime_error& re) { } catch (runtime_error& re) {
cout << "could not get interface IP: " << re.what(); cout << "could not get interface IP: " << re.what();
return ""; return "";
@ -94,7 +91,8 @@ string getInterfaceIP(const string& interface)
// get name of the default route interface // get name of the default route interface
string getDefaultRouteNetworkInterface() string getDefaultRouteNetworkInterface()
{ {
array<char, 128> buffer; const int BUFSIZE(128);
array<char, BUFSIZE> buffer{};
string interfaceName; string interfaceName;
#ifdef __APPLE__ // MacOS #ifdef __APPLE__ // MacOS
@ -108,15 +106,15 @@ string getDefaultRouteNetworkInterface()
return ""; return "";
} }
while (!feof(file.get())) { while (feof(file.get()) == 0) {
if (fgets(buffer.data(), 128, file.get()) != nullptr) { if (fgets(buffer.data(), BUFSIZE, file.get()) != nullptr) {
interfaceName += buffer.data(); interfaceName += buffer.data();
} }
} }
boost::algorithm::trim(interfaceName); boost::algorithm::trim(interfaceName);
if (interfaceName == "") { if (interfaceName.empty()) {
LOG(error) << "Could not detect default route network interface name"; LOG(error) << "Could not detect default route network interface name";
} else { } else {
LOG(debug) << "Detected network interface name for the default route: " << interfaceName; LOG(debug) << "Detected network interface name for the default route: " << interfaceName;

View File

@ -9,16 +9,15 @@
#include <fairmq/tools/Process.h> #include <fairmq/tools/Process.h>
#include <fairmq/tools/Strings.h> #include <fairmq/tools/Strings.h>
#include <boost/process.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/process.hpp>
#include <signal.h> // kill, signals #include <csignal> // kill, signals
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <thread>
#include <stdexcept> #include <stdexcept>
#include <thread>
#include <utility>
using namespace std; using namespace std;
namespace bp = boost::process; namespace bp = boost::process;
@ -28,9 +27,9 @@ namespace bs = boost::system;
class LinePrinter class LinePrinter
{ {
public: public:
LinePrinter(stringstream& out, const string& prefix) LinePrinter(stringstream& out, string prefix)
: fOut(out) : fOut(out)
, fPrefix(prefix) , fPrefix(move(prefix))
{} {}
// prints line with prefix on both cout (thread-safe) and output stream // prints line with prefix on both cout (thread-safe) and output stream
@ -82,8 +81,8 @@ execute_result execute(const string& cmd, const string& prefix, const string& in
bp::async_pipe errorPipe(ios); bp::async_pipe errorPipe(ios);
const string delimiter = "\n"; const string delimiter = "\n";
ba::deadline_timer inputTimer(ios, boost::posix_time::milliseconds(100)); ba::deadline_timer inputTimer(ios, boost::posix_time::milliseconds(100)); // NOLINT
ba::deadline_timer signalTimer(ios, boost::posix_time::milliseconds(100)); ba::deadline_timer signalTimer(ios, boost::posix_time::milliseconds(100)); // NOLINT
// child process // child process
bp::child c(cmd, bp::std_out > outputPipe, bp::std_err > errorPipe, bp::std_in < inputPipe); bp::child c(cmd, bp::std_out > outputPipe, bp::std_err > errorPipe, bp::std_in < inputPipe);
@ -91,7 +90,7 @@ execute_result execute(const string& cmd, const string& prefix, const string& in
p.Print(ToString("fair::mq::tools::execute: pid: ", pid)); p.Print(ToString("fair::mq::tools::execute: pid: ", pid));
// handle std_in with a delay // handle std_in with a delay
if (input != "") { if (!input.empty()) {
inputTimer.async_wait([&](const bs::error_code& ec1) { inputTimer.async_wait([&](const bs::error_code& ec1) {
if (!ec1) { if (!ec1) {
ba::async_write(inputPipe, inputBuffer, [&](const bs::error_code& ec2, size_t /* n */) { ba::async_write(inputPipe, inputBuffer, [&](const bs::error_code& ec2, size_t /* n */) {

View File

@ -12,10 +12,10 @@
// otherwise on some systems we'd get boost::uuids::entropy_error // otherwise on some systems we'd get boost::uuids::entropy_error
#define BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX #define BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX
#include <boost/functional/hash.hpp>
#include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp> #include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp> #include <boost/uuid/uuid_io.hpp>
#include <boost/functional/hash.hpp>
using namespace std; using namespace std;