| 1 |
#include <pbcopper/utility/Alarm.h> |
| 2 |
|
| 3 |
#include <pbcopper/json/JSON.h> |
| 4 |
|
| 5 |
#include <boost/uuid/uuid.hpp> |
| 6 |
#include <boost/uuid/uuid_generators.hpp> |
| 7 |
#include <boost/uuid/uuid_io.hpp> |
| 8 |
|
| 9 |
#include <fstream> |
| 10 |
#include <ostream> |
| 11 |
#include <utility> |
| 12 |
|
| 13 |
namespace PacBio { |
| 14 |
namespace Utility { |
| 15 |
namespace { |
| 16 |
|
| 17 |
PacBio::JSON::Json ToJson(const Alarm& a) |
| 18 |
{ |
| 19 |
auto result = PacBio::JSON::Json::object(); |
| 20 |
result["name"] = a.Name(); |
| 21 |
result["message"] = a.Message(); |
| 22 |
result["id"] = a.Id(); |
| 23 |
result["exception"] = a.Exception(); |
| 24 |
result["severity"] = a.Severity(); |
| 25 |
result["owner"] = a.Owner(); |
| 26 |
return result; |
| 27 |
} |
| 28 |
|
| 29 |
} // namespace |
| 30 |
|
| 31 |
Alarm::Alarm(std::string name, std::string message) |
| 32 |
: Alarm{std::move(name), std::move(message), "ERROR", "", ""} |
| 33 |
{} |
| 34 |
|
| 35 |
Alarm::Alarm(std::string name, std::string message, std::string severity, std::string info, |
| 36 |
std::string exception) |
| 37 |
: message_{std::move(message)} |
| 38 |
, name_{std::move(name)} |
| 39 |
, exception_{std::move(exception)} |
| 40 |
, info_{std::move(info)} |
| 41 |
, severity_{std::move(severity)} |
| 42 |
{ |
| 43 |
static boost::uuids::random_generator gen; |
| 44 |
const boost::uuids::uuid uuid = gen(); |
| 45 |
uuid_ = boost::uuids::to_string(uuid); |
| 46 |
} |
| 47 |
|
| 48 |
Alarm::Alarm(const AlarmException& e) |
| 49 |
: Alarm{e.Name(), e.Message(), e.Severity(), e.Info(), e.Exception()} |
| 50 |
{} |
| 51 |
|
| 52 |
const std::string& Alarm::Exception() const { return exception_; } |
| 53 |
|
| 54 |
Alarm& Alarm::Exception(std::string exception) |
| 55 |
{ |
| 56 |
exception_ = std::move(exception); |
| 57 |
return *this; |
| 58 |
} |
| 59 |
|
| 60 |
const std::string& Alarm::Id() const { return uuid_; } |
| 61 |
|
| 62 |
Alarm& Alarm::Id(std::string uuid) |
| 63 |
{ |
| 64 |
uuid_ = std::move(uuid); |
| 65 |
return *this; |
| 66 |
} |
| 67 |
|
| 68 |
const std::string& Alarm::Info() const { return info_; } |
| 69 |
|
| 70 |
Alarm& Alarm::Info(std::string info) |
| 71 |
{ |
| 72 |
info_ = std::move(info); |
| 73 |
return *this; |
| 74 |
} |
| 75 |
|
| 76 |
const std::string& Alarm::Message() const { return message_; } |
| 77 |
|
| 78 |
Alarm& Alarm::Message(std::string message) |
| 79 |
{ |
| 80 |
message_ = std::move(message); |
| 81 |
return *this; |
| 82 |
} |
| 83 |
|
| 84 |
const std::string& Alarm::Name() const { return name_; } |
| 85 |
|
| 86 |
Alarm& Alarm::Name(std::string name) |
| 87 |
{ |
| 88 |
name_ = std::move(name); |
| 89 |
return *this; |
| 90 |
} |
| 91 |
|
| 92 |
const std::string& Alarm::Owner() const { return owner_; } |
| 93 |
|
| 94 |
Alarm& Alarm::Owner(std::string owner) |
| 95 |
{ |
| 96 |
owner_ = std::move(owner); |
| 97 |
return *this; |
| 98 |
} |
| 99 |
|
| 100 |
const std::string& Alarm::Severity() const { return severity_; } |
| 101 |
|
| 102 |
Alarm& Alarm::Severity(std::string severity) |
| 103 |
{ |
| 104 |
severity_ = std::move(severity); |
| 105 |
return *this; |
| 106 |
} |
| 107 |
|
| 108 |
void Alarm::Print(const std::filesystem::path& filename) const |
| 109 |
{ |
| 110 |
std::ofstream f{filename}; |
| 111 |
Print(f); |
| 112 |
} |
| 113 |
|
| 114 |
void Alarm::Print(std::ostream& out) const |
| 115 |
{ |
| 116 |
const auto result = ToJson(*this); |
| 117 |
out << result.dump(4); |
| 118 |
} |
| 119 |
|
| 120 |
void Alarm::WriteAlarms(const std::filesystem::path& filename, const std::vector<Alarm>& alarms, |
| 121 |
const std::string& applicationName) |
| 122 |
{ |
| 123 |
if (filename.empty()) { |
| 124 |
throw std::runtime_error{"[pbcopper] alarm ERROR: cannot write to empty alarm filename"}; |
| 125 |
} |
| 126 |
|
| 127 |
std::ofstream f{filename}; |
| 128 |
WriteAlarms(f, alarms, applicationName); |
| 129 |
} |
| 130 |
|
| 131 |
void Alarm::WriteAlarms(std::ostream& out, const std::vector<Alarm>& alarms, |
| 132 |
const std::string& applicationName) |
| 133 |
{ |
| 134 |
auto jsonAlarms = PacBio::JSON::Json::array(); |
| 135 |
for (const auto& alarm : alarms) { |
| 136 |
auto jsonAlarm = ToJson(alarm); |
| 137 |
if (alarm.Owner().empty() && !applicationName.empty()) { |
| 138 |
jsonAlarm["owner"] = applicationName; |
| 139 |
} |
| 140 |
jsonAlarms.emplace_back(std::move(jsonAlarm)); |
| 141 |
} |
| 142 |
out << jsonAlarms.dump(4); |
| 143 |
} |
| 144 |
|
| 145 |
std::ostream& operator<<(std::ostream& out, const Alarm& alarm) |
| 146 |
{ |
| 147 |
alarm.Print(out); |
| 148 |
return out; |
| 149 |
} |
| 150 |
|
| 151 |
AlarmException::AlarmException(std::string sourceFilename, std::string functionName, |
| 152 |
int32_t lineNumber, std::string name, std::string message, |
| 153 |
std::string severity, std::string info, |
| 154 |
std::string exception) noexcept |
| 155 |
: sourceFilename_{std::move(sourceFilename)} |
| 156 |
, functionName_{std::move(functionName)} |
| 157 |
, lineNumber_{lineNumber} |
| 158 |
, name_{std::move(name)} |
| 159 |
, message_{std::move(message)} |
| 160 |
, severity_{std::move(severity)} |
| 161 |
, info_{std::move(info)} |
| 162 |
, exception_{std::move(exception)} |
| 163 |
{} |
| 164 |
|
| 165 |
AlarmException::AlarmException(std::string sourceFilename, std::string functionName, |
| 166 |
int32_t lineNumber, AlarmType type, std::string message, |
| 167 |
std::string severity, std::string info, |
| 168 |
std::string exception) noexcept |
| 169 |
: AlarmException{std::move(sourceFilename), std::move(functionName), lineNumber, |
| 170 |
NameForAlarmType(type), std::move(message), std::move(severity), |
| 171 |
std::move(info), std::move(exception)} |
| 172 |
{} |
| 173 |
|
| 174 |
// Debugging Info |
| 175 |
const char* AlarmException::SourceFilename() const noexcept { return sourceFilename_.c_str(); } |
| 176 |
|
| 177 |
const char* AlarmException::FunctionName() const noexcept { return functionName_.c_str(); } |
| 178 |
|
| 179 |
int32_t AlarmException::LineNumber() const noexcept { return lineNumber_; } |
| 180 |
|
| 181 |
// Used by Alarms API |
| 182 |
const char* AlarmException::Name() const noexcept { return name_.c_str(); } |
| 183 |
|
| 184 |
const char* AlarmException::Message() const noexcept { return message_.c_str(); } |
| 185 |
|
| 186 |
const char* AlarmException::Severity() const noexcept { return severity_.c_str(); } |
| 187 |
|
| 188 |
const char* AlarmException::Info() const noexcept { return info_.c_str(); } |
| 189 |
|
| 190 |
const char* AlarmException::Exception() const noexcept { return exception_.c_str(); } |
| 191 |
|
| 192 |
std::string NameForAlarmType(const AlarmType type) |
| 193 |
{ |
| 194 |
const std::map<AlarmType, std::string> lookup{ |
| 195 |
{AlarmType::BARCODE, "BarcodeError"}, |
| 196 |
{AlarmType::CHEMISTRY_MODEL, "ChemistryModelError"}, |
| 197 |
{AlarmType::CLI, "CommandLineArgsError"}, |
| 198 |
{AlarmType::FILE_FORMAT, "FileFormatError"}, |
| 199 |
{AlarmType::FILE_NOT_FOUND, "FileNotFoundError"}, |
| 200 |
}; |
| 201 |
|
| 202 |
const auto found = lookup.find(type); |
| 203 |
if (found == lookup.cend()) { |
| 204 |
return std::string{}; |
| 205 |
} else { |
| 206 |
return found->second; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} // namespace Utility |
| 211 |
} // namespace PacBio |