项目作者: badaix

项目描述 :
Header-only C++ logging library
高级语言: C++
项目地址: git://github.com/badaix/aixlog.git
创建时间: 2017-07-16T15:11:54Z
项目社区:https://github.com/badaix/aixlog

开源协议:MIT License

下载


AixLog

Header-only C++ logging library

Github Releases
Build Status
Language grade: C/C++

Features

  • Single header file implementation
    • Simply include and use it!
    • No dependcies, just vanilla C++11
  • Permissive MIT license
  • Use ostream operator <<
    • Unobtrusive, typesafe and expressive
    • Easy to switch from existing “cout logging”
  • Fancy name
  • Native support for various platforms (through Sinks)
    • Linux, Unix: Syslog
    • macOS: Unified logging (os_log), Syslog (<10.12)
    • Android: Android Log
    • Windows: Event log, OutputDebugString
  • Several Sinks:
    • cout
    • cerr
    • Sink with custom callback function
      • implement your own log sink in a lambda with a single line of code
    • Easy to add more…
  • Manipulators for
    • Different log levels: TRACE, DEBUG, INFO, NOTICE, WARNING, ERROR, FATAL
      LOG(ERROR) << "some error happened!"
      LOG(DEBUG) << "Just a debug message"
    • Conditional logging: simply put COND(bool) in front
      LOG(INFO) << COND(false) << "will not be logged\n"
      LOG(INFO) << COND(true) << "will be logged\n"
    • Support for tags:
      LOG(INFO, "my tag") << "some message"
      …is the same as…
      LOG(INFO) << TAG("my tag") << "some message"
    • Capture function and line number and timestamp
    • Filters: filter by tag and/or by severity what message is logged where, e.g.
      • Add a syslog sink with the filters *:error, SYSLOG:trace to receive only messages with severity error or with tag SYSLOG
      • Add another cout sink with filter *:debug to receive all messages with debug or higher severity
    • Support for colors:
      • Foreground: LOG(INFO) << COLOR(red) << "red foreground"
      • Foreground and background: LOG(INFO) << COLOR(yellow, blue) << "yellow on blue background"

Basic usage

To use AixLog, you must once pass a log sink to AixLog:Log::init, e.g. a SinkCout:

  1. AixLog::Log::init<AixLog::SinkCout>(AixLog::Severity::trace);
  2. LOG(INFO) << "Hello, World!\n";

This will print

  1. 2017-09-28 11-01-16.049 [Info] (main) Hello, World!

There are two overloads for AixLog:Log::init:

  1. one creates and returns an instance of a Sink (as in the example above)

    1. auto sink = AixLog::Log::init<AixLog::SinkCout>(AixLog::Severity::trace);

The sink can be used to change the severity or to remove it from the Logger

  1. one takes a vector of Sinks

    1. auto sink_cout = make_shared<AixLog::SinkCout>(AixLog::Severity::trace);
    2. auto sink_file = make_shared<AixLog::SinkFile>(AixLog::Severity::trace, "logfile.log");
    3. AixLog::Log::init({sink_cout, sink_file});

This will log to both: cout and to file logfile.log

Advanced usage

You can easily fit AixLog to your needs by adding your own sink, that derives from the Sink class. Or even more simple, by using SinkCallback with a custom call back function:

  1. AixLog::Log::init<AixLog::SinkCallback>(AixLog::Severity::trace, AixLog::Type::all,
  2. [](const AixLog::Metadata& metadata, const std::string& message)
  3. {
  4. cout << "Callback:\n\tmsg: " << message << "\n\ttag: " << metadata.tag.text << "\n\tsever: " << AixLog::Log::to_string(metadata.severity) << " (" << (int)metadata.severity << "\n";
  5. if (metadata.timestamp)
  6. cout << "\ttime: " << metadata.timestamp.to_string() << "\n";
  7. if (metadata.function)
  8. cout << "\tfunc: " << metadata.function.name << "\n\tline: " << metadata.function.line << "\n\tfile: " << metadata.function.file << "\n";
  9. }
  10. );
  11. LOG(INFO) << TAG("test") << "Hello, Lambda!\n";

This will print

  1. Callback:
  2. msg: Hello, Lambda!
  3. tag: test
  4. sever: Info (2)
  5. type: normal
  6. time: 2017-09-28 11-46-32.179
  7. func: main
  8. line: 36
  9. file: aixlog_test.cpp

Usage example

  1. #include "aixlog.hpp"
  2. using namespace std;
  3. int main(int argc, char** argv)
  4. {
  5. AixLog::Log::init(
  6. {
  7. /// Log normal (i.e. non-special) logs to SinkCout
  8. make_shared<AixLog::SinkCout>(AixLog::Severity::trace, "cout: %Y-%m-%d %H-%M-%S.#ms [#severity] (#tag) #message"),
  9. /// Log error and higher severity messages to cerr
  10. make_shared<AixLog::SinkCerr>(AixLog::Severity::error, AixLog::Type::all, "cerr: %Y-%m-%d %H-%M-%S.#ms [#severity] (#tag)"),
  11. /// Log special logs to native log (Syslog on Linux, Android Log on Android, EventLog on Windows, Unified logging on Apple)
  12. make_shared<AixLog::SinkNative>("aixlog", AixLog::Severity::trace, AixLog::Type::special),
  13. /// Callback log sink with cout logging in a lambda function
  14. /// Could also do file logging
  15. make_shared<AixLog::SinkCallback>(AixLog::Severity::trace, AixLog::Type::all,
  16. [](const AixLog::Metadata& metadata, const std::string& message)
  17. {
  18. cout << "Callback:\n\tmsg: " << message << "\n\ttag: " << metadata.tag.text << "\n\tsever: " << AixLog::Log::to_string(metadata.severity) << " (" << (int)metadata.severity << "\n";
  19. if (metadata.timestamp)
  20. cout << "\ttime: " << metadata.timestamp.to_string() << "\n";
  21. if (metadata.function)
  22. cout << "\tfunc: " << metadata.function.name << "\n\tline: " << metadata.function.line << "\n\tfile: " << metadata.function.file << "\n";
  23. }
  24. )
  25. }
  26. );
  27. /// Log with info severity
  28. LOG(INFO) << "LOG(INFO)\n";
  29. /// ... with a tag
  30. LOG(INFO, "guten tag") << "LOG(INFO, \"guten tag\")\n";
  31. /// ... with an explicit tag (same result as above)
  32. LOG(INFO) << TAG("guten tag") << "LOG(INFO) << TAG(\"guten tag\")\n";
  33. /// Different log severities
  34. LOG(FATAL) << "LOG(FATAL)\nLOG(FATAL) Second line\n";
  35. LOG(FATAL) << TAG("hello") << "LOG(FATAL) << TAG(\"hello\") no line break";
  36. LOG(FATAL) << "LOG(FATAL) 2 no line break";
  37. LOG(ERROR) << "LOG(ERROR): change in log-level will add a line break";
  38. LOG(WARNING) << "LOG(WARNING)";
  39. LOG(NOTICE) << "LOG(NOTICE)";
  40. LOG(INFO) << "LOG(INFO)\n";
  41. LOG(INFO) << TAG("my tag") << "LOG(INFO) << TAG(\"my tag\")n";
  42. LOG(DEBUG) << "LOG(DEBUG)\n";
  43. LOG(TRACE) << "LOG(TRACE)\n";
  44. /// Conditional logging
  45. LOG(DEBUG) << COND(1 == 1) << "LOG(DEBUG) will be logged\n";
  46. LOG(DEBUG) << COND(1 == 2) << "LOG(DEBUG) will not be logged\n";
  47. /// Colors :-)
  48. LOG(FATAL) << "LOG(FATAL) " << AixLog::Color::red << "red" << AixLog::Color::none << ", default color\n";
  49. LOG(FATAL) << "LOG(FATAL) " << COLOR(red) << "red" << COLOR(none) << ", default color (using macros)\n";
  50. LOG(FATAL) << "LOG(FATAL) " << AixLog::TextColor(AixLog::Color::yellow, AixLog::Color::blue) << "yellow on blue background" << AixLog::Color::none << ", default color\n";
  51. LOG(FATAL) << "LOG(FATAL) " << COLOR(yellow, blue) << "yellow on blue background" << COLOR(none) << ", default color (using macros)\n";
  52. }