Index

lognestmonster / 03180fa

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
14425 Nov 2019 18:57ac423c9Rearrange code to stop compiler warningsJosh Stockin11216N

Blob @ lognestmonster / src / c / lognestmonster.h

text/plain10371 bytesdownload raw
1// lognestmonster Copyright (c) 2019 Joshua 'joshuas3' Stockin
2// lognestmonster.h
3// C header file for implementation of the lognestmonster logging library
4
5// <https://github.com/JoshuaS3/lognestmonster/>.
6
7
8// This file is part of lognestmonster.
9
10// lognestmonster is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14
15// lognestmonster is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19
20// You should have received a copy of the GNU General Public License
21// along with lognestmonster. If not, see <https://www.gnu.org/licenses/>.
22
23#ifndef __LOGNESTMONSTER__
24#define __LOGNESTMONSTER__ 1
25
26// SEMANTICS
27// internal definitions: lnm_lower_camel_case
28// public definitions: lnmUpperCamelCase
29
30// stdc inclusions
31
32#include <stdint.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/time.h>
36
37
38// Base definitions
39
40enum lnmVerbosityLevel {lnmInfo, lnmDebug, lnmVerbose, lnmVeryVerbose, lnmWarning, lnmError};
41typedef uint8_t * lnmItem;
42typedef uint8_t * lnmQueue;
43
44
45// Pushable structure
46
47typedef struct {
48 uint16_t length;
49 lnmItem * pushed;
50} lnm_pushable;
51
52lnm_pushable * lnm_new_pushable() {
53 lnm_pushable * new_pushable = malloc(sizeof(lnm_pushable));
54 new_pushable->length = 0;
55 new_pushable->pushed = malloc(0);
56 return new_pushable;
57}
58
59void lnm_pushable_push(lnm_pushable * pushable, lnmItem item) {
60 if (pushable->length+1 >= 65535) {
61 printf("lognestmonster (lnm_pushable_push): pushable reached cap length 65535. exiting...\n");
62 exit(1);
63 }
64 pushable->pushed = realloc(pushable->pushed, sizeof(lnmItem)*(pushable->length+1)); // reallocate with size: length+1
65 pushable->pushed[pushable->length] = item;
66 pushable->length += 1;
67}
68
69int lnm_pushable_indexof(lnm_pushable * pushable, lnmItem item) {
70 int len = pushable->length;
71 for (int iter = 0; iter<len; iter++) {
72 if (item == pushable->pushed[iter]) return iter;
73 }
74 return -1;
75}
76
77void lnm_pushable_remove(lnm_pushable * pushable, int index) {
78 if (index>=pushable->length || index < 0) {
79 printf("lognestmonster (lnm_pushable_remove): attempt to remove index out of pushable bounds. exiting...\n");
80 exit(1);
81 }
82 lnmItem * new_pushed = malloc(sizeof(lnmItem)*(pushable->length-1)); // map array excluding index
83 for (int iter = 0; iter<index; iter++) {
84 new_pushed[iter] = pushable->pushed[iter];
85 }
86 for (int iter = index+1; iter<pushable->length; iter++) {
87 new_pushed[iter-1] = pushable->pushed[iter];
88 }
89 free(pushable->pushed);
90 pushable->length--;
91 pushable->pushed = new_pushed;
92}
93
94
95// Statement and event structure definitions
96
97typedef struct {
98 uint8_t type:1; // Used internally; 0 = statement, 1 = event
99 uint8_t boolpushed:1; // whether or not this log item has been pushed
100 lnm_pushable * pushed; // array of memory locations for lnm_log_event and lnm_log_statement structs
101} lnm_log_event;
102
103typedef struct {
104 // word 1, 4 bytes data 4 bytes padding
105 uint8_t type:1; // Used internally; 0 = statement, 1 = event
106 uint8_t boolpushed:1; // whether or not this log item has been pushed
107 uint8_t verbosity:3; // lnmVerbosityLevel, 0-5
108 uint8_t tag_size; // character length of the tag
109 uint16_t message_size; // character length of the message
110
111 // word 2, 8 bytes data
112 uint64_t timestamp; // 64-bit millisecond timestamp
113
114 // word 3, 8 bytes data
115 char * log; // tag string + message string
116} lnm_log_statement;
117
118
119// Queue structure definition
120
121typedef struct {
122 char * name;
123 char * out_path;
124 uint64_t timestamp;
125 lnm_pushable * pushed;
126} lnm_queue;
127
128
129// Library utilities
130
131unsigned long lnm_getus(void) {
132 struct timeval current_time;
133 gettimeofday(&current_time, NULL);
134 unsigned long ms = (current_time.tv_sec*1000000+current_time.tv_usec);
135 return ms;
136}
137
138unsigned long lnm_getms(void) {
139 return lnm_getus()/1000;
140}
141
142
143int lnm_isstatement(lnmItem item) {
144 lnm_log_statement * s = (lnm_log_statement *)item;
145 return !s->type;
146}
147
148
149// Item registry utils
150
151lnm_pushable * lnm_registered_queues;
152lnm_pushable * lnm_registered_items;
153
154void lnm_registry_update(void) { // scan each registered item
155 for (int iter = 0; iter < lnm_registered_items->length; iter++) {
156 lnm_log_statement * s = (lnm_log_statement *)lnm_registered_items->pushed[iter];
157 if (s->boolpushed == 1) { // if the registered item has been pushed elsewhere, remove it from the top level of the registry
158 lnm_pushable_remove(lnm_registered_items, iter);
159 iter--;
160 }
161 }
162}
163
164
165// Core library
166
167lnmQueue lnmQueueInit(char * name, char * out_path) {
168 if (lnm_registered_queues == NULL) {
169 lnm_registered_queues = lnm_new_pushable();
170 }
171 if (lnm_registered_items == NULL) {
172 lnm_registered_items = lnm_new_pushable();
173 }
174
175 lnm_queue * new_queue = malloc(sizeof(lnm_queue));
176 new_queue->name = malloc(strlen(name)+1);
177 new_queue->out_path = malloc(strlen(out_path)+1);
178 strcpy(new_queue->name, name);
179 strcpy(new_queue->out_path, out_path);
180 new_queue->pushed = lnm_new_pushable();
181
182 lnm_pushable_push(lnm_registered_queues, (lnmQueue)new_queue);
183 return (lnmQueue)new_queue;
184}
185
186lnmQueue lnmQueueByName(char * name) {
187 if (lnm_registered_queues == NULL) {
188 printf("lognestmonster (lnmQueueByName): queue registry is nonexistant. exiting...\n");
189 exit(1);
190 }
191 if (lnm_registered_queues->length == 0) {
192 printf("lognestmonster (lnmQueueByName): queue registry is empty. exiting...\n");
193 exit(1);
194 }
195 for (int iter = 0; iter<lnm_registered_queues->length; iter++) {
196 lnm_queue * iterqueue = (lnm_queue *)lnm_registered_queues->pushed[iter];
197 if (strcmp(iterqueue->name, name)==0) {
198 return (lnmQueue)iterqueue;
199 }
200 }
201 printf("lognestmonster (lnmQueueByName): queue not found in registry. exiting...\n");
202 exit(1);
203}
204
205
206lnmItem lnmStatement(uint8_t verbosity, char * tag, char * message) {
207 lnm_log_statement * new_statement = malloc(sizeof(lnm_log_statement));
208 new_statement->type = 0;
209 new_statement->verbosity = verbosity;
210 new_statement->timestamp = lnm_getus();
211 int tlen = strlen(tag);
212 if (tlen > 255 || tlen < 0) {
213 printf("lognestmonster (lnmStatement): tag length %i is longer than the cap 255 characters. exiting...\n", tlen);
214 exit(1);
215 }
216 int mlen = strlen(message);
217 if (mlen > 65535 || mlen < 0) {
218 printf("lognestmonster (lnmStatement): message length %i is longer than the cap 65535 characters. exiting...\n", mlen);
219 exit(1);
220 }
221 new_statement->tag_size = tlen;
222 new_statement->message_size = mlen;
223 new_statement->log = malloc(tlen+mlen+1);
224 strcpy(new_statement->log, tag);
225 strcat(new_statement->log, message);
226 lnm_registry_update();
227 lnm_pushable_push(lnm_registered_items, (lnmItem)new_statement);
228 return (lnmItem)new_statement;
229}
230
231
232lnmItem lnmEvent(void) {
233 lnm_log_event * new_event = malloc(sizeof(lnm_log_event));
234 new_event->type = 1;
235 new_event->pushed = lnm_new_pushable();
236 lnm_registry_update();
237 lnm_pushable_push(lnm_registered_items, (lnmItem)new_event);
238 return (lnmItem)new_event;
239}
240
241void lnmEventPush(lnmItem event, lnmItem item) {
242 if (event == item) {
243 printf("lognestmonster (lnmEventPush): attempt to push event to self. exiting...\n");
244 exit(1);
245 }
246 lnm_log_statement * item_cast = (lnm_log_statement *)item;
247 if (item_cast->boolpushed == 1) {
248 printf("lognestmonster (lnmEventPush): attempt to push an already-pushed log item. exiting...\n");
249 exit(1);
250 }
251 lnm_log_event * event_t = (lnm_log_event *)event;
252 if (event_t->type != 1) {
253 printf("lognestmonster (lnmEventPush): cannot cast non-event to event type. exiting...\n");
254 exit(1);
255 }
256 lnm_pushable_push(event_t->pushed, item);
257 item_cast->boolpushed = 1;
258 lnm_registry_update();
259}
260
261void lnmEventPushS(lnmItem event, uint8_t verbosity, char * tag, char * message) {
262 lnmItem statement = lnmStatement(verbosity, tag, message);
263 lnmEventPush(event, statement);
264}
265
266lnmItem lnmEventI(lnmItem item) {
267 lnmItem event = lnmEvent();
268 lnmEventPush(event, item);
269 return event;
270}
271
272lnmItem lnmEventS(uint8_t verbosity, char * tag, char * message) {
273 lnmItem statement = lnmStatement(verbosity, tag, message);
274 return lnmEventI(statement);
275}
276
277
278void lnm_debug_tabs(int count) {
279 for (int i = 0; i < count; i++) {
280 printf(" ");
281 }
282}
283
284void lnm_debug_parse_item(lnmItem item, int tabcount) {
285 if (lnm_isstatement(item)) {
286 lnm_log_statement * statement = (lnm_log_statement *) item;
287 lnm_debug_tabs(tabcount);
288 printf("Statement {\n");
289
290 lnm_debug_tabs(tabcount+1);
291 char * verbosity;
292 switch (statement->verbosity) {
293 case 0:
294 verbosity = "INFO";
295 break;
296 case 1:
297 verbosity = "DEBUG";
298 break;
299 case 2:
300 verbosity = "VERBOSE";
301 break;
302 case 3:
303 verbosity = "VERYVERBOSE";
304 break;
305 case 4:
306 verbosity = "WARNING";
307 break;
308 case 5:
309 verbosity = "ERROR";
310 break;
311 }
312 printf("Verbosity %s\n", verbosity);
313
314 lnm_debug_tabs(tabcount+1);
315 printf("Timestamp %ld\n", statement->timestamp);
316
317 lnm_debug_tabs(tabcount+1);
318 char tag[statement->tag_size+1];
319 strncpy(tag, statement->log, statement->tag_size);
320 tag[statement->tag_size] = '\0';
321 printf("Tag (%i) \"%s\"\n", statement->tag_size, tag);
322
323 lnm_debug_tabs(tabcount+1);
324 char message[statement->message_size+1];
325 strncpy(message, statement->log+statement->tag_size, statement->message_size);
326 message[statement->message_size] = '\0';
327 printf("Message (%i) \"%s\"\n", statement->message_size, message);
328
329 lnm_debug_tabs(tabcount);
330 printf("}\n");
331 } else if (!lnm_isstatement(item)) {
332 lnm_log_event * event = (lnm_log_event *) item;
333 lnm_debug_tabs(tabcount);
334 printf("Event (%i) [\n", event->pushed->length);
335 for (int i = 0; i < event->pushed->length; i++) {
336 lnmItem item = event->pushed->pushed[i];
337 lnm_debug_parse_item(item, tabcount + 1);
338 }
339 lnm_debug_tabs(tabcount);
340 printf("]\n");
341 } else {
342 printf("lognestmonster (lnm_debug_parse_item): unknown item type. exiting...\n");
343 exit(1);
344 }
345}
346
347void lnm_debug_parse_registry() {
348 printf("Top level registry (%i) {\n", lnm_registered_items->length);
349 for (int iter = 0; iter < lnm_registered_items->length; iter++) {
350 lnm_debug_parse_item(lnm_registered_items->pushed[iter], 1);
351 }
352 printf("}\n");
353}
354
355#endif
356