Index

lognestmonster / 446acd7

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
19324 Mar 2020 21:05446acd7Fix memory leak in lnm_registry_freeJosh Stockin16751G

Blob @ lognestmonster / src / c / lognestmonster.h

text/plain19436 bytesdownload raw
1/* lognestmonster Copyright (c) 2020 Joshua 'joshuas3' Stockin
2 * <https://joshstock.in>
3 * <https://github.com/JoshuaS3/lognestmonster>
4 *
5 * This software is licensed and distributed under the terms of the MIT License:
6 * ----- BEGIN LICENSE -----
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 * ----- END LICENSE -----
25 *
26 * This comment block and its contents, including this disclaimer, MUST be
27 * preserved in all copies or distributions of this software's source.
28 */
29
30// lognestmonster.h
31// C header file for implementation of the lognestmonster logging library
32
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38
39#ifndef LOGNESTMONSTER_H
40#define LOGNESTMONSTER_H 1
41
42
43#include <stdint.h> // necessary include for type declaration
44
45
46enum lnmVerbosityLevel {lnmInfo, lnmDebug, lnmVerbose, lnmVeryVerbose, lnmWarning, lnmError};
47typedef uint8_t * lnmItem;
48typedef uint8_t * lnmQueue;
49
50
51lnmQueue lnmQueueInit(char * name, char * out_path);
52lnmQueue lnmQueueByName(char * name);
53
54lnmItem lnmStatement(enum lnmVerbosityLevel verbosity, char * message);
55
56lnmItem lnmEvent(char * tag);
57lnmItem lnmEventI(char * tag, lnmItem item);
58lnmItem lnmEventS(char * tag, enum lnmVerbosityLevel verbosity, char * message);
59void lnmEventPush(lnmItem event, lnmItem item);
60void lnmEventPushS(lnmItem event, enum lnmVerbosityLevel verbosity, char * message);
61
62
63#ifdef LNM_ALL // expose private utilities
64
65// type definitions
66typedef struct lnm_pushable lnm_pushable;
67typedef struct lnm_log_event lnm_log_event;
68typedef struct lnm_log_statement lnm_log_statement;
69typedef struct lnm_queue lnm_queue;
70
71// lnm_pushable utilities
72lnm_pushable * lnm_new_pushable(void);
73void lnm_pushable_realloc(lnm_pushable * pushable);
74void lnm_pushable_push(lnm_pushable * pushable, lnmItem item);
75void lnm_pushable_pop(lnm_pushable * pushable);
76void lnm_pushable_remove(lnm_pushable * pushable, uint32_t index);
77void lnm_pushable_free(lnm_pushable * pushable);
78
79// misc utilities
80unsigned long lnm_getus(void);
81
82// registry utilities
83void lnm_registry_update(void);
84
85// memory management utilities
86void lnm_free_item(lnmItem item);
87void lnm_free_registry(void);
88void lnm_free_queue(lnmQueue queue);
89
90#endif // LNM_ALL
91
92
93#if defined(LNM_DEBUG) || defined(LNM_ALL)
94void lnm_debug_tabs(int tab_count);
95void lnm_debug_parse_item(lnmItem item, int tab_count);
96void lnm_debug_parse_registry(void);
97void lnm_debug_parse_queue(lnmQueue queue);
98#endif // LNM_DEBUG || LNM_ALL
99
100
101#ifdef LNM_INIT // define the library
102
103
104#include <stdint.h>
105#include <stdio.h>
106#include <stdlib.h>
107#include <string.h>
108
109
110static const uint8_t LNM_STATEMENT = 0;
111static const uint8_t LNM_EVENT = 1;
112
113
114// lnm_pushable utilities
115
116
117typedef struct lnm_pushable {
118 uint32_t capacity;
119 uint32_t length;
120 lnmItem * frame;
121} lnm_pushable;
122
123
124void lnm_pushable_realloc(lnm_pushable * pushable) {
125 if (pushable->length > pushable->capacity) {
126 if (pushable->capacity > UINT32_MAX / 2) {
127 printf("lognestmonster (lnm_pushable_realloc): pushable reached max capacity of 2^32-1. exiting...\n");
128 abort();
129 }
130 pushable->frame = realloc(pushable->frame, sizeof(lnmItem) * (pushable->capacity *= 2));
131 if (pushable->frame == NULL) {
132 printf("lognestmonster (lnm_pushable_realloc): call to realloc() returned NULL. exiting...\n");
133 abort();
134 }
135 } else if (pushable->length < (pushable->capacity / 2)) {
136 while (pushable->length < (pushable->capacity / 2) && pushable->capacity > 8) {
137 pushable->capacity /= 2;
138 }
139 pushable->frame = realloc(pushable->frame, sizeof(lnmItem) * (pushable->capacity));
140 if (pushable->frame == NULL) {
141 printf("lognestmonster (lnm_pushable_realloc): call to realloc() returned NULL. exiting...\n");
142 abort();
143 }
144 }
145}
146
147
148lnm_pushable * lnm_new_pushable(void) {
149 lnm_pushable * new_pushable = calloc(1, sizeof(lnm_pushable));
150 if (new_pushable == NULL) {
151 printf("lognestmonster (lnm_new_pushable): call to calloc() returned NULL. exiting...\n");
152 abort();
153 }
154 new_pushable->capacity = 8;
155 new_pushable->length = 0;
156 new_pushable->frame = calloc(8, sizeof(lnmItem));
157 if (new_pushable->frame == NULL) {
158 printf("lognestmonster (lnm_new_pushable): call to calloc() returned NULL. exiting...\n");
159 abort();
160 }
161 return new_pushable;
162}
163
164
165void lnm_pushable_push(lnm_pushable * pushable, lnmItem item) {
166 pushable->length++;
167 lnm_pushable_realloc(pushable);
168 pushable->frame[pushable->length - 1] = item;
169}
170
171
172void lnm_pushable_pop(lnm_pushable * pushable) {
173 pushable->length--;
174 lnm_pushable_realloc(pushable);
175}
176
177
178void lnm_pushable_remove(lnm_pushable * pushable, uint32_t index) {
179 if (index >= pushable->length) {
180 printf("lognestmonster (lnm_pushable_remove): attempt to remove index out of pushable bounds. exiting...\n");
181 abort();
182 }
183 if (index == pushable->length - 1) {
184 lnm_pushable_pop(pushable);
185 } else {
186 // shift entire array from index until end
187 for (uint32_t iter = index; iter < pushable->length - 1; iter++) {
188 pushable->frame[iter] = pushable->frame[iter + 1];
189 }
190 pushable->length--;
191 lnm_pushable_realloc(pushable);
192 }
193}
194
195
196void lnm_pushable_free(lnm_pushable * pushable) {
197 free(pushable->frame);
198 free(pushable);
199}
200
201
202// log event and log statement definitions
203
204
205typedef struct lnm_log_event {
206 // word 1, 1 byte data 7 bytes padding
207 uint8_t type:1; // used internally; 0 = statement, 1 = event
208 uint8_t pushed:1; // whether or not this log item has been pushed
209
210 // word 2, 8 bytes data
211 char * tag; // null-terminated tag string
212
213 // word 3, 8 bytes data
214 lnm_pushable * pushable; // pushable of lnmItems
215} lnm_log_event;
216
217
218typedef struct lnm_log_statement {
219 // word 1, 1 byte data 7 bytes padding
220 uint8_t type:1; // used internally; 0 = statement, 1 = event
221 uint8_t pushed:1; // whether or not this log item has been pushed
222 uint8_t verbosity:3; // lnmVerbosityLevel, 0-5
223
224 // word 2, 8 bytes data
225 uint64_t timestamp; // 64-bit millisecond timestamp
226
227 // word 3, 8 bytes data
228 char * log; // null-terminated message string
229} lnm_log_statement;
230
231
232// queue structure definition
233
234
235typedef struct lnm_queue {
236 char * name;
237 char * out_path;
238 uint64_t timestamp;
239 lnm_pushable * pushable;
240} lnm_queue;
241
242
243// time utilities
244
245
246#if defined(__unix__) || defined(unix) || defined(__unix) || defined(__CYGWIN__)
247#include <sys/time.h>
248static struct timeval lnm_current_time;
249#elif defined(_WIN32) || defined(__WINDOWS__)
250#include <sysinfoapi.h>
251static FILETIME lnm_win32_filetime;
252#else
253#error lognestmonster: Windows NT or a POSIX-compliant system were not detected. Implement your own system time functions or compile on a compliant system.
254#endif
255
256
257uint64_t lnm_getus(void) {
258 uint64_t us;
259#if defined(__unix__) || defined(unix) || defined(__unix) || defined(__CYGWIN__)
260 gettimeofday(&lnm_current_time, NULL);
261 us = (lnm_current_time.tv_sec*1000000+lnm_current_time.tv_usec);
262#elif defined(_WIN32) || defined(__WINDOWS__)
263 // get system time in ticks
264 GetSystemTimeAsFileTime(&lnm_win32_filetime);
265 // load time from two 32-bit words into one 64-bit integer
266 us = lnm_win32_filetime.dwHighDateTime << 32;
267 us |= lnm_win32_filetime.dwLowDateTime;
268 // convert to microseconds
269 us /= 10;
270 // convert from time since Windows NT epoch to time since Unix epoch
271 us -= 11644473600000000ULL;
272#endif
273 return us;
274}
275
276
277// item registry utils
278
279
280void lnm_free_item(lnmItem item);
281static lnm_pushable * lnm_registered_queues;
282static lnm_pushable * lnm_registered_items;
283
284
285void lnm_registry_update(void) {
286 // iterate through registry
287 for (uint32_t iter = 0; iter < lnm_registered_items->length; iter++) {
288 lnm_log_statement * item = (lnm_log_statement *)(lnm_registered_items->frame[iter]);
289 // if the registered item has been pushed elsewhere, remove it from the top level of the registry
290 if (item->pushed) {
291 lnm_pushable_remove(lnm_registered_items, iter);
292 iter--;
293 }
294 }
295}
296
297
298void lnm_registry_push(lnmItem item) {
299 if (lnm_registered_items == NULL) {
300 lnm_registered_items = lnm_new_pushable();
301 }
302 lnm_pushable_push(lnm_registered_items, item);
303}
304
305
306void lnm_registry_free() {
307 lnm_registry_update();
308 for (uint32_t iter = 0; iter < lnm_registered_items->length;) {
309 lnm_free_item(lnm_registered_items->frame[iter]);
310 }
311 lnm_registered_items->length = 0;
312 lnm_pushable_realloc(lnm_registered_items);
313}
314
315
316void lnm_registry_flush_item(lnmItem item) {
317 lnm_log_statement * item_cast = (lnm_log_statement *)item;
318 if (!item_cast->pushed) {
319 item_cast->pushed = 1;
320 lnm_registry_update();
321 }
322}
323
324
325// core library utilities
326
327
328int lnm_item_type(lnmItem item) {
329 return ((lnm_log_statement *)item)->type;
330}
331
332void lnm_free_item(lnmItem item) {
333 if (lnm_item_type(item) == LNM_STATEMENT) {
334 // cast item
335 lnm_log_statement * statement = (lnm_log_statement *)item;
336 // flush item out of registry
337 lnm_registry_flush_item(item);
338 // free item and its contents
339 free(statement->log);
340 free(statement);
341 } else if (lnm_item_type(item) == LNM_EVENT) {
342 // create breadcrumb navigation array with root 'item'
343 lnm_pushable * breadcrumb = lnm_new_pushable();
344 lnm_pushable_push(breadcrumb, item);
345 // continually iterate breadcrumb until it's empty
346 while (breadcrumb->length > 0) {
347 // get current item (deepest element of the breadcrumb nav, aka 'z' in 'x > y > z')
348 lnmItem current = breadcrumb->frame[breadcrumb->length - 1];
349 // pop it from the breadcrumb nav
350 lnm_pushable_pop(breadcrumb);
351 // flush item out of registry
352 lnm_registry_flush_item(current);
353 if (lnm_item_type(current) == LNM_STATEMENT) {
354 // cast current item
355 lnm_log_statement * current_statement = (lnm_log_statement *)current;
356 // free statement and its contents
357 free(current_statement->log);
358 free(current_statement);
359 continue;
360 } else if (lnm_item_type(current) == LNM_EVENT) {
361 // cast current item
362 lnm_log_event * current_event = (lnm_log_event *)current;
363 if (current_event->pushable->length > 0) {
364 // the event has children, add them to the breadcrumb
365 for (uint32_t iter = 0; iter < current_event->pushable->length; iter++) {
366 lnmItem current_event_child = current_event->pushable->frame[iter];
367 lnm_pushable_push(breadcrumb, current_event_child);
368 }
369 }
370 // free original event
371 lnm_pushable_free(current_event->pushable);
372 free(current_event->tag);
373 free(current_event);
374 continue;
375 } else {
376 printf("lognestmonster (lnm_free_item): item in log tree has non-statement and non-event type. exiting...\n");
377 abort();
378 }
379 }
380 lnm_pushable_free(breadcrumb);
381 } else {
382 printf("lognestmonster (lnm_free_item): log tree is non-statement and non-event type. exiting...\n");
383 abort();
384 }
385}
386
387
388// queue utilities
389
390
391void lnm_free_queue(lnmQueue queue) {
392 lnm_queue * queue_cast = (lnm_queue *)queue;
393 for (uint32_t iter = 0; iter < queue_cast->pushable->length; iter++) {
394 lnm_free_item(queue_cast->pushable->frame[iter]);
395 lnm_pushable_remove(queue_cast->pushable, iter);
396 iter--;
397 }
398}
399
400
401lnmQueue lnmQueueInit(char * name, char * out_path) {
402 // create queue and item registries if not created
403 if (lnm_registered_queues == NULL) {
404 lnm_registered_queues = lnm_new_pushable();
405 }
406 if (lnm_registered_items == NULL) {
407 lnm_registered_items = lnm_new_pushable();
408 }
409 // allocate and populate a new Queue object
410 lnm_queue * new_queue = calloc(1, sizeof(lnm_queue));
411 if (new_queue == NULL) {
412 printf("lognestmonster (lnmQueueInit): call to calloc() returned NULL. exiting...\n");
413 abort();
414 }
415 new_queue->name = malloc(strlen(name)+1);
416 new_queue->out_path = malloc(strlen(out_path)+1);
417 if (new_queue->name == NULL || new_queue->out_path == NULL) {
418 printf("lognestmonster (lnmQueueInit): call to malloc() returned NULL. exiting...\n");
419 abort();
420 }
421 strcpy(new_queue->name, name);
422 strcpy(new_queue->out_path, out_path);
423 new_queue->timestamp = lnm_getus();
424 new_queue->pushable = lnm_new_pushable();
425 // enter new Queue into registry
426 lnm_pushable_push(lnm_registered_queues, (lnmQueue)new_queue);
427 return (lnmQueue)new_queue;
428}
429
430
431lnmQueue lnmQueueByName(char * name) {
432 if (lnm_registered_queues == NULL) {
433 printf("lognestmonster (lnmQueueByName): queue registry is nonexistent. exiting...\n");
434 abort();
435 }
436 if (lnm_registered_queues->length == 0) {
437 printf("lognestmonster (lnmQueueByName): queue registry is empty. exiting...\n");
438 abort();
439 }
440 for (uint32_t iter = 0; iter < lnm_registered_queues->length; iter++) {
441 lnm_queue * queue = (lnm_queue *)lnm_registered_queues->frame[iter];
442 if (strcmp(queue->name, name) == 0) {
443 return (lnmQueue)queue;
444 }
445 }
446 printf("lognestmonster (lnmQueueByName): queue not found in registry. exiting...\n");
447 abort();
448}
449
450
451void lnmQueuePush(lnmQueue queue, lnmItem item) {
452 if (queue == NULL || item == NULL) {
453 printf("lognestmonster (lnmQueuePush): cannot perform operation on NULL arguments. exiting...\n");
454 abort();
455 }
456 lnm_log_statement * statement = (lnm_log_statement *)item;
457 if (statement->pushed == 1) {
458 printf("lognestmonster (lnmQueuePush): attempt to push an already-pushed log item. exiting...\n");
459 abort();
460 }
461 // flush out of registry
462 lnm_registry_flush_item(item);
463 // add to queue
464 lnm_pushable_push(((lnm_queue *)queue)->pushable, item);
465}
466
467
468lnmItem lnmStatement(enum lnmVerbosityLevel verbosity, char * message) {
469 if (message == NULL) {
470 printf("lognestmonster (lnmStatement): cannot perform operation on NULL argument. exiting...\n");
471 abort();
472 }
473 lnm_log_statement * new_statement = calloc(1, sizeof(lnm_log_statement));
474 if (new_statement == NULL) {
475 printf("lognestmonster (lnmStatement): call to calloc() returned NULL. exiting...\n");
476 abort();
477 }
478 new_statement->type = LNM_STATEMENT;
479 new_statement->pushed = 0;
480 new_statement->verbosity = verbosity;
481 new_statement->timestamp = lnm_getus();
482 // enforce string lengths
483 int message_len = strlen(message) + 1;
484 // copy message to new_statement->log
485 new_statement->log = malloc(message_len);
486 if (new_statement->log == NULL) {
487 printf("lognestmonster (lnmStatement): call to malloc() returned NULL. exiting...\n");
488 abort();
489 }
490 strcpy(new_statement->log, message);
491 // add to registry
492 lnm_registry_push((lnmItem)new_statement);
493 return (lnmItem)new_statement;
494}
495
496
497lnmItem lnmEvent(char * tag) {
498 if (tag == NULL) {
499 printf("lognestmonster (lnmEvent): cannot perform operation on NULL argument. exiting...\n");
500 abort();
501 }
502 lnm_log_event * new_event = calloc(1, sizeof(lnm_log_event));
503 if (new_event == NULL) {
504 printf("lognestmonster (lnmEvent): call to calloc() returned NULL. exiting...\n");
505 abort();
506 }
507 new_event->type = LNM_EVENT;
508 new_event->pushed = 0;
509 new_event->pushable = lnm_new_pushable();
510 // copy tag to event
511 int tag_len = strlen(tag) + 1;
512 new_event->tag = malloc(tag_len);
513 if (new_event->tag == NULL) {
514 printf("lognestmonster (lnmEvent): call to malloc() returned NULL. exiting...\n");
515 abort();
516 }
517 strcpy(new_event->tag, tag);
518 // add to registry
519 lnm_registry_push((lnmItem)new_event);
520 return (lnmItem)new_event;
521}
522
523
524void lnmEventPush(lnmItem event, lnmItem item) {
525 if (event == NULL || item == NULL) {
526 printf("lognestmonster (lnmEventPush): cannot perform operation on NULL arguments. exiting...\n");
527 abort();
528 }
529 if (event == item) {
530 printf("lognestmonster (lnmEventPush): attempt to push event to self. exiting...\n");
531 abort();
532 }
533 lnm_log_statement * item_cast = (lnm_log_statement *)item;
534 if (item_cast->pushed == 1) {
535 printf("lognestmonster (lnmEventPush): attempt to push an already-pushed log item. exiting...\n");
536 abort();
537 }
538 if (lnm_item_type(event) != LNM_EVENT) {
539 printf("lognestmonster (lnmEventPush): cannot cast non-event to event type. exiting...\n");
540 abort();
541 }
542 lnm_log_event * event_cast = (lnm_log_event *)event;
543 lnm_pushable_push(event_cast->pushable, item);
544 lnm_registry_flush_item(item);
545}
546
547
548void lnmEventPushS(lnmItem event, enum lnmVerbosityLevel verbosity, char * message) {
549 lnmItem statement = lnmStatement(verbosity, message);
550 lnmEventPush(event, statement);
551}
552
553
554lnmItem lnmEventI(char * tag, lnmItem item) {
555 lnmItem event = lnmEvent(tag);
556 lnmEventPush(event, item);
557 return event;
558}
559
560
561lnmItem lnmEventS(char * tag, enum lnmVerbosityLevel verbosity, char * message) {
562 lnmItem event = lnmEvent(tag);
563 lnmEventPushS(event, verbosity, message);
564 return event;
565}
566
567
568#ifdef LNM_DEBUG
569#include <inttypes.h>
570
571void lnm_debug_tabs(int tab_count) {
572 for (int i = 0; i < tab_count; i++) {
573 printf(" ");
574 }
575}
576
577
578void lnm_debug_parse_item(lnmItem item, int tab_count) {
579 if (lnm_item_type(item) == LNM_STATEMENT) {
580 lnm_log_statement * statement = (lnm_log_statement *) item;
581 lnm_debug_tabs(tab_count);
582
583 char * verbosity;
584 switch (statement->verbosity) {
585 case 0:
586 verbosity = "INFO";
587 break;
588 case 1:
589 verbosity = "DEBUG";
590 break;
591 case 2:
592 verbosity = "VERBOSE";
593 break;
594 case 3:
595 verbosity = "VERYVERBOSE";
596 break;
597 case 4:
598 verbosity = "WARNING";
599 break;
600 case 5:
601 verbosity = "ERROR";
602 break;
603 }
604
605 printf("%" PRIu64 " (%s) :: %s\n", statement->timestamp, verbosity, statement->log);
606 } else if (lnm_item_type(item) == LNM_EVENT) {
607 lnm_log_event * event = (lnm_log_event *) item;
608 lnm_debug_tabs(tab_count);
609 printf("Event (%" PRIu32 "/%" PRIu32 ") %s [\n", event->pushable->length, event->pushable->capacity, event->tag);
610 for (uint32_t iter = 0; iter < event->pushable->length; iter++) {
611 lnmItem item = event->pushable->frame[iter];
612 lnm_debug_parse_item(item, tab_count + 1);
613 }
614 lnm_debug_tabs(tab_count);
615 printf("]\n");
616 } else {
617 printf("lognestmonster (lnm_debug_parse_item): unknown item type. exiting...\n");
618 abort();
619 }
620}
621
622
623void lnm_debug_parse_registry(void) {
624 printf("Top level registry (%" PRIu32 "/%" PRIu32 ") [\n", lnm_registered_items->length, lnm_registered_items->capacity);
625 for (uint32_t iter = 0; iter < lnm_registered_items->length; iter++) {
626 lnm_debug_parse_item(lnm_registered_items->frame[iter], 1);
627 }
628 printf("]\n");
629}
630
631
632void lnm_debug_parse_queue(lnmQueue queue) {
633 lnm_queue * queue_cast = (lnm_queue *)queue;
634 printf("Queue \"%s\" at %s (%" PRIu32 "/%" PRIu32 ") [\n", queue_cast->name, queue_cast->out_path, queue_cast->pushable->length, queue_cast->pushable->capacity);
635 for (uint32_t iter = 0; iter < queue_cast->pushable->length; iter++) {
636 lnm_debug_parse_item((lnmItem)queue_cast->pushable->frame[iter], 1);
637 }
638 printf("]\n");
639}
640#endif // LNM_DEBUG
641#endif // LNM_INIT
642#endif // LOGNESTMONSTER_H
643
644
645#ifdef __cplusplus
646}
647#endif
648