Index

lognestmonster / af2a257

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
15530 Nov 2019 11:44af2a257Update exit conditionsJosh Stockin158N

Blob @ lognestmonster / src / c / lognestmonster.h

text/plain13418 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
24// SEMANTICS
25// internal definitions: lnm_lower_camel_case
26// public definitions: lnmUpperCamelCase
27
28
29// Declarations
30#ifndef __LOGNESTMONSTER__
31#define __LOGNESTMONSTER__ 1
32
33// stdc inclusions
34
35#include <stdint.h>
36#include <inttypes.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41// non-universal inclusions
42
43#include <sys/time.h>
44
45
46enum lnmVerbosityLevel {lnmInfo, lnmDebug, lnmVerbose, lnmVeryVerbose, lnmWarning, lnmError};
47typedef uint8_t * lnmItem;
48typedef uint8_t * lnmQueue;
49
50lnmQueue lnmQueueInit(char * name, char * out_path);
51lnmQueue lnmQueueByName(char * name);
52lnmItem lnmStatement(uint8_t verbosity, char * tag, char * message);
53lnmItem lnmEvent(void);
54void lnmEventPush(lnmItem event, lnmItem item);
55void lnmEventPushS(lnmItem event, uint8_t verbosity, char * tag, char * message);
56lnmItem lnmEventI(lnmItem item);
57lnmItem lnmEventS(uint8_t verbosity, char * tag, char * message);
58
59void lnm_debug_parse_item();
60void lnm_debug_parse_registry();
61
62#endif
63
64
65#ifdef DEFINE_LOGNESTMONSTER // one-time definitions
66
67
68// Pushable structure
69
70typedef struct {
71 uint16_t length;
72 lnmItem * pushed;
73} lnm_pushable;
74
75lnm_pushable * lnm_new_pushable() {
76 lnm_pushable * new_pushable = malloc(sizeof(lnm_pushable));
77 new_pushable->length = 0;
78 new_pushable->pushed = malloc(0);
79 return new_pushable;
80}
81
82void lnm_pushable_push(lnm_pushable * pushable, lnmItem item) {
83 pushable->pushed = realloc(pushable->pushed, sizeof(lnmItem)*(pushable->length+1)); // reallocate with size: length+1
84 pushable->pushed[pushable->length] = item;
85 pushable->length += 1;
86}
87
88int lnm_pushable_indexof(lnm_pushable * pushable, lnmItem item) {
89 int len = pushable->length;
90 for (int iter = 0; iter<len; iter++) {
91 if (item == pushable->pushed[iter]) return iter;
92 }
93 printf("lognestmonster (lnm_pushable_indexof): cannot find item in pushable. exiting...\n");
94 exit(1);
95}
96
97void lnm_pushable_pop(lnm_pushable * pushable) {
98 pushable->pushed = realloc(pushable->pushed, sizeof(lnmItem)*(pushable->length--));
99}
100
101void lnm_pushable_remove(lnm_pushable * pushable, int index) {
102 if (index>=pushable->length || index < 0) {
103 printf("lognestmonster (lnm_pushable_remove): attempt to remove index out of pushable bounds. exiting...\n");
104 exit(1);
105 }
106 lnmItem * new_pushed = malloc(sizeof(lnmItem)*(pushable->length-1)); // map array excluding index
107 for (int iter = 0; iter<index; iter++) {
108 new_pushed[iter] = pushable->pushed[iter];
109 }
110 for (int iter = index+1; iter<pushable->length; iter++) {
111 new_pushed[iter-1] = pushable->pushed[iter];
112 }
113 free(pushable->pushed);
114 pushable->length--;
115 pushable->pushed = new_pushed;
116}
117
118void lnm_pushable_free(lnm_pushable * pushable) {
119 free(pushable->pushed);
120 free(pushable);
121}
122
123
124// Statement and event structure definitions
125
126typedef struct {
127 uint8_t type:1; // Used internally; 0 = statement, 1 = event
128 uint8_t boolpushed:1; // whether or not this log item has been pushed
129 lnm_pushable * pushed; // array of memory locations for lnm_log_event and lnm_log_statement structs
130} lnm_log_event;
131
132typedef struct {
133 // word 1, 4 bytes data 4 bytes padding
134 uint8_t type:1; // Used internally; 0 = statement, 1 = event
135 uint8_t boolpushed:1; // whether or not this log item has been pushed
136 uint8_t verbosity:3; // lnmVerbosityLevel, 0-5
137 uint8_t tag_size; // character length of the tag
138 uint16_t message_size; // character length of the message
139
140 // word 2, 8 bytes data
141 uint64_t timestamp; // 64-bit millisecond timestamp
142
143 // word 3, 8 bytes data
144 char * log; // tag string + message string
145} lnm_log_statement;
146
147
148// Queue structure definition
149
150typedef struct {
151 char * name;
152 char * out_path;
153 uint64_t timestamp;
154 lnm_pushable * pushed;
155} lnm_queue;
156
157
158// Library utilities
159
160unsigned long lnm_getus(void) {
161 struct timeval current_time;
162 gettimeofday(&current_time, NULL);
163 unsigned long ms = (current_time.tv_sec*1000000+current_time.tv_usec);
164 return ms;
165}
166
167unsigned long lnm_getms(void) {
168 return lnm_getus()/1000;
169}
170
171
172int lnm_isstatement(lnmItem item) {
173 lnm_log_statement * s = (lnm_log_statement *)item;
174 return !s->type;
175}
176
177
178// Item registry utils
179
180static lnm_pushable * lnm_registered_queues;
181static lnm_pushable * lnm_registered_items;
182static int lnm_registry_update_count;
183
184void lnm_registry_update(void) { // scan each registered item
185 for (int iter = 0; iter < lnm_registered_items->length; iter++) {
186 lnm_log_statement * s = (lnm_log_statement *)lnm_registered_items->pushed[iter];
187 if (s->boolpushed == 1) { // if the registered item has been pushed elsewhere, remove it from the top level of the registry
188 lnm_registry_update_count++;
189 lnm_pushable_remove(lnm_registered_items, iter);
190 iter--;
191 }
192 }
193}
194
195
196// Core library
197
198void lnm_free_item(lnmItem item) { // i'm so sorry
199 lnm_log_statement * item_cast = (lnm_log_statement *)item;
200 if (item_cast->boolpushed == 0) { // flush item out of registry
201 item_cast->boolpushed = 1;
202 lnm_registry_update();
203 }
204 if (item_cast->type == 0) {
205 free(item_cast->log);
206 free(item_cast);
207 } else if (item_cast->type == 1) {
208 lnm_log_event * event_cast = (lnm_log_event *)item_cast;
209
210 lnm_pushable * breadcrumb = lnm_new_pushable();
211 lnm_pushable_push(breadcrumb, (lnmItem)event_cast); // add event_cast as the first step down the breadcrumb
212 while (breadcrumb->length > 0) { // while there are items still in the breadcrumb
213 lnm_log_statement * breadcrumb_item = (lnm_log_statement *)breadcrumb->pushed[breadcrumb->length-1]; // fetch the last (deepest) item
214 if (breadcrumb_item->type == 0) { // the item is a statement
215 lnm_pushable_pop(breadcrumb); // remove it from the breadcrumb
216 free(breadcrumb_item->log); // and free it
217 free(breadcrumb_item);
218 } else if (breadcrumb_item->type == 1) {
219 lnm_log_event * breadcrumb_item_cast = (lnm_log_event *)breadcrumb_item; // item is an event, cast pointer
220 if (breadcrumb_item_cast->pushed->length > 0) { // if the event is not empty
221 lnm_pushable_push(breadcrumb, breadcrumb_item_cast->pushed->pushed[breadcrumb_item_cast->pushed->length-1]); // push the last item of event into the breadcrumb
222 lnm_pushable_pop(breadcrumb_item_cast->pushed); // and remove it from this event's index
223 // there is now a new breadcrumb navigation layer. loop back to check the new item...
224 } else {
225 lnm_pushable_pop(breadcrumb); // event is finally empty, remove it from the breadcrumb and free it
226 lnm_pushable_free(breadcrumb_item_cast->pushed);
227 free(breadcrumb_item_cast);
228 }
229 }
230 }
231 } else {
232 printf("lognestmonster (lnm_free_item): non-log item passed to function. exiting...\n");
233 exit(1);
234 }
235}
236
237void lnm_free_registry() {
238 for (int iter = 0; iter < lnm_registered_items->length; iter++) {
239 lnm_free_item(lnm_registered_items->pushed[iter]);
240 }
241}
242
243
244lnmQueue lnmQueueInit(char * name, char * out_path) {
245 if (lnm_registered_queues == NULL) {
246 lnm_registered_queues = lnm_new_pushable();
247 }
248 if (lnm_registered_items == NULL) {
249 lnm_registered_items = lnm_new_pushable();
250 }
251
252 lnm_queue * new_queue = malloc(sizeof(lnm_queue));
253 new_queue->name = malloc(strlen(name)+1);
254 new_queue->out_path = malloc(strlen(out_path)+1);
255 strcpy(new_queue->name, name);
256 strcpy(new_queue->out_path, out_path);
257 new_queue->pushed = lnm_new_pushable();
258
259 lnm_pushable_push(lnm_registered_queues, (lnmQueue)new_queue);
260 return (lnmQueue)new_queue;
261}
262
263lnmQueue lnmQueueByName(char * name) {
264 if (lnm_registered_queues == NULL) {
265 printf("lognestmonster (lnmQueueByName): queue registry is nonexistant. exiting...\n");
266 exit(1);
267 }
268 if (lnm_registered_queues->length == 0) {
269 printf("lognestmonster (lnmQueueByName): queue registry is empty. exiting...\n");
270 exit(1);
271 }
272 for (int iter = 0; iter<lnm_registered_queues->length; iter++) {
273 lnm_queue * iterqueue = (lnm_queue *)lnm_registered_queues->pushed[iter];
274 if (strcmp(iterqueue->name, name)==0) {
275 return (lnmQueue)iterqueue;
276 }
277 }
278 printf("lognestmonster (lnmQueueByName): queue not found in registry. exiting...\n");
279 exit(1);
280}
281
282
283lnmItem lnmStatement(uint8_t verbosity, char * tag, char * message) {
284 lnm_log_statement * new_statement = malloc(sizeof(lnm_log_statement));
285 new_statement->type = 0;
286 new_statement->verbosity = verbosity;
287 new_statement->timestamp = lnm_getus();
288 int tlen = strlen(tag);
289 if (tlen > 256 || tlen < 0) {
290 printf("lognestmonster (lnmStatement): tag length %i is longer than the cap 256 characters. exiting...\n", tlen);
291 exit(1);
292 }
293 int mlen = strlen(message);
294 if (mlen > 65536 || mlen < 0) {
295 printf("lognestmonster (lnmStatement): message length %i is longer than the cap 65536 characters. exiting...\n", mlen);
296 exit(1);
297 }
298 new_statement->tag_size = tlen;
299 new_statement->message_size = mlen;
300 new_statement->log = malloc(tlen+mlen+1);
301 strcpy(new_statement->log, tag);
302 strcat(new_statement->log, message);
303 lnm_registry_update();
304 lnm_pushable_push(lnm_registered_items, (lnmItem)new_statement);
305 return (lnmItem)new_statement;
306}
307
308
309lnmItem lnmEvent(void) {
310 lnm_log_event * new_event = malloc(sizeof(lnm_log_event));
311 new_event->type = 1;
312 new_event->pushed = lnm_new_pushable();
313 lnm_registry_update();
314 lnm_pushable_push(lnm_registered_items, (lnmItem)new_event);
315 return (lnmItem)new_event;
316}
317
318void lnmEventPush(lnmItem event, lnmItem item) {
319 if (event == item) {
320 printf("lognestmonster (lnmEventPush): attempt to push event to self. exiting...\n");
321 exit(1);
322 }
323 lnm_log_statement * item_cast = (lnm_log_statement *)item;
324 if (item_cast->boolpushed == 1) {
325 printf("lognestmonster (lnmEventPush): attempt to push an already-pushed log item. exiting...\n");
326 exit(1);
327 }
328 lnm_log_event * event_t = (lnm_log_event *)event;
329 if (event_t->type != 1) {
330 printf("lognestmonster (lnmEventPush): cannot cast non-event to event type. exiting...\n");
331 exit(1);
332 }
333 lnm_pushable_push(event_t->pushed, item);
334 item_cast->boolpushed = 1;
335 lnm_registry_update();
336}
337
338void lnmEventPushS(lnmItem event, uint8_t verbosity, char * tag, char * message) {
339 lnmItem statement = lnmStatement(verbosity, tag, message);
340 lnmEventPush(event, statement);
341}
342
343lnmItem lnmEventI(lnmItem item) {
344 lnmItem event = lnmEvent();
345 lnmEventPush(event, item);
346 return event;
347}
348
349lnmItem lnmEventS(uint8_t verbosity, char * tag, char * message) {
350 lnmItem statement = lnmStatement(verbosity, tag, message);
351 return lnmEventI(statement);
352}
353
354
355void lnm_debug_tabs(int count) {
356 for (int i = 0; i < count; i++) {
357 printf(" ");
358 }
359}
360
361void lnm_debug_parse_item(lnmItem item, int tabcount) {
362 if (lnm_isstatement(item)) {
363 lnm_log_statement * statement = (lnm_log_statement *) item;
364 lnm_debug_tabs(tabcount);
365 printf("Statement {\n");
366
367 lnm_debug_tabs(tabcount+1);
368 char * verbosity;
369 switch (statement->verbosity) {
370 case 0:
371 verbosity = "INFO";
372 break;
373 case 1:
374 verbosity = "DEBUG";
375 break;
376 case 2:
377 verbosity = "VERBOSE";
378 break;
379 case 3:
380 verbosity = "VERYVERBOSE";
381 break;
382 case 4:
383 verbosity = "WARNING";
384 break;
385 case 5:
386 verbosity = "ERROR";
387 break;
388 }
389 printf("Verbosity %s\n", verbosity);
390
391 lnm_debug_tabs(tabcount+1);
392 printf("Timestamp %" PRIu64 "\n", statement->timestamp);
393
394 lnm_debug_tabs(tabcount+1);
395 char tag[statement->tag_size+1];
396 strncpy(tag, statement->log, statement->tag_size);
397 tag[statement->tag_size] = '\0';
398 printf("Tag (%" PRIu8 ") \"%s\"\n", statement->tag_size, tag);
399
400 lnm_debug_tabs(tabcount+1);
401 char message[statement->message_size+1];
402 strncpy(message, statement->log+statement->tag_size, statement->message_size);
403 message[statement->message_size] = '\0';
404 printf("Message (%" PRIu16 ") \"%s\"\n", statement->message_size, message);
405
406 lnm_debug_tabs(tabcount);
407 printf("}\n");
408 } else if (!lnm_isstatement(item)) {
409 lnm_log_event * event = (lnm_log_event *) item;
410 lnm_debug_tabs(tabcount);
411 printf("Event (%" PRIu16 ") [\n", event->pushed->length);
412 for (int i = 0; i < event->pushed->length; i++) {
413 lnmItem item = event->pushed->pushed[i];
414 lnm_debug_parse_item(item, tabcount + 1);
415 }
416 lnm_debug_tabs(tabcount);
417 printf("]\n");
418 } else {
419 printf("lognestmonster (lnm_debug_parse_item): unknown item type. exiting...\n");
420 exit(1);
421 }
422}
423
424void lnm_debug_parse_registry() {
425 printf("Top level registry (%" PRIu16 ") {\n", lnm_registered_items->length);
426 for (int iter = 0; iter < lnm_registered_items->length; iter++) {
427 lnm_debug_parse_item(lnm_registered_items->pushed[iter], 1);
428 }
429 printf("}\n");
430}
431
432#endif // DEFINE_LOGNESTMONSTER
433