common.cxx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include "logger/exception.h"
  11. #include "logger/logpacket.h"
  12. namespace logging {
  13. timeval now() {
  14. struct timeval tv;
  15. gettimeofday( &tv, nullptr );
  16. return tv;
  17. }
  18. #define X(token) if (lower_case == #token) return level::token; else
  19. level level_from_string(std::string const & str) {
  20. std::string lower_case;
  21. std::transform(str.begin(), str.end(),
  22. std::back_inserter(lower_case), &tolower);
  23. LIST_OF_LOGGING_LEVELS
  24. throw invalid_property{str + " is not a log level"};
  25. }
  26. #undef X
  27. #define X(token) if (lvl == level::token) return #token; else
  28. char const * level_to_string(level lvl) {
  29. LIST_OF_LOGGING_LEVELS
  30. throw std::domain_error{"unknown logging level in switch"};
  31. }
  32. #undef X
  33. std::string to_string(level lvl, bool uppercase) {
  34. std::string str = level_to_string(lvl);
  35. if (uppercase) {
  36. std::transform(str.begin(), str.end(), str.begin(), &toupper);
  37. }
  38. return str;
  39. }
  40. std::ostream & operator<<(std::ostream & os, level l) {
  41. return os << std::string(to_string(l, true));
  42. }
  43. }