fledge-iot/fledge

Display all Fledge logs in stdout during unit tests

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
Right now the Logger class only logs into specific Fledge output files and not into the standard output, meaning they will not appear when running unit tests.
Having a compile time flag to make all Fledge logs appear in stdout while running unit tests would be quite useful in plugin debugging phase.

Describe the solution you'd like
A solution would be to provide log function wrappers that would call both the Logger and the printf() function when a compile time flag is on so that the log is visible in both places.
Example of wrapper function for one log level:

namespace LogUtils {
    template<class... Args>
    void log_debug(const std::string& format, Args&&... args) {  
        #ifdef UNIT_TEST
        printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
        fflush(stdout);
        #endif
        Logger::getLogger()->debug(format.c_str(), std::forward<Args>(args)...);
    }
}

Example of precompiler definition to add to the tests CMakeLists:

target_compile_definitions(${PROJECT_NAME} PRIVATE UNIT_TEST)

Example of usage:

LogUtils::log_debug("Logging some debug data: %s", data.c_str());

Describe alternatives you've considered
A more advanced approach would be to further upgrade the Logger class into handling multiple types of appenders so that the user could define its own appenders when configuring Fledge (see examples of implementation in https://log4cpp.sourceforge.net/).
This was not retained so far as it would have more impact on existing code and may result in interface evolutions.

Additional context
N/A