Skip to content

Commit

Permalink
Update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Sep 7, 2023
1 parent f3a64b5 commit 3dac56d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
30 changes: 15 additions & 15 deletions cpp/devices/ctapdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ static bool br_setif(int br_socket_fd, const char* bridgename, const char* ifnam
ifreq ifr;
ifr.ifr_ifindex = if_nametoindex(ifname);
if (ifr.ifr_ifindex == 0) {
Strerrno("Can't if_nametoindex " + string(ifname));
LogErrno("Can't if_nametoindex " + string(ifname));
return false;
}
strncpy(ifr.ifr_name, bridgename, IFNAMSIZ - 1);
if (ioctl(br_socket_fd, add ? SIOCBRADDIF : SIOCBRDELIF, &ifr) < 0) {
Strerrno("Can't ioctl " + string(add ? "SIOCBRADDIF" : "SIOCBRDELIF"));
LogErrno("Can't ioctl " + string(add ? "SIOCBRADDIF" : "SIOCBRDELIF"));
return false;
}
return true;
Expand All @@ -60,7 +60,7 @@ CTapDriver::~CTapDriver()
{
if (m_hTAP != -1) {
if (int br_socket_fd; (br_socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
Strerrno("Can't open bridge socket");
LogErrno("Can't open bridge socket");
} else {
spdlog::trace("brctl delif " + BRIDGE_NAME + " piscsi0");
if (!br_setif(br_socket_fd, BRIDGE_NAME.c_str(), "piscsi0", false)) {
Expand All @@ -83,7 +83,7 @@ static bool ip_link(int fd, const char* ifname, bool up) {
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1); // Need to save room for null terminator
int err = ioctl(fd, SIOCGIFFLAGS, &ifr);
if (err) {
Strerrno("Can't ioctl SIOCGIFFLAGS");
LogErrno("Can't ioctl SIOCGIFFLAGS");
return false;
}
ifr.ifr_flags &= ~IFF_UP;
Expand All @@ -92,7 +92,7 @@ static bool ip_link(int fd, const char* ifname, bool up) {
}
err = ioctl(fd, SIOCSIFFLAGS, &ifr);
if (err) {
Strerrno("Can't ioctl SIOCSIFFLAGS");
LogErrno("Can't ioctl SIOCSIFFLAGS");
return false;
}
return true;
Expand Down Expand Up @@ -133,7 +133,7 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)
spdlog::trace("Opening tap device");
// TAP device initilization
if ((m_hTAP = open("/dev/net/tun", O_RDWR)) < 0) {
Strerrno("Can't open tun");
LogErrno("Can't open tun");
return false;
}

Expand All @@ -149,7 +149,7 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)

int ret = ioctl(m_hTAP, TUNSETIFF, (void *)&ifr);
if (ret < 0) {
Strerrno("Can't ioctl TUNSETIFF");
LogErrno("Can't ioctl TUNSETIFF");

close(m_hTAP);
return false;
Expand All @@ -159,15 +159,15 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)

const int ip_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (ip_fd < 0) {
Strerrno("Can't open ip socket");
LogErrno("Can't open ip socket");

close(m_hTAP);
return false;
}

const int br_socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (br_socket_fd < 0) {
Strerrno("Can't open bridge socket");
LogErrno("Can't open bridge socket");

close(m_hTAP);
close(ip_fd);
Expand Down Expand Up @@ -213,7 +213,7 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)
spdlog::trace("brctl addbr " + BRIDGE_NAME);

if (ioctl(br_socket_fd, SIOCBRADDBR, BRIDGE_NAME.c_str()) < 0) {
Strerrno("Can't ioctl SIOCBRADDBR");
LogErrno("Can't ioctl SIOCBRADDBR");

return cleanUp();
}
Expand Down Expand Up @@ -246,7 +246,7 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)
spdlog::trace("brctl addbr " + BRIDGE_NAME);

if (ioctl(br_socket_fd, SIOCBRADDBR, BRIDGE_NAME.c_str()) < 0) {
Strerrno("Can't ioctl SIOCBRADDBR");
LogErrno("Can't ioctl SIOCBRADDBR");

return cleanUp();
}
Expand All @@ -256,7 +256,7 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)
strncpy(ifr_a.ifr_name, BRIDGE_NAME.c_str(), IFNAMSIZ);
if (auto addr = (sockaddr_in*)&ifr_a.ifr_addr;
inet_pton(AF_INET, address.c_str(), &addr->sin_addr) != 1) {
Strerrno("Can't convert '" + address + "' into a network address");
LogErrno("Can't convert '" + address + "' into a network address");

return cleanUp();
}
Expand All @@ -266,15 +266,15 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)
strncpy(ifr_n.ifr_name, BRIDGE_NAME.c_str(), IFNAMSIZ);
if (auto mask = (sockaddr_in*)&ifr_n.ifr_addr;
inet_pton(AF_INET, netmask.c_str(), &mask->sin_addr) != 1) {
Strerrno("Can't convert '" + netmask + "' into a netmask");
LogErrno("Can't convert '" + netmask + "' into a netmask");

return cleanUp();
}

spdlog::trace("ip address add " + inet + " dev " + BRIDGE_NAME);

if (ioctl(ip_fd, SIOCSIFADDR, &ifr_a) < 0 || ioctl(ip_fd, SIOCSIFNETMASK, &ifr_n) < 0) {
Strerrno("Can't ioctl SIOCSIFADDR or SIOCSIFNETMASK");
LogErrno("Can't ioctl SIOCSIFADDR or SIOCSIFNETMASK");

return cleanUp();
}
Expand Down Expand Up @@ -308,7 +308,7 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)

ifr.ifr_addr.sa_family = AF_INET;
if (ioctl(m_hTAP, SIOCGIFHWADDR, &ifr) < 0) {
Strerrno("Can't ioctl SIOCGIFHWADDR");
LogErrno("Can't ioctl SIOCGIFHWADDR");

return cleanUp();
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/piscsi/piscsi_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ bool PiscsiExecutor::ShutDown(const CommandContext& context, const string& mode)
DetachAll();

if (system("init 0") == -1) {
Strerrno("System shutdown failed");
LogErrno("System shutdown failed");
}
}
else if (mode == "reboot") {
Expand All @@ -459,7 +459,7 @@ bool PiscsiExecutor::ShutDown(const CommandContext& context, const string& mode)
DetachAll();

if (system("init 6") == -1) {
Strerrno("System reboot failed");
LogErrno("System reboot failed");
}
}
else {
Expand Down
5 changes: 3 additions & 2 deletions cpp/shared/piscsi_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "piscsi_version.h"
#include "piscsi_util.h"
#include <spdlog/spdlog.h>
#include <cassert>
#include <cstring>
#include <sstream>
Expand Down Expand Up @@ -91,9 +92,9 @@ string piscsi_util::GetExtensionLowerCase(string_view filename)
return ext.empty() ? "" : ext.substr(1);
}

string piscsi_util::Strerrno(const string& msg)
void piscsi_util::LogErrno(const string& msg)
{
return msg + ": " + string(strerror(errno));
spdlog::error(msg + ": " + string(strerror(errno)));
}

// Pin the thread to a specific CPU
Expand Down
2 changes: 1 addition & 1 deletion cpp/shared/piscsi_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace piscsi_util

string GetExtensionLowerCase(string_view);

string Strerrno(const string&);
void LogErrno(const string&);

void FixCpu(int);
}

0 comments on commit 3dac56d

Please sign in to comment.