Add some utilities for exception handling and detecting whether stderr is a terminal
This commit is contained in:
parent
df6c22f640
commit
8bb8a2020c
@ -145,7 +145,7 @@ namespace cpptrace {
|
||||
void print() const;
|
||||
void print(std::ostream& stream) const;
|
||||
void print(std::ostream& stream, bool color) const;
|
||||
std::string to_string() const;
|
||||
std::string to_string(bool color = false) const;
|
||||
void clear();
|
||||
bool empty() const noexcept;
|
||||
/* operator<<(ostream, ..), std::format support, and iterators exist for this object */
|
||||
@ -224,10 +224,17 @@ or wether they're rethrown to the caller.
|
||||
speed is prioritized. If using this function, set the cache mode at the very start of your program before any traces are
|
||||
performed.
|
||||
|
||||
`cpptrace::isatty` and the fileno definitions are useful for deciding whether to use color when printing stack taces.
|
||||
|
||||
```cpp
|
||||
namespace cpptrace {
|
||||
std::string demangle(const std::string& name);
|
||||
void absorb_trace_exceptions(bool absorb);
|
||||
bool isatty(int fd);
|
||||
|
||||
extern const int stdin_fileno;
|
||||
extern const int stderr_fileno;
|
||||
extern const int stdout_fileno;
|
||||
|
||||
enum class cache_mode {
|
||||
// Only minimal lookup tables
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
#define CPPTRACE_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
@ -103,7 +102,7 @@ namespace cpptrace {
|
||||
CPPTRACE_API void print(std::ostream& stream, bool color) const;
|
||||
CPPTRACE_API void clear();
|
||||
CPPTRACE_API bool empty() const noexcept;
|
||||
CPPTRACE_API std::string to_string() const;
|
||||
CPPTRACE_API std::string to_string(bool color = false) const;
|
||||
CPPTRACE_API friend std::ostream& operator<<(std::ostream& stream, const stacktrace& trace);
|
||||
|
||||
using iterator = std::vector<stacktrace_frame>::iterator;
|
||||
@ -127,8 +126,13 @@ namespace cpptrace {
|
||||
|
||||
// utilities:
|
||||
CPPTRACE_API std::string demangle(const std::string& name);
|
||||
CPPTRACE_API bool isatty(int fd);
|
||||
CPPTRACE_API void absorb_trace_exceptions(bool absorb);
|
||||
|
||||
CPPTRACE_API extern const int stdin_fileno;
|
||||
CPPTRACE_API extern const int stderr_fileno;
|
||||
CPPTRACE_API extern const int stdout_fileno;
|
||||
|
||||
enum class cache_mode {
|
||||
// Only minimal lookup tables
|
||||
prioritize_memory,
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
@ -233,9 +234,9 @@ namespace cpptrace {
|
||||
}
|
||||
|
||||
CPPTRACE_API
|
||||
std::string stacktrace::to_string() const {
|
||||
std::string stacktrace::to_string(bool color) const {
|
||||
std::ostringstream oss;
|
||||
print(oss, false, false);
|
||||
print(oss, color, false);
|
||||
return std::move(oss).str();
|
||||
}
|
||||
|
||||
@ -318,6 +319,15 @@ namespace cpptrace {
|
||||
return detail::demangle(name);
|
||||
}
|
||||
|
||||
CPPTRACE_API
|
||||
bool isatty(int fd) {
|
||||
return detail::isatty(fd);
|
||||
}
|
||||
|
||||
CPPTRACE_API const int stdin_fileno = detail::fileno(stdin);
|
||||
CPPTRACE_API const int stdout_fileno = detail::fileno(stdout);
|
||||
CPPTRACE_API const int stderr_fileno = detail::fileno(stderr);
|
||||
|
||||
namespace detail {
|
||||
std::atomic_bool absorb_trace_exceptions(true); // NOSONAR
|
||||
std::atomic<enum cache_mode> cache_mode(cache_mode::prioritize_speed); // NOSONAR
|
||||
|
||||
@ -22,12 +22,30 @@
|
||||
|
||||
#if IS_WINDOWS
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace cpptrace {
|
||||
namespace detail {
|
||||
inline bool isatty(int fd) {
|
||||
#if IS_WINDOWS
|
||||
return _isatty(fd);
|
||||
#else
|
||||
return ::isatty(fd);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int fileno(FILE* stream) {
|
||||
#if IS_WINDOWS
|
||||
return _fileno(stream);
|
||||
#else
|
||||
return ::fileno(stream);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline std::vector<std::string> split(const std::string& str, const std::string& delims) {
|
||||
std::vector<std::string> vec;
|
||||
size_t old_pos = 0;
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
#include <string>
|
||||
|
||||
void trace() {
|
||||
cpptrace::absorb_trace_exceptions(false);
|
||||
cpptrace::generate_trace().print();
|
||||
throw cpptrace::exception_with_message("foobar");
|
||||
}
|
||||
|
||||
void foo(int n) {
|
||||
@ -35,5 +35,8 @@ int main() try {
|
||||
cpptrace::absorb_trace_exceptions(false);
|
||||
function_one(0);
|
||||
} catch(cpptrace::exception& e) {
|
||||
std::cerr << e.what();
|
||||
std::cerr << "Error: "
|
||||
<< e.get_raw_what()
|
||||
<< '\n';
|
||||
e.get_trace().print(std::cerr, cpptrace::isatty(cpptrace::stderr_fileno));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user