From 6ac9f9e35372ce60c575ae01191b634e13659403 Mon Sep 17 00:00:00 2001 From: anthwlock Date: Sun, 23 Jun 2024 17:10:19 +0200 Subject: [PATCH] add custom assert --- Makefile | 2 +- src/atom.cpp | 2 -- src/atom.h | 1 - src/avc1/nal.cpp | 1 - src/avc1/sps-info.cpp | 1 - src/codec.cpp | 1 - src/common.cpp | 41 ++++++++++++++++++++++++++++++++ src/common.h | 54 ++++++++++++++++++++++++++++++++++++++++--- src/file.cpp | 1 - src/hvc1/nal.cpp | 1 - src/main.cpp | 1 + src/mp4.cpp | 1 - src/track.cpp | 1 - 13 files changed, 94 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index a353502..d2730ec 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ else endif endif -CXXFLAGS += -std=c++11 -D_FILE_OFFSET_BITS=64 +CXXFLAGS += -std=c++17 -D_FILE_OFFSET_BITS=64 ifeq ($(IS_RELEASE), 1) CXXFLAGS += -O3 diff --git a/src/atom.cpp b/src/atom.cpp index b367a75..965f455 100644 --- a/src/atom.cpp +++ b/src/atom.cpp @@ -9,8 +9,6 @@ #include // setw #include -#include - #include "AP_AtomDefinitions.h" diff --git a/src/atom.h b/src/atom.h index 87f0121..74fea8a 100644 --- a/src/atom.h +++ b/src/atom.h @@ -5,7 +5,6 @@ extern "C" { } #include #include -#include #include "common.h" #include "file.h" diff --git a/src/avc1/nal.cpp b/src/avc1/nal.cpp index 011df6e..8afef59 100644 --- a/src/avc1/nal.cpp +++ b/src/avc1/nal.cpp @@ -1,7 +1,6 @@ #include "nal.h" #include -#include #include #include "../common.h" diff --git a/src/avc1/sps-info.cpp b/src/avc1/sps-info.cpp index 4135c20..b29987e 100644 --- a/src/avc1/sps-info.cpp +++ b/src/avc1/sps-info.cpp @@ -6,7 +6,6 @@ #include #include -#include #include "../common.h" #include "nal.h" diff --git a/src/codec.cpp b/src/codec.cpp index ecc0c4d..e07fe99 100644 --- a/src/codec.cpp +++ b/src/codec.cpp @@ -2,7 +2,6 @@ #include #include -#include extern "C" { #include diff --git a/src/common.cpp b/src/common.cpp index 1d27704..c11feab 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -4,6 +4,7 @@ #include // setprecision #include #include +#include extern "C" { #include "libavcodec/avcodec.h" @@ -47,6 +48,7 @@ bool g_dont_omit = false; bool g_noise_buffer_active = false; bool g_ignore_out_of_bound_chunks = false; bool g_skip_existing = false; +bool g_fast_assert = false; bool g_no_ctts = false; bool g_is_gui = false; uint g_num_w2 = 0; @@ -486,3 +488,42 @@ FILE* _my_open(const char* path, const wchar_t* mode) { return _wfopen(pathW.c_str(), mode); } #endif + +void callPstack() { +#ifdef _WIN32 + // Do nothing on Windows +#else + // Check if pstack is available + if (system("which pstack > /dev/null 2>&1") == 0) { + pid_t pid = getpid(); // Get the current process ID + string cmd = "pstack " + to_string(pid); + + cerr << "\n+ " << cmd << endl; + system(cmd.c_str()); + cerr << "\n"; + } else { + cerr << "pstack is not available on this system." << endl; + } +#endif +} + +// Function to trim leading and trailing whitespace from a string +string trim(const string &str) { + size_t first = str.find_first_not_of(' '); + if (first == string::npos) return ""; + size_t last = str.find_last_not_of(' '); + return str.substr(first, last - first + 1); +} + +// split the string by comma and trim whitespace from each part +vector splitAndTrim(const string &str) { + vector result; + stringstream ss(str); + string item; + + while (getline(ss, item, ',')) { + result.push_back(trim(item)); + } + + return result; +} diff --git a/src/common.h b/src/common.h index 2c28561..b271fce 100644 --- a/src/common.h +++ b/src/common.h @@ -28,9 +28,11 @@ extern uint g_max_partsize, // max theoretical part size g_max_buf_sz_needed, // for determining part size g_max_partsize_default; -extern bool g_interactive, g_muted, g_ignore_unknown, g_stretch_video, g_show_tracks, - g_dont_write, g_use_chunk_stats, g_dont_exclude, g_dump_repaired, g_search_mdat, - g_strict_nal_frame_check, g_ignore_forbidden_nal_bit, g_noise_buffer_active, g_dont_omit, +extern bool g_interactive, g_muted, g_ignore_unknown, g_stretch_video, + g_show_tracks, g_dont_write, g_use_chunk_stats, g_dont_exclude, + g_dump_repaired, g_search_mdat, g_strict_nal_frame_check, + g_ignore_forbidden_nal_bit, g_noise_buffer_active, g_dont_omit, + g_fast_assert, g_ignore_out_of_bound_chunks, g_skip_existing, g_no_ctts, g_is_gui; extern int64_t g_range_start, g_range_end; extern std::string g_dst_path; @@ -215,4 +217,50 @@ FILE* _my_open(const char* path, const wchar_t* mode); #define my_open fopen #endif +void callPstack(); +std::vector splitAndTrim(const std::string &str); + +template +void printArgs(std::ostream& os, const std::string& sep, const std::string& argNames, Args... args) { + std::vector names = splitAndTrim(argNames); + if (names.size()) { + os << sep; + int i = 0; + bool first = true; // Flag to check if it is the first element + ([&](const auto& arg) { + if (!first) { + os << ", "; + } + first = false; + os << names[i++] << "=" << arg; + }(args), ...); + } + os << "\n"; +} + + +#ifndef NDEBUG + #define assertt(Expr, ...) if (!(Expr)) __assertt(#Expr, __PRETTY_FUNCTION__, __FILE__, __LINE__, #__VA_ARGS__, ##__VA_ARGS__) +#else + #define assertt(...); +#endif + +template +void __assertt(const char* expr_str, const char* fn, const char* file, int line, const std::string& argNames, Args... args) +{ + disableNoiseBuffer(); + + std::cerr << file << ":" << line << ": " << fn << ": Assertion `" << expr_str << "' failed."; + + printArgs(std::cerr, " // ", argNames, args...); + + if (g_fast_assert) exit(1); + + callPstack(); + abort(); +} + +#undef assert +#define assert assertt + #endif // HELPER_H diff --git a/src/file.cpp b/src/file.cpp index 9b59bf6..831148a 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "common.h" diff --git a/src/hvc1/nal.cpp b/src/hvc1/nal.cpp index 99998ce..6cfac7d 100644 --- a/src/hvc1/nal.cpp +++ b/src/hvc1/nal.cpp @@ -1,7 +1,6 @@ #include "nal.h" #include -#include #include #include "../common.h" diff --git a/src/main.cpp b/src/main.cpp index f660f04..93ac177 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -157,6 +157,7 @@ int main(int argc, char *argv[]) { else if (a == "dst") arg_dst = kExpectArg; else if (a == "skip") g_skip_existing = true; else if (a == "mp") arg_mp = kExpectArg; + else if (a == "fa") g_fast_assert = true; else if (arg.size() > 2) {cerr << "Error: seperate multiple options with space! See '-h'\n"; return -1;} else usage(); } diff --git a/src/mp4.cpp b/src/mp4.cpp index 3fab4a8..d97c36b 100644 --- a/src/mp4.cpp +++ b/src/mp4.cpp @@ -18,7 +18,6 @@ */ -#include #include #include #include // setprecision diff --git a/src/track.cpp b/src/track.cpp index 5775c76..91b3323 100644 --- a/src/track.cpp +++ b/src/track.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include // setprecision