Skip to content
victor edited this page Aug 12, 2025 · 2 revisions

🛍️ Smart_Store logger

Understanding LOG_CONTEXT in Smart_Store

What It Does

The LOG_CONTEXT macro is a custom logging utility used in Smart_Store to record detailed runtime information. It helps developers and maintainers trace issues, monitor system behavior, and debug efficiently

Log Structure

  • LogLevel::LEVEL: Specifies the severity or type of the log. Common levels include:
  • INFO: General system events (e.g. startup, shutdown)
  • DEBUG: Developer-level details for troubleshooting
  • WARNING: Non-critical issues that may need attention
  • ERR: Errors that affect functionality
  • DISPLAY: User-facing or final status messages
  • "Message": A descriptive string explaining what’s happening. This should be clear and actionable.
  • ContextData: Additional data that gives context to the log. This could be:
  • An ErrorCode enum (e.g. INVALID_INPUT, ITEM_NOT_FOUND)
  • An exception object (std::current_exception())
  • A pointer (nullptr)
  • A boolean flag (true or false)
  • An optional string hint (std::optionalstd::string)

Why It’s Useful

  • Traceability: Logs include timestamps, function names, and context, making it easy to trace the source of issues.
  • Debugging: Developers can quickly identify what went wrong and where.
  • Monitoring: Helps in observing system health and behavior over time.
  • Documentation: Serves as a runtime record of system events and decisions.

Here is a practical example of how to use it:

// Include Smart_Store's item management system
#include "t_manager/ItemManager.h"

// Include Smart_Store's logging system
#include "err_log/Logger.hpp"

// Standard library headers
#include <iostream>
#include <string>

/**
 * Attempts to load an item by name.
 * Logs an error if the name is empty, and a warning if the item is not found.
 */
void loadItem(const std::string& itemName) {
    if (itemName.empty()) {
        // Log error: item name is invalid
        LOG_CONTEXT(LogLevel::ERR, "Failed to load item", ErrorCode::INVALID_INPUT);
        return;
    }

    // Simulate item lookup failure
    bool found = false;
    if (!found) {
        // Log warning: item not found in store
        LOG_CONTEXT(LogLevel::WARNING, "Item not found in store", false);
        return;
    }
}

/**
 * Simulates a risky operation that throws an exception.
 * Catches and logs the exception.
 */
void riskyOperation() {
    try {
        // Simulate a runtime error
        throw std::runtime_error("Something went wrong");
    } catch (...) {
        // Log error: exception caught
        LOG_CONTEXT(LogLevel::ERR, "Exception caught during operation", 
                   std::make_exception_ptr(std::runtime_error("Something went wrong")));
    }
}

/**
 * Checks if a pointer is null.
 * Logs an error if the pointer is null.
 */
void* checkPointer(int* ptr) {
    if (ptr == nullptr) {
        // Log error: null pointer detected
        LOG_CONTEXT(LogLevel::ERR, "Null pointer detected", nullptr);
        // Return nullptr to indicate the error
        return nullptr;
    }

    // Return the original pointer cast to void* if it's valid
    return static_cast<void*>(ptr);
}

/**
 * Simulates a configuration check.
 * Logs a warning with an optional hint.
 */
std::optional<std::string> configCheck() {
    std::optional<std::string> hint = "Missing configuration value";
    // Log warning: configuration issue detected
    LOG_CONTEXT(LogLevel::WARNING, "Configuration issue detected", hint);
    // Return the hint for further processing
    return hint;
}

/**
 * Logs the status of a boolean flag.
 * Useful for debugging conditional logic.
 */
bool booleanFlagTest(bool flag) {
    // Log debug: flag status
    LOG_CONTEXT(LogLevel::DEBUG, "Flag status check", flag);
    // Return the flag status
    return flag;
}

/*
* A simple operation that does not throw exceptions.
* Demonstrates logging without context.
*/
void voidOperation(){
    //Log with message only
    LOG_CONTEXT(LogLevel::INFO, "Logging only message to the console.", {});
}

/**
 * Entry point of the Smart_Store system.
 * Logs system start, runs test scenarios, and logs completion.
 */
int main() {
    // Log info: system startup
    LOG_CONTEXT(LogLevel::INFO, "Smart_Store system started", {});

    // You can equally use this to log without context:
   std::cout << Logger::getColorCode(LogColor::CYAN) + "Smart_Store system started" + Logger::getColorCode(LogColor::RESET);
   std::cout << Logger::getColorCode(LogColor::RED) + "Smart_Store system started" + Logger::getColorCode(LogColor::RESET);
   
    voidOperation();              // Test: simple operation without exceptions
    loadItem("");                    // Test: empty item name and item not found
    riskyOperation();               // Test: exception handling
    checkPointer(nullptr);         // Test: null pointer check
    configCheck();                 // Test: configuration warning
    booleanFlagTest(true);         // Test: boolean flag logging

    // Log display: system shutdown
    LOG_CONTEXT(LogLevel::DISPLAY, "Smart_Store system completed", ErrorCode::GENERIC_CODE);

    return 0;
}

🛍️ Smart_Store

::| Development Guide |::

If you encounter any issues and bugs, please reach out via email or GitHub issues.

Clone this wiki locally