CHECK宏支持自定义错误提示

This commit is contained in:
ziyue
2021-10-16 16:24:40 +08:00
parent 11eb04f094
commit b10fc52384
4 changed files with 55 additions and 18 deletions

View File

@@ -11,6 +11,8 @@
#ifndef ZLMEDIAKIT_MACROS_H
#define ZLMEDIAKIT_MACROS_H
#include <iostream>
#include <sstream>
#if defined(__MACH__)
#include <arpa/inet.h>
#include <machine/endian.h>
@@ -38,7 +40,7 @@
#endif
#ifndef CHECK
#define CHECK(exp) Assert_Throw(!(exp), #exp, __FUNCTION__, __FILE__, __LINE__)
#define CHECK(exp,...) mediakit::Assert_ThrowCpp(!(exp), #exp, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
#endif//CHECK
#ifndef MAX
@@ -62,14 +64,39 @@
#define FMP4_SCHEMA "fmp4"
#define DEFAULT_VHOST "__defaultVhost__"
extern const char SERVER_NAME[];
#ifdef __cplusplus
extern "C" {
#endif
extern void Assert_Throw(int failed, const char *exp, const char *func, const char *file, int line);
extern void Assert_Throw(int failed, const char *exp, const char *func, const char *file, int line, const char *str);
#ifdef __cplusplus
}
#endif
namespace mediakit {
extern const char SERVER_NAME[];
void printArgs(std::ostream &out);
template<typename First>
void printArgs(std::ostream &out, First &&first) {
out << std::forward<First>(first);
}
template<typename First, typename ...ARGS>
void printArgs(std::ostream &out, First &&first, ARGS &&...args) {
out << std::forward<First>(first);
printArgs(out, std::forward<ARGS>(args)...);
}
template<typename ...ARGS>
void Assert_ThrowCpp(int failed, const char *exp, const char *func, const char *file, int line, ARGS &&...args) {
if (failed) {
std::stringstream ss;
printArgs(ss, std::forward<ARGS>(args)...);
Assert_Throw(failed, exp, func, file, line, ss.str().data());
}
}
}//namespace mediakit
#endif //ZLMEDIAKIT_MACROS_H