// // common.cxx // logging // // Created by Sam Jaffe on 4/1/19. // #include "common.h" #include #include #include "logger/exception.h" namespace logging { timeval now() { struct timeval tv; gettimeofday( &tv, nullptr ); return tv; } #define X(token) if (lower_case == #token) return level::token; else level level_from_string(std::string const & str) { std::string lower_case; std::transform(str.begin(), str.end(), std::back_inserter(lower_case), &tolower); LIST_OF_LOGGING_LEVELS throw invalid_property{str + " is not a log level"}; } #undef X #define X(token) if (lvl == level::token) return #token; else char const * level_to_string(level lvl) { LIST_OF_LOGGING_LEVELS throw std::domain_error{"unknown logging level in switch"}; } #undef X std::string to_string(level lvl, bool uppercase) { std::string str = level_to_string(lvl); if (uppercase) { std::transform(str.begin(), str.end(), str.begin(), &toupper); } return str; } std::ostream & operator<<(std::ostream & os, level l) { return os << std::string(to_string(l, true)); } }