1
0

Compare commits

3 Commits

Author SHA1 Message Date
31c624797f revert: revert the delete of std::stacktrace in rust panic.
- i revert previous changes because currently code have not been compiled in clang.
2025-09-28 22:02:54 +08:00
6ecf6935d8 fix: use new format to replace all printf as much as possible.
- ironpad is a very conservative module so I don't want to bring any changes for it except bug fix.
2025-09-28 21:57:18 +08:00
d6b1d7fd46 fix: redirect all std::format to my personal format 2025-09-28 21:44:44 +08:00
5 changed files with 20 additions and 19 deletions

View File

@ -157,6 +157,15 @@ PUBLIC
$<$<CXX_COMPILER_ID:MSVC>:/Zc:__cplusplus> $<$<CXX_COMPILER_ID:MSVC>:/Zc:__cplusplus>
) )
# Fix GCC std::stacktrace link error
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 14)
target_link_libraries(YYCCommonplace PRIVATE stdc++exp)
else ()
target_link_libraries(YYCCommonplace PRIVATE stdc++_libbacktrace)
endif ()
endif ()
# Install binary and headers # Install binary and headers
install(TARGETS YYCCommonplace install(TARGETS YYCCommonplace
EXPORT YYCCommonplaceTargets EXPORT YYCCommonplaceTargets

View File

@ -27,6 +27,7 @@
#define REINTERPRET ::yycc::string::reinterpret #define REINTERPRET ::yycc::string::reinterpret
#define WINFCT ::yycc::windows::winfct #define WINFCT ::yycc::windows::winfct
#define FOPEN ::yycc::patch::fopen #define FOPEN ::yycc::patch::fopen
using namespace std::literals::string_view_literals; using namespace std::literals::string_view_literals;
namespace yycc::carton::ironpad { namespace yycc::carton::ironpad {

View File

@ -19,9 +19,6 @@
namespace yycc::patch::format { namespace yycc::patch::format {
// TODO: order all use of std::format redirect to this function.
// TODO: all use of OP::printf should also switch to this function if possible.
template<class... Args> template<class... Args>
std::string format(std::format_string<Args...> fmt, Args&&... args) { std::string format(std::format_string<Args...> fmt, Args&&... args) {
return std::vformat(fmt.get(), std::make_format_args(args...)); return std::vformat(fmt.get(), std::make_format_args(args...));

View File

@ -4,6 +4,7 @@
#include <cstdlib> #include <cstdlib>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <stacktrace>
#define TERMCOLOR ::yycc::carton::termcolor #define TERMCOLOR ::yycc::carton::termcolor
@ -11,14 +12,7 @@ using namespace yycc::patch::stream;
namespace yycc::rust::panic { namespace yycc::rust::panic {
// TODO: void panic(const char* file, int line, const std::u8string_view& msg) {
// 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.
void panic(const char* file, int line, const std::string_view& msg) {
// Output message in stderr. // Output message in stderr.
auto& dst = std::cerr; auto& dst = std::cerr;
@ -29,6 +23,9 @@ namespace yycc::rust::panic {
dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl; dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl;
// User custom message // User custom message
dst << "note: " << msg << std::endl; dst << "note: " << msg << std::endl;
// Stacktrace message if we support it.
dst << "stacktrace: " << std::endl;
dst << std::stacktrace::current() << std::endl;
// Restore color // Restore color
dst << TERMCOLOR::reset(); dst << TERMCOLOR::reset();

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "../patch/format.hpp"
#include <string_view> #include <string_view>
#include <format> #include <format>
@ -22,19 +23,15 @@
*/ */
namespace yycc::rust::panic { namespace yycc::rust::panic {
/**
* @brief Immediately crashes the entire program like Rust's \c panic! macro.
* @details The macro parameter is the additional message to display.
*/
#define RS_PANIC(msg) ::yycc::rust::panic::panic(__FILE__, __LINE__, (msg))
/** /**
* @brief Immediately crashes the entire program like Rust's \c panic! macro. * @brief Immediately crashes the entire program like Rust's \c panic! macro.
* @details * @details
* The macro parameters are the message to format and its arguments, following \c std::format syntax. * The macro parameters are the message to format and its arguments, following \c std::format syntax.
* This macro essentially calls \c std::format internally. * This macro essentially calls \c std::format internally.
* However, this format function is specially modified that it can accept UTF8 format string and UTF8 string argument.
* More preciously, it is "format" in \c yycc::patch::format namespace.
*/ */
#define RS_PANICF(msg, ...) RS_PANIC(std::format(msg __VA_OPT__(,) __VA_ARGS__)) #define RS_PANIC(msg, ...) ::yycc::rust::panic::panic(__FILE__, __LINE__, ::yycc::patch::format::format(msg __VA_OPT__(, ) __VA_ARGS__))
/** /**
* @brief Immediately crashes the entire program like Rust's \c panic! macro. * @brief Immediately crashes the entire program like Rust's \c panic! macro.
@ -45,6 +42,6 @@ namespace yycc::rust::panic {
* @param[in] line Line number in source file where panic occurred. Usually filled by macros. * @param[in] line Line number in source file where panic occurred. Usually filled by macros.
* @param[in] msg Message to display during panic. * @param[in] msg Message to display during panic.
*/ */
[[noreturn]] void panic(const char* file, int line, const std::string_view& msg); [[noreturn]] void panic(const char* file, int line, const std::u8string_view& msg);
} // namespace yycc::rust::panic } // namespace yycc::rust::panic