summaryrefslogtreecommitdiff
path: root/src/Logging.hpp
blob: 38553f25f0054374173d7d6697dd7b09611498a3 (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
75
76
77
// Copyright (C) 2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#pragma once

#include "system.hpp"

#include "FormatNonstdStringView.hpp"

#include "third_party/fmt/core.h"
#include "third_party/fmt/format.h"
#include "third_party/nonstd/optional.hpp"
#include "third_party/nonstd/string_view.hpp"

#include <string>
#include <utility>

// Log a raw message (plus a newline character).
#define LOG_RAW(message_)                                                      \
  do {                                                                         \
    if (Logging::enabled()) {                                                  \
      Logging::log(nonstd::string_view(message_));                             \
    }                                                                          \
  } while (false)

// Log a message (plus a newline character) described by a format string with at
// least one placeholder. `format` is compile-time checked if CMAKE_CXX_STANDARD
// >= 14.
#define LOG(format_, ...) LOG_RAW(fmt::format(FMT_STRING(format_), __VA_ARGS__))

// Log a message (plus a newline character) described by a format string with at
// least one placeholder without flushing and with a reused timestamp. `format`
// is compile-time checked if CMAKE_CXX_STANDARD >= 14.
#define BULK_LOG(format_, ...)                                                 \
  do {                                                                         \
    if (Logging::enabled()) {                                                  \
      Logging::bulk_log(fmt::format(FMT_STRING(format_), __VA_ARGS__));        \
    }                                                                          \
  } while (false)

class Config;

namespace Logging {

// Initialize global logging state. Must be called once before using the other
// logging functions.
void init(const Config& config);

// Return whether logging is enabled to at least one destination.
bool enabled();

// Log `message` (plus a newline character).
void log(nonstd::string_view message);

// Log `message` (plus a newline character) without flushing and with a reused
// timestamp.
void bulk_log(nonstd::string_view message);

// Write the current log memory buffer `path`.
void dump_log(const std::string& path);

} // namespace Logging