From 4e8702a5b141e2296049bb8d247086f04641a0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= Date: Sat, 2 Dec 2023 12:42:34 +0100 Subject: [PATCH] Logger: avoid leaking fullpath (#1250) --- Common++/header/Logger.h | 45 ++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/Common++/header/Logger.h b/Common++/header/Logger.h index ced696a42d..282f798813 100644 --- a/Common++/header/Logger.h +++ b/Common++/header/Logger.h @@ -11,6 +11,33 @@ #define LOG_MODULE UndefinedLogModule #endif +// Use __FILE_NAME__ to avoid leaking complete full path +#ifdef __FILE_NAME__ +#define PCAPPP_FILENAME __FILE_NAME__ +#else +#define PCAPPP_FILENAME __FILE__ +#endif + +#define PCPP_LOG(level, message) do \ + { \ + std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \ + (*sstream) << message; \ + pcpp::Logger::getInstance().internalPrintLogMessage(sstream, level, PCAPPP_FILENAME, __FUNCTION__, __LINE__); \ + } while(0) + +#define PCPP_LOG_DEBUG(message) do \ + { \ + if (pcpp::Logger::getInstance().logsEnabled() && pcpp::Logger::getInstance().isDebugEnabled(LOG_MODULE)) \ + { \ + PCPP_LOG(pcpp::Logger::Debug, message); \ + } \ + } while(0) + +#define PCPP_LOG_ERROR(message) do \ + { \ + PCPP_LOG(pcpp::Logger::Error, message); \ + } while (0) + /// @file /** @@ -230,24 +257,6 @@ namespace pcpp static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line); }; - -#define PCPP_LOG_DEBUG(message) do \ - { \ - if (pcpp::Logger::getInstance().logsEnabled() && pcpp::Logger::getInstance().isDebugEnabled(LOG_MODULE)) \ - { \ - std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \ - (*sstream) << message; \ - pcpp::Logger::getInstance().internalPrintLogMessage(sstream, pcpp::Logger::Debug, __FILE__, __FUNCTION__, __LINE__); \ - } \ - } while(0) - -#define PCPP_LOG_ERROR(message) do \ - { \ - std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \ - (*sstream) << message; \ - pcpp::Logger::getInstance().internalPrintLogMessage(sstream, pcpp::Logger::Error, __FILE__, __FUNCTION__, __LINE__); \ - } while (0) - } // namespace pcpp #endif /* PCAPPP_LOGGER */