Coder Social home page Coder Social logo

minilog's Introduction

🪵 minilog MIT Build Status

Minimalistic logging library with threads and manual callstacks.

Features

  • Easy to use (2 files, C-style interface)
  • Multiple outputs (a text log file, an HTML log file, Android logcat, system console with colors)
  • Thread-safe (write to log from multiple threads, set thread names)
  • Cross-platform (Windows, Linux, OS X, Android)
  • Callbacks (if you want to intercept formatted log messages)
  • C++17

Basic usage

#include "minilog/minilog.h"
...
minilog::initialize("log.txt", {});
minilog::log(minilog::Log, "Hello world!");
minilog::log(minilog::Log, "%s", "Hello again!");
minilog::log(minilog::Warning, "Warning!!!");
minilog::deinitialize();

Console output:

image

HTML log + manual callstack + multiple threads

All calls to log(), callstackPushProc(), callstackPopProc() are thread-safe. Furthermore, callstacks are per thread.

void testThread()
{
	minilog::initialize("log_thread.html", { .htmlLog = true });
	std::thread t([]()
	{
		minilog::threadNameSet("OtherThread");
		minilog::callstackPushProc("std::thread->");
		minilog::log(minilog::Log, "Hello from another thread!");
		minilog::log(minilog::Warning, "Warning from another thread!!!");
		minilog::callstackPopProc();
	});
	minilog::callstackPushProc("testThread()->");
	minilog::log(minilog::Log, "Hello world!");
	minilog::log(minilog::Warning, "Warning!!!");
	minilog::callstackPopProc();
	t.join();
	minilog::deinitialize();
}

If you want, you can use optional LLOGL(), LLOGW(), LLOGD() macros instead of calling minilog::log() directly.

HTML output:

image

You can also use the CallstackScope class to manage your callstack in RAII-style.

Intercept formatted messages

If you have a GameConsole class which can display messages within your in-game UI, you can intercept logs the following way:

minilog::LogCallback cb = { .userData = this };
cb.funcs[minilog::Log] = [](void* data, const char* msg) { reinterpret_cast<GameConsole*>(data)->Display(msg); };
cb.funcs[minilog::Warning] = [](void* data, const char* msg) { reinterpret_cast<GameConsole*>(data)->DisplayError(msg); };
cb.funcs[minilog::FatalError] = [](void* data, const char* msg) { reinterpret_cast<GameConsole*>(data)->DisplayError(msg); };
minilog::callbackAdd(cb);

All callback invocations are guarded by a mutex and will not happen concurrently (but may be invoked from multiple threads).

minilog's People

Contributors

corporateshark 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.