2025-06-23 16:22:55 +08:00
|
|
|
#include "panic.hpp"
|
2025-08-19 13:58:05 +08:00
|
|
|
#include "../carton/termcolor.hpp"
|
2025-09-28 21:37:05 +08:00
|
|
|
#include "../patch/stream.hpp"
|
2025-06-23 16:22:55 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
|
2025-08-19 13:58:05 +08:00
|
|
|
#define TERMCOLOR ::yycc::carton::termcolor
|
2025-09-28 21:37:05 +08:00
|
|
|
|
|
|
|
using namespace yycc::patch::stream;
|
2025-08-19 13:58:05 +08:00
|
|
|
|
2025-06-23 16:22:55 +08:00
|
|
|
namespace yycc::rust::panic {
|
|
|
|
|
2025-08-19 21:47:21 +08:00
|
|
|
// TODO:
|
|
|
|
// I sadly remove the stacktrace feature for panic function.
|
|
|
|
// Because in GCC, it has link error (can be fixed by extra link option).
|
|
|
|
// In Clang, it even lack the whole header file.
|
|
|
|
// It seems that STL providers are not ready for this feature. So I decide remove it entirely.
|
|
|
|
// Once every STL probiders have ready for this, I will add it back.
|
|
|
|
|
2025-06-23 16:22:55 +08:00
|
|
|
void panic(const char* file, int line, const std::string_view& msg) {
|
|
|
|
// Output message in stderr.
|
|
|
|
auto& dst = std::cerr;
|
|
|
|
|
|
|
|
// Print error message if we support it.
|
2025-08-19 13:58:05 +08:00
|
|
|
// Setup color
|
2025-09-28 21:37:05 +08:00
|
|
|
dst << TERMCOLOR::foreground(TERMCOLOR::Color::Red);
|
2025-06-23 16:22:55 +08:00
|
|
|
// File name and line number message
|
|
|
|
dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl;
|
|
|
|
// User custom message
|
|
|
|
dst << "note: " << msg << std::endl;
|
2025-08-19 13:58:05 +08:00
|
|
|
// Restore color
|
2025-09-28 21:37:05 +08:00
|
|
|
dst << TERMCOLOR::reset();
|
2025-06-23 16:22:55 +08:00
|
|
|
|
|
|
|
// Make sure all messages are flushed into screen.
|
|
|
|
dst.flush();
|
|
|
|
|
|
|
|
// Force exit
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace yycc::rust::panic
|