common.cxx 819 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // common.cxx
  3. // logging
  4. //
  5. // Created by Sam Jaffe on 4/1/19.
  6. //
  7. #include "common.h"
  8. #include <cassert>
  9. #include <ctime>
  10. #define X(l) \
  11. case logging::L##l: \
  12. return #l;
  13. const char* logging::level_header(log_level l) {
  14. assert(l != logging::LNONE);
  15. switch (l) {
  16. X(FATAL)
  17. X(CRITICAL)
  18. X(ERROR)
  19. X(WARNING)
  20. X(INFO)
  21. X(DEBUG)
  22. X(TRACE)
  23. X(NONE)
  24. }
  25. }
  26. #undef X
  27. timeval logging::now() {
  28. struct timeval tv;
  29. gettimeofday( &tv, nullptr );
  30. return tv;
  31. }
  32. std::string logging::timestamp() {
  33. time_t round = std::time(NULL);
  34. char buf[32];
  35. std::strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", std::localtime(&round));
  36. return buf;
  37. }
  38. namespace logging {
  39. std::ostream & operator<<(std::ostream & os, log_level l) {
  40. return os << std::string(level_header(l));
  41. }
  42. }