RF24Log  0.1.3
Unified logging library
NativePrintLogger.cpp
Go to the documentation of this file.
1 
13 #ifndef ARDUINO
14 #include <cstdio> // sprintf()
15 
16 #if defined (PICO_BUILD)
17 #include <pico/stdlib.h> // to_ms_since_boot(), get_absolute_time()
18 #else
19 #include <ctime> // time_t, struct tm*, time(), localtime(), strftime()
20 #endif
21 
22 #include "NativePrintLogger.h"
23 
24 /****************************************************************************/
25 
27 {
28 }
29 
30 /****************************************************************************/
31 
33 {
34  #if defined(PICO_BUILD)
35  printf_P("%10lu", to_ms_since_boot(get_absolute_time()));
37  #else // !defined (PICO_BUILD) && !defined (ARDUINO)
38  char buffer[21];
39  time_t rawtime;
40  struct tm* timeinfo;
41  time(&rawtime);
42  timeinfo = localtime(&rawtime);
43 
44  strftime(buffer, 20, "%F:%H:%M:%S", timeinfo);
45  buffer[20] = RF24LOG_DELIMITER;
46  printf_P("%s", buffer);
47  #endif // defined (PICO_BUILD) && !defined (ARDUINO)
48 }
49 
50 /****************************************************************************/
51 
52 void NativePrintLogger::appendChar(char data, uint16_t depth)
53 {
54  while (depth > 0)
55  {
56  --depth;
57  printf_P("%c", data);
58  }
59 }
60 
61 /****************************************************************************/
62 
64 {
65 
66  printf_P("%li", (long)data);
67 }
68 
69 /****************************************************************************/
70 
71 void NativePrintLogger::appendUInt(unsigned long data, uint8_t base)
72 {
73  if (base == 2)
74  {
75  if (!data)
76  {
77  printf_P("0"); // output a zero
78  return;
79  }
80  char buffer[64];
81  uint8_t index = 0;
82  while (data)
83  {
84  // get representation as a reversed string
85  buffer[index] = (data & 1) + 48;
86  data >>= 1;
87  ++index;
88  }
89  while (index)
90  {
91  appendChar(buffer[--index]); // dump reversed string 1 char at a time
92  }
93  }
94  else if (base == 8)
95  {
96  printf_P("%o", (unsigned int)data);
97  }
98  else if (base == 16)
99  {
100  printf_P("%X", (unsigned int)data);
101  }
102  else
103  {
104  printf_P("%lu", (unsigned long)data);
105  }
106 }
107 
108 /****************************************************************************/
109 
110 void NativePrintLogger::appendDouble(double data, uint8_t precision)
111 {
112  char fmt_buf[64];
113  sprintf(fmt_buf, "%%.%dF", precision); // prepares a fmt str ("%.nF")
114  printf_P(fmt_buf, data);
115 }
116 
117 /****************************************************************************/
118 
119 void NativePrintLogger::appendStr(const char* data)
120 {
121  printf_P("%s", data);
122 }
123 
124 #endif /* ARDUINO */
handler for printf-based function calls
#define printf_P
macro pointing to the natively available printf()
#define RF24LOG_DELIMITER
Change The Delimiter character used in the header prefix of log messages.
void appendTimestamp()
output a timestamp
void appendChar(char data, uint16_t depth=1)
append a character a number of times
void appendUInt(unsigned long data, uint8_t base=10)
append an ‘unsigned’ (only +) number
void appendStr(const char *data)
append a c-string
void appendInt(long data)
append a signed (+/-) number
NativePrintLogger()
Construct a new NativePrintLogger object using stdout.
void appendDouble(double data, uint8_t precision=2)
append a floating point number