Skip to content

ut‐log: Overview: UT Log (ut_log.h)

Ulrond edited this page Jul 22, 2024 · 1 revision

Purpose:

This module simplifies logging during unit testing. It offers:

  • Formatted Output: Provides a set of macros (UT_LOG, UT_LOG_STEP, etc.) to easily create log messages with different levels (info, debug, warning, error) and prefixes.
  • Coloured Output: Uses ANSI escape codes to add colours to log messages in the console, making them more readable.
  • Log File: Automatically writes log messages to a file, whose path can be configured.

Key Features:

  • Logging Macros:
    • UT_LOG: Basic logging with file, line, and optional prefix.
    • UT_LOG_PREFIX: Logging with a custom prefix.
    • UT_LOG_STEP, UT_LOG_INFO, UT_LOG_DEBUG, UT_LOG_WARNING, UT_LOG_ERROR: Macros for specific log levels, each with a distinct color.
    • UT_LOG_ASSERT: Special macro for logging assertion failures.
  • Log File Management:
    • UT_log_setLogFilePath: Sets the path for the log file.
    • UT_log_getLogFilename: Gets the name of the active log file.
  • Core Logging Functions:
    • UT_log: Internal function to handle logging, including timestamp, file, line, and function name.
    • UT_logPrefix: Same as UT_log, but with an added prefix.

Example Usage:

#include <ut_log.h>

void testFunction() {
    UT_LOG("Starting test function.");           // Basic log
    UT_LOG_STEP("Executing step 1.");             // Log a test step
    UT_LOG_INFO("Some information to log.");      // Informational log
    // ... rest of test logic

    if (errorCondition) {
        UT_LOG_ERROR("An error occurred.");       // Error log
    }
}

int main() {
    UT_log_setLogFilePath("/path/to/logfile.txt"); // Set log file path
    testFunction();
    // ... rest of test code
    return 0;
}

How It Works:

  1. Macros: The logging macros (UT_LOG, etc.) simplify the process of calling the underlying UT_log or UT_logPrefix functions. They automatically insert file, line, and other relevant information into the log messages.
  2. Log Functions: UT_log and UT_logPrefix are the core functions that format and write log messages to the console (with colours) and the specified log file.
  3. Log File: The module maintains a file for storing all log messages for later analysis.

Additional Notes:

  • Colour Support: The coloured output will only work in terminals that support ANSI escape codes.
  • Customization: The log file path can be customized, and you can add more macros for specific log levels if needed.