6 #include <satcat5/ethernet.h>
8 #include <satcat5/ip_core.h>
12 namespace io = satcat5::io;
13 namespace log = satcat5::log;
18 #ifndef SATCAT5_LOG_EMOJI
19 #define SATCAT5_LOG_EMOJI 1
28 if (g_log_dst) {g_log_dst = 0; ok =
false;}
33 inline char hex_lookup(
unsigned val) {
56 inline void next_digit(
char* out,
unsigned& wridx, T& val, T zpad, T place) {
60 while (val >= place) {++digit; val -= place;}
63 if ((digit >
'0') || (wridx > 0) || (place <= zpad))
71 static constexpr
unsigned LOG_ITOA_BUFF32 = 11;
72 static unsigned log_itoa32(
char* out, u32 val, u32 zpad) {
74 next_digit<u32>(out, wridx, val, zpad, 1000000000u);
75 next_digit<u32>(out, wridx, val, zpad, 100000000u);
76 next_digit<u32>(out, wridx, val, zpad, 10000000u);
77 next_digit<u32>(out, wridx, val, zpad, 1000000u);
78 next_digit<u32>(out, wridx, val, zpad, 100000u);
79 next_digit<u32>(out, wridx, val, zpad, 10000u);
80 next_digit<u32>(out, wridx, val, zpad, 1000u);
81 next_digit<u32>(out, wridx, val, zpad, 100u);
82 next_digit<u32>(out, wridx, val, zpad, 10u);
83 out[wridx++] = val +
'0';
88 static constexpr
unsigned LOG_ITOA_BUFF64 = 21;
89 static unsigned log_itoa64(
char* out, u64 val, u64 zpad) {
91 next_digit<u64>(out, wridx, val, zpad, 10000000000000000000ull);
92 next_digit<u64>(out, wridx, val, zpad, 1000000000000000000ull);
93 next_digit<u64>(out, wridx, val, zpad, 100000000000000000ull);
94 next_digit<u64>(out, wridx, val, zpad, 10000000000000000ull);
95 next_digit<u64>(out, wridx, val, zpad, 1000000000000000ull);
96 next_digit<u64>(out, wridx, val, zpad, 100000000000000ull);
97 next_digit<u64>(out, wridx, val, zpad, 10000000000000ull);
98 next_digit<u64>(out, wridx, val, zpad, 1000000000000ull);
99 next_digit<u64>(out, wridx, val, zpad, 100000000000ull);
100 next_digit<u64>(out, wridx, val, zpad, 10000000000ull);
101 next_digit<u64>(out, wridx, val, zpad, 1000000000ull);
102 next_digit<u64>(out, wridx, val, zpad, 100000000ull);
103 next_digit<u64>(out, wridx, val, zpad, 10000000ull);
104 next_digit<u64>(out, wridx, val, zpad, 1000000ull);
105 next_digit<u64>(out, wridx, val, zpad, 100000ull);
106 next_digit<u64>(out, wridx, val, zpad, 10000ull);
107 next_digit<u64>(out, wridx, val, zpad, 1000ull);
108 next_digit<u64>(out, wridx, val, zpad, 100ull);
109 next_digit<u64>(out, wridx, val, zpad, 10ull);
110 out[wridx++] = val +
'0';
118 return SATCAT5_LOG_EMOJI ?
"\xE2\x98\xA0\xEF\xB8\x8F" :
"Crit";
120 return SATCAT5_LOG_EMOJI ?
"\xE2\x9D\x8C" :
"Error";
122 return SATCAT5_LOG_EMOJI ?
"\xE2\x9A\xA0\xEF\xB8\x8F" :
"Warn";
124 return SATCAT5_LOG_EMOJI ?
"\xF0\x9F\x92\xAC" :
"Info";
126 return SATCAT5_LOG_EMOJI ?
"\xE2\x9A\x99\xEF\xB8\x8F" :
"Debug";
135 #if SATCAT5_ALLOW_DELETION
136 log::EventHandler::~EventHandler() {
150 s8 priority,
unsigned nbytes,
const char* msg)
154 m_dst->write_str(
"\t");
155 m_dst->write_bytes(nbytes, msg);
156 m_dst->write_str(
"\r\n");
157 m_dst->write_finalize();
161 : m_priority(priority)
167 : m_priority(priority)
172 Log::Log(s8 priority,
const char* str1,
const char* str2)
173 : m_priority(priority)
180 Log::Log(s8 priority,
const void* str,
unsigned nbytes)
181 : m_priority(priority)
183 m_buff.
wr_fix((
const char*)str, nbytes);
193 dst->
log_event(m_priority, m_buff.len(), m_buff.m_buff);
205 m_buff.
wr_h32(val ? 1:0, 1);
242 for (
unsigned a = 0 ; a < nbytes ; ++a)
292 const char* end = str +
len;
293 while (str != end && m_wridx < SATCAT5_LOG_MAXLEN)
294 m_buff[m_wridx++] = *(str++);
299 while (*str && m_wridx < SATCAT5_LOG_MAXLEN)
300 m_buff[m_wridx++] = *(str++);
304 for (
unsigned a = 0 ; m_wridx < SATCAT5_LOG_MAXLEN && a < nhex ; ++a) {
305 unsigned shift = 4 * (nhex-a-1);
306 m_buff[m_wridx++] = hex_lookup(val >> shift);
311 for (
unsigned a = 0 ; m_wridx < SATCAT5_LOG_MAXLEN && a < nhex ; ++a) {
312 unsigned shift = 4 * (nhex-a-1);
313 m_buff[m_wridx++] = hex_lookup(val >> shift);
318 char temp[LOG_ITOA_BUFF32];
319 log_itoa32(temp, val, zpad);
324 char temp[LOG_ITOA_BUFF64];
325 log_itoa64(temp, val, zpad);
330 wr_str(val < 0 ?
"-" :
"+");
331 wr_d32(satcat5::util::abs_s32(val), zpad);
335 wr_str(val < 0 ?
"-" :
"+");
336 wr_d64(satcat5::util::abs_s64(val), zpad);
Abstract API for reading byte-streams and packets.
u8 read_u8()
One of many functions for reading integer/floating point values, see details.
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
Abstract API for writing byte-streams and packets.
void write_str(const char *str)
Write the contents of a null-terminated string.
virtual bool write_finalize()
Mark end of frame and release temporary working data.
Defines the interface for accepting Log messages.
virtual void log_event(s8 priority, unsigned nbytes, const char *msg)=0
Callback for each formatted Log message.
EventHandler()
Constructor automatically manages the list of active handler objects.
Internal buffer used by the Log class.
unsigned len() const
Number of characters written to this buffer.
void wr_s32(s32 val, unsigned zpad=0)
Write a signed integer (s32) in decimal format.
void wr_d64(u64 val, unsigned zpad=0)
Write an unsigned integer (u64) in decimal format.
void wr_s64(s64 val, unsigned zpad=0)
Write a signed integer (s64) in decimal format.
void wr_h32(u32 val, unsigned nhex=8)
Write an integer (u32) in hexadecimal format.
void wr_fix(const char *str, unsigned len)
Write a fixed-length UTF-8 string.
void wr_str(const char *str)
Write a null-terminated UTF-8 string.
void wr_h64(u64 val, unsigned nhex=16)
Write an integer (u64) in hexadecimal format.
const char * c_str()
Buffer contents, in the form of a null-terminated string.
void wr_dec(u32 val)
Legacy alias for wr_d32.
void wr_d32(u32 val, unsigned zpad=0)
Write an unsigned integer (u32) in decimal format.
The Log class creates and formats one log message.
Log & write10(s32 val)
Print integer as a decimal value with no leading zeros.
Log(s8 priority)
Constructor sets priority and optionally the first string.
~Log()
Destructor sends the message.
Log & write(const char *str)
Formatting methods for various data types.
ToWriteable(satcat5::io::Writeable *dst)
Bind this object to the designated output interface.
void log_event(s8 priority, unsigned nbytes, const char *msg) override
Implement the EventHandler API.
static void remove(T *&list, T *item)
Remove the designated item from the list.
static void add(T *&list, T *item)
Add new item to front or back, whichever is simpler.
static T * next(const T *item)
Fetch pointer to the next item.
I/O interface core definitions.
Diagnostic logging to UART and/or Ethernet ports.
constexpr s8 CRITICAL
Define basic priority codes for log messages.
const char * priority_label(s8 priority)
Convert priority code to a human-readable UTF-8 string.
constexpr s8 WARNING
Define basic priority codes for log messages.
constexpr s8 ERROR
Define basic priority codes for log messages.
bool pre_test_reset()
Hard-reset of global variables at the start of each unit test.
constexpr s8 INFO
Define basic priority codes for log messages.
An Ethernet MAC address (with serializable interface).
void log_to(satcat5::log::LogBuffer &wr) const
Format this field as a human-readable string.
IPv4 address is a 32-bit unsigned integer.
void log_to(satcat5::log::LogBuffer &wr) const
Format this IP address in standard form, e.g., "192.168.1.2".
Miscellaneous mathematical utility functions.