Share
Embed
Transcript
10 slides
-
$ ελοΫτϨʔεͷϥΠϒϥϦ • ελοΫτϨʔεɺσόοάͷͨΊͷػೳͰ͢ • Τϥʔͷॴ͕Ͳ͏͍͏ܦ࿏Λḷͬͯͦ͜ʹࢸͬͨͷ͔ΛऔಘͰ͖ ·͢ ҾͱΕͳ͍ • ελοΫτϨʔεϓϥοτϑΥʔϜʹऔಘͰ͖Δํ๏͕ఏڙ͞Ε
͍ͯ·͕ͨ͠ɺඪ४ϥΠϒϥϦʹೖͬͨ͜ͱͰڞ௨Խ͞Ε·ͨ͠ • ࠓճɺ͜ͷϥΠϒϥϦͷ͍ํͱఆٛΛղઆ͍͖ͯ͠·͢ -
جຊతͳ͍ํ #include <print> #include <stacktrace> void g() { std::println("{}", std::stacktrace::current());
} void f() { g(); } int main() { f(); } 0# g() at /app/example.cpp:5 1# f() at /app/example.cpp:9 2# main at /app/example.cpp:13 3# at :0 4# __libc_start_main at :0 5# _start at :0 6# -
͜ͷߦΛߟ͑Δ • std::basic_stacktraceͱ͍͏Ϋϥε͕ελοΫτϨʔεΛऔಘ͢Δ ͨΊͷΫϥε • ͦͷผ໊ͱͯ͠std::stacktrace͕ఆٛ͞ΕΔ std::println("{}", std::stacktrace::current()); namespace
std { template <class Allocator> class basic_stacktrace; using stacktrace = basic_stacktrace<allocator<stacktrace_entry>>; } -
͜ͷߦΛߟ͑Δ • std::basic_stacktraceΫϥεͷTUBUJDϝϯόؔcurrent()Ͱ ελοΫτϨʔεΛऔಘ͢Δ • skip max_depthʹ͍ͭͯ͋ͱͰઆ໌ std::println("{}", std::stacktrace::current());
static basic_stacktrace current(); static basic_stacktrace current(size_type skip); static basic_stacktrace current(size_type skip, size_type max_depth); -
͜ͷߦΛߟ͑Δ • std::basic_stacktraceΫϥεจࣈྻͷมػೳ͕͋Δ • std::format()༻ͷformatterಛघԽ • ग़ྗετϦʔϜ༻ͷoperator<<Φʔόʔϩʔυ • to_string()ϝϯόؔ
std::println("{}", std::stacktrace::current()); cout << std::stacktrace::current() << endl; std::string trace = std::stacktrace::current().to_string(); -
͜ͷߦΛߟ͑Δ • std::basic_stacktraceΫϥεstd::stacktrace_entryΫϥεͷྻ • operator[ ] at()ɺsize()ϝϯόؔbegin() end()Λ֤ͬͯߦͷ ใʹΞΫηεͰ͖Δ
• औಘͰ͖ΔใҎԼɿ • source_file() ιʔεϑΝΠϧ໊ ύε • source_line() ߦ൪߸ • description() આ໌ ໊ؔfunction_name()ͳ͍ • operator bool() ۭͰͳ͍͔ఆ • ελοΫτϨʔεͷ֤ߦͷ൪߸std::basic_stacktraceଆͰ͚͍ͭͯΔ std::println("{}", std::stacktrace::current()); 0# g() at /app/example.cpp:5 1# f() at /app/example.cpp:9 2# main at /app/example.cpp:13 3# at :0 4# __libc_start_main at :0 5# _start at :0 6# -
ओͳར༻ํ๏ template <class E> void throw_exception(string_view error) { println(stderr, "{}",
stacktrace::current(1)); throw E{string{error}}; } void g() { throw_exception<invalid_argument>("error"); } • ελοΫτϨʔεΛग़ྗ͔ͯ͠Βྫ֎Λ͛Δ • current(1)ͱ͢Δ͜ͱͰࣗͷؔΛআ͍ͨελοΫτϨʔεʹͰ͖Δ • ΤϥʔΛग़ྗ͢Δؔࣗମͷใ͍͍͍ͩͨΒͳ͍ ΤϥʔॲཧΛ։࢝͢Δॴ͕࠷ॳʹ΄͍͠ -
Τϥʔ༰ͷ͕͍ͪ 0# g() at /app/example.cpp:12 1# f() at /app/example.cpp:16 2#
main at /app/example.cpp:20 3# at :0 4# __libc_start_main at :0 5# _start at :0 6# terminate called after throwing an instance of 'std::invalid_argument' what(): error 0# void throw_exception<invalid_argument>(str ing_view) at /app/example.cpp:7 1# g() at /app/example.cpp:12 2# f() at /app/example.cpp:16 3# main at /app/example.cpp:20 4# at :0 5# __libc_start_main at :0 6# _start at :0 7# terminate called after throwing an instance of 'std::invalid_argument' what(): error current()ͷ߹ current(1)ͷ߹