Skip to content

Commit

Permalink
Move member function
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Sep 7, 2023
1 parent a89ebe0 commit eb9fb28
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 79 deletions.
61 changes: 59 additions & 2 deletions cpp/piscsi/piscsi_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,63 @@ void Piscsi::CreateInitialDevices(const optargs_type& optargs) const
cout << device_list << flush;
}

bool Piscsi::SetLogLevel(const string& log_level)
{
int id = -1;
int lun = -1;
string level = log_level;

if (size_t separator_pos = log_level.find(COMPONENT_SEPARATOR); separator_pos != string::npos) {
level = log_level.substr(0, separator_pos);

const string l = log_level.substr(separator_pos + 1);
separator_pos = l.find(COMPONENT_SEPARATOR);
if (separator_pos != string::npos) {
const string error = ProcessId(l, ScsiController::LUN_MAX, id, lun);
if (!error.empty()) {
spdlog::warn("Invalid device ID/LUN specifier '" + l + "'");
return false;
}
}
else if (!GetAsUnsignedInt(l, id)) {
spdlog::warn("Invalid device ID specifier '" + l + "'");
return false;
}
}

// For backwards compatibility with PiSCSI <= 23.04, where non-standard spdlog log levels were used
if (level == "warn") {
level = "warning";
}
else if (level == "err") {
level = "error";
}

const level::level_enum l = level::from_str(level);
// Compensate for spdlog using 'off' for unknown levels
if (to_string_view(l) != level) {
spdlog::warn("Invalid log level '" + level + "'");
return false;
}

set_level(l);
DeviceLogger::SetLogIdAndLun(id, lun);

if (id != -1) {
if (lun == -1) {
spdlog::info("Set log level for device ID " + to_string(id) + " to '" + level + "'");
}
else {
spdlog::info("Set log level for device ID " + to_string(id) + ", LUN " + to_string(lun) + " to '" + level + "'");
}
}
else {
spdlog::info("Set log level to '" + level + "'");
}

return true;
}

bool Piscsi::ExecuteCommand(const CommandContext& context, const PbCommand& command)
{
if (!access_token.empty() && access_token != GetParam(command, "token")) {
Expand All @@ -334,7 +391,7 @@ bool Piscsi::ExecuteCommand(const CommandContext& context, const PbCommand& comm
switch(command.operation()) {
case LOG_LEVEL: {
const string log_level = GetParam(command, "level");
if (const bool status = PiscsiExecutor::SetLogLevel(log_level); !status) {
if (const bool status = SetLogLevel(log_level); !status) {
context.ReturnLocalizedError(LocalizationKey::ERROR_LOG_LEVEL, log_level);
}
else {
Expand Down Expand Up @@ -485,7 +542,7 @@ int Piscsi::run(span<char *> args)
const auto logger = stdout_color_mt("piscsi stdout logger");

// current_log_level may have been updated by ParseArguments()
PiscsiExecutor::SetLogLevel(current_log_level);
SetLogLevel(current_log_level);

if (!InitBus()) {
cerr << "Error: Can't initialize bus" << endl;
Expand Down
2 changes: 2 additions & 0 deletions cpp/piscsi/piscsi_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Piscsi
// TODO Should not be static and should be moved to PiscsiService
static bool ExecuteCommand(const CommandContext&, const PbCommand&);

static bool SetLogLevel(const string&);

DeviceLogger device_logger;

// A static instance is needed because of the signal handler
Expand Down
57 changes: 0 additions & 57 deletions cpp/piscsi/piscsi_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,63 +166,6 @@ bool PiscsiExecutor::ProcessCmd(const CommandContext& context, const PbCommand&
return context.ReturnStatus();
}

bool PiscsiExecutor::SetLogLevel(const string& log_level)
{
int id = -1;
int lun = -1;
string level = log_level;

if (size_t separator_pos = log_level.find(COMPONENT_SEPARATOR); separator_pos != string::npos) {
level = log_level.substr(0, separator_pos);

const string l = log_level.substr(separator_pos + 1);
separator_pos = l.find(COMPONENT_SEPARATOR);
if (separator_pos != string::npos) {
const string error = ProcessId(l, ScsiController::LUN_MAX, id, lun);
if (!error.empty()) {
spdlog::warn("Invalid device ID/LUN specifier '" + l + "'");
return false;
}
}
else if (!GetAsUnsignedInt(l, id)) {
spdlog::warn("Invalid device ID specifier '" + l + "'");
return false;
}
}

// For backwards compatibility with PiSCSI <= 23.04, where non-standard spdlog log levels were used
if (level == "warn") {
level = "warning";
}
else if (level == "err") {
level = "error";
}

const level::level_enum l = level::from_str(level);
// Compensate for spdlog using 'off' for unknown levels
if (to_string_view(l) != level) {
spdlog::warn("Invalid log level '" + level + "'");
return false;
}

set_level(l);
DeviceLogger::SetLogIdAndLun(id, lun);

if (id != -1) {
if (lun == -1) {
spdlog::info("Set log level for device ID " + to_string(id) + " to '" + level + "'");
}
else {
spdlog::info("Set log level for device ID " + to_string(id) + ", LUN " + to_string(lun) + " to '" + level + "'");
}
}
else {
spdlog::info("Set log level to '" + level + "'");
}

return true;
}

bool PiscsiExecutor::Start(PrimaryDevice& device, bool dryRun) const
{
if (!dryRun) {
Expand Down
1 change: 0 additions & 1 deletion cpp/piscsi/piscsi_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class PiscsiExecutor

bool ProcessDeviceCmd(const CommandContext&, const PbDeviceDefinition&, const PbCommand&, bool);
bool ProcessCmd(const CommandContext&, const PbCommand&);
static bool SetLogLevel(const string&);
bool Start(PrimaryDevice&, bool) const;
bool Stop(PrimaryDevice&, bool) const;
bool Eject(PrimaryDevice&, bool) const;
Expand Down
19 changes: 0 additions & 19 deletions cpp/test/piscsi_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,6 @@ TEST_F(PiscsiExecutorTest, ProcessCmd)
EXPECT_FALSE(executor->ProcessCmd(context, command1));
}

TEST_F(PiscsiExecutorTest, SetLogLevel)
{
auto bus = make_shared<MockBus>();
auto controller_manager = make_shared<ControllerManager>(*bus);
MockAbstractController controller(controller_manager, 0);
PiscsiImage piscsi_image;
PiscsiExecutor executor(piscsi_image, *controller_manager);

EXPECT_TRUE(executor.SetLogLevel("trace"));
EXPECT_TRUE(executor.SetLogLevel("debug"));
EXPECT_TRUE(executor.SetLogLevel("info"));
EXPECT_TRUE(executor.SetLogLevel("warning"));
EXPECT_TRUE(executor.SetLogLevel("error"));
EXPECT_TRUE(executor.SetLogLevel("off"));
EXPECT_TRUE(executor.SetLogLevel("warn"));
EXPECT_TRUE(executor.SetLogLevel("err"));
EXPECT_FALSE(executor.SetLogLevel("xyz"));
}

TEST_F(PiscsiExecutorTest, Attach)
{
const int ID = 3;
Expand Down

0 comments on commit eb9fb28

Please sign in to comment.