Index

lognestmonster / 679072b

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
19630 Mar 2020 10:37679072bUse const char * type instead of char *Josh Stockin11615G

Blob @ lognestmonster / src / c / lognestmonster.h

text/plain18836 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(const char * name, const char * out_path);
52lnmQueue lnmQueueByName(const char * name);
53
54lnmItem lnmStatement(enum lnmVerbosityLevel verbosity, const char * message);
55
56lnmItem lnmEvent(const char * tag);
57lnmItem lnmEventI(const char * tag, lnmItem item);
58lnmItem lnmEventS(const char * tag, enum lnmVerbosityLevel verbosity, const char * message);
59void lnmEventPush(lnmItem event, lnmItem item);
60void lnmEventPushS(lnmItem event, enum lnmVerbosityLevel verbosity, const 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
80_Noreturn void lnm_abort(const char * function_traceback, const char * message);
81unsigned long lnm_getus(void);
82
83// registry utilities
84void lnm_registry_update(void);
85
86// memory management utilities
87void lnm_free_item(lnmItem item);
88void lnm_free_registry(void);
89void lnm_free_queue(lnmQueue queue);
90
91#endif // LNM_ALL
92
93
94#if defined(LNM_DEBUG) || defined(LNM_ALL)
95void lnm_debug_tabs(int tab_count);
96void lnm_debug_parse_item(lnmItem item, int tab_count);
97void lnm_debug_parse_registry(void);
98void lnm_debug_parse_queue(lnmQueue queue);
99#endif // LNM_DEBUG || LNM_ALL
100
101
102#ifdef LNM_INIT // define the library
103
104
105#include <stdint.h>
106#include <stdio.h>
107#include <stdlib.h>
108#include <string.h>
109
110
111static const uint8_t LNM_STATEMENT = 0;
112static const uint8_t LNM_EVENT = 1;
113
114
115_Noreturn void lnm_abort(const char * function_traceback, const char * message) {
116 printf("lognestmonster (%s): %s. aborting...\n", function_traceback, message);
117 abort();
118}
119
120
121// lnm_pushable utilities
122
123
124typedef struct lnm_pushable {
125 uint32_t capacity;
126 uint32_t length;
127 lnmItem * frame;
128} lnm_pushable;
129
130
131void lnm_pushable_realloc(lnm_pushable * pushable) {
132 if (pushable->length > pushable->capacity) {
133 if (pushable->capacity > UINT32_MAX / 2) {
134 lnm_abort("lnm_pushable_realloc", "pushable can't surpass max capacity of 2^16");
135 }
136 pushable->frame = realloc(pushable->frame, sizeof(lnmItem) * (pushable->capacity *= 2));
137 if (pushable->frame == NULL) {
138 lnm_abort("lnm_pushable_realloc", "call to realloc() returned NULL");
139 }
140 } else if (pushable->length < (pushable->capacity / 2)) {
141 while (pushable->length < (pushable->capacity / 2) && pushable->capacity > 8) {
142 pushable->capacity /= 2;
143 }
144 pushable->frame = realloc(pushable->frame, sizeof(lnmItem) * (pushable->capacity));
145 if (pushable->frame == NULL) {
146 lnm_abort("lnm_pushable_realloc", "call to realloc() returned NULL");
147 }
148 }
149}
150
151
152lnm_pushable * lnm_new_pushable(void) {
153 lnm_pushable * new_pushable = calloc(1, sizeof(lnm_pushable));
154 if (new_pushable == NULL) {
155 lnm_abort("lnm_new_pushable", "call to calloc() returned NULL");
156 }
157 new_pushable->capacity = 8;
158 new_pushable->length = 0;
159 new_pushable->frame = calloc(8, sizeof(lnmItem));
160 if (new_pushable->frame == NULL) {
161 lnm_abort("lnm_new_pushable", "call to calloc() returned NULL");
162 }
163 return new_pushable;
164}
165
166
167void lnm_pushable_push(lnm_pushable * pushable, lnmItem item) {
168 pushable->length++;
169 lnm_pushable_realloc(pushable);
170 pushable->frame[pushable->length - 1] = item;
171}
172
173
174void lnm_pushable_pop(lnm_pushable * pushable) {
175 pushable->length--;
176 lnm_pushable_realloc(pushable);
177}
178
179
180void lnm_pushable_remove(lnm_pushable * pushable, uint32_t index) {
181 if (index >= pushable->length) {
182 lnm_abort("lnm_pushable_remove", "attempt to remove index out of pushable bounds");
183 }
184 if (index == pushable->length - 1) {
185 lnm_pushable_pop(pushable);
186 } else {
187 // shift entire array from index until end
188 for (uint32_t iter = index; iter < pushable->length - 1; iter++) {
189 pushable->frame[iter] = pushable->frame[iter + 1];
190 }
191 pushable->length--;
192 lnm_pushable_realloc(pushable);
193 }
194}
195
196
197void lnm_pushable_free(lnm_pushable * pushable) {
198 free(pushable->frame);
199 free(pushable);
200}
201
202
203// log event and log statement definitions
204
205
206typedef struct lnm_log_event {
207 // word 1, 1 byte data 7 bytes padding
208 uint8_t type:1; // used internally; 0 = statement, 1 = event
209 uint8_t pushed:1; // whether or not this log item has been pushed
210
211 // word 2, 8 bytes data
212 char * tag; // null-terminated tag string
213
214 // word 3, 8 bytes data
215 lnm_pushable * pushable; // pushable of lnmItems
216} lnm_log_event;
217
218
219typedef struct lnm_log_statement {
220 // word 1, 1 byte data 7 bytes padding
221 uint8_t type:1; // used internally; 0 = statement, 1 = event
222 uint8_t pushed:1; // whether or not this log item has been pushed
223 uint8_t verbosity:3; // lnmVerbosityLevel, 0-5
224
225 // word 2, 8 bytes data
226 uint64_t timestamp; // 64-bit millisecond timestamp
227
228 // word 3, 8 bytes data
229 char * log; // null-terminated message string
230} lnm_log_statement;
231
232
233// queue structure definition
234
235
236typedef struct lnm_queue {
237 char * name;
238 char * out_path;
239 uint64_t timestamp;
240 lnm_pushable * pushable;
241} lnm_queue;
242
243
244// time utilities
245
246
247#if defined(__unix__) || defined(unix) || defined(__unix) || defined(__CYGWIN__)
248#include <sys/time.h>
249static struct timeval lnm_current_time;
250#elif defined(_WIN32) || defined(__WINDOWS__)
251#include <sysinfoapi.h>
252static FILETIME lnm_win32_filetime;
253#else
254#error lognestmonster: Neither Windows NT nor a POSIX-compliant system were detected. Implement your own system time functions or compile on a compliant system.
255#endif
256
257
258uint64_t lnm_getus(void) {
259 uint64_t us;
260#if defined(__unix__) || defined(unix) || defined(__unix) || defined(__CYGWIN__)
261 gettimeofday(&lnm_current_time, NULL);
262 us = (lnm_current_time.tv_sec*1000000+lnm_current_time.tv_usec);
263#elif defined(_WIN32) || defined(__WINDOWS__)
264 // get system time in ticks
265 GetSystemTimeAsFileTime(&lnm_win32_filetime);
266 // load time from two 32-bit words into one 64-bit integer
267 us = lnm_win32_filetime.dwHighDateTime << 32;
268 us |= lnm_win32_filetime.dwLowDateTime;
269 // convert to microseconds
270 us /= 10;
271 // convert from time since Windows NT epoch to time since Unix epoch
272 us -= 116444736000000000ULL;
273#endif
274 return us;
275}
276
277
278// item registry utils
279
280
281void lnm_free_item(lnmItem item);
282static lnm_pushable * lnm_registered_queues;
283static lnm_pushable * lnm_registered_items;
284
285
286void lnm_registry_update(void) {
287 // iterate through registry
288 for (uint32_t iter = 0; iter < lnm_registered_items->length; iter++) {
289 lnm_log_statement * item = (lnm_log_statement *)(lnm_registered_items->frame[iter]);
290 // if the registered item has been pushed elsewhere, remove it from the top level of the registry
291 if (item->pushed) {
292 lnm_pushable_remove(lnm_registered_items, iter);
293 iter--;
294 }
295 }
296}
297
298
299void lnm_registry_push(lnmItem item) {
300 if (lnm_registered_items == NULL) {
301 lnm_registered_items = lnm_new_pushable();
302 }
303 lnm_pushable_push(lnm_registered_items, item);
304}
305
306
307void lnm_registry_free() {
308 lnm_registry_update();
309 for (uint32_t iter = 0; iter < lnm_registered_items->length;) {
310 lnm_free_item(lnm_registered_items->frame[lnm_registered_items->length-1]);
311 }
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 lnm_abort("lnm_free_item", "item in log tree has non-statement and non-event type");
377 }
378 }
379 lnm_pushable_free(breadcrumb);
380 } else {
381 lnm_abort("lnm_free_item", "log tree is non-statement and non-event type");
382 }
383}
384
385
386// queue utilities
387
388
389void lnm_free_queue(lnmQueue queue) {
390 lnm_queue * queue_cast = (lnm_queue *)queue;
391 for (uint32_t iter = 0; iter < queue_cast->pushable->length; iter++) {
392 lnm_free_item(queue_cast->pushable->frame[iter]);
393 lnm_pushable_remove(queue_cast->pushable, iter);
394 iter--;
395 }
396}
397
398
399lnmQueue lnmQueueInit(const char * name, const char * out_path) {
400 // create queue and item registries if not created
401 if (lnm_registered_queues == NULL) {
402 lnm_registered_queues = lnm_new_pushable();
403 }
404 if (lnm_registered_items == NULL) {
405 lnm_registered_items = lnm_new_pushable();
406 }
407 // allocate and populate a new Queue object
408 lnm_queue * new_queue = calloc(1, sizeof(lnm_queue));
409 if (new_queue == NULL) {
410 lnm_abort("lnmQueueInit", "call to calloc() returned NULL");
411 }
412 new_queue->name = malloc(strlen(name)+1);
413 new_queue->out_path = malloc(strlen(out_path)+1);
414 if (new_queue->name == NULL || new_queue->out_path == NULL) {
415 lnm_abort("lnmQueueInit", "call to malloc() returned NULL");
416 }
417 strcpy(new_queue->name, name);
418 strcpy(new_queue->out_path, out_path);
419 new_queue->timestamp = lnm_getus();
420 new_queue->pushable = lnm_new_pushable();
421 // enter new Queue into registry
422 lnm_pushable_push(lnm_registered_queues, (lnmQueue)new_queue);
423 return (lnmQueue)new_queue;
424}
425
426
427lnmQueue lnmQueueByName(const char * name) {
428 if (lnm_registered_queues == NULL) {
429 lnm_abort("lnmQueueByName", "queue registry is nonexistent");
430 }
431 if (lnm_registered_queues->length == 0) {
432 lnm_abort("lnmQueueByName", "queue registry is empty");
433 }
434 for (uint32_t iter = 0; iter < lnm_registered_queues->length; iter++) {
435 lnm_queue * queue = (lnm_queue *)lnm_registered_queues->frame[iter];
436 if (strcmp(queue->name, name) == 0) {
437 return (lnmQueue)queue;
438 }
439 }
440 lnm_abort("lnmQueueByName", "queue not found in registry");
441}
442
443
444void lnmQueuePush(lnmQueue queue, lnmItem item) {
445 if (queue == NULL || item == NULL) {
446 lnm_abort("lnmQueuePush", "cannot perform operation on NULL arguments");
447 }
448 lnm_log_statement * statement = (lnm_log_statement *)item;
449 if (statement->pushed == 1) {
450 lnm_abort("lnmQueuePush", "attempt to push an already-pushed log item");
451 }
452 // flush out of registry
453 lnm_registry_flush_item(item);
454 // add to queue
455 lnm_pushable_push(((lnm_queue *)queue)->pushable, item);
456}
457
458
459lnmItem lnmStatement(enum lnmVerbosityLevel verbosity, const char * message) {
460 if (message == NULL) {
461 lnm_abort("lnmStatement", "cannot perform operation on NULL argument");
462 }
463 lnm_log_statement * new_statement = calloc(1, sizeof(lnm_log_statement));
464 if (new_statement == NULL) {
465 lnm_abort("lnmStatement", "call to calloc() returned NULL");
466 }
467 new_statement->type = LNM_STATEMENT;
468 new_statement->pushed = 0;
469 new_statement->verbosity = verbosity;
470 new_statement->timestamp = lnm_getus();
471 // enforce string lengths
472 int message_len = strlen(message) + 1;
473 // copy message to new_statement->log
474 new_statement->log = malloc(message_len);
475 if (new_statement->log == NULL) {
476 lnm_abort("lnmStatement", "call to malloc() returned NULL");
477 }
478 strcpy(new_statement->log, message);
479 // add to registry
480 lnm_registry_push((lnmItem)new_statement);
481 return (lnmItem)new_statement;
482}
483
484
485lnmItem lnmEvent(const char * tag) {
486 if (tag == NULL) {
487 lnm_abort("lnmEvent", "cannot perform operation on NULL argument");
488 }
489 lnm_log_event * new_event = calloc(1, sizeof(lnm_log_event));
490 if (new_event == NULL) {
491 lnm_abort("lnmEvent", "call to calloc() returned NULL");
492 }
493 new_event->type = LNM_EVENT;
494 new_event->pushed = 0;
495 new_event->pushable = lnm_new_pushable();
496 // copy tag to event
497 int tag_len = strlen(tag) + 1;
498 new_event->tag = malloc(tag_len);
499 if (new_event->tag == NULL) {
500 lnm_abort("lnmEvent", "call to malloc() returned NULL");
501 }
502 strcpy(new_event->tag, tag);
503 // add to registry
504 lnm_registry_push((lnmItem)new_event);
505 return (lnmItem)new_event;
506}
507
508
509void lnmEventPush(lnmItem event, lnmItem item) {
510 if (event == NULL || item == NULL) {
511 lnm_abort("lnmEventPush", "cannot perform operation on NULL arguments");
512 }
513 if (event == item) {
514 lnm_abort("lnmEventPush", "attempt to push event to self");
515 }
516 lnm_log_statement * item_cast = (lnm_log_statement *)item;
517 if (item_cast->pushed == 1) {
518 lnm_abort("lnmEventPush", "attempt to push an already-pushed log item");
519 }
520 if (lnm_item_type(event) != LNM_EVENT) {
521 lnm_abort("lnmEventPush", "cannot cast non-event to event type");
522 }
523 lnm_log_event * event_cast = (lnm_log_event *)event;
524 lnm_pushable_push(event_cast->pushable, item);
525 lnm_registry_flush_item(item);
526}
527
528
529void lnmEventPushS(lnmItem event, enum lnmVerbosityLevel verbosity, const char * message) {
530 lnmItem statement = lnmStatement(verbosity, message);
531 lnmEventPush(event, statement);
532}
533
534
535lnmItem lnmEventI(const char * tag, lnmItem item) {
536 lnmItem event = lnmEvent(tag);
537 lnmEventPush(event, item);
538 return event;
539}
540
541
542lnmItem lnmEventS(const char * tag, enum lnmVerbosityLevel verbosity, const char * message) {
543 lnmItem event = lnmEvent(tag);
544 lnmEventPushS(event, verbosity, message);
545 return event;
546}
547
548
549#ifdef LNM_DEBUG
550#include <inttypes.h>
551
552void lnm_debug_tabs(int tab_count) {
553 for (int i = 0; i < tab_count; i++) {
554 printf(" ");
555 }
556}
557
558
559void lnm_debug_parse_item(lnmItem item, int tab_count) {
560 if (lnm_item_type(item) == LNM_STATEMENT) {
561 lnm_log_statement * statement = (lnm_log_statement *) item;
562 lnm_debug_tabs(tab_count);
563
564 char * verbosity;
565 switch (statement->verbosity) {
566 case 0:
567 verbosity = "INFO";
568 break;
569 case 1:
570 verbosity = "DEBUG";
571 break;
572 case 2:
573 verbosity = "VERBOSE";
574 break;
575 case 3:
576 verbosity = "VERYVERBOSE";
577 break;
578 case 4:
579 verbosity = "WARNING";
580 break;
581 case 5:
582 verbosity = "ERROR";
583 break;
584 }
585
586 printf("%" PRIu64 " (%s) :: %s\n", statement->timestamp, verbosity, statement->log);
587 } else if (lnm_item_type(item) == LNM_EVENT) {
588 lnm_log_event * event = (lnm_log_event *) item;
589 lnm_debug_tabs(tab_count);
590 printf("Event (%" PRIu32 "/%" PRIu32 ") %s [\n", event->pushable->length, event->pushable->capacity, event->tag);
591 for (uint32_t iter = 0; iter < event->pushable->length; iter++) {
592 lnmItem item = event->pushable->frame[iter];
593 lnm_debug_parse_item(item, tab_count + 1);
594 }
595 lnm_debug_tabs(tab_count);
596 printf("]\n");
597 } else {
598 lnm_abort("lnm_debug_parse_item", "unknown item type");
599 }
600}
601
602
603void lnm_debug_parse_registry(void) {
604 printf("Top level registry (%" PRIu32 "/%" PRIu32 ") [\n", lnm_registered_items->length, lnm_registered_items->capacity);
605 for (uint32_t iter = 0; iter < lnm_registered_items->length; iter++) {
606 lnm_debug_parse_item(lnm_registered_items->frame[iter], 1);
607 }
608 printf("]\n");
609}
610
611
612void lnm_debug_parse_queue(lnmQueue queue) {
613 lnm_queue * queue_cast = (lnm_queue *)queue;
614 printf("Queue \"%s\" at %s (%" PRIu32 "/%" PRIu32 ") [\n", queue_cast->name, queue_cast->out_path, queue_cast->pushable->length, queue_cast->pushable->capacity);
615 for (uint32_t iter = 0; iter < queue_cast->pushable->length; iter++) {
616 lnm_debug_parse_item((lnmItem)queue_cast->pushable->frame[iter], 1);
617 }
618 printf("]\n");
619}
620#endif // LNM_DEBUG
621#endif // LNM_INIT
622#endif // LOGNESTMONSTER_H
623
624
625#ifdef __cplusplus
626}
627#endif
628