diff --git a/src/headers/debug_op.h b/src/headers/debug_op.h index e7379914b62c2532e81d4a1b6ca52dbc313c53b1..a948700b2cbdb956969f8e5c971cbcf519521324 100644 --- a/src/headers/debug_op.h +++ b/src/headers/debug_op.h @@ -124,7 +124,8 @@ int isChroot(void); * @param line Line number. * @param func Function name. * @param msg Message to send into the log. + * @param args Variable arguments list. */ -void mtLoggingFunctionsWrapper(int level, const char* tag, const char* file, int line, const char* func, const char* msg); +void mtLoggingFunctionsWrapper(int level, const char* tag, const char* file, int line, const char* func, const char* msg, va_list args); #endif /* DEBUG_H */ diff --git a/src/shared/debug_op.c b/src/shared/debug_op.c index fe33db747dda0587ad99b1608eaac7ce53483f6f..4b14f3b2a097a3d2d9db172abb83d9711c84aa5e 100644 --- a/src/shared/debug_op.c +++ b/src/shared/debug_op.c @@ -692,25 +692,32 @@ char * win_strerror(unsigned long error) { } #endif -void mtLoggingFunctionsWrapper(int level, const char* tag, const char* file, int line, const char* func, const char* msg) { +void mtLoggingFunctionsWrapper(int level, const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { switch(level) { case(LOGLEVEL_DEBUG): - _mtdebug1(tag, file, line, func, msg); + if (dbg_flag >= 1) { + _log(level, tag, file, line, func, msg, args); + } break; case(LOGLEVEL_DEBUG_VERBOSE): - _mtdebug2(tag, file, line, func, msg); + if (dbg_flag >= 2) { + _log(LOGLEVEL_DEBUG, tag, file, line, func, msg, args); + } break; case(LOGLEVEL_INFO): - _mtinfo(tag, file, line, func, msg); - break; case(LOGLEVEL_WARNING): - _mtwarn(tag, file, line, func, msg); - break; case(LOGLEVEL_ERROR): - _mterror(tag, file, line, func, msg); + _log(level, tag, file, line, func, msg, args); break; case(LOGLEVEL_CRITICAL): - _mterror_exit(tag, file, line, func, msg); + _log(level, tag, file, line, func, msg, args); +#ifdef WIN32 + /* If not MA */ +#ifndef MA + WinSetError(); +#endif +#endif + exit(1); break; default: break; diff --git a/src/shared_modules/common/commonDefs.h b/src/shared_modules/common/commonDefs.h index 8206bc1aaee8e36fb3004a2f188f173a3fe12b0f..1f0ff13ccaebdc5d72e687c5570b2c3ef7c6b901 100644 --- a/src/shared_modules/common/commonDefs.h +++ b/src/shared_modules/common/commonDefs.h @@ -13,6 +13,7 @@ #define _COMMON_DEFS_H_ #include "cJSON.h" +#include <stdarg.h> /** * @brief Represents the different host types to be used. @@ -140,8 +141,9 @@ typedef void((*log_fnc_t)(const char* msg)); * @param line Line number where the log is generated. * @param func Function name where the log is generated. * @param msg Message to be logged. + * @param args Variable list args. */ -typedef void ((*full_log_fnc_t)(int level, const char* tag, const char* file, int line, const char* func, const char* msg)); +typedef void ((*full_log_fnc_t)(int level, const char* tag, const char* file, int line, const char* func, const char* msg, va_list args)); /** * @brief Definition to indicate the unlimited queue. diff --git a/src/shared_modules/content_manager/include/contentManager.hpp b/src/shared_modules/content_manager/include/contentManager.hpp index 5d4727c2fae5aa4e5f08e385a1cd17831806a95c..6d1fdd3b43bf7b7789e559ddbb6c190aae7dc003 100644 --- a/src/shared_modules/content_manager/include/contentManager.hpp +++ b/src/shared_modules/content_manager/include/contentManager.hpp @@ -41,10 +41,13 @@ public: * @param logFunction Log function. * */ - void - start(const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& - logFunction); + void start(const std::function<void(const int, + const std::string&, + const std::string&, + const int, + const std::string&, + const std::string&, + va_list)>& logFunction); /** * @brief Stop module facade. diff --git a/src/shared_modules/content_manager/src/contentModule.cpp b/src/shared_modules/content_manager/src/contentModule.cpp index 593145f846ade820e4f08ae2b665d70677a4f979..cde361df9abacfef37fc61189859df61b7f8c4ad 100644 --- a/src/shared_modules/content_manager/src/contentModule.cpp +++ b/src/shared_modules/content_manager/src/contentModule.cpp @@ -16,8 +16,8 @@ #include <utility> void ContentModule::start( - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& + const std::function<void( + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>& logFunction) { ContentModuleFacade::instance().start(logFunction); @@ -66,8 +66,9 @@ extern "C" const std::string& file, const int line, const std::string& func, - const std::string& logMessage) - { callbackLog(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str()); }); + const std::string& logMessage, + va_list args) + { callbackLog(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str(), args); }); } void content_manager_stop() diff --git a/src/shared_modules/content_manager/src/contentModuleFacade.cpp b/src/shared_modules/content_manager/src/contentModuleFacade.cpp index 44cb280fbb7488a6aa22ef64eac23deac1f5fec1..ae0ce61a9a02eab561f6ac59b31b5574b9e71ef4 100644 --- a/src/shared_modules/content_manager/src/contentModuleFacade.cpp +++ b/src/shared_modules/content_manager/src/contentModuleFacade.cpp @@ -13,13 +13,13 @@ namespace Log { std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; void ContentModuleFacade::start( - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& + const std::function<void( + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>& logFunction) { Log::assignLogFunction(logFunction); diff --git a/src/shared_modules/content_manager/src/contentModuleFacade.hpp b/src/shared_modules/content_manager/src/contentModuleFacade.hpp index 620cb4eb4ef5d65e21bdf4465dab9a79307616d7..c4b26665a39cf2c157b7e70eec5e393e6dc73ccd 100644 --- a/src/shared_modules/content_manager/src/contentModuleFacade.hpp +++ b/src/shared_modules/content_manager/src/contentModuleFacade.hpp @@ -33,10 +33,13 @@ public: * * @param logFunction Log function. */ - void - start(const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& - logFunction); + void start(const std::function<void(const int, + const std::string&, + const std::string&, + const int, + const std::string&, + const std::string&, + va_list)>& logFunction); /** * @brief Clear providers. diff --git a/src/shared_modules/content_manager/tests/component/actionOrchestrator_test.cpp b/src/shared_modules/content_manager/tests/component/actionOrchestrator_test.cpp index 516475a7c43983d795af146aa34578ef8e8fd8f8..a11c61ae036d9574fe1dcde1249beef6b0c69411 100644 --- a/src/shared_modules/content_manager/tests/component/actionOrchestrator_test.cpp +++ b/src/shared_modules/content_manager/tests/component/actionOrchestrator_test.cpp @@ -20,7 +20,7 @@ namespace Log { std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; diff --git a/src/shared_modules/content_manager/tests/unit/zipDecompressor_test.cpp b/src/shared_modules/content_manager/tests/unit/zipDecompressor_test.cpp index dd6e852bdacd7b62dd8f36e5d2755c907b288f94..ad1110a9a8ec16074d67d0d7ac99811cbf688d8f 100644 --- a/src/shared_modules/content_manager/tests/unit/zipDecompressor_test.cpp +++ b/src/shared_modules/content_manager/tests/unit/zipDecompressor_test.cpp @@ -20,7 +20,7 @@ namespace Log { std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; diff --git a/src/shared_modules/content_manager/testtool/main.cpp b/src/shared_modules/content_manager/testtool/main.cpp index 281bc61a04ad3c1e2f4f52f91f611de5e40b5a37..871dd68a450f2bef31c2f00b6e0e7fe4a57f3c02 100644 --- a/src/shared_modules/content_manager/testtool/main.cpp +++ b/src/shared_modules/content_manager/testtool/main.cpp @@ -70,7 +70,8 @@ void logFunction(const int logLevel, const std::string& file, const int line, const std::string& func, - const std::string& message) + const std::string& message, + va_list args) { auto pos {file.find_last_of('/')}; if (pos != std::string::npos) @@ -88,23 +89,26 @@ void logFunction(const int logLevel, {LOGLEVEL_CRITICAL, "CRITICAL"}}; const auto levelTag {"[" + LOG_LEVEL_TAGS.at(logLevel) + "]"}; + char formattedStr[OS_MAXSTR] = {0}; + vsnprintf(formattedStr, OS_MAXSTR, message.c_str(), args); + if (logLevel == LOGLEVEL_ERROR || logLevel == LOGLEVEL_CRITICAL) { // Error logs. - std::cerr << tag << ":" << levelTag << ": " << message.c_str() << std::endl; + std::cerr << tag << ":" << levelTag << ": " << formattedStr << std::endl; } else if (logLevel == LOGLEVEL_INFO || logLevel == LOGLEVEL_WARNING) { // Info and warning logs. - std::cout << tag << ":" << levelTag << ": " << message.c_str() << std::endl; + std::cout << tag << ":" << levelTag << ": " << formattedStr << std::endl; } else { // Debug logs. if (VERBOSE) { - std::cout << tag << ":" << levelTag << ":" << fileName << ":" << line << " " << func << ": " - << message.c_str() << std::endl; + std::cout << tag << ":" << levelTag << ":" << fileName << ":" << line << " " << func << ": " << formattedStr + << std::endl; } } } diff --git a/src/shared_modules/indexer_connector/include/indexerConnector.hpp b/src/shared_modules/indexer_connector/include/indexerConnector.hpp index 9076320c35211a45f11ec6fe7ace78d18ab006cf..0f64b597f5c3df50dba0cfba1ddc431a252f3b75 100644 --- a/src/shared_modules/indexer_connector/include/indexerConnector.hpp +++ b/src/shared_modules/indexer_connector/include/indexerConnector.hpp @@ -63,12 +63,15 @@ public: * @param templatePath Path to the template file. * @param logFunction Callback function to be called when trying to log a message. */ - explicit IndexerConnector( - const nlohmann::json& config, - const std::string& templatePath, - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& - logFunction = {}); + explicit IndexerConnector(const nlohmann::json& config, + const std::string& templatePath, + const std::function<void(const int, + const std::string&, + const std::string&, + const int, + const std::string&, + const std::string&, + va_list)>& logFunction = {}); ~IndexerConnector(); diff --git a/src/shared_modules/indexer_connector/src/indexerConnector.cpp b/src/shared_modules/indexer_connector/src/indexerConnector.cpp index 675ddc276cee73697860a34e2b5ee2815f7f20f0..108d4acbfaaba51007e0a2cf56fd5fb43b5dd230 100644 --- a/src/shared_modules/indexer_connector/src/indexerConnector.cpp +++ b/src/shared_modules/indexer_connector/src/indexerConnector.cpp @@ -19,7 +19,7 @@ namespace Log { std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; constexpr auto IC_NAME {"indexer-connnector"}; @@ -36,8 +36,8 @@ constexpr auto DATABASE_BASE_PATH = "queue/indexer/"; IndexerConnector::IndexerConnector( const nlohmann::json& config, const std::string& templatePath, - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& + const std::function<void( + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>& logFunction) { if (logFunction) diff --git a/src/shared_modules/indexer_connector/testtool/main.cpp b/src/shared_modules/indexer_connector/testtool/main.cpp index 605301c10283b5e2ec7708380181ec8290354657..8eb8caafc9c879fd31878c2716e2e37e2473aee8 100644 --- a/src/shared_modules/indexer_connector/testtool/main.cpp +++ b/src/shared_modules/indexer_connector/testtool/main.cpp @@ -4,6 +4,8 @@ #include <iomanip> #include <iostream> +auto constexpr MAXLEN {65536}; + std::string generateRandomString(size_t length) { const char alphanum[] = "0123456789" @@ -100,7 +102,8 @@ int main(const int argc, const char* argv[]) const std::string& file, const int line, const std::string& func, - const std::string& message) + const std::string& message, + va_list args) { auto pos = file.find_last_of('/'); if (pos != std::string::npos) @@ -108,16 +111,18 @@ int main(const int argc, const char* argv[]) pos++; } std::string fileName = file.substr(pos, file.size() - pos); + char formattedStr[MAXLEN] = {0}; + vsnprintf(formattedStr, MAXLEN, message.c_str(), args); if (logLevel != LOG_ERROR) { std::cout << tag << ":" << fileName << ":" << line << " " << func - << " : " << message.c_str() << std::endl; + << " : " << formattedStr << std::endl; } else { std::cerr << tag << ":" << fileName << ":" << line << " " << func - << " : " << message.c_str() << std::endl; + << " : " << formattedStr << std::endl; } }); // Read events file. diff --git a/src/shared_modules/rsync/include/rsync.hpp b/src/shared_modules/rsync/include/rsync.hpp index 8e476c60937a9e7a0030f003a5e145d532c3a364..a976b51568418359ea138f129ab25009809bd366 100644 --- a/src/shared_modules/rsync/include/rsync.hpp +++ b/src/shared_modules/rsync/include/rsync.hpp @@ -50,7 +50,7 @@ class EXPORTED RemoteSync * * @param logFunction Log function. */ - static void initializeFullLogFunction(const std::function<void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& logFunction); + static void initializeFullLogFunction(const std::function<void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>& logFunction); /** * @brief Remote sync initializes the instance. diff --git a/src/shared_modules/rsync/src/rsync.cpp b/src/shared_modules/rsync/src/rsync.cpp index 3d919e6c58ca215baddbe5d0ac6de21f38a611ad..dd91cb4fe00c8dd8e8393717967c13fc458478bc 100644 --- a/src/shared_modules/rsync/src/rsync.cpp +++ b/src/shared_modules/rsync/src/rsync.cpp @@ -20,7 +20,7 @@ namespace Log { std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; @@ -57,9 +57,10 @@ EXPORTED void rsync_initialize_full_log_function(full_log_fnc_t logFunction) const std::string & file, const int line, const std::string & func, - const std::string & logMessage) + const std::string & logMessage, + va_list args) { - logFunction(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str()); + logFunction(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str(), args); }); } @@ -243,7 +244,7 @@ void RemoteSync::initialize(std::function<void(const std::string&)> logFunction) void RemoteSync::initializeFullLogFunction( const std::function < - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&) > & + void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list) > & logFunction) { Log::assignLogFunction(logFunction); diff --git a/src/shared_modules/utils/loggerHelper.h b/src/shared_modules/utils/loggerHelper.h index 431abc4c497ec1e8085a9fd19abbf767edeccea3..bf372f946d4a0c85e6729fa8da8b4b749efa8441 100644 --- a/src/shared_modules/utils/loggerHelper.h +++ b/src/shared_modules/utils/loggerHelper.h @@ -54,17 +54,20 @@ namespace Log #pragma GCC visibility push(hidden) extern std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; #pragma GCC visibility pop #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" - static void assignLogFunction( - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& - logFunction) + static void assignLogFunction(const std::function<void(const int, + const std::string&, + const std::string&, + const int, + const std::string&, + const std::string&, + va_list)>& logFunction) { if (!GLOBAL_LOG_FUNCTION) { @@ -94,12 +97,10 @@ namespace Log { std::va_list args; va_start(args, msg); - char formattedStr[MAXLEN] = {0}; - vsnprintf(formattedStr, MAXLEN, msg, args); - va_end(args); - GLOBAL_LOG_FUNCTION( - LOGLEVEL_INFO, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr); + GLOBAL_LOG_FUNCTION(LOGLEVEL_INFO, tag, sourceFile.file, sourceFile.line, sourceFile.func, msg, args); + + va_end(args); } } @@ -116,12 +117,11 @@ namespace Log { std::va_list args; va_start(args, msg); - char formattedStr[MAXLEN] = {0}; - vsnprintf(formattedStr, MAXLEN, msg, args); - va_end(args); GLOBAL_LOG_FUNCTION( - LOGLEVEL_WARNING, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr); + LOGLEVEL_WARNING, tag, sourceFile.file, sourceFile.line, sourceFile.func, msg, args); + + va_end(args); } } @@ -138,12 +138,10 @@ namespace Log { std::va_list args; va_start(args, msg); - char formattedStr[MAXLEN] = {0}; - vsnprintf(formattedStr, MAXLEN, msg, args); - va_end(args); - GLOBAL_LOG_FUNCTION( - LOGLEVEL_DEBUG, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr); + GLOBAL_LOG_FUNCTION(LOGLEVEL_DEBUG, tag, sourceFile.file, sourceFile.line, sourceFile.func, msg, args); + + va_end(args); } } @@ -160,12 +158,11 @@ namespace Log { std::va_list args; va_start(args, msg); - char formattedStr[MAXLEN] = {0}; - vsnprintf(formattedStr, MAXLEN, msg, args); - va_end(args); GLOBAL_LOG_FUNCTION( - LOGLEVEL_DEBUG_VERBOSE, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr); + LOGLEVEL_DEBUG_VERBOSE, tag, sourceFile.file, sourceFile.line, sourceFile.func, msg, args); + + va_end(args); } } @@ -182,12 +179,10 @@ namespace Log { std::va_list args; va_start(args, msg); - char formattedStr[MAXLEN] = {0}; - vsnprintf(formattedStr, MAXLEN, msg, args); - va_end(args); - GLOBAL_LOG_FUNCTION( - LOGLEVEL_ERROR, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr); + GLOBAL_LOG_FUNCTION(LOGLEVEL_ERROR, tag, sourceFile.file, sourceFile.line, sourceFile.func, msg, args); + + va_end(args); } } }; diff --git a/src/shared_modules/utils/tests/loggerHelper_test.cpp b/src/shared_modules/utils/tests/loggerHelper_test.cpp index 3625132f19bb2af07689b1e924a139e12d8923f3..1398ba1b085dc45e82aa6cdd4abfcf5fcf6a74d6 100644 --- a/src/shared_modules/utils/tests/loggerHelper_test.cpp +++ b/src/shared_modules/utils/tests/loggerHelper_test.cpp @@ -17,7 +17,7 @@ namespace Log { std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; @@ -35,45 +35,62 @@ constexpr auto WARNING_REGEX_THREAD = "warning Tag .+\\.cpp \\d+ operator\\(\\) constexpr auto TAG = "Tag"; -void debugVerboseTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg) +void debugVerboseTestFunction( + const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { + char buffer[MAXLEN]; + vsnprintf(buffer, MAXLEN, msg, args); + ssOutput << "debug_verbose" - << " " << tag << " " << file << " " << line << " " << func << " " << msg << std::endl; + << " " << tag << " " << file << " " << line << " " << func << " " << buffer << std::endl; } -void debugTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg) +void debugTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { + char buffer[MAXLEN]; + vsnprintf(buffer, MAXLEN, msg, args); + ssOutput << "debug" - << " " << tag << " " << file << " " << line << " " << func << " " << msg << std::endl; + << " " << tag << " " << file << " " << line << " " << func << " " << buffer << std::endl; } -void infoTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg) +void infoTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { + char buffer[MAXLEN]; + vsnprintf(buffer, MAXLEN, msg, args); + ssOutput << "info" - << " " << tag << " " << file << " " << line << " " << func << " " << msg << std::endl; + << " " << tag << " " << file << " " << line << " " << func << " " << buffer << std::endl; } -void warningTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg) +void warningTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { + char buffer[MAXLEN]; + vsnprintf(buffer, MAXLEN, msg, args); + ssOutput << "warning" - << " " << tag << " " << file << " " << line << " " << func << " " << msg << std::endl; + << " " << tag << " " << file << " " << line << " " << func << " " << buffer << std::endl; } -void errorTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg) +void errorTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { + char buffer[MAXLEN]; + vsnprintf(buffer, MAXLEN, msg, args); + ssOutput << "error" - << " " << tag << " " << file << " " << line << " " << func << " " << msg << std::endl; + << " " << tag << " " << file << " " << line << " " << func << " " << buffer << std::endl; } -void logFunctionWrapper(int level, const char* tag, const char* file, int line, const char* func, const char* msg) +void logFunctionWrapper( + int level, const char* tag, const char* file, int line, const char* func, const char* msg, va_list args) { switch (level) { - case (Log::LOGLEVEL_DEBUG): debugTestFunction(tag, file, line, func, msg); break; - case (Log::LOGLEVEL_DEBUG_VERBOSE): debugVerboseTestFunction(tag, file, line, func, msg); break; - case (Log::LOGLEVEL_INFO): infoTestFunction(tag, file, line, func, msg); break; - case (Log::LOGLEVEL_WARNING): warningTestFunction(tag, file, line, func, msg); break; - case (Log::LOGLEVEL_ERROR): errorTestFunction(tag, file, line, func, msg); break; + case (Log::LOGLEVEL_DEBUG): debugTestFunction(tag, file, line, func, msg, args); break; + case (Log::LOGLEVEL_DEBUG_VERBOSE): debugVerboseTestFunction(tag, file, line, func, msg, args); break; + case (Log::LOGLEVEL_INFO): infoTestFunction(tag, file, line, func, msg, args); break; + case (Log::LOGLEVEL_WARNING): warningTestFunction(tag, file, line, func, msg, args); break; + case (Log::LOGLEVEL_ERROR): errorTestFunction(tag, file, line, func, msg, args); break; default: break; } } diff --git a/src/shared_modules/utils/tests/loggerHelper_test.h b/src/shared_modules/utils/tests/loggerHelper_test.h index 702c53aa2066b9a12a2b0a2af07246431c1594d6..18ea9310498bcca92bfb4467d7e78f0b9489a9da 100644 --- a/src/shared_modules/utils/tests/loggerHelper_test.h +++ b/src/shared_modules/utils/tests/loggerHelper_test.h @@ -21,7 +21,8 @@ void debugTestFunction(const char* tag, const char* file, int line, const char* void infoTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg); void warningTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg); void errorTestFunction(const char* tag, const char* file, int line, const char* func, const char* msg); -void logFunctionWrapper(int level, const char* tag, const char* file, int line, const char* func, const char* msg); +void logFunctionWrapper( + int level, const char* tag, const char* file, int line, const char* func, const char* msg, va_list args); std::stringstream ssOutput; class LoggerHelperTest : public ::testing::Test @@ -38,8 +39,9 @@ protected: const std::string& file, const int line, const std::string& func, - const std::string& logMessage) - { logFunctionWrapper(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str()); }); + const std::string& logMessage, + va_list args) + { logFunctionWrapper(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str(), args); }); } virtual void SetUp() diff --git a/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScanner.hpp b/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScanner.hpp index f1a6e97958eb97ede8054496fe51e950ddf05f7e..74928dac8dc9c490364f13cdc2a4fd8c4c74f8ad 100644 --- a/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScanner.hpp +++ b/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScanner.hpp @@ -45,12 +45,15 @@ public: * @param configuration Scanner configuration. * @param noWaitToStop If true, the scanner will not wait to stop if any process is running. */ - void - start(const std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& - logFunction, - const nlohmann::json& configuration, - bool noWaitToStop = true); + void start(const std::function<void(const int, + const std::string&, + const std::string&, + const int, + const std::string&, + const std::string&, + va_list)>& logFunction, + const nlohmann::json& configuration, + bool noWaitToStop = true); /** * @brief Stops vulnerability scanner. * diff --git a/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScannerDefs.hpp b/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScannerDefs.hpp index b8872d5e6e76c1f9864a67ae44d84ae888e10f1f..96dd795b1010c3e1335499277eca5443d4f301a7 100644 --- a/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScannerDefs.hpp +++ b/src/wazuh_modules/vulnerability_scanner/include/vulnerabilityScannerDefs.hpp @@ -21,7 +21,7 @@ namespace Log { inline std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)> + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)> GLOBAL_LOG_FUNCTION; }; diff --git a/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScanner.cpp b/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScanner.cpp index 08fa829474f5c6e0d01e973b02ea789113e10feb..1b4b6c5a461b7fb727ff0fb088781c3c9bd499ef 100644 --- a/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScanner.cpp +++ b/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScanner.cpp @@ -19,8 +19,8 @@ // TODO: Remove LCOV flags once the implementation of the 'Indexer Connector' module is completed // LCOV_EXCL_START void VulnerabilityScanner::start( - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& + const std::function<void( + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>& logFunction, const nlohmann::json& configuration, const bool noWaitToStop) @@ -52,8 +52,9 @@ extern "C" const std::string& file, const int line, const std::string& func, - const std::string& logMessage) - { callbackLog(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str()); }, + const std::string& logMessage, + va_list args) + { callbackLog(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str(), args); }, configurationNlohmann); } diff --git a/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.cpp b/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.cpp index 1781557b5461dfd9a56e9b9e5e522bf8ce66617e..1c88396be5f57c011b304de42093e85ce45dba96 100644 --- a/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.cpp +++ b/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.cpp @@ -24,8 +24,8 @@ int MICROSEC_FACTOR = 1000000; // TODO: Remove LCOV flags once the implementation of the 'Indexer Connector' module is completed // LCOV_EXCL_START void VulnerabilityScannerFacade::start( - const std::function< - void(const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& + const std::function<void( + const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>& logFunction, const nlohmann::json& configuration, const bool noWaitToStop) diff --git a/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.hpp b/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.hpp index ced4144cc3f4992678c24de4beb0839d09aa2c05..14570b4465aa4339f1c0601e2fa6760cd6d07d9f 100644 --- a/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.hpp +++ b/src/wazuh_modules/vulnerability_scanner/src/vulnerabilityScannerFacade.hpp @@ -40,12 +40,15 @@ public: * @param configuration Facade configuration. * @param noWaitToStop If true, the facade will not wait to stop if any process is running. */ - void - start(const std::function<void( - const int, const std::string&, const std::string&, const int, const std::string&, const std::string&)>& - logFunction, - const nlohmann::json& configuration, - bool noWaitToStop = true); + void start(const std::function<void(const int, + const std::string&, + const std::string&, + const int, + const std::string&, + const std::string&, + va_list)>& logFunction, + const nlohmann::json& configuration, + bool noWaitToStop = true); /** * @brief Stops facade. diff --git a/src/wazuh_modules/vulnerability_scanner/tests/component/vulnerabilityScannerFacade_test.cpp b/src/wazuh_modules/vulnerability_scanner/tests/component/vulnerabilityScannerFacade_test.cpp index d8d9485bc7461ed7a7661cf3fff40a775c4b6b9e..f81abd6dfa2453356038eb57d6e90d80494bff80 100644 --- a/src/wazuh_modules/vulnerability_scanner/tests/component/vulnerabilityScannerFacade_test.cpp +++ b/src/wazuh_modules/vulnerability_scanner/tests/component/vulnerabilityScannerFacade_test.cpp @@ -23,7 +23,8 @@ void logFunction(const int logLevel, const std::string& file, const int line, const std::string& func, - const std::string& logMessage) + const std::string& logMessage, + va_list args) { std::ignore = logLevel; std::ignore = logMessage; diff --git a/src/wazuh_modules/vulnerability_scanner/testtool/databaseFeedManager/main.cpp b/src/wazuh_modules/vulnerability_scanner/testtool/databaseFeedManager/main.cpp index 8e48d12507e0d84d170a3b4e665902dd2a2a7275..1f3f5bbefe98efc23073a9815af7fc63f28f6230 100644 --- a/src/wazuh_modules/vulnerability_scanner/testtool/databaseFeedManager/main.cpp +++ b/src/wazuh_modules/vulnerability_scanner/testtool/databaseFeedManager/main.cpp @@ -116,13 +116,19 @@ int main(const int argc, const char* argv[]) { // LOGLEVEL_INFO, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr - Log::assignLogFunction([](const int, - const std::string& tag, - const std::string& file, - const int line, - const std::string&, - const std::string& str) - { std::cout << tag << "->" << file << ":" << line << " " << str << std::endl; }); + Log::assignLogFunction( + [](const int, + const std::string& tag, + const std::string& file, + const int line, + const std::string&, + const std::string& str, + va_list args) + { + char formattedStr[MAXLEN] = {0}; + vsnprintf(formattedStr, MAXLEN, str.c_str(), args); + std::cout << tag << "->" << file << ":" << line << " " << formattedStr << std::endl; + }); // Reset required directories if (std::filesystem::exists("./queue/vd")) { diff --git a/src/wazuh_modules/vulnerability_scanner/testtool/scanner/main.cpp b/src/wazuh_modules/vulnerability_scanner/testtool/scanner/main.cpp index fe84f8d05b62c064846d9ec9d333eff192fc5646..c0e7f57d1ec2af94fea5f97a7738d4b2d19fdf82 100644 --- a/src/wazuh_modules/vulnerability_scanner/testtool/scanner/main.cpp +++ b/src/wazuh_modules/vulnerability_scanner/testtool/scanner/main.cpp @@ -22,6 +22,8 @@ #include <iostream> #include <thread> +auto constexpr MAXLEN {65536}; + int main(const int argc, const char* argv[]) { try @@ -53,7 +55,8 @@ int main(const int argc, const char* argv[]) const std::string& file, const int line, const std::string& func, - const std::string& message) + const std::string& message, + va_list args) { auto pos = file.find_last_of('/'); if (pos != std::string::npos) @@ -61,15 +64,17 @@ int main(const int argc, const char* argv[]) pos++; } std::string fileName = file.substr(pos, file.size() - pos); + char formattedStr[MAXLEN] = {0}; + vsnprintf(formattedStr, MAXLEN, message.c_str(), args); if (logLevel != LOG_ERROR) { - std::cout << tag << ":" << fileName << ":" << line << " " << func << " : " << message.c_str() + std::cout << tag << ":" << fileName << ":" << line << " " << func << " : " << formattedStr << std::endl; } else { - std::cerr << tag << ":" << fileName << ":" << line << " " << func << " : " << message.c_str() + std::cerr << tag << ":" << fileName << ":" << line << " " << func << " : " << formattedStr << std::endl; } }, diff --git a/src/wazuh_modules/vulnerability_scanner/testtool/versionMatcher/main.cpp b/src/wazuh_modules/vulnerability_scanner/testtool/versionMatcher/main.cpp index 447bf850ccf3af73d1c1df1698e2ecb6f3d4c238..d8ebd40b999f10cb882649df0c16273e0375ddc0 100644 --- a/src/wazuh_modules/vulnerability_scanner/testtool/versionMatcher/main.cpp +++ b/src/wazuh_modules/vulnerability_scanner/testtool/versionMatcher/main.cpp @@ -249,13 +249,19 @@ int main(const int argc, const char* argv[]) { // LOGLEVEL_INFO, tag, sourceFile.file, sourceFile.line, sourceFile.func, formattedStr - Log::assignLogFunction([](const int, - const std::string& tag, - const std::string& file, - const int line, - const std::string&, - const std::string& str) - { std::cout << tag << "->" << file << ":" << line << " " << str << std::endl; }); + Log::assignLogFunction( + [](const int, + const std::string& tag, + const std::string& file, + const int line, + const std::string&, + const std::string& str, + va_list args) + { + char formattedStr[MAXLEN] = {0}; + vsnprintf(formattedStr, MAXLEN, str.c_str(), args); + std::cout << tag << "->" << file << ":" << line << " " << formattedStr << std::endl; + }); // Reset required directories if (std::filesystem::exists("./queue/vd")) {