mirror of
https://github.com/akuker/RASCSI.git
synced 2026-04-20 11:17:58 +00:00
Issues 1179 and 1182 (#1232)
* Update logging * Remove duplicate code * Update unit tests * Clean up includes * Merge ProtobufSerializer into protobuf_util namespace * Precompile regex * Add const * Add Split() convenience method, update log level/ID parsing * Move log.h to legacy folder * Elimininate gotos * Fixes for gcc 13 * Update compiler flags * Update default folder handling * Use references instead of pointers * Move code for better encapsulation * Move code * Remove unused method argument * Move device logger * Remove redundant to_string * Rename for consistency * Update handling of protobuf pointers * Simplify protobuf usage * Memory handling update * Add hasher
This commit is contained in:
@@ -3,24 +3,50 @@
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2021-2022 Uwe Seimet
|
||||
// Copyright (C) 2021-2023 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "shared/log.h"
|
||||
#include "generated/piscsi_interface.pb.h"
|
||||
#include "shared/piscsi_exceptions.h"
|
||||
#include "shared/protobuf_util.h"
|
||||
#include "command_context.h"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace piscsi_interface;
|
||||
using namespace protobuf_util;
|
||||
|
||||
void CommandContext::Cleanup()
|
||||
bool CommandContext::ReadCommand()
|
||||
{
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
// Read magic string
|
||||
array<byte, 6> magic;
|
||||
if (const size_t bytes_read = ReadBytes(fd, magic); bytes_read) {
|
||||
if (bytes_read != magic.size() || memcmp(magic.data(), "RASCSI", magic.size())) {
|
||||
throw io_exception("Invalid magic");
|
||||
}
|
||||
|
||||
// Fetch the command
|
||||
DeserializeMessage(fd, command);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CommandContext::WriteResult(const PbResult& result) const
|
||||
{
|
||||
// The descriptor is -1 when devices are not attached via the remote interface but by the piscsi tool
|
||||
if (fd != -1) {
|
||||
SerializeMessage(fd, result);
|
||||
}
|
||||
}
|
||||
|
||||
void CommandContext::WriteSuccessResult(PbResult& result) const
|
||||
{
|
||||
result.set_status(true);
|
||||
WriteResult(result);
|
||||
}
|
||||
|
||||
bool CommandContext::ReturnLocalizedError(LocalizationKey key, const string& arg1, const string& arg2,
|
||||
@@ -33,7 +59,7 @@ bool CommandContext::ReturnLocalizedError(LocalizationKey key, PbErrorCode error
|
||||
const string& arg2, const string& arg3) const
|
||||
{
|
||||
// For the logfile always use English
|
||||
LOGERROR("%s", localizer.Localize(key, "en", arg1, arg2, arg3).c_str())
|
||||
spdlog::error(localizer.Localize(key, "en", arg1, arg2, arg3));
|
||||
|
||||
return ReturnStatus(false, localizer.Localize(key, locale, arg1, arg2, arg3), error_code, false);
|
||||
}
|
||||
@@ -42,17 +68,12 @@ bool CommandContext::ReturnStatus(bool status, const string& msg, PbErrorCode er
|
||||
{
|
||||
// Do not log twice if logging has already been done in the localized error handling above
|
||||
if (log && !status && !msg.empty()) {
|
||||
LOGERROR("%s", msg.c_str())
|
||||
spdlog::error(msg);
|
||||
}
|
||||
|
||||
if (fd == -1) {
|
||||
if (!msg.empty()) {
|
||||
if (status) {
|
||||
cerr << "Error: " << msg << endl;
|
||||
}
|
||||
else {
|
||||
cout << msg << endl;
|
||||
}
|
||||
cerr << "Error: " << msg << endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -60,8 +81,18 @@ bool CommandContext::ReturnStatus(bool status, const string& msg, PbErrorCode er
|
||||
result.set_status(status);
|
||||
result.set_error_code(error_code);
|
||||
result.set_msg(msg);
|
||||
serializer.SerializeMessage(fd, result);
|
||||
WriteResult(result);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
bool CommandContext::ReturnSuccessStatus() const
|
||||
{
|
||||
return ReturnStatus(true, "", PbErrorCode::NO_ERROR_CODE, true);
|
||||
}
|
||||
|
||||
bool CommandContext::ReturnErrorStatus(const string& msg) const
|
||||
{
|
||||
return ReturnStatus(false, msg, PbErrorCode::NO_ERROR_CODE, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user