blob: be84de065eb6bec1aa43f3ada8924e46ce24bc6c [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
Ed Tanous5f34a9c2017-02-28 12:35:13 -08008#include <cstdio>
9#include <cstdlib>
10#include <ctime>
11#include <iostream>
12#include <sstream>
Ed Tanous99923322017-03-03 14:21:24 -080013#include <string>
Ed Tanous5f34a9c2017-02-28 12:35:13 -080014
15#include <g3log/g3log.hpp>
16#include <g3log/logworker.hpp>
17
Ed Tanous99923322017-03-03 14:21:24 -080018namespace crow {
19enum class LogLevel {
Ed Tanous5f34a9c2017-02-28 12:35:13 -080020#ifndef ERROR
Ed Tanous99923322017-03-03 14:21:24 -080021 DEBUG = 0,
22 INFO,
23 WARNING,
24 ERROR,
25 CRITICAL,
Ed Tanous5f34a9c2017-02-28 12:35:13 -080026#endif
27
Ed Tanous99923322017-03-03 14:21:24 -080028 Debug = 0,
29 Info,
30 Warning,
31 Error,
32 Critical,
33};
Ed Tanous5f34a9c2017-02-28 12:35:13 -080034
Ed Tanous99923322017-03-03 14:21:24 -080035class logger {
36 public:
37 logger(std::string prefix, LogLevel level) : level_(level) {
38 // no op, let g3 handle th log levels
39 }
Ed Tanous5f34a9c2017-02-28 12:35:13 -080040
Ed Tanous99923322017-03-03 14:21:24 -080041 //
42 template <typename T>
43 logger& operator<<(T const& value) {
44#ifdef CROW_ENABLE_LOGGING
45 if (level_ >= get_current_log_level()) {
46 stringstream_ << value;
47 }
48#endif
49 return *this;
50 }
Ed Tanous5f34a9c2017-02-28 12:35:13 -080051
Ed Tanous99923322017-03-03 14:21:24 -080052 //
53 static void setLogLevel(LogLevel level) { get_log_level_ref() = level; }
Ed Tanous5f34a9c2017-02-28 12:35:13 -080054
Ed Tanous99923322017-03-03 14:21:24 -080055 static LogLevel get_current_log_level() { return get_log_level_ref(); }
Ed Tanous5f34a9c2017-02-28 12:35:13 -080056
Ed Tanous99923322017-03-03 14:21:24 -080057 private:
58 //
59 static LogLevel& get_log_level_ref() {
60 static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL;
61 return current_level;
62 }
Ed Tanous5f34a9c2017-02-28 12:35:13 -080063
Ed Tanous99923322017-03-03 14:21:24 -080064 //
65 std::ostringstream stringstream_;
66 LogLevel level_;
67};
Ed Tanous5f34a9c2017-02-28 12:35:13 -080068}
69
Ed Tanous99923322017-03-03 14:21:24 -080070#define CROW_LOG_CRITICAL LOG(FATAL)
71#define CROW_LOG_ERROR LOG(WARNING)
72#define CROW_LOG_WARNING LOG(WARNING)
73#define CROW_LOG_INFO LOG(INFO)
74#define CROW_LOG_DEBUG LOG(DEBUG)
Ed Tanousc4771fb2017-03-13 13:39:49 -070075
76
77
78/*
79#define CROW_LOG_CRITICAL \
80 if (false) \
81 crow::logger("CRITICAL", crow::LogLevel::Critical)
82#define CROW_LOG_ERROR \
83 if (false) \
84 crow::logger("ERROR ", crow::LogLevel::Error)
85#define CROW_LOG_WARNING \
86 if (false) \
87 crow::logger("WARNING ", crow::LogLevel::Warning)
88#define CROW_LOG_INFO \
89 if (false) \
90 crow::logger("INFO ", crow::LogLevel::Info)
91#define CROW_LOG_DEBUG \
92 if (false) \
93 crow::logger("DEBUG ", crow::LogLevel::Debug)
94*/