MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <print>
5
6#include <fe/term.h>
7
8#include "mim/flags.h"
9
10#include "mim/util/dbg.h"
11
12namespace mim {
13
14namespace fs = std::filesystem;
15
16/// Facility to log what you are doing.
17/// @see @ref log "Logging Macros"
18class Log {
19public:
20 Log(const Flags& flags)
21 : flags_(flags) {}
22
23 enum class Level { Error, Warn, Info, Verbose, Debug, Trace };
24
25 /// @name Getters
26 ///@{
27 const Flags& flags() const { return flags_; }
28 Level level() const { return max_level_; }
29 std::ostream& ostream() const {
30 assert(ostream_);
31 return *ostream_;
32 }
33 explicit operator bool() const { return ostream_; } ///< Checks if Log::ostream_ is set.
34 ///@}
35
36 /// @name Setters
37 ///@{
38 Log& set(std::ostream* ostream) {
39 ostream_ = ostream;
40 return *this;
41 }
42 Log& set(Level max_level) {
43 max_level_ = max_level;
44 return *this;
45 }
46 ///@}
47
48 /// @name Log
49 /// Output @p fmt to Log::ostream; does nothing if Log::ostream is `nullptr`.
50 /// @see @ref log "Logging Macros"
51 ///@{
52 template<class... Args>
53 void log(Level level, Loc loc, std::format_string<Args...> fmt, Args&&... args) const {
54 if (ostream_ && level <= max_level_) {
55 std::print(ostream(), "{}{}:{}{}:{} ", level2color(level), level2acro(level), fe::term::FG::Gray, loc,
56 fe::term::FG::Reset);
57 std::println(ostream(), fmt, std::forward<Args>(args)...);
58#ifdef MIM_ENABLE_CHECKS
59 if ((level == Level::Error && flags().break_on_error) || (level == Level::Warn && flags().break_on_warn))
60 fe::breakpoint();
61#endif
62 }
63 }
64 template<class... Args>
65 void log(Level level, const char* file, uint16_t line, std::format_string<Args...> fmt, Args&&... args) {
66 auto path = fs::path(file);
67 log(level, Loc(&path, line), fmt, std::forward<Args>(args)...);
68 }
69 ///@}
70
71 /// @name Conversions
72 ///@{
73 static char level2acro(Level);
74 static fe::term::FG level2color(Level level);
75 ///@}
76
77private:
78 const Flags& flags_;
79 std::ostream* ostream_ = nullptr;
80 Level max_level_ = Level::Error;
81};
82
83/// @name Logging Macros
84/// @anchor log
85/// Macros for different mim::Log::Level%s for ease of use.
86///@{
87// clang-format off
88#define ELOG(...) log().log(mim::Log::Level::Error, __FILE__, __LINE__, __VA_ARGS__)
89#define WLOG(...) log().log(mim::Log::Level::Warn, __FILE__, __LINE__, __VA_ARGS__)
90#define ILOG(...) log().log(mim::Log::Level::Info, __FILE__, __LINE__, __VA_ARGS__)
91#define VLOG(...) log().log(mim::Log::Level::Verbose, __FILE__, __LINE__, __VA_ARGS__)
92/// Vaporizes to nothingness in `Debug` build.
93#ifndef NDEBUG
94#define DLOG(...) log().log(mim::Log::Level::Debug, __FILE__, __LINE__, __VA_ARGS__)
95#define TLOG(...) log().log(mim::Log::Level::Trace, __FILE__, __LINE__, __VA_ARGS__)
96#else
97#define DLOG(...) log()
98#define TLOG(...) log()
99#endif
100// clang-format on
101///@}
102
103} // namespace mim
static char level2acro(Level)
Definition log.cpp:6
Level level() const
Definition log.h:28
static fe::term::FG level2color(Level level)
Definition log.cpp:18
std::ostream & ostream() const
Definition log.h:29
const Flags & flags() const
Definition log.h:27
Log(const Flags &flags)
Definition log.h:20
Log & set(std::ostream *ostream)
Definition log.h:38
Level
Definition log.h:23
void log(Level level, Loc loc, std::format_string< Args... > fmt, Args &&... args) const
Definition log.h:53
Log & set(Level max_level)
Definition log.h:42
void log(Level level, const char *file, uint16_t line, std::format_string< Args... > fmt, Args &&... args)
Definition log.h:65
Definition ast.h:14
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11