SatCat5
log.h
Go to the documentation of this file.
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
44 
45 #pragma once
46 
47 #include <satcat5/list.h>
48 #include <satcat5/types.h>
49 
50 // Default parameters:
51 #ifndef SATCAT5_LOG_MAXLEN // Maximum string length per message
52 #define SATCAT5_LOG_MAXLEN 255
53 #endif
54 
55 #ifndef SATCAT5_LOG_CONCISE // Enable concise syntax?
56 #define SATCAT5_LOG_CONCISE 1
57 #endif
58 
59 #ifndef SATCAT5_WELCOME_EMOJI // UTF-8 string: [satellite] [smiling cat] [five o'clock]
60 #define SATCAT5_WELCOME_EMOJI "\xf0\x9f\x9b\xb0\xef\xb8\x8f\xf0\x9f\x90\xb1\xf0\x9f\x95\x94"
61 #endif
62 
63 namespace satcat5 {
64  namespace log {
72  class EventHandler {
73  public:
76  virtual void log_event(s8 priority, unsigned nbytes, const char* msg) = 0;
77  protected:
79  EventHandler();
80  ~EventHandler() SATCAT5_OPTIONAL_DTOR;
81  private:
82  friend satcat5::util::ListCore;
83  satcat5::log::EventHandler* m_next;
84  };
85 
94  class ToWriteable final : public satcat5::log::EventHandler {
95  public:
97  explicit ToWriteable(satcat5::io::Writeable* dst);
98 
100  void log_event(s8 priority, unsigned nbytes, const char* msg) override;
101 
102  private:
103  satcat5::io::Writeable* const m_dst;
104  };
105 
109  constexpr s8 DEBUG = -20;
110  constexpr s8 INFO = -10;
111  constexpr s8 WARNING = 0;
112  constexpr s8 ERROR = +10;
113  constexpr s8 CRITICAL = +20;
115 
121  const char* priority_label(s8 priority);
122 
134  class LogBuffer final {
135  public:
137  LogBuffer() : m_wridx(0) {}
138 
140  const char* c_str();
141 
143  void wr_fix(const char* str, unsigned len);
145  void wr_str(const char* str);
148  void wr_h32(u32 val, unsigned nhex = 8);
151  void wr_h64(u64 val, unsigned nhex = 16);
155  void wr_d32(u32 val, unsigned zpad = 0);
158  void wr_d64(u64 val, unsigned zpad = 0);
161  void wr_s32(s32 val, unsigned zpad = 0);
164  void wr_s64(s64 val, unsigned zpad = 0);
166  inline void wr_dec(u32 val) {wr_d32(val);}
168  inline void wr_hex(u32 val) {wr_h32(val);}
169 
171  unsigned len() const {return m_wridx;}
172 
173  private:
174  friend satcat5::log::Log;
175 
176  // Forbid use of copy constructor.
177  LogBuffer(const LogBuffer&) = delete;
178  LogBuffer& operator=(const LogBuffer&) = delete;
179 
180  // Null-terminate the working buffer.
181  inline void terminate() {m_buff[m_wridx] = 0;}
182 
183  // Internal buffer.
184  unsigned m_wridx;
185  char m_buff[SATCAT5_LOG_MAXLEN+1];
186  };
187 
195  class Log final {
196  public:
199  explicit Log(s8 priority);
200  Log(s8 priority, const char* str);
201  Log(s8 priority, const char* str1, const char* str2);
202  Log(s8 priority, const void* str, unsigned nbytes);
204 
206  ~Log();
207 
216  Log& write(const char* str);
217  Log& write(bool val);
218  Log& write(u8 val);
219  Log& write(u16 val);
220  Log& write(u32 val);
221  Log& write(u64 val);
222  Log& write(satcat5::io::Readable* rd);
223  Log& write(const u8* val, unsigned nbytes);
224  Log& write(const satcat5::eth::MacAddr& mac);
225  Log& write(const satcat5::ip::Addr& ip);
227 
233  Log& write10(s32 val); // Signed (e.g., "+1234" or "-1234")
234  Log& write10(s64 val);
235  Log& write10(u32 val); // Unsigned (e.g., "1234")
236  Log& write10(u64 val);
237  inline Log& write10(s8 val) {return write10(s32(val));}
238  inline Log& write10(s16 val) {return write10(s32(val));}
239  inline Log& write10(u8 val) {return write10(u32(val));}
240  inline Log& write10(u16 val) {return write10(u32(val));}
242 
251  template <class T> inline Log& write_obj(const T& obj)
252  {obj.log_to(m_buff); return *this;}
253 
254  private:
255  // Forbid use of copy constructor.
256  Log(const Log&) = delete;
257  Log& operator=(const Log&) = delete;
258 
259  const s8 m_priority;
261  };
262 
267  bool pre_test_reset();
268  }
269 }
270 
271 // Enable concise syntax?
272 // This requires adding a few objects to the top-level namespace.
273 #if SATCAT5_LOG_CONCISE
274  using satcat5::log::Log;
275  constexpr s8 LOG_DEBUG = satcat5::log::DEBUG;
276  constexpr s8 LOG_INFO = satcat5::log::INFO;
277  constexpr s8 LOG_WARNING = satcat5::log::WARNING;
278  constexpr s8 LOG_ERROR = satcat5::log::ERROR;
279  constexpr s8 LOG_CRITICAL = satcat5::log::CRITICAL;
280 #endif
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Defines the interface for accepting Log messages.
Definition: log.h:72
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.
Definition: log.cc:129
Internal buffer used by the Log class.
Definition: log.h:134
unsigned len() const
Number of characters written to this buffer.
Definition: log.h:171
LogBuffer()
Constructor for an empty buffer.
Definition: log.h:137
void wr_hex(u32 val)
Legacy alias for wr_h32.
Definition: log.h:168
void wr_dec(u32 val)
Legacy alias for wr_d32.
Definition: log.h:166
The Log class creates and formats one log message.
Definition: log.h:195
Log & write10(s8 val)
Print integer as a decimal value with no leading zeros.
Definition: log.h:237
Log & write10(u8 val)
Print integer as a decimal value with no leading zeros.
Definition: log.h:239
Log & write10(s16 val)
Print integer as a decimal value with no leading zeros.
Definition: log.h:238
Log & write_obj(const T &obj)
Templated wrapper for custom output formatting.
Definition: log.h:251
Log & write10(u16 val)
Print integer as a decimal value with no leading zeros.
Definition: log.h:240
Copy Log messages to any Writeable interface.
Definition: log.h:94
Templated functions for manipulating singly-linked lists.
constexpr s8 CRITICAL
Define basic priority codes for log messages.
Definition: log.h:113
const char * priority_label(s8 priority)
Convert priority code to a human-readable UTF-8 string.
Definition: log.cc:116
constexpr s8 WARNING
Define basic priority codes for log messages.
Definition: log.h:111
constexpr s8 ERROR
Define basic priority codes for log messages.
Definition: log.h:112
constexpr s8 DEBUG
Define basic priority codes for log messages.
Definition: log.h:109
constexpr s8 INFO
Define basic priority codes for log messages.
Definition: log.h:110
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
Basic type aliases and prototypes used throughout SatCat5.