c_logger.cxx 931 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // c_logger.cxx
  3. // logging
  4. //
  5. // Created by Sam Jaffe on 4/1/19.
  6. //
  7. #include "logger/c_logger.h"
  8. #include "common.h"
  9. #include "logger/detail/logger_impl.h"
  10. using namespace logging;
  11. c_logger::c_logger(std::string const & name,
  12. std::shared_ptr<logger_impl> impl)
  13. : logger_name_(name), impl_(impl)
  14. {
  15. }
  16. c_logger::~c_logger() {
  17. if (impl_) impl_->flush();
  18. }
  19. void c_logger::vlognf(level level, size_t num_bytes, char const* fmt,
  20. va_list args) {
  21. if (!impl_->should_log(level)) return;
  22. const std::unique_ptr<char[]> data(new char[num_bytes]);
  23. int n = vsnprintf(data.get(), num_bytes, fmt, args);
  24. if (n >= 0) {
  25. data[num_bytes-1] = '\0';
  26. impl_->write({ now(), level, {}, logger_name_, data.get() });
  27. }
  28. }
  29. void c_logger::log(level level, std::string const& msg) {
  30. impl_->write({ now(), level, {}, logger_name_, msg });
  31. }
  32. void c_logger::flush() {
  33. impl_->flush();
  34. }