co-locate set log level function

This commit is contained in:
Tony Kuker 2023-02-04 22:51:35 -06:00
parent 92c5ca40a2
commit e9b3949122
9 changed files with 115 additions and 84 deletions

View File

@ -101,7 +101,8 @@ SRC_PROTOBUF = \
SRC_SHARED = \
shared/piscsi_version.cpp \
shared/piscsi_util.cpp \
shared/shared_memory.cpp
shared/shared_memory.cpp \
shared/log.cpp
SRC_PISCSI_CORE = $(shell find ./piscsi -name '*.cpp')
SRC_PISCSI_CORE += $(shell find ./controllers -name '*.cpp')

View File

@ -64,9 +64,11 @@ bool ScsiDump::Banner(const vector<char*>& args) const
<< " FILE is the dump file path.\n"
<< " BUFFER_SIZE is the transfer buffer size in bytes, at least " << to_string(MINIMUM_BUFFER_SIZE)
<< " bytes. Default is 1 MiB.\n"
<< " -v Enable verbose logging.\n"
<< " -L Log Level. {trace|debug|info|warn|err|off}, default is 'info'.\n"
<< " -v Enable verbose logging. (deprecated - use -L instead)\n"
<< " -r Restore instead of dump.\n"
<< " -p Generate .properties file to be used with the PiSCSI web interface. Only valid for dump mode.\n"
<< " -q Executes Inquiry of the specified target device, then exits.\n"
<< flush;
return false;
@ -95,7 +97,7 @@ void ScsiDump::ParseArguments(const vector<char*>& args)
int buffer_size = DEFAULT_BUFFER_SIZE;
opterr = 0;
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "i:f:s:t:rvp")) != -1) {
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "i:f:s:t:L:qrvp")) != -1) {
switch (opt) {
case 'i':
if (!GetAsUnsignedInt(optarg, initiator_id) || initiator_id > 7) {
@ -122,7 +124,8 @@ void ScsiDump::ParseArguments(const vector<char*>& args)
} break;
case 'v':
set_level(level::trace);
LOGWARN("The -v option is deprecated. Please use -L debug instead.")
set_level(level::debug);
break;
case 'r':
@ -133,6 +136,17 @@ void ScsiDump::ParseArguments(const vector<char*>& args)
properties_file = true;
break;
case 'L':
if (!piscsi_log_level::set_log_level(optarg)) {
throw parser_exception("Invalid log level \"" + string(optarg) +
"\". Valid options are {trace|debug|info|warn|err|off}");
}
break;
case 'q':
inquiry_only = true;
break;
default:
break;
}
@ -142,7 +156,7 @@ void ScsiDump::ParseArguments(const vector<char*>& args)
throw parser_exception("Target ID and PiSCSI board ID must not be identical");
}
if (filename.empty()) {
if (!inquiry_only && filename.empty()) {
throw parser_exception("Missing filename");
}
@ -431,18 +445,27 @@ int ScsiDump::run(const vector<char*>& args)
try {
ParseArguments(args);
// #ifndef USE_SEL_EVENT_ENABLE
// cerr << "Error: No PiSCSI hardware support" << endl;
// return EXIT_FAILURE;
// #endif
inquiry_info_t inq_info = GetDeviceInfo();
return DumpRestore();
if (!inquiry_only) {
int dump_restore_result = DumpRestore(inq_info);
if (dump_restore_result != EXIT_SUCCESS) {
LOGWARN("Error occured while dumping/restoring the drive")
return dump_restore_result;
}
}
if (properties_file && !restore) {
GeneratePropertiesFile(filename, inq_info);
}
return EXIT_SUCCESS;
} catch (const parser_exception& e) {
cerr << "Error: " << e.what() << endl;
// CleanUp();
CleanUp();
// return EXIT_FAILURE;
return EXIT_FAILURE;
}
CleanUp();
@ -450,12 +473,10 @@ int ScsiDump::run(const vector<char*>& args)
return EXIT_SUCCESS;
}
int ScsiDump::DumpRestore()
int ScsiDump::DumpRestore(inquiry_info_t& inq_info)
{
LOGTRACE("%s", __PRETTY_FUNCTION__)
const auto inq_info = GetDeviceInfo();
fstream fs;
fs.open(filename, (restore ? ios::in : ios::out) | ios::binary);
@ -544,10 +565,6 @@ int ScsiDump::DumpRestore()
<< " KiB per second)\n";
cout << divider_str << "\n";
if (properties_file && !restore) {
GeneratePropertiesFile(filename, inq_info);
}
return EXIT_SUCCESS;
}

View File

@ -45,7 +45,7 @@ class ScsiDump
bool Banner(const vector<char*>&) const;
bool Init() const;
void ParseArguments(const vector<char*>&);
int DumpRestore();
int DumpRestore(inquiry_info_t&);
inquiry_info_t GetDeviceInfo();
void WaitPhase(phase_t) const;
void Selection() const;
@ -83,5 +83,7 @@ class ScsiDump
bool properties_file = false;
bool inquiry_only = false;
static inline const string divider_str = "----------------------------------------";
};

View File

@ -53,33 +53,6 @@ void ScsiLoop::Banner(const vector<char *> &args) const
}
}
bool ScsiLoop::SetLogLevel(const string &log_level)
{
if (log_level == "trace") {
set_level(level::trace);
} else if (log_level == "debug") {
set_level(level::debug);
} else if (log_level == "info") {
set_level(level::info);
} else if (log_level == "warn") {
set_level(level::warn);
} else if (log_level == "err") {
set_level(level::err);
} else if (log_level == "critical") {
set_level(level::critical);
} else if (log_level == "off") {
set_level(level::off);
} else {
return false;
}
current_log_level = log_level;
LOGINFO("Set log level to '%s'", current_log_level.c_str())
return true;
}
void ScsiLoop::TerminationHandler(int signum)
{
exit(signum);
@ -114,7 +87,7 @@ bool ScsiLoop::ParseArgument(const vector<char *> &args)
}
if (!log_level.empty()) {
SetLogLevel(log_level);
piscsi_log_level::set_log_level(log_level);
}
return true;

View File

@ -28,5 +28,4 @@ class ScsiLoop
void Banner(const vector<char *> &) const;
static void TerminationHandler(int signum);
bool ParseArgument(const vector<char *> &);
bool SetLogLevel(const string &);
};

View File

@ -48,34 +48,7 @@ void ScsiSim::Banner(const vector<char*>& args) const
}
}
bool ScsiSim::SetLogLevel(const string& log_level)
{
if (log_level == "trace") {
set_level(level::trace);
enable_debug = true;
} else if (log_level == "debug") {
set_level(level::debug);
enable_debug = true;
} else if (log_level == "info") {
set_level(level::info);
} else if (log_level == "warn") {
set_level(level::warn);
} else if (log_level == "err") {
set_level(level::err);
} else if (log_level == "critical") {
set_level(level::critical);
} else if (log_level == "off") {
set_level(level::off);
} else {
return false;
}
current_log_level = log_level;
LOGINFO("Set log level to '%s'", current_log_level.c_str())
return true;
}
void ScsiSim::TerminationHandler(int signum)
{
@ -116,7 +89,7 @@ bool ScsiSim::ParseArgument(const vector<char*>& args)
}
if (!log_level.empty()) {
SetLogLevel(log_level);
piscsi_log_level::set_log_level(log_level);
}
return true;

View File

@ -29,7 +29,6 @@ class ScsiSim
void Banner(const vector<char*>&) const;
static void TerminationHandler(int signum);
bool ParseArgument(const vector<char*>&);
bool SetLogLevel(const string&);
int InitSharedMemory();
void TeardownSharedMemory();

45
cpp/shared/log.cpp Normal file
View File

@ -0,0 +1,45 @@
//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI for Raspberry Pi
//
// Powered by XM6 TypeG Technology.
// Copyright (C) 2016-2020 GIMONS
// Copyright (C) 2020-2023 akuker
//
//---------------------------------------------------------------------------
#include "log.h"
#include "spdlog/spdlog.h"
using namespace spdlog;
level::level_enum piscsi_log_level::current_log_level = level::info;
std::array<string_view_t, (int)level::level_enum::n_levels> piscsi_log_level::spdlog_names = SPDLOG_LEVEL_NAMES;
bool piscsi_log_level::set_log_level(const string& log_level)
{
if (log_level == "trace") {
current_log_level = level::trace;
} else if (log_level == "debug") {
current_log_level = level::debug;
} else if (log_level == "info") {
current_log_level = level::info;
} else if (log_level == "warn") {
current_log_level = level::warn;
} else if (log_level == "err") {
current_log_level = level::err;
} else if (log_level == "critical") {
current_log_level = level::critical;
} else if (log_level == "off") {
current_log_level = level::off;
} else {
return false;
}
set_level(current_log_level);
LOGINFO("Set log level to '%s'", get_log_level_str().data())
return true;
}

View File

@ -12,18 +12,40 @@
#pragma once
#include "spdlog/spdlog.h"
#include <array>
#include <string>
using namespace std;
static const int LOGBUF_SIZE = 512;
#define SPDLOGWRAPPER(loglevel, ...) \
{ \
char logbuf[LOGBUF_SIZE]; \
snprintf(logbuf, sizeof(logbuf), __VA_ARGS__); \
spdlog::log(loglevel, logbuf); \
};
#define SPDLOGWRAPPER(loglevel, ...) \
{ \
char logbuf[LOGBUF_SIZE]; \
snprintf(logbuf, sizeof(logbuf), __VA_ARGS__); \
spdlog::log(loglevel, logbuf); \
};
#define LOGTRACE(...) SPDLOGWRAPPER(spdlog::level::trace, __VA_ARGS__)
#define LOGDEBUG(...) SPDLOGWRAPPER(spdlog::level::debug, __VA_ARGS__)
#define LOGINFO(...) SPDLOGWRAPPER(spdlog::level::info, __VA_ARGS__)
#define LOGWARN(...) SPDLOGWRAPPER(spdlog::level::warn, __VA_ARGS__)
#define LOGERROR(...) SPDLOGWRAPPER(spdlog::level::err, __VA_ARGS__)
class piscsi_log_level
{
public:
static bool set_log_level(const string&);
static spdlog::level::level_enum get_log_level()
{
return current_log_level;
};
static const spdlog::string_view_t& get_log_level_str()
{
return spdlog_names[current_log_level];
};
private:
static spdlog::level::level_enum current_log_level;
static std::array<spdlog::string_view_t, (int)spdlog::level::level_enum::n_levels> spdlog_names;
};