speciesgen/src/error.cpp

47 lines
1.6 KiB
C++

/*
speciesgen
Copyright (C) 2022-2023 prisixia
This file is part of speciesgen.
speciesgen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
speciesgen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with speciesgen. If not, see <https://www.gnu.org/licenses/>.
*/
#include "error.h"
namespace SpeciesGen {
//----------------------------------------------------------------------
// ErrorHandler
//----------------------------------------------------------------------
ErrorHandler::ErrorHandler(std::string handlerName)
: m_handlerName(std::move(handlerName)) {}
std::string ErrorHandler::GetError(const size_t index) {
const std::string handlerName = m_handlerName;
const std::string &error = m_errorLog.at(index);
const char *format = "[ERROR/%s]: %s";
const int bufferSize =
std::snprintf(nullptr, 0, format, handlerName.c_str(), error.c_str()) +
1;
std::unique_ptr<char[]> buffer(new char[bufferSize]);
std::snprintf(buffer.get(), bufferSize, format, handlerName.c_str(),
error.c_str());
return std::string(buffer.get(), buffer.get() + bufferSize - 1);
}
void ErrorHandler::ClearErrors() { m_errorLog.clear(); }
} // namespace SpeciesGen