c_logger.cxx 949 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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_impl.h"
  10. #include "logger/logpacket.h"
  11. using namespace logging;
  12. c_logger::c_logger(std::string const & name,
  13. std::shared_ptr<logger_impl> impl)
  14. : logger_name_(name), impl_(impl)
  15. {
  16. }
  17. c_logger::~c_logger() {
  18. if (impl_) impl_->flush();
  19. }
  20. void c_logger::vlognf(level level, size_t num_bytes, char const* fmt,
  21. va_list args) {
  22. if (!impl_->should_log(level)) return;
  23. const std::unique_ptr<char[]> data(new char[num_bytes]);
  24. int n = vsnprintf(data.get(), num_bytes, fmt, args);
  25. if (n >= 0) {
  26. data[num_bytes-1] = '\0';
  27. impl_->write({ now(), level, {}, logger_name_, {data.get()} });
  28. }
  29. }
  30. void c_logger::log(level level, std::string const& msg) {
  31. impl_->write({ now(), level, {}, logger_name_, msg });
  32. }
  33. void c_logger::flush() {
  34. impl_->flush();
  35. }