From e1a2ab0d7ef55fddb9bf7a0325a384a3f92ad834 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 10 Jan 2019 11:09:13 +0400 Subject: [PATCH] Log only source base name in assertions. --- Telegram/SourceFiles/base/assertion.h | 60 +++++++++++++++++++-------- Telegram/SourceFiles/logs.h | 19 +++++++-- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/Telegram/SourceFiles/base/assertion.h b/Telegram/SourceFiles/base/assertion.h index c67873f617..c354915d01 100644 --- a/Telegram/SourceFiles/base/assertion.h +++ b/Telegram/SourceFiles/base/assertion.h @@ -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(...) diff --git a/Telegram/SourceFiles/logs.h b/Telegram/SourceFiles/logs.h index e27b8ed6a6..f9279b3bda 100644 --- a/Telegram/SourceFiles/logs.h +++ b/Telegram/SourceFiles/logs.h @@ -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))