Skip to content

Commit

Permalink
add custom assert
Browse files Browse the repository at this point in the history
  • Loading branch information
anthwlock committed Jun 23, 2024
1 parent 60379a9 commit 6ac9f9e
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <iomanip> // setw
#include <string.h>

#include <assert.h>

#include "AP_AtomDefinitions.h"


Expand Down
1 change: 0 additions & 1 deletion src/atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern "C" {
}
#include <vector>
#include <string>
#include <cassert>

#include "common.h"
#include "file.h"
Expand Down
1 change: 0 additions & 1 deletion src/avc1/nal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "nal.h"

#include <iostream>
#include <cassert>
#include <cstdint>

#include "../common.h"
Expand Down
1 change: 0 additions & 1 deletion src/avc1/sps-info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>

#include <cassert>

#include "../common.h"
#include "nal.h"
Expand Down
1 change: 0 additions & 1 deletion src/codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <iostream>
#include <vector>
#include <cassert>

extern "C" {
#include <stdint.h>
Expand Down
41 changes: 41 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iomanip> // setprecision
#include <sstream>
#include <cmath>
#include <unistd.h>

extern "C" {
#include "libavcodec/avcodec.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<string> splitAndTrim(const string &str) {
vector<string> result;
stringstream ss(str);
string item;

while (getline(ss, item, ',')) {
result.push_back(trim(item));
}

return result;
}
54 changes: 51 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -215,4 +217,50 @@ FILE* _my_open(const char* path, const wchar_t* mode);
#define my_open fopen
#endif

void callPstack();
std::vector<std::string> splitAndTrim(const std::string &str);

template <typename... Args>
void printArgs(std::ostream& os, const std::string& sep, const std::string& argNames, Args... args) {
std::vector<std::string> 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 <typename... Args>
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
1 change: 0 additions & 1 deletion src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cassert>
#include <libgen.h>

#include "common.h"
Expand Down
1 change: 0 additions & 1 deletion src/hvc1/nal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "nal.h"

#include <iostream>
#include <cassert>
#include <cstdint>

#include "../common.h"
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 0 additions & 1 deletion src/mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include <assert.h>
#include <string>
#include <iostream>
#include <iomanip> // setprecision
Expand Down
1 change: 0 additions & 1 deletion src/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <iostream>
#include <vector>
#include <string.h>
#include <assert.h>
#include <random>
#include <iomanip> // setprecision

Expand Down

0 comments on commit 6ac9f9e

Please sign in to comment.