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
198
199
200
201
202
203
204
#pragma once
///@file

#include "lix/libutil/config.hh"
#include "lix/libutil/fmt.hh"
#include "lix/libutil/json-fwd.hh"

#include <cassert>
#include <format>
#include <optional>
#include <string_view>

namespace nix
{

/**
 * This was previously defined in libcmd, because libutil doesn't know what the hell a progress bar is.
 * But we want this to be a setting, and all the other logging stuff is here in libutil.
 */
struct LogFormat
{
    enum class LogFormatValue : uint8_t
    {
        Auto,
        Raw,
        RawWithLogs,
        InternalJson,
        Bar,
        BarWithLogs,
        Multiline,
        MultilineWithLogs,
    };

    using enum LogFormatValue;

public:
    LogFormatValue value;

    // Boilerplate.
public:
    constexpr LogFormat() noexcept : value(Auto) { }
    // Intentionally implicit constructor. For once.
    constexpr LogFormat(LogFormatValue const & value) noexcept : value(value) { }

    // Intentionally implicit operator. For once.
    constexpr operator LogFormatValue() const noexcept
    {
        return value;
    }

    static constexpr std::optional<LogFormatValue> parse(std::string_view str)
    {
        if (str == "auto") {
            return Auto;
        } else if (str == "raw") {
            return Raw;
        } else if (str == "raw-with-logs") {
            return RawWithLogs;
        } else if (str == "internal-json") {
            return InternalJson;
        } else if (str == "bar") {
            return Bar;
        } else if (str == "bar-with-logs") {
            return BarWithLogs;
        } else if (str == "multiline") {
            return Multiline;
        } else if (str == "multiline-with-logs") {
            return MultilineWithLogs;
        }

        return std::nullopt;
    }

    constexpr std::string_view toStr() const noexcept
    {
        switch (value) {
        case Auto:
            return "auto";
        case Raw:
            return "raw";
        case RawWithLogs:
            return "raw-with-logs";
        case InternalJson:
            return "internal-json";
        case Bar:
            return "bar";
        case BarWithLogs:
            return "bar-with-logs";
        case Multiline:
            return "multiline";
        case MultilineWithLogs:
            return "multiline-with-logs";
        }
    }

    // Actual API.
public:
    /** Does nothing if not applicable.
     *
    */
    constexpr LogFormatValue withoutLogs() const noexcept
    {
        switch (value) {
        case Auto:
            [[fallthrough]];
        case Raw:
            [[fallthrough]];
        case Bar:
            [[fallthrough]];
        case Multiline:
            [[fallthrough]];
        case InternalJson:
            return *this;

        case RawWithLogs:
            return Raw;
        case BarWithLogs:
            return Bar;
        case MultilineWithLogs:
            return Multiline;
        }
    }

    constexpr LogFormatValue withLogs() const noexcept
    {
        switch (value) {
        case Auto:
            [[fallthrough]];
        case RawWithLogs:
            [[fallthrough]];
        case BarWithLogs:
            [[fallthrough]];
        case MultilineWithLogs:
            [[fallthrough]];
        case InternalJson:
            return *this;

        case Raw:
            return RawWithLogs;
        case Bar:
            return BarWithLogs;
        case Multiline:
            return MultilineWithLogs;
        }
    }
};

using LogFormatValue = LogFormat::LogFormatValue;

template<>
struct LegacyFormat<LogFormat> : std::true_type { };
template<>
struct LegacyFormat<LogFormatValue> : std::true_type { };

template<>
struct json::is_integral_enum<nix::LogFormat> : std::true_type {};
template<>
struct json::is_integral_enum<nix::LogFormatValue> : std::true_type {};

/** Note: you'll have to include `config-impl.hh` when you want to use methods from this type. */
struct LogFormatSetting : public BaseSetting<LogFormat>
{
    // I hate global state, man.
    LogFormat autoValue = LogFormat::RawWithLogs;

    LogFormatSetting(
        Config * options,
        const LogFormat & def,
        const std::string & name,
        const std::string & description,
        const std::set<std::string> & aliases = {},
        bool documentDefault = true,
        std::optional<ExperimentalFeature> experimentalFeature = std::nullopt,
        bool deprecated = false
    ) : BaseSetting<LogFormat>(def, true, name, description, aliases, experimentalFeature, deprecated)
    {
        options->addSetting(this);
    }
};

void to_json(JSON & j, const LogFormat & self);
void from_json(const JSON & j, LogFormat & self);

}

template<>
struct std::formatter<nix::LogFormat> : std::formatter<std::string_view>
{
    template<typename Ctx>
    constexpr Ctx::iterator format(nix::LogFormat self, Ctx & ctx) const
    {
        return std::formatter<std::string_view>::format(self.toStr(), ctx);
    }
};

template<>
struct std::formatter<nix::LogFormatValue> : std::formatter<nix::LogFormat>
{
    template<typename Ctx>
    constexpr Ctx::iterator format(nix::LogFormatValue self, Ctx & ctx) const
    {
        return std::formatter<nix::LogFormatValue>::format(self, ctx);
    }
};