Google Logging (glog)

repository·master·Indexed 27 days ago

https://github.com/google/glog

A C++14 library for application-level logging providing stream-based APIs and helper macros. It features severity levels (INFO, WARNING, ERROR, FATAL), verbose logging (VLOG), runtime condition checks via CHECK macros, and failure signal handling for diagnostic stack traces. The library supports configuration via command-line flags or environment variables and can be built using Bazel or CMake. Note: This project is deprecated and scheduled for archiving on 2025-06-30.

Tokens
5.9K
Snippets
18
Records
47
Agent score
93%

What's inside glog

  1. Enable the automatic log cleaner

    master

    You can enable a background process that automatically removes old log files. When enabled, glog checks for overdue logs whenever a flush is performed. A log file is considered overdue if its last modified time is greater than the duration specified. The files are removed using unlink().

    using namespace std::chrono_literals;
    // Keep logs for 3 days
    google::EnableLogCleaner(24h * 3);
  2. Build glog using CMake

    master
    You can build glog using CMake on most platforms. The standard workflow involves configuring the build tree with cmake -S . -B build, building the project with cmake --build build, and optionally running tests or installing the library.
  3. Install glog using vcpkg

    master

    You can install glog using the vcpkg dependency manager. This requires cloning the vcpkg repository, bootstrapping it, integrating it with your environment, and then installing the glog port.

    git clone https://github.com/Microsoft/vcpkg.git
    cd vcpkg
    ./bootstrap-vcpkg.sh
    ./vcpkg integrate install
    ./vcpkg install glog
  4. Choose a stack-unwinder for glog

    master

    Depending on your system constraints and usage of InstallFailureSignalHandler(), you can choose from three stack-unwinder options:

    1. libunwind (Recommended): The preferred choice for 64-bit systems to avoid deadlocks when using InstallFailureSignalHandler().
    2. glibc Built-in Stack-unwinder: The default choice if libunwind is not detected. Use this only if you do not use InstallFailureSignalHandler() or if you are willing to risk rare deadlocks during signal handling.
    3. Frame Pointer based Stack-unwinder: An alternative that requires your application, glog, and system libraries (like libc) to all be compiled with a frame pointer. Note that this is not the default for x86-64.
  5. Install glog using Conan

    master

    You can install glog using the Conan package manager. First, ensure Conan is installed via pip, then use the conan install command specifying the conancenter remote and the desired glog version.

    pip install conan
    conan install -r conancenter glog/<glog-version>@
  6. Submit a patch to glog

    master

    Follow these steps to contribute code to the project:

    1. Open an issue: Describe the bug or feature you intend to address. Mention in the issue that you plan to work on it so it can be assigned to you.
    2. Fork and Branch: Fork the repository and create a new branch for your changes. Use separate branches for different groups of changes to ensure pull requests are focused.
    3. Commit: Use well-formed commit messages for each change to maintain project consistency.
    4. Pull Request: Push your commits to your fork and submit a pull request.
  7. Integrate glog into a CMake project using find_package

    master

    If glog has been built using CMake or installed via a package manager, you can integrate it into your project using find_package. This method automatically handles necessary compile definitions and options for your target. Link against the glog::glog target.

    cmake_minimum_required (VERSION 3.16)
    project (myproj VERSION 1.0)
    
    find_package (glog 0.8.0 REQUIRED)
    
    add_executable (myapp main.cpp)
    target_link_libraries (myapp glog::glog)
  8. Integrate glog into a CMake project using add_subdirectory

    master

    To include glog directly from a subdirectory of your project, use the add_subdirectory command instead of find_package. In this configuration, the glog::glog target acts as an ALIAS to the main glog library target.

    cmake_minimum_required (VERSION 3.16)
    project (myproj VERSION 1.0)
    
    # Replace find_package with add_subdirectory
    add_subdirectory(path/to/glog)
    
    add_executable (myapp main.cpp)
    target_link_libraries (myapp glog::glog)
  9. Install the default failure signal handler

    master
    To dump useful diagnostic information (like stack traces) when your program crashes on signals such as SIGSEGV, install the default signal handler using google::InstallFailureSignalHandler(). This is particularly useful for debugging segmentation faults.
  10. Sign the Contributor License Agreement (CLA)

    master

    All contributions to Google projects require a signed Contributor License Agreement (CLA). This grants Google permission to use and redistribute your contributions.

    • Individuals: If you are writing original code and own the intellectual property, sign the individual CLA.
    • Corporations: If you are contributing on behalf of a company, sign the corporate CLA.

    Note: If you have already submitted a CLA for another Google project, you likely do not need to submit it again.

  11. Add glog to a Bazel project

    master

    To use glog in a Bazel-based project, add the glog dependency and an archive_override to your MODULE.bazel file. Once configured, you can depend on @glog//:glog in your cc_binary or cc_library rules and include the header using #include <glog/logging.h>.

    bazel_dep(name = "glog")
    
    archive_override(
        module_name = "glog",
        urls = "https://github.com/google/glog/archive/cc0de6c200375b33d907ee7632eee2f173b33a09.tar.gz",
        strip_prefix = "glog-cc0de6c200375b33d907ee7632eee2f173b33a09",
        integrity = "sha256-rUrv4EBkdc+4Wbhfxp+KoRstlj2Iw842/OpLfDq0ivg=",
    )
    cc_binary(
        name = "main",
        srcs = ["main.cc"],
        deps = ["@glog//:glog"],
    )