/**
 * 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 INTERFACE_UTILS_ERROR_H_
#define INTERFACE_UTILS_ERROR_H_

#include <stdlib.h>
#include <signal.h>
#include <exception>
#include <execinfo.h>
#include <iostream>

#include "tilefwk/error.h"
#include "tilefwk/tilefwk_log.h"

namespace npu::tile_fwk {
struct TerminateHandler {
    TerminateHandler() {
        struct sigaction sa;
        sa.sa_handler = TerminateHandler::SigAction;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART;
        sigaction(SIGSEGV, &sa, &ori);

        std::set_terminate([] {
            try {
                auto eptr = std::current_exception();
                if (eptr) {
                    std::rethrow_exception(eptr);
                }
            } catch (const std::exception &e) {
                FUNCTION_LOGE("Caught exception: %s", e.what());
                std::cerr << "Caught exception: '" << e.what() << "'\n";
            }
            fflush(nullptr);
            _Exit(1);
        });
    }

    static void SigAction(int signo) {
        (void)signo;
        auto &backtrace = GetBacktrace(0x2, 0x10)->Get();
        FUNCTION_LOGE("segment fault!!!\n%s", backtrace);
        std::cerr << "segment fault!!!\n" << backtrace << std::endl;
        fflush(nullptr);
        _Exit(1);
    }

    ~TerminateHandler() { sigaction(SIGSEGV, &ori, nullptr); }

    struct sigaction ori;
};
} // namespace npu::tile_fwk
#endif