GithubHelp home page GithubHelp logo

ngrodzitski / logr Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 119 KB

Logger frontend substitution for spdlog, glog, ...

License: BSD 3-Clause "New" or "Revised" License

CMake 7.96% C++ 87.01% Python 4.97% Shell 0.06%

logr's Introduction

Logr

What is it? (TL;DR)

Project status: beta-draft.

This is a experimentsl project showing my approach to build a logger interface used in application development. It is a revised version of what I've been using for the last couple of years in various projects.

This is not a self-contained logging library. Consider it as a "frontend" or a "wrapper" for whatever logging routine of your choice (which can be integrated as a backend). It can be used "as is" in your application or it can be adopted for your case or maybe you can just pick some ideas and came up with a better solution.

Consider the following reasoning behing logr design here.

Build

By itself logr is a header-only library. So talking about build we mean build tests, examples and benchmarks.

Prerequisetes

  • C++17 compiler.
  • CMake.
  • Package manager (conan, or vcpkg).
  • Clone repository with submodules (don't forget --recurse-submodules option).

Logr relies on "backend" logging library. And it comes with ready to go backends for a couple of libraries which are considered to be provided by package manager.

Build on linux with Conan

One of the script in build-scripts folder can be used.

Build with gcc 9.3 and run tests.

./build-scripts/gcc93_conan.sh --run-ctest

Or build it manually:

mkdir build && cd build

# Install conan dependencies.
conan install \
    -s compiler=gcc \
    -s compiler.version=9.3 \
    -s compiler.libcxx=libstdc++11 \
    -s build_type=Release \
    --build missing \
    ..

# Assume you deafult compiler is GCC 9.3.
# Otherwise set it explicitly:
#     export CC=gcc-9
#     export CPP=g++-9
#     export AR=gcc-ar-9
#     export CXX=g++-9

cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . -j $(nproc)
ctest -T test

Build on linux with Conan dockers

Using one of the dockers from docker hub conanio:

docker run -ti --user=$(id -u):$(id -g) \
           -v $(pwd):/sources \
           -w /sources \
           -e CONAN_USER_HOME=/sources/_conan \
           conanio/gcc10 \
           ./build-scripts/gcc_default.sh \
               --run-ctest

Build on windows with Conan

Build with msvc16 (aka vs2019, aka vc142):

rd /s /q _msvc16_conan
mkdir _msvc16_conan
cd _msvc16_conan

REM Precompiled log4cplus in conan-center seems to be broken
REM so we force its build `-b log4cplus`.
conan install -s compiler="Visual Studio" ^
              -s compiler.version=16 ^
              -s build_type=Release ^
              -s compiler.runtime=MD ^
              --build missing ^
              -b log4cplus ^
              ..

cmake -G "Visual Studio 16 2019" ^
      -DCMAKE_BUILD_TYPE=Release ^
      ..

cmake --build . -j 4 --config Release
ctest -T test

Build with Vcpkg

To build tests, examples and benchmarks the following packages should be installed: glog, spdlog, log4cplus, Boost, gtest, benchmark.

Having all dependencies in place one can build the project using the following commands:

mkdir _vcpkg_build
cd _vcpkg_build

cmake -DCMAKE_BUILD_TYPE=Release -DLOGR_PKG_PROVIDER=vcpkg  -DCMAKE_TOOLCHAIN_FILE=PATH_TO/vcpkg/scripts/buildsystems/vcpkg.cmake ..

cmake --build . -j 4 --config Release
ctest -T test

Using logr

A sample code using spdlogger with logr:

#include <spdlog/sinks/stdout_sinks.h>

#include <logr/logr.hpp>
#include <logr/spdlog_backend.hpp>

int main()
{
    int x=42, y=0xFFFF;

    logr::spdlog_logger_t<> logger{
        "console",
        std::make_shared< spdlog::sinks::stdout_sink_st >(),
        logr::log_message_level::trace
    };

    logger.info( "Hello World! [raw message]" );

    logger.info( []() {
        return "Hello World! [message-builder by return]";
    } );

    logger.info( []( auto & out ) {
        fmt::format_to( "Hello World! [message-builder by write-to]" );
    } );

    logger.info( [&]( auto & out ) {
        format_to( out, "X is {}! and Y is {}", x, y );
    } );

    logger.info( [&]( auto out ) {
        format_to( out, "Y is {}! and X is {}", y, x );
    } );

    logger.info( [&x]( auto & out ) {
        out.format_to( "X is {}! and Y is skipped", x );
    } );

    logger.info( [&y]( auto out ) {
        ::fmt::format_to( out.buf(), "Y is {}! and X is skipped", y );
    } );
}

See all examples.

Naming

The original idea behind the library came from restinio in which a custom logger can be integrated to RESTinio to track its internal events. That is pretty much the major idea around Logr, so logr = LOGger from Restinio to show gratitude to great library. And on the other hand it just 4 letters long which is good for namespaces.

License

Logr is distributed under the BSD 3-Clause License.

Libraries used as logger backends are distributed under their license:

logr's People

Contributors

ngrodzitski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.