common.cxx 1.1 KB

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