summaryrefslogtreecommitdiff
path: root/include/crow_g3_logger.hpp
blob: a7e7d9ffbf5539e3957f5f117e614589bde6bbef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once

// This file overrides the default crow logging framework to use g3 instead.
// It implements enough of the interfaces of the crow logging framework to work correctly
// but deletes the ILogHandler interface, as usage of that would be counter to the g3
// handler management, and would cause performance issues.

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <string>

#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>

namespace crow {
enum class LogLevel {
#ifndef ERROR
  DEBUG = 0,
  INFO,
  WARNING,
  ERROR,
  CRITICAL,
#endif

  Debug = 0,
  Info,
  Warning,
  Error,
  Critical,
};

class logger {
 public:
  logger(std::string prefix, LogLevel level) : level_(level) {
    // no op, let g3 handle th log levels
  }

  //
  template <typename T>
  logger& operator<<(T const& value) {
#ifdef CROW_ENABLE_LOGGING
    if (level_ >= get_current_log_level()) {
      stringstream_ << value;
    }
#endif
    return *this;
  }

  //
  static void setLogLevel(LogLevel level) { get_log_level_ref() = level; }

  static LogLevel get_current_log_level() { return get_log_level_ref(); }

 private:
  //
  static LogLevel& get_log_level_ref() {
    static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL;
    return current_level;
  }

  //
  std::ostringstream stringstream_;
  LogLevel level_;
};
}

#define CROW_LOG_CRITICAL LOG(FATAL)
#define CROW_LOG_ERROR LOG(WARNING)
#define CROW_LOG_WARNING LOG(WARNING)
#define CROW_LOG_INFO LOG(INFO)
#define CROW_LOG_DEBUG LOG(DEBUG)