aboutsummaryrefslogtreecommitdiff
path: root/log.h
blob: 3e26501df7e0584f997bb997c13b4a028a06bfba (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef UTILS_LOG_H
#define UTILS_LOG_H

#include <cassert>
#include <chrono>
#include <cstdio>
#include <type_traits>

// -- LOGGER MACROS ------------------------------------------------------------

#define FAIL(log, fmt, ...) LOG(log, kFail, fmt, ##__VA_ARGS__)
#define WARN(log, fmt, ...) LOG(log, kWarn, fmt, ##__VA_ARGS__)
#define INFO(log, fmt, ...) LOG(log, kInfo, fmt, ##__VA_ARGS__)
#define DBG0(log, fmt, ...) LOG(log, kDbg0, fmt, ##__VA_ARGS__)

// -- LOGGER MACROS DETAILS ----------------------------------------------------

#define LOG(log_, lvl, fmt, ...)                                             \
  do {                                                                       \
    struct fmt_str_must_be_str_literal {                                     \
      constexpr fmt_str_must_be_str_literal(const char* ptr) : ptr{ptr} {}   \
      const char* ptr;                                                       \
    };                                                                       \
    /* Check if FMT is a string literal, construction fails otherwise. */    \
    constexpr fmt_str_must_be_str_literal _{fmt};                            \
    (void)_;                                                                 \
                                                                             \
    int lint_fmt_string(const char*, ...)                                    \
        __attribute__((format(printf, 1, 2)));                               \
    /* Check fmt args against fmt string and warn if -Wformat is enabled. */ \
    (void)sizeof(lint_fmt_string(fmt, ##__VA_ARGS__));                       \
                                                                             \
    if (logging::log_level::lvl <= (log_)->get_level())                      \
      (log_)->template log<logging::log_level::lvl>(fmt, ##__VA_ARGS__);     \
  } while (0)

namespace logging {

// -- LOGGING LEVELS -----------------------------------------------------------

#define LOG_LEVELS(M) \
  M(kFail, "FAIL")    \
  M(kWarn, "WARN")    \
  M(kInfo, "INFO")    \
  M(kDbg0, "DBG0")

#define M(val, ignore) val,
enum log_level { LOG_LEVELS(M) };
#undef M

#define M(ignore, val) val,
constexpr const char* kLogPrefix[] = {LOG_LEVELS(M)};
#undef M

namespace detail {

// -- SANITIZE FMT ARG HELPER (META FN) ----------------------------------------

template <typename T>
constexpr inline bool is_one_of() {
  return false;
}

template <typename T, typename U, typename... Args>
constexpr inline bool is_one_of() {
  return std::is_same<T, U>::value || is_one_of<T, Args...>();
}

template <typename Arg>
constexpr inline Arg sanitize_fmt_args(Arg arg) {
  static_assert(
      is_one_of<Arg, char, unsigned char, int, unsigned, float, double>() ||
          std::is_pointer<Arg>::value,
      "Invalid FMT arg type!");
  return arg;
}

template <std::size_t N>
struct formatter {
  template <typename... Args>
  std::size_t operator()(char* str,
                         std::size_t size,
                         const char* fmt,
                         Args... args) {
    static_assert(sizeof...(Args) == N, "");
    return std::snprintf(str, size, fmt, args...);
  }
};

template <>
struct formatter<0> {
  std::size_t operator()(char* str, std::size_t size, const char* fmt) {
    return std::snprintf(str, size, "%s", fmt);
  }
};

// -- TIME STAMP HELPER --------------------------------------------------------

template <int UtcOffset = 0>
struct time_stamp {
  using clock = std::chrono::system_clock;
  using repr = clock::rep;
  using time_point = clock::time_point;

  repr us() const {
    return to_duration<std::chrono::microseconds>() % 1000;
  }
  repr ms() const {
    return to_duration<std::chrono::milliseconds>() % 1000;
  }
  repr s() const {
    return to_duration<std::chrono::seconds>() % 60;
  }
  repr m() const {
    return to_duration<std::chrono::minutes>() % 60;
  }
  repr h() const {
    return to_duration<std::chrono::hours>() % 24 + UtcOffset;
  }

 private:
  template <typename ToDuration>
  repr to_duration() const {
    return std::chrono::duration_cast<ToDuration>(m_time.time_since_epoch())
        .count();
  }

  time_point m_time{clock::now()};
};
}  // namespace detail

// -- LOGGER -------------------------------------------------------------------

template <bool WithTimestamp = true, std::size_t BufSize = 128>
struct logger {
  constexpr logger() = default;
  constexpr logger(log_level lvl) : m_lvl{lvl} {}

  log_level get_level() const {
    return m_lvl;
  }
  void set_level(log_level lvl) {
    m_lvl = lvl;
  }

  template <log_level L, typename... Args>
  constexpr void log(const char* fmt, Args... args);

 private:
  log_level m_lvl{kInfo};
  char m_buf[BufSize];
};

// -- LOGGER IMPLEMENTATION ----------------------------------------------------

template <bool WithTimestamp, std::size_t BufSize>
template <log_level L, typename... Args>
constexpr void logger<WithTimestamp, BufSize>::log(const char* fmt,
                                                   Args... args) {
  std::size_t pos{0};

  // Add timestamp if enabled.
  if (WithTimestamp) {
    detail::time_stamp<2> ts;
    pos += std::snprintf(m_buf + pos, BufSize - pos,
                         "[%02ld:%02ld:%02ld:%03ld%03ld] ", ts.h(), ts.m(),
                         ts.s(), ts.ms(), ts.us());
    assert(pos > 0);
  }

  // Add log level prefix.
  pos += std::snprintf(m_buf + pos, BufSize - pos, "%s: ", kLogPrefix[L]);
  assert(pos < BufSize);

  // Add log message using user specified fmt string.
  //
  // SAFETY: User of this function is responsible to provide a "safe" fmt
  // string. When using the provided macros we check that the user specifies a
  // string literal as fmt string and hence the user controls the fmt string.
  // Additionally, we sanitize the arguments to only allow explicitly specified
  // argument types.
  detail::formatter<sizeof...(Args)> formatter;
  pos += formatter(m_buf + pos, BufSize - pos, fmt,
                   detail::sanitize_fmt_args(args)...);
  assert(pos < BufSize);

  // Ensure terminated with new line and null terminator.
  assert(pos < BufSize);
  m_buf[pos++] = '\n';

  // Write out log message.
  std::fwrite(m_buf, pos < BufSize ? pos : BufSize, 1 /* nmemb */, stderr);
}

}  // namespace logging

#endif