1
0
Files
YYCCommonplace/src/yycc/rust/panic.cpp

40 lines
1.1 KiB
C++
Raw Normal View History

#include "panic.hpp"
2025-08-19 13:58:05 +08:00
#include "../carton/termcolor.hpp"
#include "../patch/stream.hpp"
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stacktrace>
2025-08-19 13:58:05 +08:00
#define TERMCOLOR ::yycc::carton::termcolor
using namespace yycc::patch::stream;
2025-08-19 13:58:05 +08:00
namespace yycc::rust::panic {
void panic(const char* file, int line, const std::u8string_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
dst << TERMCOLOR::foreground(TERMCOLOR::Color::Red);
// 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;
// Stacktrace message if we support it.
dst << "stacktrace: " << std::endl;
dst << std::stacktrace::current() << std::endl;
2025-08-19 13:58:05 +08:00
// Restore color
dst << TERMCOLOR::reset();
// Make sure all messages are flushed into screen.
dst.flush();
// Force exit
std::abort();
}
} // namespace yycc::rust::panic