NetBurner 3.5.0
PDF Version
 
fastlog.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#include <predef.h>
6#include <stdint.h>
7#include <stdarg.h>
8#include <nbrtos.h>
9#include <string.h>
10
11#ifndef FASTLOG_BUFSIZ
12#define FASTLOG_BUFSIZ (0x80000)
13#endif
14//#define ENABLE_FASTLOG
15
16extern "C" {
17 int NBFastLog(void *logger, uint16_t fileId, uint16_t lineNum, const char *format, ...);
18 void NBFastLogDump(void *l, bool clearAfter, int type);
19}
20
21int loggerputchars(void *data, const char *c, int n);
22namespace fastlog {
23
24typedef enum {
25 eEntryType_Empty,
26 eEntryType_ULong,
27 eEntryType_String,
28 eEntryType_BinPtr,
29} eEntryType_t;
30
31class logger {
32private:
33 class CritObj {
34 logger *pLogger;
35 public:
36 CritObj(logger *l)
37 : pLogger(l)
38 {
39// USER_ENTER_CRITICAL()
40// return;
41 if (pLogger->useFromIsr)
42 {
43 USER_ENTER_CRITICAL()
44 }
45 else
46 {
47 pLogger->crit.Enter();
48 }
49 }
50 ~CritObj()
51 {
52// USER_EXIT_CRITICAL()
53// return;
54 if (pLogger->useFromIsr)
55 {
56 USER_EXIT_CRITICAL()
57 }
58 else
59 {
60 pLogger->crit.Leave();
61 }
62 }
63 };
64 struct entry_t {
65 uint16_t tickOffset;
66 uint16_t line;
67 uint16_t fileId;
68 uint8_t tcb_idx;
69 eEntryType_t type;
70 union {
71 uint32_t data_ulong;
72 struct {
73 uint16_t dataLen;
74 uint8_t data_bin[];
75 }__attribute__((packed));
76 struct {
77 uint16_t strLen;
78 char data_str[];
79 }__attribute__((packed));
80 };
81 uint32_t getSize() const;
82 } __attribute__((packed));
83public:
84 class fileEntry {
85 fileEntry *pNext;
86 const char *fileName;
87 uint16_t id;
88 public:
89 fileEntry(const char *fileName);
90 inline uint16_t getId() const { return id; }
91 inline const char *getFilename() const { return fileName; }
92 inline const fileEntry *getNext() const { return pNext; }
93 inline static const fileEntry *getFirst()
94 { return fileIds; }
95
96 private:
97 static fileEntry * fileIds;
98 static uint16_t count;
99 };
100
101 enum eFullAction_t {
102 eFull_Roll,
103 eFull_Drop,
104 eFull_DumpAndClear
105 };
106private:
107 int logFd;
108 uint32_t start;
109 uint32_t end;
110 uint32_t rollovers;
111 tick_t startTick;
112 tick_t endTick;
113 OS_CRIT crit;
114 bool useFromIsr;
115 bool empty;
116 eFullAction_t fullAction;
117 uint32_t sentinel;
118 uint8_t logBuf[FASTLOG_BUFSIZ];
119
120 bool getNextByte(uint32_t *idx, uint8_t *b);
121 bool DoFull(uint32_t entrySize);
122 bool allocateEntry(uint32_t newEnd, uint32_t entrySize);
123 void createEntry(entry_t &e);
124 const char *getFileName(uint16_t id);
125
126 void dumpBin(uint32_t idx, uint16_t len);
127 void dumpstr(uint32_t idx, uint16_t len);
128 bool getEntry(uint32_t *idx, entry_t *e);
129 void dumpEntry(tick_t prevTick, entry_t &e, uint32_t *dataStart);
130
131 int log_vsprintf(uint16_t fileId, uint16_t lineNum, const char *format, va_list &vl);
132public:
133 logger(int logFd = 1, eFullAction_t fullAction = eFull_Roll, bool useFromIsr = false);
134
135 void log(uint16_t fileId, uint16_t lineNum, uint16_t len, uint8_t *pdata, eEntryType_t t);
136
137 inline void log_line(uint16_t fileId, uint16_t lineNum)
138 { log(fileId, lineNum, 0, NULL, eEntryType_Empty); }
139 inline void log_ulong(uint16_t fileId, uint16_t lineNum, uint32_t val)
140 { log(fileId, lineNum, sizeof(val), (uint8_t*)&(val), eEntryType_ULong); }
141
142 inline void log_bin(uint16_t fileId, uint16_t lineNum, uint16_t len, uint8_t *pdata)
143 { log(fileId, lineNum, len, pdata, eEntryType_BinPtr); }
144
145 inline void log_str(uint16_t fileId, uint16_t lineNum, const char *s)
146 { log(fileId, lineNum, strlen(s), (uint8_t*)s, eEntryType_String); }
147
148 int log_sprintf(uint16_t fileId, uint16_t lineNum, const char *format, ...);
149
150 void dumpLog(bool clearAfter = true, int type = -1);
151 void clear();
152 void setLogFd(int fd) { logFd = fd; }
153
154 friend int ::loggerputchars(void *data, const char *c, int n);
155 friend int ::NBFastLog(void *logger, uint16_t fileId, uint16_t lineNum, const char *format, ...);
156};
157
158}
159
160extern fastlog::logger FastStdLogger;
161
162#ifdef ENABLE_FASTLOG
163#define FASTLOG_INIT_FILE() \
164static fastlog::logger::fileEntry _fastlog_ThisFile(__FILE__)
165
166#define FASTLOG_LINE(_logger) (_logger).log_line(_fastlog_ThisFile.getId(), __LINE__)
167#define FASTLOG_ULONG(_logger, val) (_logger).log_ulong(_fastlog_ThisFile.getId(), __LINE__, (val))
168#define FASTLOG_BINARY(_logger, len, data) (_logger).log_bin(_fastlog_ThisFile.getId(), __LINE__, (len), (data))
169#define FASTLOG_STRING(_logger, s) (_logger).log_str(_fastlog_ThisFile.getId(), __LINE__, (s))
170#define FASTLOG_SPRINTF(_logger, ...) (_logger).log_sprintf(_fastlog_ThisFile.getId(), __LINE__, __VA_ARGS__)
171
172#define FASTLOG_SHOWLOG(_logger) (_logger).dumpLog()
173#else
174#define FASTLOG_INIT_FILE()
175
176#define FASTLOG_LINE(_logger)
177#define FASTLOG_ULONG(_logger, val)
178#define FASTLOG_BINARY(_logger, len, data)
179#define FASTLOG_STRING(_logger, s)
180#define FASTLOG_SPRINTF(_logger, ...)
181
182#define FASTLOG_SHOWLOG(_logger)
183#endif
NetBurner Real-Time Operating System (NBRTOS) API.
An OS_CRIT object is used to establish critical sections of code that can only be run by one task at ...
Definition nbrtos.h:1084