RaidZeroLib
Coroutine robot framework for VEX V5
Loading...
Searching...
No Matches
Log.hpp
1#pragma once
2#include <cstdint>
3#include <format>
4#include <functional>
5#include <string>
6#include <string_view>
7#include <utility>
8
9#include "RaidZeroLib/ThirdParty/au/au.hpp"
10#include "RaidZeroLib/api/Units/Units.hpp"
11
14template <typename Unit, typename Rep>
15struct std::formatter<::au::Quantity<Unit, Rep>> : ::au::QuantityFormatter<Unit, Rep, ::std::formatter> {};
16
17namespace rz {
18
22class Log {
23 public:
24 Log() = delete;
25
26 enum class Level { Debug, Info, Warn, Error };
27
28 struct Record {
31 std::uint64_t tick;
32 std::string_view message;
33 };
34
35 using Output = std::function<void(const Record&)>;
36
37 template <class... Args>
38 static void debug(std::format_string<Args...> format, Args&&... args) {
39 log(Level::Debug, format, std::forward<Args>(args)...);
40 }
41
42 template <class... Args>
43 static void info(std::format_string<Args...> format, Args&&... args) {
44 log(Level::Info, format, std::forward<Args>(args)...);
45 }
46
47 template <class... Args>
48 static void warn(std::format_string<Args...> format, Args&&... args) {
49 log(Level::Warn, format, std::forward<Args>(args)...);
50 }
51
52 template <class... Args>
53 static void error(std::format_string<Args...> format, Args&&... args) {
54 log(Level::Error, format, std::forward<Args>(args)...);
55 }
56
58 static void setMinimumLevel(Level level) noexcept;
59 [[nodiscard]] static Level minimumLevel() noexcept;
60
62 static void setOutput(Output output);
63
65 static void toStdout(const Record& record);
66
68 [[nodiscard]] static std::string format(const Record& record);
69
70 private:
71 template <class... Args>
72 static void log(Level level, std::format_string<Args...> format, Args&&... args) {
73 if (level < minimumLevel()) {
74 return;
75 }
76 write(level, std::format(format, std::forward<Args>(args)...));
77 }
78
80 static void write(Level level, std::string_view message);
81};
82
83} // namespace rz
static void debug(std::format_string< Args... > format, Args &&... args)
Definition Log.hpp:38
static void setMinimumLevel(Level level) noexcept
Messages below this level are dropped before they are formatted. Defaults to Info.
Log()=delete
static std::string format(const Record &record)
The standard line for a record, e.g. "[12.340s #1234] warn: message".
static void info(std::format_string< Args... > format, Args &&... args)
Definition Log.hpp:43
static void toStdout(const Record &record)
The default output: prints format(record) to stdout.
static Level minimumLevel() noexcept
static void error(std::format_string< Args... > format, Args &&... args)
Definition Log.hpp:53
std::function< void(const Record &)> Output
Definition Log.hpp:35
static void setOutput(Output output)
Where records go. Defaults to toStdout; pass [](const Log::Record&) {} to discard.
Level
Definition Log.hpp:26
@ Info
Definition Log.hpp:26
@ Warn
Definition Log.hpp:26
@ Error
Definition Log.hpp:26
@ Debug
Definition Log.hpp:26
static void warn(std::format_string< Args... > format, Args &&... args)
Definition Log.hpp:48
au::QuantityPointD< au::Seconds > Time
Definition Units.hpp:17
Definition DriverControl.hpp:11
Definition Log.hpp:28
Time time
start of the tick it was logged in (0 before the runtime exists)
Definition Log.hpp:30
Level level
Definition Log.hpp:29
std::string_view message
Definition Log.hpp:32
std::uint64_t tick
Runtime::tickCount() when it was logged (0 before the first tick)
Definition Log.hpp:31