diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a371a29..098f1883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog - [Changelog](#changelog) +- [v0.7.1](#v071) - [v0.7.0](#v070) - [v0.6.3](#v063) - [v0.6.2](#v062) @@ -20,6 +21,24 @@ - [v0.1.1](#v011) - [v0.1](#v01) +# v0.7.1 + +Added +- Better support for finding libunwind on macos https://github.com/jeremy-rifkin/cpptrace/pull/162 +- Support for libbacktrace under mingw https://github.com/jeremy-rifkin/cpptrace/pull/166 + +Fixed +- Computation of object address for safe object frames https://github.com/jeremy-rifkin/cpptrace/issues/169 +- Nested microfmt in cpptrace's namespace due to an ODR problem with libassert https://github.com/jeremy-rifkin/libassert/issues/103 +- Compilation on iOS https://github.com/jeremy-rifkin/cpptrace/pull/167 +- Compilation on old MSVC https://github.com/jeremy-rifkin/cpptrace/pull/165 +- Dbghelp use on 32 bit https://github.com/jeremy-rifkin/cpptrace/issues/170 +- Warning in brand new cmake due to `FetchContent_Populate` being deprecated https://github.com/jeremy-rifkin/cpptrace/issues/171 + +Other changes +- Bumped the buffer size for execinfo and CaptureStackBackTrace to 400 frames +- Switched to execinfo.h for unwinding on clang/apple clang on macos due to `_Unwind` not working with `-fno-exceptions` https://github.com/jeremy-rifkin/cpptrace/issues/161 + # v0.7.0 Added diff --git a/CMakeLists.txt b/CMakeLists.txt index e8645f75..21bf152c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ set(package_name "cpptrace") project( cpptrace - VERSION 0.7.0 + VERSION 0.7.1 DESCRIPTION "Simple, portable, and self-contained stacktrace library for C++11 and newer " HOMEPAGE_URL "https://github.com/jeremy-rifkin/cpptrace" LANGUAGES C CXX @@ -246,6 +246,9 @@ target_sources( src/unwind/unwind_with_nothing.cpp src/unwind/unwind_with_unwind.cpp src/unwind/unwind_with_winapi.cpp + src/utils/microfmt.cpp + src/utils/utils.cpp + src/platform/dbghelp_syminit_manager.cpp ) target_include_directories( diff --git a/README.md b/README.md index 614495d3..ebfe37e2 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ include(FetchContent) FetchContent_Declare( cpptrace GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git - GIT_TAG v0.7.0 # + GIT_TAG v0.7.1 # ) FetchContent_MakeAvailable(cpptrace) target_link_libraries(your_target cpptrace::cpptrace) @@ -793,7 +793,7 @@ include(FetchContent) FetchContent_Declare( cpptrace GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git - GIT_TAG v0.7.0 # + GIT_TAG v0.7.1 # ) FetchContent_MakeAvailable(cpptrace) target_link_libraries(your_target cpptrace::cpptrace) @@ -809,7 +809,7 @@ information. ```sh git clone https://github.com/jeremy-rifkin/cpptrace.git -git checkout v0.7.0 +git checkout v0.7.1 mkdir cpptrace/build cd cpptrace/build cmake .. -DCMAKE_BUILD_TYPE=Release @@ -852,7 +852,7 @@ you when installing new libraries. ```ps1 git clone https://github.com/jeremy-rifkin/cpptrace.git -git checkout v0.7.0 +git checkout v0.7.1 mkdir cpptrace/build cd cpptrace/build cmake .. -DCMAKE_BUILD_TYPE=Release @@ -870,7 +870,7 @@ To install just for the local user (or any custom prefix): ```sh git clone https://github.com/jeremy-rifkin/cpptrace.git -git checkout v0.7.0 +git checkout v0.7.1 mkdir cpptrace/build cd cpptrace/build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/wherever @@ -953,7 +953,7 @@ make install cd ~/scratch/cpptrace-test git clone https://github.com/jeremy-rifkin/cpptrace.git cd cpptrace -git checkout v0.7.0 +git checkout v0.7.1 mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=On -DCPPTRACE_USE_EXTERNAL_LIBDWARF=On -DCMAKE_PREFIX_PATH=~/scratch/cpptrace-test/resources -DCMAKE_INSTALL_PREFIX=~/scratch/cpptrace-test/resources @@ -973,7 +973,7 @@ cpptrace and its dependencies. Cpptrace is available through conan at https://conan.io/center/recipes/cpptrace. ``` [requires] -cpptrace/0.7.0 +cpptrace/0.7.1 [generators] CMakeDeps CMakeToolchain diff --git a/cmake/has_stackwalk.cpp b/cmake/has_stackwalk.cpp index bd9127c6..bf03bd5b 100644 --- a/cmake/has_stackwalk.cpp +++ b/cmake/has_stackwalk.cpp @@ -1,3 +1,4 @@ +#define WIN32_LEAN_AND_MEAN #include #include diff --git a/src/binary/module_base.cpp b/src/binary/module_base.cpp index 12e2f7c2..88deca2f 100644 --- a/src/binary/module_base.cpp +++ b/src/binary/module_base.cpp @@ -1,10 +1,9 @@ #include "binary/module_base.hpp" -#include "utils/common.hpp" +#include "platform/platform.hpp" #include "utils/utils.hpp" #include -#include #include #include @@ -17,7 +16,6 @@ #include "binary/elf.hpp" #endif #elif IS_WINDOWS - #include #include "binary/pe.hpp" #endif diff --git a/src/binary/module_base.hpp b/src/binary/module_base.hpp index 5053be06..a15879eb 100644 --- a/src/binary/module_base.hpp +++ b/src/binary/module_base.hpp @@ -1,7 +1,6 @@ #ifndef IMAGE_MODULE_BASE_HPP #define IMAGE_MODULE_BASE_HPP -#include "utils/common.hpp" #include "utils/utils.hpp" #include diff --git a/src/binary/object.cpp b/src/binary/object.cpp index d0849576..0920e428 100644 --- a/src/binary/object.cpp +++ b/src/binary/object.cpp @@ -1,6 +1,6 @@ #include "binary/object.hpp" -#include "utils/common.hpp" +#include "platform/platform.hpp" #include "utils/utils.hpp" #include "binary/module_base.hpp" @@ -16,6 +16,7 @@ #include // needed for dladdr1's link_map info #endif #elif IS_WINDOWS + #define WIN32_LEAN_AND_MEAN #include #endif diff --git a/src/binary/object.hpp b/src/binary/object.hpp index 9200642e..24415d3b 100644 --- a/src/binary/object.hpp +++ b/src/binary/object.hpp @@ -1,14 +1,16 @@ #ifndef OBJECT_HPP #define OBJECT_HPP -#include "utils/common.hpp" -#include "utils/utils.hpp" -#include "binary/module_base.hpp" - -#include #include +#include namespace cpptrace { + +struct object_frame; +struct safe_object_frame; + +using frame_ptr = std::uintptr_t; + namespace detail { object_frame get_frame_object_info(frame_ptr address); diff --git a/src/binary/pe.cpp b/src/binary/pe.cpp index 04d9961c..107084a3 100644 --- a/src/binary/pe.cpp +++ b/src/binary/pe.cpp @@ -1,16 +1,16 @@ #include "binary/pe.hpp" -#include "utils/common.hpp" +#include "platform/platform.hpp" #include "utils/error.hpp" #include "utils/utils.hpp" #if IS_WINDOWS #include -#include #include #include #include +#define WIN32_LEAN_AND_MEAN #include namespace cpptrace { diff --git a/src/binary/pe.hpp b/src/binary/pe.hpp index c81d335c..051fc03a 100644 --- a/src/binary/pe.hpp +++ b/src/binary/pe.hpp @@ -1,7 +1,7 @@ #ifndef PE_HPP #define PE_HPP -#include "utils/common.hpp" +#include "platform/platform.hpp" #include "utils/utils.hpp" #if IS_WINDOWS diff --git a/src/cpptrace.cpp b/src/cpptrace.cpp index cbb81b01..ff4ce56c 100644 --- a/src/cpptrace.cpp +++ b/src/cpptrace.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include "demangle/demangle.hpp" #include "platform/exception_type.hpp" #include "utils/common.hpp" +#include "utils/microfmt.hpp" #include "utils/utils.hpp" #include "binary/object.hpp" #include "binary/safe_dl.hpp" diff --git a/src/demangle/demangle_with_winapi.cpp b/src/demangle/demangle_with_winapi.cpp index a41f93bb..6bfd5763 100644 --- a/src/demangle/demangle_with_winapi.cpp +++ b/src/demangle/demangle_with_winapi.cpp @@ -4,6 +4,7 @@ #include +#define WIN32_LEAN_AND_MEAN #include #include diff --git a/src/from_current.cpp b/src/from_current.cpp index 308735f7..b164d337 100644 --- a/src/from_current.cpp +++ b/src/from_current.cpp @@ -6,14 +6,12 @@ #include #include "platform/platform.hpp" -#include "utils/common.hpp" #include "utils/microfmt.hpp" -#include "utils/utils.hpp" #ifndef _MSC_VER - #include #include #if IS_WINDOWS + #define WIN32_LEAN_AND_MEAN #include #else #include @@ -25,7 +23,7 @@ #endif #else #include - #include + #include #endif #endif #endif diff --git a/src/platform/dbghelp_syminit_manager.cpp b/src/platform/dbghelp_syminit_manager.cpp new file mode 100644 index 00000000..774db78b --- /dev/null +++ b/src/platform/dbghelp_syminit_manager.cpp @@ -0,0 +1,45 @@ +#include "platform/platform.hpp" + +#if IS_WINDOWS + +#include "platform/dbghelp_syminit_manager.hpp" + +#include "utils/error.hpp" +#include "utils/microfmt.hpp" + +#include + +#define WIN32_LEAN_AND_MEAN +#include +#include + +namespace cpptrace { +namespace detail { + + dbghelp_syminit_manager::~dbghelp_syminit_manager() { + for(auto handle : set) { + if(!SymCleanup(handle)) { + ASSERT(false, microfmt::format("Cpptrace SymCleanup failed with code {}\n", GetLastError()).c_str()); + } + } + } + + void dbghelp_syminit_manager::init(HANDLE proc) { + if(set.count(proc) == 0) { + if(!SymInitialize(proc, NULL, TRUE)) { + throw internal_error("SymInitialize failed {}", GetLastError()); + } + set.insert(proc); + } + } + + // Thread-safety: Must only be called from symbols_with_dbghelp while the dbghelp_lock lock is held + dbghelp_syminit_manager& get_syminit_manager() { + static dbghelp_syminit_manager syminit_manager; + return syminit_manager; + } + +} +} + +#endif diff --git a/src/platform/dbghelp_syminit_manager.hpp b/src/platform/dbghelp_syminit_manager.hpp index 7a1ca935..9d91d848 100644 --- a/src/platform/dbghelp_syminit_manager.hpp +++ b/src/platform/dbghelp_syminit_manager.hpp @@ -1,42 +1,21 @@ #ifndef DBGHELP_SYMINIT_MANAGER_HPP #define DBGHELP_SYMINIT_MANAGER_HPP -#include "utils/common.hpp" -#include "utils/utils.hpp" - #include -#include -#include - namespace cpptrace { namespace detail { struct dbghelp_syminit_manager { - std::unordered_set set; - - ~dbghelp_syminit_manager() { - for(auto handle : set) { - if(!SymCleanup(handle)) { - ASSERT(false, microfmt::format("Cpptrace SymCleanup failed with code {}\n", GetLastError()).c_str()); - } - } - } + // The set below contains Windows `HANDLE` objects, `void*` is used to avoid + // including the (expensive) Windows header here + std::unordered_set set; - void init(HANDLE proc) { - if(set.count(proc) == 0) { - if(!SymInitialize(proc, NULL, TRUE)) { - throw internal_error("SymInitialize failed {}", GetLastError()); - } - set.insert(proc); - } - } + ~dbghelp_syminit_manager(); + void init(void* proc); }; // Thread-safety: Must only be called from symbols_with_dbghelp while the dbghelp_lock lock is held - inline dbghelp_syminit_manager& get_syminit_manager() { - static dbghelp_syminit_manager syminit_manager; - return syminit_manager; - } + dbghelp_syminit_manager& get_syminit_manager(); } } diff --git a/src/platform/path.hpp b/src/platform/path.hpp index f19320ad..afb6775e 100644 --- a/src/platform/path.hpp +++ b/src/platform/path.hpp @@ -1,10 +1,13 @@ #ifndef PATH_HPP #define PATH_HPP -#include "utils/common.hpp" #include "platform/platform.hpp" +#include +#include + #if IS_WINDOWS +#define WIN32_LEAN_AND_MEAN #include #endif diff --git a/src/platform/program_name.hpp b/src/platform/program_name.hpp index a51f677a..7d22cda6 100644 --- a/src/platform/program_name.hpp +++ b/src/platform/program_name.hpp @@ -7,6 +7,7 @@ #include "platform/platform.hpp" #if IS_WINDOWS +#define WIN32_LEAN_AND_MEAN #include #define CPPTRACE_MAX_PATH MAX_PATH diff --git a/src/snippets/snippet.cpp b/src/snippets/snippet.cpp index ffa19d3d..c776d3e6 100644 --- a/src/snippets/snippet.cpp +++ b/src/snippets/snippet.cpp @@ -9,6 +9,7 @@ #include #include "utils/common.hpp" +#include "utils/microfmt.hpp" #include "utils/utils.hpp" namespace cpptrace { diff --git a/src/symbols/dwarf/dwarf.hpp b/src/symbols/dwarf/dwarf.hpp index 073bae4c..72be3564 100644 --- a/src/symbols/dwarf/dwarf.hpp +++ b/src/symbols/dwarf/dwarf.hpp @@ -3,10 +3,10 @@ #include #include "utils/error.hpp" +#include "utils/microfmt.hpp" #include "utils/utils.hpp" #include -#include #include #ifdef CPPTRACE_USE_NESTED_LIBDWARF_HEADER_PATH diff --git a/src/symbols/dwarf/dwarf_resolver.cpp b/src/symbols/dwarf/dwarf_resolver.cpp index 76edc0ef..a262812e 100644 --- a/src/symbols/dwarf/dwarf_resolver.cpp +++ b/src/symbols/dwarf/dwarf_resolver.cpp @@ -10,15 +10,16 @@ #include "utils/utils.hpp" #include "platform/path.hpp" #include "platform/program_name.hpp" // For CPPTRACE_MAX_PATH + +#if IS_APPLE #include "binary/mach-o.hpp" +#endif #include #include #include #include -#include #include -#include #include #include #include diff --git a/src/symbols/dwarf/resolver.hpp b/src/symbols/dwarf/resolver.hpp index 20734d3e..0d7c28c1 100644 --- a/src/symbols/dwarf/resolver.hpp +++ b/src/symbols/dwarf/resolver.hpp @@ -3,7 +3,7 @@ #include #include "symbols/symbols.hpp" -#include "utils/common.hpp" +#include "platform/platform.hpp" #include diff --git a/src/symbols/symbols.hpp b/src/symbols/symbols.hpp index dfe4a7b7..9e25fc04 100644 --- a/src/symbols/symbols.hpp +++ b/src/symbols/symbols.hpp @@ -3,11 +3,11 @@ #include -#include -#include +#include +#include #include - -#include "binary/object.hpp" +#include +#include namespace cpptrace { namespace detail { diff --git a/src/symbols/symbols_core.cpp b/src/symbols/symbols_core.cpp index a77896e7..984ef54a 100644 --- a/src/symbols/symbols_core.cpp +++ b/src/symbols/symbols_core.cpp @@ -1,9 +1,11 @@ +#include + #include "symbols/symbols.hpp" #include #include -#include "utils/common.hpp" +#include "utils/error.hpp" #include "binary/object.hpp" namespace cpptrace { diff --git a/src/symbols/symbols_with_addr2line.cpp b/src/symbols/symbols_with_addr2line.cpp index 8a472386..d943ded3 100644 --- a/src/symbols/symbols_with_addr2line.cpp +++ b/src/symbols/symbols_with_addr2line.cpp @@ -3,6 +3,7 @@ #include #include "symbols/symbols.hpp" #include "utils/common.hpp" +#include "utils/microfmt.hpp" #include "utils/utils.hpp" #include diff --git a/src/symbols/symbols_with_dbghelp.cpp b/src/symbols/symbols_with_dbghelp.cpp index 981cb109..6e040252 100644 --- a/src/symbols/symbols_with_dbghelp.cpp +++ b/src/symbols/symbols_with_dbghelp.cpp @@ -3,14 +3,16 @@ #include #include "symbols/symbols.hpp" #include "platform/dbghelp_syminit_manager.hpp" +#include "binary/object.hpp" +#include "utils/common.hpp" +#include "utils/error.hpp" -#include #include #include -#include #include #include +#define WIN32_LEAN_AND_MEAN #include #include diff --git a/src/symbols/symbols_with_dl.cpp b/src/symbols/symbols_with_dl.cpp index 8b3d35a1..c3049b57 100644 --- a/src/symbols/symbols_with_dl.cpp +++ b/src/symbols/symbols_with_dl.cpp @@ -2,6 +2,7 @@ #include #include "symbols/symbols.hpp" +#include "binary/module_base.hpp" #include #include diff --git a/src/symbols/symbols_with_libbacktrace.cpp b/src/symbols/symbols_with_libbacktrace.cpp index 360ff945..bdab1691 100644 --- a/src/symbols/symbols_with_libbacktrace.cpp +++ b/src/symbols/symbols_with_libbacktrace.cpp @@ -3,6 +3,8 @@ #include #include "symbols/symbols.hpp" #include "platform/program_name.hpp" +#include "utils/error.hpp" +#include "utils/common.hpp" #include #include diff --git a/src/symbols/symbols_with_libdwarf.cpp b/src/symbols/symbols_with_libdwarf.cpp index 5456057e..3a492656 100644 --- a/src/symbols/symbols_with_libdwarf.cpp +++ b/src/symbols/symbols_with_libdwarf.cpp @@ -5,7 +5,6 @@ #include #include "dwarf/resolver.hpp" #include "utils/common.hpp" -#include "utils/error.hpp" #include "utils/utils.hpp" #include @@ -15,8 +14,6 @@ #include #include -#include -#include namespace cpptrace { namespace detail { diff --git a/src/symbols/symbols_with_nothing.cpp b/src/symbols/symbols_with_nothing.cpp index c1c4c6c6..f2afd82c 100644 --- a/src/symbols/symbols_with_nothing.cpp +++ b/src/symbols/symbols_with_nothing.cpp @@ -2,6 +2,7 @@ #include #include "symbols/symbols.hpp" +#include "utils/common.hpp" #include diff --git a/src/unwind/unwind.hpp b/src/unwind/unwind.hpp index 285ed431..d37e8361 100644 --- a/src/unwind/unwind.hpp +++ b/src/unwind/unwind.hpp @@ -1,8 +1,7 @@ #ifndef UNWIND_HPP #define UNWIND_HPP -#include "utils/common.hpp" -#include "utils/utils.hpp" +#include #include #include diff --git a/src/unwind/unwind_with_dbghelp.cpp b/src/unwind/unwind_with_dbghelp.cpp index bf74e7b1..eb90ea52 100644 --- a/src/unwind/unwind_with_dbghelp.cpp +++ b/src/unwind/unwind_with_dbghelp.cpp @@ -6,10 +6,9 @@ #include "utils/utils.hpp" #include "platform/dbghelp_syminit_manager.hpp" -#include -#include #include #include +#include #include #include diff --git a/src/unwind/unwind_with_winapi.cpp b/src/unwind/unwind_with_winapi.cpp index e5888e9a..445519d8 100644 --- a/src/unwind/unwind_with_winapi.cpp +++ b/src/unwind/unwind_with_winapi.cpp @@ -9,6 +9,7 @@ #include #include +#define WIN32_LEAN_AND_MEAN #include // Fucking windows headers diff --git a/src/utils/common.hpp b/src/utils/common.hpp index a323b48a..db2518cd 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -1,14 +1,12 @@ #ifndef COMMON_HPP #define COMMON_HPP -#include -#include -#include - #include #include "platform/platform.hpp" +#include + #define ESC "\033[" #define RESET ESC "0m" #define RED ESC "31m" @@ -31,8 +29,8 @@ namespace detail { static const stacktrace_frame null_frame { 0, 0, - nullable::null(), - nullable::null(), + nullable::null(), + nullable::null(), "", "", false diff --git a/src/utils/error.hpp b/src/utils/error.hpp index 6594c1fa..02480b15 100644 --- a/src/utils/error.hpp +++ b/src/utils/error.hpp @@ -2,11 +2,10 @@ #define ERROR_HPP #include -#include #include #include -#include "utils/common.hpp" +#include "platform/platform.hpp" #include "utils/microfmt.hpp" #if IS_MSVC diff --git a/src/utils/microfmt.cpp b/src/utils/microfmt.cpp new file mode 100644 index 00000000..4877732a --- /dev/null +++ b/src/utils/microfmt.cpp @@ -0,0 +1,13 @@ +#include "utils/microfmt.hpp" + +#include + +namespace cpptrace { +namespace detail { + + std::ostream& get_cout() { + return std::cout; + } + +} +} diff --git a/src/utils/microfmt.hpp b/src/utils/microfmt.hpp index 66bb34fa..abadafc3 100644 --- a/src/utils/microfmt.hpp +++ b/src/utils/microfmt.hpp @@ -1,11 +1,10 @@ #ifndef MICROFMT_HPP #define MICROFMT_HPP -#include #include #include #include -#include +#include #include #if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L) #include @@ -286,9 +285,11 @@ namespace microfmt { return detail::format<1>(fmt, fmt + std::strlen(fmt), {detail::format_value(1)}); } + std::ostream& get_cout(); + template void print(const S& fmt, Args&&... args) { - std::cout< diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp new file mode 100644 index 00000000..80de3440 --- /dev/null +++ b/src/utils/utils.cpp @@ -0,0 +1,58 @@ +#include "utils/utils.hpp" + +#if IS_WINDOWS + #include + #define WIN32_LEAN_AND_MEAN + #include +#else + #include + #include +#endif + +namespace cpptrace { +namespace detail { + + bool isatty(int fd) { + #if IS_WINDOWS + return _isatty(fd); + #else + return ::isatty(fd); + #endif + } + + int fileno(std::FILE* stream) { + #if IS_WINDOWS + return _fileno(stream); + #else + return ::fileno(stream); + #endif + } + + void enable_virtual_terminal_processing_if_needed() noexcept { + // enable colors / ansi processing if necessary + #if IS_WINDOWS + // https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing + #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING + constexpr DWORD ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4; + #endif + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD dwMode = 0; + if(hOut == INVALID_HANDLE_VALUE) return; + if(!GetConsoleMode(hOut, &dwMode)) return; + if(dwMode != (dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) + if(!SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return; + #endif + } + + bool directory_exists(const std::string& path) { + #if IS_WINDOWS + DWORD dwAttrib = GetFileAttributesA(path.c_str()); + return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); + #else + struct stat sb; + return stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode); + #endif + } + +} +} diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index ecea6920..ea2637d0 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -7,12 +7,8 @@ #include #include #include -#include -#include #include #include -#include -#include #include #include #include @@ -20,33 +16,13 @@ #include "utils/common.hpp" #include "utils/error.hpp" -#include "utils/microfmt.hpp" - -#if IS_WINDOWS - #include - #include -#else - #include - #include -#endif + + namespace cpptrace { namespace detail { - inline bool isatty(int fd) { - #if IS_WINDOWS - return _isatty(fd); - #else - return ::isatty(fd); - #endif - } - - inline int fileno(std::FILE* stream) { - #if IS_WINDOWS - return _fileno(stream); - #else - return ::fileno(stream); - #endif - } + bool isatty(int fd); + int fileno(std::FILE* stream); inline std::vector split(const std::string& str, const std::string& delims) { std::vector vec; @@ -162,21 +138,7 @@ namespace detail { return byte_swapper{}(value); } - inline void enable_virtual_terminal_processing_if_needed() noexcept { - // enable colors / ansi processing if necessary - #if IS_WINDOWS - // https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing - #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING - constexpr DWORD ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4; - #endif - HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); - DWORD dwMode = 0; - if(hOut == INVALID_HANDLE_VALUE) return; - if(!GetConsoleMode(hOut, &dwMode)) return; - if(dwMode != (dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) - if(!SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return; - #endif - } + void enable_virtual_terminal_processing_if_needed() noexcept; constexpr unsigned n_digits(unsigned value) noexcept { return value < 10 ? 1 : 1 + n_digits(value / 10); @@ -455,15 +417,7 @@ namespace detail { } // shamelessly stolen from stackoverflow - inline bool directory_exists(const std::string& path) { - #if IS_WINDOWS - DWORD dwAttrib = GetFileAttributesA(path.c_str()); - return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); - #else - struct stat sb; - return stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode); - #endif - } + bool directory_exists(const std::string& path); inline std::string basename(const std::string& path) { // Assumes no trailing /'s diff --git a/test/unit/from_current.cpp b/test/unit/from_current.cpp index 8f1aa91c..990afbec 100644 --- a/test/unit/from_current.cpp +++ b/test/unit/from_current.cpp @@ -1,6 +1,4 @@ #include -#include -#include #include #include diff --git a/test/unit/from_current_z.cpp b/test/unit/from_current_z.cpp index 4a26ede7..52bacb34 100644 --- a/test/unit/from_current_z.cpp +++ b/test/unit/from_current_z.cpp @@ -1,6 +1,4 @@ #include -#include -#include #include #include diff --git a/test/unit/object_trace.cpp b/test/unit/object_trace.cpp index d3449efb..b34155e1 100644 --- a/test/unit/object_trace.cpp +++ b/test/unit/object_trace.cpp @@ -1,8 +1,4 @@ -#include -#include -#include -#include -#include +#include #include #include diff --git a/test/unit/raw_trace.cpp b/test/unit/raw_trace.cpp index 2e376148..b5a27155 100644 --- a/test/unit/raw_trace.cpp +++ b/test/unit/raw_trace.cpp @@ -1,8 +1,5 @@ -#include -#include -#include -#include -#include +#include +#include #include #include diff --git a/test/unit/stacktrace.cpp b/test/unit/stacktrace.cpp index e252de25..e59ba838 100644 --- a/test/unit/stacktrace.cpp +++ b/test/unit/stacktrace.cpp @@ -1,8 +1,4 @@ -#include -#include -#include -#include -#include +#include #include #include diff --git a/test/unit/traced_exception.cpp b/test/unit/traced_exception.cpp index dee67c29..c93f25b8 100644 --- a/test/unit/traced_exception.cpp +++ b/test/unit/traced_exception.cpp @@ -1,6 +1,3 @@ -#include -#include -#include #include #include