2022-10-06 14:15:19 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-06 14:15:19 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021-2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/log.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "generated/piscsi_interface.pb.h"
|
2022-10-06 14:15:19 +00:00
|
|
|
#include "command_context.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
2022-10-06 14:15:19 +00:00
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
void CommandContext::Cleanup()
|
2022-10-06 14:15:19 +00:00
|
|
|
{
|
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
2022-10-08 17:26:04 +00:00
|
|
|
fd = -1;
|
2022-10-06 14:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandContext::ReturnLocalizedError(LocalizationKey key, const string& arg1, const string& arg2,
|
|
|
|
const string& arg3) const
|
|
|
|
{
|
|
|
|
return ReturnLocalizedError(key, NO_ERROR_CODE, arg1, arg2, arg3);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandContext::ReturnLocalizedError(LocalizationKey key, PbErrorCode error_code, const string& arg1,
|
|
|
|
const string& arg2, const string& arg3) const
|
|
|
|
{
|
|
|
|
// For the logfile always use English
|
|
|
|
LOGERROR("%s", localizer.Localize(key, "en", arg1, arg2, arg3).c_str())
|
|
|
|
|
|
|
|
return ReturnStatus(false, localizer.Localize(key, locale, arg1, arg2, arg3), error_code, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandContext::ReturnStatus(bool status, const string& msg, PbErrorCode error_code, bool log) const
|
|
|
|
{
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
if (!msg.empty()) {
|
|
|
|
if (status) {
|
|
|
|
cerr << "Error: " << msg << endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cout << msg << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PbResult result;
|
|
|
|
result.set_status(status);
|
|
|
|
result.set_error_code(error_code);
|
|
|
|
result.set_msg(msg);
|
|
|
|
serializer.SerializeMessage(fd, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|