Log only source base name in assertions.

This commit is contained in:
John Preston 2019-01-10 11:09:13 +04:00
parent a605c110a8
commit e1a2ab0d7e
2 changed files with 59 additions and 20 deletions

View File

@ -19,7 +19,10 @@ void log(const char *message, const char *file, int line);
inline constexpr void noop() {
}
[[noreturn]] inline void fail(const char *message, const char *file, int line) {
[[noreturn]] inline void fail(
const char *message,
const char *file,
int line) {
log(message, file, line);
// Crash with access violation and generate crash report.
@ -30,24 +33,36 @@ inline constexpr void noop() {
std::abort();
}
#ifndef GSL_UNLIKELY
#define DEFINED_GSL_UNLIKELY_
#define GSL_UNLIKELY(expression) (expression)
#endif // GSL_UNLIKELY
inline constexpr void validate(bool condition, const char *message, const char *file, int line) {
(GSL_UNLIKELY(!(condition))) ? fail(message, file, line) : noop();
constexpr const char* extract_basename(const char* path, size_t size) {
while (size != 0 && path[size - 1] != '/' && path[size - 1] != '\\') {
--size;
}
return path + size;
}
#ifdef DEFINED_GSL_UNLIKELY_
#undef GSL_UNLIKELY
#undef DEFINED_GSL_UNLIKELY_
#endif // DEFINED_GSL_UNLIKELY_
} // namespace assertion
} // namespace base
#define AssertCustom(condition, message) (::base::assertion::validate(condition, message, __FILE__, __LINE__))
#if defined(__clang__) || defined(__GNUC__)
#define AssertUnlikelyHelper(x) __builtin_expect(!!(x), 0)
#else
#define AssertUnlikelyHelper(x) (!!(x))
#endif
#define AssertValidationCondition(condition, message, file, line)\
((AssertUnlikelyHelper(!(condition)))\
? ::base::assertion::fail(message, file, line)\
: ::base::assertion::noop())
#define SOURCE_FILE_BASENAME (::base::assertion::extract_basename(\
__FILE__,\
sizeof(__FILE__)))
#define AssertCustom(condition, message) (AssertValidationCondition(\
condition,\
message,\
SOURCE_FILE_BASENAME,\
__LINE__))
#define Assert(condition) AssertCustom(condition, "\"" #condition "\"")
// Define our own versions of Expects() and Ensures().
@ -55,17 +70,28 @@ inline constexpr void validate(bool condition, const char *message, const char *
#ifdef Expects
#undef Expects
#endif // Expects
#define Expects(condition) (::base::assertion::validate(condition, "\"" #condition "\"", __FILE__, __LINE__))
#define Expects(condition) (AssertValidationCondition(\
condition,\
"\"" #condition "\"",\
SOURCE_FILE_BASENAME,\
__LINE__))
#ifdef Ensures
#undef Ensures
#endif // Ensures
#define Ensures(condition) (::base::assertion::validate(condition, "\"" #condition "\"", __FILE__, __LINE__))
#define Ensures(condition) (AssertValidationCondition(\
condition,\
"\"" #condition "\"",\
SOURCE_FILE_BASENAME,\
__LINE__))
#ifdef Unexpected
#undef Unexpected
#endif // Unexpected
#define Unexpected(message) (::base::assertion::fail("Unexpected: " message, __FILE__, __LINE__))
#define Unexpected(message) (::base::assertion::fail(\
"Unexpected: " message,\
SOURCE_FILE_BASENAME,\
__LINE__))
#ifdef _DEBUG
#define AssertIsDebug(...)

View File

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#pragma once
#include "base/basic_types.h"
#include "base/assertion.h"
namespace Core {
class Launcher;
@ -70,11 +71,23 @@ inline MemoryBuffer mb(const void *ptr, uint32 size) {
#define LOG(msg) (Logs::writeMain(QString msg))
//usage LOG(("log: %1 %2").arg(1).arg(2))
#define DEBUG_LOG(msg) { if (Logs::DebugEnabled() || !Logs::started()) Logs::writeDebug(__FILE__, __LINE__, QString msg); }
#define DEBUG_LOG(msg) {\
if (Logs::DebugEnabled() || !Logs::started()) {\
Logs::writeDebug(SOURCE_FILE_BASENAME, __LINE__, QString msg);\
}\
}
//usage DEBUG_LOG(("log: %1 %2").arg(1).arg(2))
#define TCP_LOG(msg) { if (Logs::DebugEnabled() || !Logs::started()) Logs::writeTcp(QString msg); }
#define TCP_LOG(msg) {\
if (Logs::DebugEnabled() || !Logs::started()) {\
Logs::writeTcp(QString msg);\
}\
}
//usage TCP_LOG(("log: %1 %2").arg(1).arg(2))
#define MTP_LOG(dc, msg) { if (Logs::DebugEnabled() || !Logs::started()) Logs::writeMtp(dc, QString msg); }
#define MTP_LOG(dc, msg) {\
if (Logs::DebugEnabled() || !Logs::started()) {\
Logs::writeMtp(dc, QString msg);\
}\
}
//usage MTP_LOG(dc, ("log: %1 %2").arg(1).arg(2))