ForkCerebrasCerebraspublished Mar 19, 2018seen 5d

Cerebras/g3log

forked from KjellKod/g3log

Open original ↗

Captured source

source ↗
published Mar 19, 2018seen 5dcaptured 13hhttp 200method plain

Cerebras/g3log

Description: G3log is an asynchronous, "crash safe", logger that is easy to use with default logging sinks or you can add your own. G3log is made with plain C++11 with no external libraries (except gtest used for unit tests). G3log is made to be cross-platform, currently running on OSX, Windows and several Linux distros. See Readme below for details of usage.

Language: C++

License: Unlicense

Stars: 2

Forks: 3

Open issues: 0

Created: 2018-03-19T16:01:54Z

Pushed: 2021-02-07T06:04:27Z

Default branch: master

Fork: yes

Parent repository: KjellKod/g3log

Archived: no

README:

G3log : Asynchronous logger with Dynamic Sinks

EXAMPLE USAGE

Optional to use either streaming or printf-like syntax

LOG(INFO) more)
#include
#include

struct CustomSink {

// Linux xterm color
// http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
enum FG_Color {YELLOW = 33, RED = 31, GREEN=32, WHITE = 97};

FG_Color GetColor(const LEVELS level) const {
if (level.value == WARNING.value) { return YELLOW; }
if (level.value == DEBUG.value) { return GREEN; }
if (g3::internal::wasFatal(level)) { return RED; }

return WHITE;
}

void ReceiveLogMessage(g3::LogMessageMover logEntry) {
auto level = logEntry.get()._level;
auto color = GetColor(level);

std::cout addSink(std::make_unique(),
&CustomSink::ReceiveLogMessage);

More sinks can be found in the repository [github.com/KjellKod/g3sinks](https://github.com/KjellKod/g3sinks).

#Code Examples Example usage where a custom sink is added. A function is called though the sink handler to the actual sink object.

// main.cpp
#include
#include
#include

#include "CustomSink.h"

int main(int argc, char**argv) {
using namespace g3;
std::unique_ptr logworker{ LogWorker::createLogWorker() };
auto sinkHandle = logworker->addSink(std::make_unique(),
&CustomSink::ReceiveLogMessage);

// initialize the logger before it can receive LOG calls
initializeLogging(logworker.get());
LOG(WARNING) call below";

// You can call in a thread safe manner public functions on your sink
// The call is asynchronously executed on your custom sink.
std::future received = sinkHandle->call(&CustomSink::Foo,
param1, param2);

// If the LogWorker is initialized then at scope exit the g3::shutDownLogging() will be called.
// This is important since it protects from LOG calls from static or other entities that will go out of
// scope at a later time.
//
// It can also be called manually:
g3::shutDownLogging();
}

// some_file.cpp : To show how easy it is to get the logger to work
// in other parts of your software

#include

void SomeFunction() {
...
LOG(INFO)
#include
#include

#include "CustomSink.h"

int main(int argc, char**argv) {
using namespace g3;
auto worker = LogWorker::createLogWorker();
auto defaultHandler = worker->addDefaultLogger(argv[0],
path_to_log_file);

// logger is initialized
g3::initializeLogging(worker.get());

LOG(DEBUG) addSink(std::make_unique(),
&CustomSink::ReceiveLogMessage);

...
}

BUILDING g3log:

----------- The default is to build an example binary 'g3log-FATAL-contract' and 'g3log-FATAL-sigsegv'. I suggest you start with that, run it and view the created log also.

If you are interested in the performance or unit tests then you can enable the creation of them in the g3log/CMakeLists.txt file. See that file for more details

cd g3log
cd 3rdParty/gtest
unzip gtest-1.7.0.zip
cd ../../
mkdir build
cd build

Configuring for installing on nix (OSX, Linux, MinGW)

Default install prefix on Linux is /usr/local To change it please set CPACK_PACKAGING_INSTALL_PREFIX

cmake -DCPACK_PACKAGING_INSTALL_PREFIX= ...

Building on Linux

cmake -DCMAKE_BUILD_TYPE=Release ..
make

Installing On Linux

sudo make install

Alternative on Debian

make package
sudo dpkg -i g3log--Linux.deb

Building on MinGW

cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..
make

Installing on MinGW

make install

Alternative using NSIS

make package
g3log--win32.exe

Building on Windows

Please use the Visual Studio 12 (2013) command prompt "Developer command prompt"

cmake -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 12" ..
msbuild g3log.sln /p:Configuration=Release

Building on *nix with Clang

cmake -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release ..
make

API description

Most of the API that you need for using g3log is described in this readme. For more API documentation and examples please continue to read the [API readme](API.markdown). Examples of what you will find here are:

  • Sink creation and utilization
  • Logging levels
  • disable/enabled levels at runtime
  • custom logging levels
  • Fatal handling
  • custom fatal handling
  • pre fatal hook
  • override of signal handling
  • disable fatal handling
  • LOG calls
  • CHECK calls

#Performance G3log aims to keep all background logging to sinks with as little log overhead as possible to the logging sink and with as small "worst case latency" as possible. For this reason g3log is a good logger for many systems that deal with critical tasks. Depending on platform the average logging overhead will differ. On my laptop the average call, when doing extreme performance testing, will be about ~2 us.

The worst case latency is kept stabile with no extreme peaks, in spite of any sudden extreme pressure. I have a blog post regarding comparing worst case latency for g3log and other loggers which might be of interest. You can find it here: https://kjellkod.wordpress.com/2015/06/30/the-worlds-fastest-logger-vs-g3log/

Feedback

If you like this logger (or not) it would be nice with some feedback. That way I can improve g3log and g2log and it is also nice to see if someone is using it.

If you have ANY questions or problems please do not hesitate in contacting me on my blog http://kjellkod.wordpress.com/2011/11/17/kjellkods-g2log-vs-googles-glog-are-asynchronous-loggers-taking-over or at ``Hedstrom at KjellKod dot cc

Say Thanks

This logger is available for free and all of its source code is public domain. A great way of saying thanks is to send a donation. It would go a long way not only to show your support but also to boost continued development.

  • $5 for a cup of coffee
  • $10 for pizza
  • $25 for a lunch or two
  • $100 for a date night with my wife (which buys family credit for evening coding)
  • $$$ for upgrading my development environment
  • $$$$ :)

Cheers…

Excerpt shown — open the source for the full document.