Skip to content

yanminhui/tinylog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TinyLog

GitHub Author GitHub License GitHub Releases GitHub Platform GitHub Issues

English 简体中文

Design goals

Thare are myriads of logging libraries of C++ out there. Many of them have extensive functionality, often complex configuration, and provide interfaces in a shared library. Our class had these design goals:

  • Trivial integration: Our whole code base consistes of a single header file tinylog.hpp. That's it. No library, no dependencies, no complex build system.

  • Custom output sink: Customize output sink by inheriting from the class basic_sink. Console color sink and file sink are available by default.

  • Custom layout: You can specify your own layout specifiers. In order to do that you can use template parameters in output sinks.

  • Formatting: Logging like printf is available. This is done by using l[w]printf marcos. Logging like std::cout is recommended.

  • STL logging: You can log STL template classes including most containers, and containers may be supported in other libraries.

  • Memory dump: Dump byte streams by using hexdump.

  • Internationalized: wchar_t and char string output is supported.

  • Thread and type safe: By default thread-safety is enabled, you can disable it by defining TINYLOG_USE_SINGLE_THREAD.

Supported compilers

Currently, the following compilers are known to work:

  • GCC 5.4.0 / Ubuntu 16.04.3 LTS
  • Clang 9.1.0 / MacOS 10.13.4
  • Microsoft Visual C++ 2015 / Windows 7

Integration

tinylog.hpp is the single required file in include. You need to add

#include "tinylog.hpp"

// use namespace tinylog
using namespace tinylog;

to the files you want to logging and set the necessary switches to enable C++11 (e.g., -std=c++11 for GCC and Clang).

Example

#include <locale>
#include <map>
#include <string>

#include "tinylog.hpp"

int main(int argc, char* argv[])
{
    using namespace tinylog;

    //--------------|
    // Setting      |
    //--------------|
    std::locale loc("");
    std::locale::global(loc);

    // Regist logger
    auto inst = registry::create_logger();

    // Setup sink: @see std::make_shared<>
    inst->create_sink<sink::console_sink>();

    constexpr auto max_file_size = 5 * 1024 * 1024; // 5MB
    inst->create_sink<sink::u8_file_sink>("default.log", max_file_size);

    // Filter level
    inst->set_level(debug);

    // [option] Output title
    dlout(inst, debug) << logger::title("TinyLog");

    //--------------|
    // Logging      |
    //--------------|
    // Normal text
    lout(info) << "Weclome to TinyLog !!!" << std::endl;

    // STL container
    std::map<std::string, size_t> const ages = {{"tl", 1}, {"js", 5}};
    lout(warn) << "ages: " << ages << std::endl;

    // Hex string
    constexpr auto text = "Bravo! The job has been done well.";
    lout(error) << "hexdump: " << text << "\n" << hexdump(text);

    return 0;
}

narrow version see example_narrow unicode version see example_unicode

example_narrow

License

The class is licensed under the MIT License:

Copyright © 2018 颜闽辉

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contact

If you have questions regarding the library, I would like to invite you to open an issue at GitHub. Opening an issue at GitHub allows other users and contributors to this library to collaborate.