| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- //
- // common.cxx
- // logging
- //
- // Created by Sam Jaffe on 4/1/19.
- //
- #include "common.h"
- #include <cassert>
- #include <ctime>
- #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));
- }
- }
|