* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef ERROR_MANAGER_H_
#define ERROR_MANAGER_H_
#include <cinttypes>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <mutex>
#include <cstring>
#include <cstdlib>
namespace error_message {
using char_t = char;
std::string TrimPath(const std::string &str);
#ifdef __GNUC__
int32_t FormatErrorMessage(char_t *str_dst, size_t dst_max,
const char_t *format, ...)__attribute__((format(printf, 3, 4)));
void ReportInnerError(const char_t *file_name, const char_t *func, uint32_t line, const std::string error_code,
const char_t *format, ...) __attribute__((format(printf, 5, 6)));
#else
int32_t FormatErrorMessage(char_t *str_dst, size_t dst_max, const char_t *format, ...);
void ReportInnerError(const char_t *file_name, const char_t *func, uint32_t line, const std::string error_code,
const char_t *format, ...);
#endif
}
constexpr size_t const LIMIT_PER_MESSAGE = 1024U;
#define REPORT_INPUT_ERROR(error_code, key, value) \
ErrorManager::GetInstance().ATCReportErrMessage(error_code, key, value)
#define REPORT_ENV_ERROR(error_code, key, value) \
ErrorManager::GetInstance().ATCReportErrMessage(error_code, key, value)
#define REPORT_INNER_ERROR(error_code, fmt, ...) \
error_message::ReportInnerError(__FILE__, &__FUNCTION__[0], __LINE__, (error_code), (fmt), ##__VA_ARGS__)
#define REPORT_CALL_ERROR REPORT_INNER_ERROR
namespace error_message {
extern const std::string kInitialize;
extern const std::string kModelCompile;
extern const std::string kModelLoad;
extern const std::string kModelExecute;
extern const std::string kFinalize;
extern const std::string kParser;
extern const std::string kOpsProtoInit;
extern const std::string kSystemInit;
extern const std::string kEngineInit;
extern const std::string kOpsKernelInit;
extern const std::string kOpsKernelBuilderInit;
extern const std::string kPrepareOptimize;
extern const std::string kOriginOptimize;
extern const std::string kSubGraphOptimize;
extern const std::string kMergeGraphOptimize;
extern const std::string kPreBuild;
extern const std::string kStreamAlloc;
extern const std::string kMemoryAlloc;
extern const std::string kTaskGenerate;
extern const std::string kOther;
struct Context {
uint64_t work_stream_id;
std::string first_stage;
std::string second_stage;
std::string log_header;
};
enum class ErrorMsgMode : uint32_t {
INTERNAL_MODE = 0U,
PROCESS_MODE = 1U,
ERR_MSG_MODE_MAX = 2U
};
struct ErrorItem {
std::string error_id;
std::string error_title;
std::string error_message;
std::string possible_cause;
std::string solution;
std::map<std::string, std::string> args_map;
std::string report_time;
friend bool operator==(const ErrorItem &lhs, const ErrorItem &rhs) noexcept {
return (lhs.error_id == rhs.error_id) && (lhs.error_message == rhs.error_message) &&
(lhs.possible_cause == rhs.possible_cause) && (lhs.solution == rhs.solution);
}
};
}
class ErrorManager {
public:
using ErrorItem = error_message::ErrorItem;
static ErrorManager &GetInstance();
int32_t Init();
int32_t Init(error_message::ErrorMsgMode error_mode);
int32_t Init(const std::string path);
int32_t ReportInterErrMessage(const std::string error_code, const std::string &error_msg);
int32_t ReportErrMessage(const std::string error_code, const std::map<std::string, std::string> &args_map);
int32_t ReportErrMsgWithoutTpl(const std::string &error_code, const std::string &errmsg);
int32_t OutputErrMessage(int32_t handle);
int32_t OutputMessage(int32_t handle);
std::string GetErrorMessage();
int32_t SetRawErrorMessages(const std::vector<ErrorItem> &items);
std::vector<ErrorItem> GetRawErrorMessages();
std::string GetWarningMessage();
void ATCReportErrMessage(const std::string error_code, const std::vector<std::string> &key = {},
const std::vector<std::string> &value = {});
int32_t ReportMstuneCompileFailedMsg(const std::string &root_graph_name,
const std::map<std::string, std::string> &msg);
int32_t GetMstuneCompileFailedMsg(const std::string &graph_name,
std::map<std::string,
std::vector<std::string>> &msg_map);
void GenWorkStreamIdDefault();
void GenWorkStreamIdBySessionGraph(const uint64_t session_id, const uint64_t graph_id);
void GenWorkStreamIdWithSessionIdGraphId(const uint64_t session_id, const uint64_t graph_id);
const std::string &GetLogHeader();
error_message::Context &GetErrorManagerContext();
void SetErrorContext(error_message::Context error_context);
void SetStage(const std::string &first_stage, const std::string &second_stage);
int32_t ParseJsonFormatString(const void *const handle, uint32_t priority = 0);
private:
struct ErrorInfoConfig {
std::string error_id;
std::string error_title;
std::string error_message;
std::string possible_cause;
std::string solution;
std::vector<std::string> arg_list;
uint32_t priority{0};
};
ErrorManager() = default;
~ErrorManager() = default;
ErrorManager(const ErrorManager &) = delete;
ErrorManager(ErrorManager &&) = delete;
ErrorManager &operator=(const ErrorManager &)& = delete;
ErrorManager &operator=(ErrorManager &&)& = delete;
int32_t ParseJsonFile(const std::string path);
static int32_t ReadJsonFile(const std::string &file_path, void *const handle);
void ClassifyCompileFailedMsg(const std::map<std::string, std::string> &msg,
std::map<std::string,
std::vector<std::string>> &classified_msg);
bool IsInnerErrorCode(const std::string &error_code) const;
bool IsUserDefinedErrorCode(const std::string &error_code);
bool IsParamCheckErrorId(const std::string &error_code) const;
inline bool IsValidErrorCode(const std::string &error_codes) const {
constexpr uint32_t kErrorCodeValidLength = 6U;
return error_codes.size() == kErrorCodeValidLength;
}
std::vector<ErrorItem> &GetErrorMsgContainerByWorkId(uint64_t work_id);
std::vector<ErrorItem> &GetWarningMsgContainerByWorkId(uint64_t work_id);
std::vector<ErrorItem> &GetErrorMsgContainer(uint64_t work_stream_id);
std::vector<ErrorItem> &GetWarningMsgContainer(uint64_t work_stream_id);
void AssembleInnerErrorMessage(const std::vector<ErrorItem> &error_messages, const std::string &first_code,
std::stringstream &err_stream) const;
void ClearErrorMsgContainerByWorkId(const uint64_t work_stream_id);
void ClearWarningMsgContainerByWorkId(const uint64_t work_stream_id);
void ClearErrorMsgContainer(const uint64_t work_stream_id);
void ClearWarningMsgContainer(const uint64_t work_stream_id);
bool is_init_ = false;
std::mutex mutex_;
std::map<std::string, ErrorInfoConfig> error_map_;
std::map<std::string, std::map<std::string, std::vector<std::string>>> compile_failed_msg_map_;
std::map<uint64_t, std::vector<ErrorItem>> error_message_per_work_id_;
std::map<uint64_t, std::vector<ErrorItem>> warning_messages_per_work_id_;
thread_local static error_message::Context error_context_;
error_message::ErrorMsgMode error_mode_ = error_message::ErrorMsgMode::INTERNAL_MODE;
std::vector<ErrorItem> error_message_process_;
std::vector<ErrorItem> warning_messages_process_;
};
#endif