Telkin
Loading...
Searching...
No Matches
Print.h
Go to the documentation of this file.
1#pragma once
2
3#include <cafe.h>
4
5#include <source_location>
6#include <utility>
7
8namespace tk {
9
10 namespace internal {
11 constexpr const char* basename(const char* path) {
12 const char* last = path;
13
14 for (const char* p = path; *p; ++p) {
15 if (*p == '/' || *p == '\\') {
16 last = p + 1;
17 }
18 }
19
20 return last;
21 }
22
23 struct LogFormat {
24 const char* fmt;
25 const char* file;
27
33 };
34 }
35
36 namespace LogColor {
37 constexpr const char* Reset = "\33[0m";
38 constexpr const char* Bold = "\33[1m";
39 constexpr const char* NoBold = "\33[22m";
40 constexpr const char* Underline = "\33[4m";
41 constexpr const char* NoUnderline = "\33[24m";
42 constexpr const char* Invert = "\33[7m";
43 constexpr const char* NoInvert = "\33[27m";
44
45 constexpr const char* Black = "\33[30m";
46 constexpr const char* Red = "\33[31m";
47 constexpr const char* Green = "\33[32m";
48 constexpr const char* Yellow = "\33[33m";
49 constexpr const char* Blue = "\33[34m";
50 constexpr const char* Magenta = "\33[35m";
51 constexpr const char* Cyan = "\33[36m";
52 constexpr const char* LightGray = "\33[37m";
53 constexpr const char* Gray = "\33[90m";
54 constexpr const char* LightRed = "\33[91m";
55 constexpr const char* LightGreen = "\33[92m";
56 constexpr const char* LightYellow = "\33[93m";
57 constexpr const char* LightBlue = "\33[94m";
58 constexpr const char* LightMagenta = "\33[95m";
59 constexpr const char* LightCyan = "\33[96m";
60 constexpr const char* White = "\33[97m";
61 constexpr const char* Default = "\33[39m";
62 //constexpr const char* Extended = "\33[38m";
63
64 namespace BG {
65 constexpr const char* Black = "\33[40m";
66 constexpr const char* Red = "\33[41m";
67 constexpr const char* Green = "\33[42m";
68 constexpr const char* Yellow = "\33[43m";
69 constexpr const char* Blue = "\33[44m";
70 constexpr const char* Magenta = "\33[45m";
71 constexpr const char* Cyan = "\33[46m";
72 constexpr const char* LightGray = "\33[47m";
73 constexpr const char* Gray = "\33[100m";
74 constexpr const char* LightRed = "\33[101m";
75 constexpr const char* LightGreen = "\33[102m";
76 constexpr const char* LightYellow = "\33[103m";
77 constexpr const char* LightBlue = "\33[104m";
78 constexpr const char* LightMagenta = "\33[105m";
79 constexpr const char* LightCyan = "\33[106m";
80 constexpr const char* White = "\33[107m";
81 constexpr const char* Default = "\33[49m";
82 //constexpr const char* Extended = "\33[48m";
83 }
84 }
85
86 template <typename... Args>
87 void print(internal::LogFormat format, Args&&... args) {
88 const auto& fmt = format.fmt;
89
90 OSReport("[%s:%d] ", format.file, format.line);
91 OSReport(fmt, std::forward<Args>(args)...);
92 }
93
94 template <typename... Args>
95 void println(internal::LogFormat format, Args&&... args) {
96 tk::print(format, std::forward<Args>(args)...);
97 OSReport("%s\n", LogColor::Reset);
98 }
99
100 template <typename... Args>
101 void warn(internal::LogFormat format, Args&&... args) {
102 OSReport("%s", LogColor::Yellow);
103 println(format, std::forward<Args>(args)...);
104 }
105
106 template <typename... Args>
107 void fatal(internal::LogFormat format, Args&&... args) {
108 const auto& fmt = format.fmt;
109
110 OSReport("%s[%s:%d] ERROR: ", LogColor::Magenta, format.file, format.line);
111 OSReport(fmt, std::forward<Args>(args)...);
112 OSReport("%s\n", LogColor::Reset); // we don't expect anything else afterwards
113
114 static char buf[512];
115
116 __os_snprintf(buf, sizeof(buf), fmt, std::forward<Args>(args)...);
117 OSFatal(buf);
118 }
119}
Definition Print.h:64
constexpr const char * Blue
Definition Print.h:69
constexpr const char * LightGray
Definition Print.h:72
constexpr const char * LightBlue
Definition Print.h:77
constexpr const char * Yellow
Definition Print.h:68
constexpr const char * White
Definition Print.h:80
constexpr const char * Green
Definition Print.h:67
constexpr const char * Gray
Definition Print.h:73
constexpr const char * Black
Definition Print.h:65
constexpr const char * Default
Definition Print.h:81
constexpr const char * Red
Definition Print.h:66
constexpr const char * LightCyan
Definition Print.h:79
constexpr const char * Cyan
Definition Print.h:71
constexpr const char * LightMagenta
Definition Print.h:78
constexpr const char * LightYellow
Definition Print.h:76
constexpr const char * Magenta
Definition Print.h:70
constexpr const char * LightRed
Definition Print.h:74
constexpr const char * LightGreen
Definition Print.h:75
Definition Print.h:36
constexpr const char * White
Definition Print.h:60
constexpr const char * LightGray
Definition Print.h:52
constexpr const char * Magenta
Definition Print.h:50
constexpr const char * Green
Definition Print.h:47
constexpr const char * Blue
Definition Print.h:49
constexpr const char * LightMagenta
Definition Print.h:58
constexpr const char * LightRed
Definition Print.h:54
constexpr const char * Black
Definition Print.h:45
constexpr const char * Default
Definition Print.h:61
constexpr const char * Red
Definition Print.h:46
constexpr const char * Yellow
Definition Print.h:48
constexpr const char * Underline
Definition Print.h:40
constexpr const char * NoUnderline
Definition Print.h:41
constexpr const char * Reset
Definition Print.h:37
constexpr const char * Invert
Definition Print.h:42
constexpr const char * LightBlue
Definition Print.h:57
constexpr const char * NoBold
Definition Print.h:39
constexpr const char * Cyan
Definition Print.h:51
constexpr const char * LightGreen
Definition Print.h:55
constexpr const char * LightYellow
Definition Print.h:56
constexpr const char * Gray
Definition Print.h:53
constexpr const char * LightCyan
Definition Print.h:59
constexpr const char * NoInvert
Definition Print.h:43
constexpr const char * Bold
Definition Print.h:38
Definition Print.h:10
constexpr const char * basename(const char *path)
Definition Print.h:11
void print(internal::LogFormat format, Args &&... args)
Definition Print.h:87
u64(*)() getTitleID_t
Definition Mod.h:16
void fatal(internal::LogFormat format, Args &&... args)
Definition Print.h:107
void warn(internal::LogFormat format, Args &&... args)
Definition Print.h:101
void println(internal::LogFormat format, Args &&... args)
Definition Print.h:95
Definition Print.h:23
const char * file
Definition Print.h:25
const char * fmt
Definition Print.h:24
u32 line
Definition Print.h:26