Skip to content

FlexiLog: A lightweight C++ logging library designed for debugging. Features template-based log control for zero-overhead disabling, rich enumeration options for detailed debug information (e.g., line number, filename, CPU usage), customizable print restrictions (max count, frequency, complex conditions), and no external dependencies.

License

Notifications You must be signed in to change notification settings

442701102/FlexiLog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlexiLog

License C++ Build Status

FlexiLog is a lightweight, high-performance C++ logging library designed to facilitate efficient debugging and monitoring. Unlike other popular logging libraries, FlexiLog offers compile-time log control with zero-overhead disabling, rich enumeration options for detailed debug information, customizable print restrictions, and operates independently without any external dependencies.

Table of Contents

Features

  • Template-Based Log Control: Enable or disable logging at compile time with zero runtime overhead.
  • Rich Enumeration Options: Choose from a variety of debug information such as line number, filename, CPU usage, and more.
  • Customizable Print Restrictions: Limit the number of log prints, control print frequency, and set complex conditions to manage log output effectively.
  • User-Defined Debug Information: Extend the library with custom debug details tailored to your project's needs.
  • No External Dependencies: Pure C++ implementation ensuring easy integration into any project without additional library requirements.
  • High Performance: Optimized for minimal impact on application performance, especially when logging is disabled.

Installation

Prerequisites

  • C++17 or later
  • Compatible with major compilers like GCC, Clang, and MSVC

Using CMake

  1. Clone the Repository

    git clone https://github.com/yourusername/FlexiLog.git
    cd FlexiLog
  2. Build and Install

    mkdir build
    cd build
    cmake ..
    make
    sudo make install

Manual Installation

  1. Download Source Files

    Download the FlexiLog.hpp header file from the repository.

  2. Include in Your Project

    Place the FlexiLog.hpp file into your project's include directory and include it in your source files:

    #include "FlexiLog.hpp"

Usage

Basic Logging

#include "FlexiLog.hpp"

int main() {
    FlexiLog::log(FlexiLog::LogLevel::INFO, "Application started.");
    FlexiLog::log(FlexiLog::LogLevel::DEBUG, "Debugging value: {}", 42);
    FlexiLog::log(FlexiLog::LogLevel::ERROR, "An error occurred: {}", "Out of memory");
    return 0;
}

Template-Based Log Control

Enable or disable logging at compile time by defining or undefining the ENABLE_FLEXILOG macro.

// To enable logging
#define ENABLE_FLEXILOG
#include "FlexiLog.hpp"

// To disable logging
// #define ENABLE_FLEXILOG
#include "FlexiLog.hpp"

When logging is disabled, all FlexiLog::log calls are removed at compile time, ensuring no performance overhead.

Rich Debug Information

Choose the debug information to include in your logs using enumerations.

#include "FlexiLog.hpp"

int main() {
    FlexiLog::setDetails({
        FlexiLog::LogDetail::LineNumber,
        FlexiLog::LogDetail::FileName,
        FlexiLog::LogDetail::CPUUsage
    });

    FlexiLog::log(FlexiLog::LogLevel::INFO, "Detailed log entry.");
    return 0;
}

Print Restrictions

Control the number of times a log message is printed and its frequency.

#include "FlexiLog.hpp"

int main() {
    FlexiLog::setMaxPrintCount(5); // Maximum 5 prints
    FlexiLog::setPrintFrequency(10); // Maximum 10 prints per second

    for(int i = 0; i < 100; ++i) {
        FlexiLog::log(FlexiLog::LogLevel::DEBUG, "This will be printed up to 5 times at 10 prints per second.");
    }
    return 0;
}

Custom Debug Information

Add your own debug information by registering custom functions.

#include "FlexiLog.hpp"

std::string getCustomInfo() {
    // Implement your custom info retrieval
    return "CustomInfo";
}

int main() {
    FlexiLog::addCustomDetail(getCustomInfo);

    FlexiLog::log(FlexiLog::LogLevel::INFO, "Log with custom info.");
    return 0;
}

API Reference

FlexiLog::LogLevel

Enumerates the severity levels of logs.

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

FlexiLog::LogDetail

Enumerates the types of debug information available.

  • LineNumber
  • FileName
  • FunctionName
  • Timestamp
  • CPUUsage
  • MemoryUsage
  • Custom

FlexiLog::log

Logs a message with the specified log level.

template<typename... Args>
static void log(LogLevel level, const std::string& message, Args&&... args);

FlexiLog::setDetails

Sets the details to include in each log entry.

static void setDetails(const std::vector<LogDetail>& details);

FlexiLog::addCustomDetail

Registers a custom function to retrieve additional debug information.

static void addCustomDetail(std::function<std::string()> customFunc);

FlexiLog::setMaxPrintCount

Sets the maximum number of times a specific log message can be printed.

static void setMaxPrintCount(int count);

FlexiLog::setPrintFrequency

Sets the maximum number of log prints allowed per second.

static void setPrintFrequency(int maxPerSecond);

FlexiLog::setPrintCondition

Sets a custom condition that must be met for a log to be printed.

static void setPrintCondition(std::function<bool()> condition);

Examples

Example 1: Basic Logging

#include "FlexiLog.hpp"

int main() {
    FlexiLog::log(FlexiLog::LogLevel::INFO, "Starting the application...");
    FlexiLog::log(FlexiLog::LogLevel::DEBUG, "Variable x = {}", 10);
    FlexiLog::log(FlexiLog::LogLevel::ERROR, "Failed to load resource: {}", "config.yaml");
    return 0;
}

Example 2: Enabling Detailed Debug Information

#include "FlexiLog.hpp"

int main() {
    FlexiLog::setDetails({
        FlexiLog::LogDetail::Timestamp,
        FlexiLog::LogDetail::FileName,
        FlexiLog::LogDetail::LineNumber,
        FlexiLog::LogDetail::CPUUsage
    });

    FlexiLog::log(FlexiLog::LogLevel::INFO, "Application initialized with detailed debug info.");
    return 0;
}

Example 3: Limiting Log Prints

#include "FlexiLog.hpp"

int main() {
    FlexiLog::setMaxPrintCount(3);
    FlexiLog::setPrintFrequency(2); // 2 prints per second

    for(int i = 0; i < 10; ++i) {
        FlexiLog::log(FlexiLog::LogLevel::DEBUG, "This debug message will be limited.");
    }

    return 0;
}

Example 4: Adding Custom Debug Information

#include "FlexiLog.hpp"

std::string getUserID() {
    // Assume we retrieve a user ID from somewhere
    return "User123";
}

int main() {
    FlexiLog::addCustomDetail(getUserID);

    FlexiLog::log(FlexiLog::LogLevel::INFO, "User action logged.");
    return 0;
}

Contributing

Contributions are welcome! Whether it's reporting bugs, suggesting features, or submitting pull requests, your input helps make FlexiLog better.

How to Contribute

  1. Fork the Repository

    Click the "Fork" button at the top right of the repository page.

  2. Clone Your Fork

    git clone https://github.com/yourusername/FlexiLog.git
    cd FlexiLog
  3. Create a New Branch

    git checkout -b feature/YourFeatureName
  4. Make Your Changes

    Implement your feature or fix bugs.

  5. Commit Your Changes

    git commit -m "Add feature: YourFeatureName"
  6. Push to Your Fork

    git push origin feature/YourFeatureName
  7. Create a Pull Request

    Go to the original repository and create a pull request from your fork.

Coding Standards

  • Follow C++ best practices and coding standards.
  • Ensure code is well-documented and readable.
  • Write unit tests for new features or bug fixes.
  • Update the README.md if necessary.

License

This project is licensed under the MIT License.


FlexiLog aims to provide developers with a powerful yet simple logging solution tailored for high-performance C++ applications. By focusing on flexibility, efficiency, and ease of use, FlexiLog stands out as a reliable tool for debugging and monitoring your projects.

For any questions, issues, or contributions, feel free to open an issue or submit a pull request on GitHub.


About

FlexiLog: A lightweight C++ logging library designed for debugging. Features template-based log control for zero-overhead disabling, rich enumeration options for detailed debug information (e.g., line number, filename, CPU usage), customizable print restrictions (max count, frequency, complex conditions), and no external dependencies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published