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