logger.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include<__vic/logger.h>
  2. #include<iostream>
  3. #include<exception>
  4. //////////////////////////////////////////////////////////////////////////////
  5. class coutput : public __vic::logger::output
  6. {
  7. public:
  8. void publish_record(__vic::logger::severity_t s,
  9. const char *rec, size_t rec_len)
  10. {
  11. (std::clog << to_string(s) << ": ").write(rec, rec_len) << std::endl;
  12. }
  13. };
  14. //////////////////////////////////////////////////////////////////////////////
  15. void run_tests()
  16. {
  17. coutput log_output;
  18. __vic::logger log(log_output, __vic::logger::severity::debug);
  19. log.info() << "Application is started";
  20. for(int i = 0; i < 5; i++)
  21. log.debug() << "Loop i = " << i;
  22. // Explicit log record construction
  23. {
  24. __vic::logger::record rec = log.warning();
  25. rec << "a ";
  26. rec << "b ";
  27. rec << "c";
  28. }
  29. #if __cpp_lib_format >= 202106L
  30. log.info("{} std::format! {} {}", "Hi", static_cast<signed char>(-1), 123);
  31. log.info("One arg: {}", 123);
  32. log.info("Curly braces: {}");
  33. log.format(__vic::logger::severity::warning, "format w/o args");
  34. #endif
  35. log.warning("Application end");
  36. }
  37. int main()
  38. {
  39. try
  40. {
  41. run_tests();
  42. return 0;
  43. }
  44. catch(const std::exception &ex)
  45. {
  46. std::cerr << ex.what() << '\n';
  47. }
  48. return 1;
  49. }