blob: e39269a579bd9f02a61282720f50ab51879f1b70 [file] [log] [blame]
Ed Tanous5f34a9c2017-02-28 12:35:13 -08001#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
8
9#include <string>
10#include <cstdio>
11#include <cstdlib>
12#include <ctime>
13#include <iostream>
14#include <sstream>
15
16#include <g3log/g3log.hpp>
17#include <g3log/logworker.hpp>
18
19namespace crow
20{
21 enum class LogLevel
22 {
23#ifndef ERROR
24 DEBUG = 0,
25 INFO,
26 WARNING,
27 ERROR,
28 CRITICAL,
29#endif
30
31 Debug = 0,
32 Info,
33 Warning,
34 Error,
35 Critical,
36 };
37
38 class logger {
39
40 public:
41
42
43 logger(std::string prefix, LogLevel level) : level_(level) {
44 // no op, let g3 handle th log levels
45
46 }
47
48 //
49 template <typename T>
50 logger& operator<<(T const &value) {
51
52 #ifdef CROW_ENABLE_LOGGING
53 if(level_ >= get_current_log_level()) {
54 stringstream_ << value;
55 }
56 #endif
57 return *this;
58 }
59
60 //
61 static void setLogLevel(LogLevel level) {
62 get_log_level_ref() = level;
63 }
64
65 static LogLevel get_current_log_level() {
66 return get_log_level_ref();
67 }
68
69 private:
70 //
71 static LogLevel& get_log_level_ref()
72 {
73 static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL;
74 return current_level;
75 }
76
77 //
78 std::ostringstream stringstream_;
79 LogLevel level_;
80 };
81}
82
83#define CROW_LOG_CRITICAL \
84 LOG(FATAL)
85#define CROW_LOG_ERROR \
86 LOG(WARNING)
87#define CROW_LOG_WARNING \
88 LOG(WARNING)
89#define CROW_LOG_INFO \
90 LOG(INFO)
91#define CROW_LOG_DEBUG \
92 LOG(DEBUG)