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 | lnm_pushable * 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 | return pushable; |
62 | } |
63 |
|
64 |
|
65 | // Statement and event structure definitions |
66 |
|
67 | typedef struct { |
68 | uint8_t type; // Used internally; 0 = statement, 1 = event |
69 | lnm_pushable * pushed; // array of memory locations for lnm_event and lnm_log_statement structs |
70 | } lnm_log_event; |
71 |
|
72 | typedef struct { |
73 | uint8_t type:1; // Used internally; 0 = statement, 1 = event |
74 | uint8_t verbosity:3; // lnmVerbosityLevel, 0-5 |
75 | uint8_t tag_size; // character length of the tag |
76 | uint16_t message_size; // character length of the message |
77 | uint64_t timestamp; // 64-bit millisecond timestamp |
78 | char * log; // tag string + message string |
79 | } lnm_log_statement; |
80 |
|
81 |
|
82 | // Core library |
83 |
|
84 | lnm_log_item lnmStatement(uint8_t verbosity, char * tag, char * message) { |
85 | lnm_log_statement * new_statement = malloc(sizeof(lnm_log_statement)); |
86 | new_statement->type = 0; |
87 | new_statement->verbosity = verbosity; |
88 | new_statement->timestamp = 0; |
89 | int tlen = strlen(tag); |
90 | if (tlen > 255 || tlen < 0) { |
91 | printf("lognestmonster: tag length %i is longer than the cap 255 characters. exiting...\n", tlen); |
92 | exit(1); |
93 | } |
94 | int mlen = strlen(message); |
95 | if (mlen > 65535 || mlen < 0) { |
96 | printf("lognestmonster: message length %i is longer than the cap 65535 characters. exiting...\n", mlen); |
97 | exit(1); |
98 | } |
99 | new_statement->tag_size = tlen; |
100 | new_statement->message_size = mlen; |
101 | new_statement->log = malloc(tlen+mlen+1); |
102 | strcpy(new_statement->log, tag); |
103 | strcat(new_statement->log, message); |
104 | return (lnm_log_item)new_statement; |
105 | } |
106 |
|
107 | #endif |
108 |
|