loguru

repository·master·Indexed 24 days ago

https://github.com/emilk/loguru

A lightweight, flexible, and fast C++ logging library designed for human-readable and grep-searchable logs. It supports printf, stream-style, and fmtlib formatting, and provides features such as command-line verbosity control, indented logging scopes, and error context capture for crashes. The library consists of two files, loguru.hpp and loguru.cpp, and requires C++11.

Tokens
1.3K
Snippets
5
Records
9
Agent score
34%

What's inside loguru

  1. Use Scopes for indented logging

    master

    Loguru supports indentation using scope macros. This is useful for visualizing nested logic or loops in your log files. When a scope is entered, subsequent log messages are indented; when the scope is exited, indentation is removed.

    int main(int argc, char* argv[]) {
        loguru::init(argc, argv);
        LOG_SCOPE_FUNCTION(INFO);
        LOG_F(INFO, "Doing some stuff...");
        for (int i=0; i<2; ++i) {
            VLOG_SCOPE_F(1, "Iteration %d", i);
            // ... logic ...
        }
        return 0;
    }
  2. Basic Loguru usage and initialization

    master

    To use Loguru, initialize it at the start of your main function using loguru::init(argc, argv). This enables command-line verbosity control (e.g., using -v flag).

    You can configure file outputs with different verbosity levels and modes (loguru::Append or loguru::Truncate).

    Key API calls:

    • loguru::init(argc, argv): Initializes the library.
    • loguru::add_file(filename, mode, verbosity): Adds a log file output.
    • loguru::g_stderr_verbosity: Controls which levels are shown on stderr.
  3. Use CMake to add Loguru

    master

    Loguru can be integrated into existing CMake projects using three standard methods:

    1. add_subdirectory()
    2. FetchContent()
    3. find_package()

    Refer to the loguru_cmake_example/CMakeLists.txt file in the repository for a concrete demonstration.

  4. Integrate Loguru into your C++ project

    master

    Loguru is a lightweight library consisting of only two files: loguru.hpp and loguru.cpp. You can integrate it in two ways:

    1. Build and link: Compile loguru.cpp along with your project files.
    2. Single-file inclusion: Add #include <loguru.cpp> in one of your own .cpp files to include the implementation directly.

    When compiling, ensure you use -std=c++11 and link against -lpthread and -ldl on relevant environments.

  5. Enable fmtlib support

    master

    To use fmtlib for formatting instead of printf-style, define LOGURU_USE_FMTLIB 1 before including loguru.hpp. You must also ensure fmtlib is available in your include path and linked correctly (or use FMT_HEADER_ONLY).

    #define LOGURU_USE_FMTLIB 1
    #include <loguru.hpp>
  6. Capture error context for crashes

    master

    The ERROR_CONTEXT macro allows you to associate specific data with a code block. This data is only printed if the program subsequently crashes. This is ideal for capturing local variables like filenames or IDs that are useful for debugging a crash but too noisy for regular logging.

    void process_file(const char* filename)
    {
        // 'filename' will only be logged if parse_file() causes a crash
        ERROR_CONTEXT("filename", filename);
        parse_file(filename);
    }
  7. Log with stream-style (std::cout) formatting

    master

    If you prefer std::ostream style syntax, define LOGURU_WITH_STREAMS before including the header. Note that this will increase compilation time slightly as it includes <sstream>.

    #define LOGURU_WITH_STREAMS 1
    #include <loguru.hpp>
    
    LOG_S(INFO) << "Look at my custom object: " << a.cross(b);
    CHECK_EQ_S(pi, 3.14) << "Maybe it is closer to " << M_PI;
  8. Use assertions and error handling

    master

    Loguru provides powerful assertion macros that can be used to validate program state. If an assertion fails, Loguru prints a stack trace and aborts the program by default.

    Macros:

    • CHECK_F(condition, format, ...): Asserts condition is true.
    • CHECK_GT_F(a, b): Asserts a > b.
    • CHECK_EQ_F(a, b, format, ...): Asserts a == b.
    • ABORT_F(format, ...): Immediately aborts the program.

    You can customize the fatal handler to throw exceptions instead of aborting.

  9. Log with printf-style formatting

    master

    By default, Loguru uses printf-style formatting. This is highly efficient and allows for precise control over output.

    Macros:

    • LOG_F(level, format, ...): Standard log message.
    • VLOG_F(level, format, ...): Dynamic verbosity log (level 0-9).
    • LOG_IF_F(level, condition, format, ...): Logs only if the condition is true.
    • DLOG_F(level, format, ...): Only logs in debug builds.
    • DCHECK_F(condition): Debug-only assertion (only checked if NDEBUG is not defined).
    LOG_F(INFO, "I'm hungry for some %.3f!", 3.14159);
    LOG_F(2, "Will only show if verbosity is 2 or higher");
    VLOG_F(get_log_level(), "Use vlog for dynamic log level");
    LOG_IF_F(ERROR, badness, "Will only show if badness happens");
    
    // Debug-only versions
    DLOG_F(INFO, "Only written in debug-builds");
    DCHECK_F(expensive_check(x));