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 |
|
36 |
|
37 | // Base definitions |
38 |
|
39 | enum lnmVerbosityLevel {lnmInfo, lnmDebug, lnmVerbose, lnmVeryVerbose, lnmWarning, lnmError}; |
40 |
|
41 |
|
42 | // Pushable structure |
43 |
|
44 | typedef uint8_t * lnm_log_item; |
45 |
|
46 | typedef struct { |
47 | uint16_t length; |
48 | lnm_log_item * pushed; |
49 | } lnm_pushable; |
50 |
|
51 | lnm_pushable * lnm_new_pushable() { |
52 | lnm_pushable * new_pushable = malloc(sizeof(lnm_pushable)); |
53 | new_pushable->length = 0; |
54 | new_pushable->pushed = malloc(0); |
55 | return new_pushable; |
56 | } |
57 | void lnm_pushable_push(lnm_pushable * pushable, lnm_log_item item) { |
58 | pushable->pushed = realloc(pushable->pushed, sizeof(lnm_log_item)*(pushable->length+1)); // reallocate with size: length+1 |
59 | pushable->pushed[pushable->length] = item; |
60 | pushable->length += 1; |
61 | } |
62 |
|
63 |
|
64 | // Statement and event structure definitions |
65 |
|
66 | typedef struct { |
67 | uint8_t type; // Used internally; 0 = statement, 1 = event |
68 | lnm_pushable * pushed; // array of memory locations for lnm_event and lnm_log_statement structs |
69 | } lnm_log_event; |
70 |
|
71 | typedef struct { |
72 | uint8_t type:1; // Used internally; 0 = statement, 1 = event |
73 | uint8_t verbosity:3; // lnmVerbosityLevel, 0-5 |
74 | uint8_t tag_size; // character length of the tag |
75 | uint16_t message_size; // character length of the message |
76 | uint64_t timestamp; // 64-bit millisecond timestamp |
77 | char * log; // tag string + message string |
78 | } lnm_log_statement; |
79 |
|
80 |
|
81 | // Core library |
82 |
|
83 | lnm_log_item lnmStatement(uint8_t verbosity, char * tag, char * message) { |
84 | lnm_log_statement * new_statement = malloc(sizeof(lnm_log_statement)); |
85 | new_statement->type = 0; |
86 | new_statement->verbosity = verbosity; |
87 | new_statement->timestamp = 0; |
88 | int tlen = strlen(tag); |
89 | if (tlen > 255 || tlen < 0) { |
90 | printf("lognestmonster: tag length %i is longer than the cap 255 characters. exiting...\n", tlen); |
91 | exit(1); |
92 | } |
93 | int mlen = strlen(message); |
94 | if (mlen > 65535 || mlen < 0) { |
95 | printf("lognestmonster: message length %i is longer than the cap 65535 characters. exiting...\n", mlen); |
96 | exit(1); |
97 | } |
98 | new_statement->tag_size = tlen; |
99 | new_statement->message_size = mlen; |
100 | new_statement->log = malloc(tlen+mlen+1); |
101 | strcpy(new_statement->log, tag); |
102 | strcat(new_statement->log, message); |
103 | return (lnm_log_item)new_statement; |
104 | } |
105 |
|
106 | #endif |
107 |
|