Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | // This file overrides the default crow logging framework to use g3 instead. |
| 4 | // It implements enough of the interfaces of the crow logging framework to work correctly |
| 5 | // but deletes the ILogHandler interface, as usage of that would be counter to the g3 |
| 6 | // handler management, and would cause performance issues. |
| 7 | |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 8 | #include <cstdio> |
| 9 | #include <cstdlib> |
| 10 | #include <ctime> |
| 11 | #include <iostream> |
| 12 | #include <sstream> |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 13 | #include <string> |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 14 | |
| 15 | #include <g3log/g3log.hpp> |
| 16 | #include <g3log/logworker.hpp> |
| 17 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 18 | namespace crow { |
| 19 | enum class LogLevel { |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 20 | #ifndef ERROR |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 21 | DEBUG = 0, |
| 22 | INFO, |
| 23 | WARNING, |
| 24 | ERROR, |
| 25 | CRITICAL, |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 26 | #endif |
| 27 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 28 | Debug = 0, |
| 29 | Info, |
| 30 | Warning, |
| 31 | Error, |
| 32 | Critical, |
| 33 | }; |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 34 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 35 | class logger { |
| 36 | public: |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 37 | logger(std::string prefix, LogLevel level) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 38 | // no op, let g3 handle th log levels |
| 39 | } |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 40 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 41 | // |
| 42 | template <typename T> |
| 43 | logger& operator<<(T const& value) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 44 | return *this; |
| 45 | } |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 46 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 47 | // |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 48 | static void setLogLevel(LogLevel level) { } |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 49 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 50 | static LogLevel get_current_log_level() { return get_log_level_ref(); } |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 51 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 52 | private: |
| 53 | // |
| 54 | static LogLevel& get_log_level_ref() { |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 55 | static LogLevel current_level = LogLevel::DEBUG; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 56 | return current_level; |
| 57 | } |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 58 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 59 | // |
| 60 | std::ostringstream stringstream_; |
| 61 | LogLevel level_; |
| 62 | }; |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 65 | #define CROW_LOG_CRITICAL LOG(FATAL) |
| 66 | #define CROW_LOG_ERROR LOG(WARNING) |
| 67 | #define CROW_LOG_WARNING LOG(WARNING) |
| 68 | #define CROW_LOG_INFO LOG(INFO) |
| 69 | #define CROW_LOG_DEBUG LOG(DEBUG) |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | #define CROW_LOG_CRITICAL \ |
| 75 | if (false) \ |
| 76 | crow::logger("CRITICAL", crow::LogLevel::Critical) |
| 77 | #define CROW_LOG_ERROR \ |
| 78 | if (false) \ |
| 79 | crow::logger("ERROR ", crow::LogLevel::Error) |
| 80 | #define CROW_LOG_WARNING \ |
| 81 | if (false) \ |
| 82 | crow::logger("WARNING ", crow::LogLevel::Warning) |
| 83 | #define CROW_LOG_INFO \ |
| 84 | if (false) \ |
| 85 | crow::logger("INFO ", crow::LogLevel::Info) |
| 86 | #define CROW_LOG_DEBUG \ |
| 87 | if (false) \ |
| 88 | crow::logger("DEBUG ", crow::LogLevel::Debug) |
| 89 | */ |