MimIR 0.1
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
5#include <rang.hpp>
6
7#include "mim/flags.h"
8
9#include "mim/util/dbg.h"
10
11namespace mim {
12
13namespace fs = std::filesystem;
14
15/// Facility to log what you are doing.
16/// @see @ref fmt "Formatted Output", @ref log "Logging Macros"
17class Log {
18public:
19 Log(const Flags& flags)
20 : flags_(flags) {}
21
22 enum class Level { Error, Warn, Info, Verbose, Debug };
23
24 /// @name Getters
25 ///@{
26 const Flags& flags() const { return flags_; }
27 Level level() const { return max_level_; }
28 std::ostream& ostream() const {
29 assert(ostream_);
30 return *ostream_;
31 }
32 explicit operator bool() const { return ostream_; } ///< Checks if Log::ostream_ is set.
33 ///@}
34
35 /// @name Setters
36 ///@{
37 Log& set(std::ostream* ostream) {
38 ostream_ = ostream;
39 return *this;
40 }
41 Log& set(Level max_level) {
42 max_level_ = max_level;
43 return *this;
44 }
45 ///@}
46
47 /// @name Log
48 /// Output @p fmt to Log::ostream; does nothing if Log::ostream is `nullptr`.
49 /// @see @ref fmt "Formatted Output", @ref log "Logging Macros"
50 ///@{
51 template<class... Args>
52 void log(Level level, Loc loc, const char* fmt, Args&&... args) const {
53 if (ostream_ && level <= max_level_) {
54 std::ostringstream oss;
55 print(ostream(), "{}{}:{}{}:{} ", level2color(level), level2acro(level), rang::fg::gray, loc,
56 rang::fg::reset);
57 print(ostream(), fmt, std::forward<Args>(args)...) << std::endl;
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, const char* 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 std::string_view level2acro(Level);
74 static rang::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/// @see @ref fmt "Formatted Output"
87///@{
88// clang-format off
89#define ELOG(...) log().log(mim::Log::Level::Error, __FILE__, __LINE__, __VA_ARGS__)
90#define WLOG(...) log().log(mim::Log::Level::Warn, __FILE__, __LINE__, __VA_ARGS__)
91#define ILOG(...) log().log(mim::Log::Level::Info, __FILE__, __LINE__, __VA_ARGS__)
92#define VLOG(...) log().log(mim::Log::Level::Verbose, __FILE__, __LINE__, __VA_ARGS__)
93/// Vaporizes to nothingness in `Debug` build.
94#ifndef NDEBUG
95#define DLOG(...) log().log(mim::Log::Level::Debug, __FILE__, __LINE__, __VA_ARGS__)
96#else
97#define DLOG(...) dummy()
98#endif
99// clang-format on
100///@}
101
102} // namespace mim
void log(Level level, const char *file, uint16_t line, const char *fmt, Args &&... args)
Definition log.h:65
Level level() const
Definition log.h:27
std::ostream & ostream() const
Definition log.h:28
const Flags & flags() const
Definition log.h:26
Log(const Flags &flags)
Definition log.h:19
Log & set(std::ostream *ostream)
Definition log.h:37
Level
Definition log.h:22
void log(Level level, Loc loc, const char *fmt, Args &&... args) const
Definition log.h:52
Log & set(Level max_level)
Definition log.h:41
static std::string_view level2acro(Level)
Definition log.cpp:6
static rang::fg level2color(Level level)
Definition log.cpp:17
Definition ast.h:14
std::ostream & print(std::ostream &os, const char *s)
Base case.
Definition print.cpp:5
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
Definition print.h:163
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11