NeKernel dev
Loading...
Searching...
No Matches
DebugOutput.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
7#pragma once
8
10#include <KernelKit/DeviceMgr.h>
11#include <NeKit/OwnPtr.h>
12#include <NeKit/Stream.h>
13#include <NeKit/Utils.h>
14
15namespace Kernel {
16class TerminalDevice;
17class DTraceDevice;
18class NeTraceDevice;
19class Utf8TerminalDevice;
20
21inline TerminalDevice end_line();
22inline TerminalDevice number(const Long& x);
23inline TerminalDevice hex_number(const Long& x);
24
25// @brief Emulates a VT100 terminal.
26class TerminalDevice final NE_DEVICE<const Char*> {
27 public:
28 TerminalDevice(void (*print)(DeviceInterface*, const Char*),
29 void (*gets)(DeviceInterface*, const Char*))
30 : DeviceInterface<const Char*>(print, gets) {}
31
32 ~TerminalDevice() override;
33
36 const Char* Name() const override { return ("TerminalDevice"); }
37
38 NE_COPY_DEFAULT(TerminalDevice)
39
41};
42
44 public:
46 void (*gets)(DeviceInterface*, const Utf8Char*))
47 : DeviceInterface<const Utf8Char*>(print, gets) {}
48
50
53 const Char* Name() const override { return ("Utf8TerminalDevice"); }
54
55 NE_COPY_DEFAULT(Utf8TerminalDevice)
56
58};
59
60inline TerminalDevice end_line() {
61 TerminalDevice self = TerminalDevice::The();
62
63 self.operator<<("\r");
64 return self;
65}
66
67inline Utf8TerminalDevice utf_end_line() {
68 Utf8TerminalDevice self = Utf8TerminalDevice::The();
69
70 self.operator<<(u8"\r");
71 return self;
72}
73
74inline TerminalDevice carriage_return() {
75 TerminalDevice self = TerminalDevice::The();
76
77 self.operator<<("\r");
78 return self;
79}
80
81inline TerminalDevice tabulate() {
82 TerminalDevice self = TerminalDevice::The();
83
84 self.operator<<("\t");
85 return self;
86}
87
89inline TerminalDevice bell() {
90 TerminalDevice self = TerminalDevice::The();
91
92 self.operator<<("\a");
93 return self;
94}
95
96namespace Detail {
97 inline TerminalDevice _write_number(const Long& x, TerminalDevice& term) {
98 UInt64 y = (x > 0 ? x : -x) / 10;
99 UInt64 h = (x > 0 ? x : -x) % 10;
100
101 if (y) _write_number(y, term);
102
103 /* fail if the number is not base-10 */
104 if (h > 10) {
105 _write_number('?', term);
106 return term;
107 }
108
109 if (y == ~0UL) y = -y;
110
111 const Char kNumbers[11] = "0123456789";
112
113 Char buf[2];
114 buf[0] = kNumbers[h];
115 buf[1] = 0;
116
117 term.operator<<(buf);
118 return term;
119 }
120
121 inline TerminalDevice _write_number_hex(const Long& x, TerminalDevice& term) {
122 UInt64 y = (x > 0 ? x : -x) / 16;
123 UInt64 h = (x > 0 ? x : -x) % 16;
124
125 if (y) _write_number_hex(y, term);
126
127 /* fail if the hex number is not base-16 */
128 if (h > 16) {
129 _write_number_hex('?', term);
130 return term;
131 }
132
133 if (y == ~0UL) y = -y;
134
135 const Char kNumbers[17] = "0123456789ABCDEF";
136
137 Char buf[2];
138 buf[0] = kNumbers[h];
139 buf[1] = 0;
140
141 term.operator<<(buf);
142 return term;
143 }
144} // namespace Detail
145
146inline TerminalDevice hex_number(const Long& x) {
147 TerminalDevice self = TerminalDevice::The();
148
149 self << "0x";
151
152 return self;
153}
154
155inline TerminalDevice number(const Long& x) {
156 TerminalDevice self = TerminalDevice::The();
157
158 Detail::_write_number(x, self);
159
160 return self;
161}
162
163inline TerminalDevice get_console_in(Char* buf) {
164 TerminalDevice self = TerminalDevice::The();
165
166 self >> buf;
167
168 return self;
169}
170
171inline constexpr auto kDebugPort = 51820;
172inline constexpr auto kDebugMagic = "VMK1.0.0;";
173inline constexpr auto kDebugVersion = 0x0100;
174
175inline constexpr SizeT kDebugCmdLen = 256U;
176
178
179inline TerminalDevice& operator<<(TerminalDevice& src, const Long& num) {
180 src = number(num);
181 return src;
182}
183} // namespace Kernel
184
185#ifdef kout
186#undef kout
187#endif // ifdef kout
188
189#define kout TerminalDevice::The()
190
191#ifdef kendl
192#undef kendl
193#endif // ifdef kendl
194
195#define kendl end_line()
196
197#ifdef kout8
198#undef kout8
199#endif // ifdef kout8
200
201#define kout8 Utf8TerminalDevice::The()
202
203#ifdef kendl8
204#undef kendl8
205#endif // ifdef kendl8
206
207#define kendl8 utf_end_line()
#define NE_COPY_DEFAULT(KLASS)
Definition Detail.h:17
#define NE_DEVICE
Definition DeviceMgr.h:27
#define STATIC
Device contract interface, represents an HW device.
Definition DeviceMgr.h:42
STATIC TerminalDevice The()
TerminalDevice(void(*print)(DeviceInterface *, const Char *), void(*gets)(DeviceInterface *, const Char *))
Definition DebugOutput.h:28
const Char * Name() const override
returns device name (terminal name)
Definition DebugOutput.h:36
STATIC Utf8TerminalDevice The()
const Char * Name() const override
returns device name (terminal name)
Definition DebugOutput.h:53
Utf8TerminalDevice(void(*print)(DeviceInterface *, const Utf8Char *), void(*gets)(DeviceInterface *, const Utf8Char *))
Definition DebugOutput.h:45
TerminalDevice _write_number(const Long &x, TerminalDevice &term)
Definition DebugOutput.h:97
TerminalDevice _write_number_hex(const Long &x, TerminalDevice &term)
Definition DebugOutput.h:121
UPS inline definitions.
Definition Device.h:12
constexpr auto kDebugMagic
Definition DebugOutput.h:172
char Char
Definition Config.h:51
TerminalDevice bell()
emulate a terminal bell, like the VT100 does.
Definition DebugOutput.h:89
TerminalDevice carriage_return()
Definition DebugOutput.h:74
TerminalDevice get_console_in(Char *buf)
Definition DebugOutput.h:163
TerminalDevice tabulate()
Definition DebugOutput.h:81
__INT64_TYPE__ Long
Definition Config.h:45
constexpr SizeT kDebugCmdLen
Definition DebugOutput.h:175
__SIZE_TYPE__ SizeT
Definition Config.h:60
TerminalDevice number(const Long &x)
Definition DebugOutput.h:155
TerminalDevice end_line()
Definition DebugOutput.h:60
TerminalDevice & operator<<(TerminalDevice &src, const Long &num)
Definition DebugOutput.h:179
constexpr auto kDebugPort
Definition DebugOutput.h:171
Utf8TerminalDevice utf_end_line()
Definition DebugOutput.h:67
char8_t Utf8Char
Definition Config.h:75
TerminalDevice hex_number(const Long &x)
Definition DebugOutput.h:146
Char rt_debug_cmd[kDebugCmdLen]
Definition DebugOutput.h:177
constexpr auto kDebugVersion
Definition DebugOutput.h:173
__UINT64_TYPE__ UInt64
Definition Config.h:48