MOS Source Code
Loading...
Searching...
No Matches
syslog.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
7
8#include <array>
9#include <mos/refcount.hpp>
10#include <mos/string_view.hpp>
11#include <mos/type_utils.hpp>
12#include <type_traits>
13
14enum class LogLevel
15{
16 FATAL = 6,
17 EMERG = 5,
18 WARN = 4,
19 EMPH = 3,
20 INFO = 2,
21 INFO2 = 1,
22 UNSET = 0,
23};
24
25extern struct Console *printk_console;
26extern "C" int snprintf(char *__restrict str, size_t size, const char *__restrict format, ...);
27extern "C" void lprintk(LogLevel loglevel, const char *format, ...);
28
29namespace mos
30{
31 template<typename M, typename... Args>
33 {
34 const std::tuple<Args...> targs;
35 explicit Preformatted(M, Args... args) : targs(args...) {};
36 };
37
38 using SyslogBuffer = std::array<char, MOS_PRINTK_BUFFER_SIZE>;
39
41 {
43 {
44 return SyslogStreamWriter(feature, level, rcCore, buf);
45 }
46
48
49 template<typename T>
50 requires std::is_integral_v<T> inline SyslogStreamWriter &operator<<(T value)
51 {
52 if (!should_print)
53 return *this;
54
55 pos += snprintf(&fmtbuffer[pos], MOS_PRINTK_BUFFER_SIZE, "%lld", (long long) value);
56 return *this;
57 }
58
59 template<typename T>
60 requires std::is_void_v<T> inline SyslogStreamWriter &operator<<(T *ptr)
61 {
62 if (!should_print)
63 return *this;
64
66 return *this;
67 }
68
69 template<typename E>
70 requires(std::is_enum_v<E>) inline SyslogStreamWriter &operator<<(E value)
71 {
72 if (!should_print)
73 return *this;
74
75 pos += snprintf(&fmtbuffer[pos], MOS_PRINTK_BUFFER_SIZE, "%d", (int) value);
76 return *this;
77 }
78
80 {
81 if (!should_print)
82 return *this;
83
85 return *this;
86 }
87
88 inline SyslogStreamWriter &operator<<(const char *str)
89 {
90 if (!should_print)
91 return *this;
92
94 return *this;
95 }
96
98 {
99 if (!should_print)
100 return *this;
101
102 pos += snprintf(&fmtbuffer[pos], MOS_PRINTK_BUFFER_SIZE, "%.*s", (int) sv.size(), sv.data());
103 return *this;
104 }
105
106 template<typename M, typename... Args>
108 {
109 mos::FormatImpl::print_m<M>(*this, fmt.targs);
110 return *this;
111 }
112
113 private:
114 explicit SyslogStreamWriter(DebugFeature feature, LogLevel level, RCCore *rcCore, SyslogBuffer &fmtbuffer);
115
116 private:
118 size_t pos = 0;
119
120 private:
122 const DebugFeature feature;
124 const bool should_print;
125 };
126
127 template<DebugFeature feature, LogLevel level>
129 {
130 static constexpr auto feature_value = feature;
131 static constexpr auto level_value = level;
132
133 template<typename T>
134 SyslogStreamWriter operator<<(const T &value) const
135 {
136 // copy-elision
137 return SyslogStreamWriter::NewStream(feature, level, &RefCounter, fmtBuffer) << value;
138 }
139
140 private:
143 };
144} // namespace mos
145
146#define DefineLogStream(name, level) \
147 constexpr auto inline m##name = mos::LoggingDescriptor<_none, LogLevel::level>(); \
148 template<DebugFeature feat> \
149 constexpr auto inline d##name = mos::LoggingDescriptor<feat, LogLevel::level>()
150
158#undef DefineLogStream
159
160#define f(_fmt) formatted_type(_fmt "")
161#define fmt(_fmt, ...) mos::Preformatted(formatted_type(_fmt ""), ##__VA_ARGS__)
162
163long do_syslog(LogLevel level, const char *file, const char *func, int line, const debug_info_entry *feat, const char *fmt, ...);
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
char args[3][16]
Definition avr_io.c:16
const CharT * data() const
MOSAPI const char *__restrict format
Definition mos_stdio.hpp:17
basic_string_view< char > string_view
std::array< char, MOS_PRINTK_BUFFER_SIZE > SyslogBuffer
Definition syslog.hpp:38
mos::shared_ptr< T > ptr
size_t size
Definition slab.cpp:32
static constexpr Stream & print_m(Stream &stream, std::tuple< TArgs... > args)
SyslogBuffer fmtBuffer
Definition syslog.hpp:141
static constexpr auto level_value
Definition syslog.hpp:131
SyslogStreamWriter operator<<(const T &value) const
Definition syslog.hpp:134
static constexpr auto feature_value
Definition syslog.hpp:130
Preformatted(M, Args... args)
Definition syslog.hpp:35
const std::tuple< Args... > targs
Definition syslog.hpp:34
SyslogStreamWriter(DebugFeature feature, LogLevel level, RCCore *rcCore, SyslogBuffer &fmtbuffer)
Definition syslog.cpp:86
SyslogStreamWriter & operator<<(const char *str)
Definition syslog.hpp:88
SyslogStreamWriter & operator<<(mos::string_view sv)
Definition syslog.hpp:97
const DebugFeature feature
Definition syslog.hpp:122
SyslogStreamWriter operator<<(const Preformatted< M, Args... > &fmt)
Definition syslog.hpp:107
SyslogStreamWriter & operator<<(T value)
Definition syslog.hpp:50
const LogLevel level
Definition syslog.hpp:123
SyslogStreamWriter & operator<<(char c)
Definition syslog.hpp:79
static SyslogStreamWriter NewStream(DebugFeature feature, LogLevel level, RCCore *rcCore, SyslogBuffer &buf)
Definition syslog.hpp:42
SyslogStreamWriter & operator<<(T *ptr)
Definition syslog.hpp:60
const bool should_print
Definition syslog.hpp:124
SyslogBuffer & fmtbuffer
Definition syslog.hpp:117
struct Console * printk_console
Definition printk.cpp:14
#define DefineLogStream(name, level)
Definition syslog.hpp:146
long do_syslog(LogLevel level, const char *file, const char *func, int line, const debug_info_entry *feat, const char *fmt,...)
Definition syslog.cpp:55
void lprintk(LogLevel loglevel, const char *format,...)
Definition printk.cpp:104
#define fmt(_fmt,...)
Definition syslog.hpp:161
LogLevel
Definition syslog.hpp:15
int snprintf(char *__restrict str, size_t size, const char *__restrict format,...)
Definition mos_stdio.cpp:16
unsigned long long u64
Definition types.h:19