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> void log(Level level, Loc loc, const char* fmt, Args&&... args) const {
52 if (ostream_ && level <= max_level_) {
53 std::ostringstream oss;
54 print(ostream(), "{}{}:{}{}:{} ", level2color(level), level2acro(level), rang::fg::gray, loc,
55 rang::fg::reset);
56 print(ostream(), fmt, std::forward<Args&&>(args)...) << std::endl;
57#ifdef MIM_ENABLE_CHECKS
58 if ((level == Level::Error && flags().break_on_error) || (level == Level::Warn && flags().break_on_warn))
59 fe::breakpoint();
60#endif
61 }
62 }
63 template<class... Args> void log(Level level, const char* file, uint16_t line, const char* fmt, Args&&... args) {
64 auto path = fs::path(file);
65 log(level, Loc(&path, line), fmt, std::forward<Args&&>(args)...);
66 }
67 ///@}
68
69 /// @name Conversions
70 ///@{
71 static std::string_view level2acro(Level);
72 static rang::fg level2color(Level level);
73 ///@}
74
75private:
76 const Flags& flags_;
77 std::ostream* ostream_ = nullptr;
78 Level max_level_ = Level::Error;
79};
80
81/// @name Logging Macros
82/// @anchor log
83/// Macros for different mim::Log::Level%s for ease of use.
84/// @see @ref fmt "Formatted Output"
85///@{
86// clang-format off
87#define ELOG(...) log().log(mim::Log::Level::Error, __FILE__, __LINE__, __VA_ARGS__)
88#define WLOG(...) log().log(mim::Log::Level::Warn, __FILE__, __LINE__, __VA_ARGS__)
89#define ILOG(...) log().log(mim::Log::Level::Info, __FILE__, __LINE__, __VA_ARGS__)
90#define VLOG(...) log().log(mim::Log::Level::Verbose, __FILE__, __LINE__, __VA_ARGS__)
91/// Vaporizes to nothingness in `Debug` build.
92#ifndef NDEBUG
93#define DLOG(...) log().log(mim::Log::Level::Debug, __FILE__, __LINE__, __VA_ARGS__)
94#else
95#define DLOG(...) dummy()
96#endif
97// clang-format on
98///@}
99
100} // namespace mim
Facility to log what you are doing.
Definition log.h:17
void log(Level level, const char *file, uint16_t line, const char *fmt, Args &&... args)
Definition log.h:63
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:51
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 cfg.h:11
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:155
Compiler switches that must be saved and looked up in later phases of compilation.
Definition flags.h:11