summaryrefslogtreecommitdiffstats
path: root/src/collectors/log2journal/log2journal-logfmt.c
blob: b500f0d82084b87434d6cb629a14294a7b44747e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// SPDX-License-Identifier: GPL-3.0-or-later

#include "log2journal.h"

#define LOGFMT_ERROR_LINE_MAX 1024
#define LOGFMT_KEY_MAX 1024

struct logfmt_state {
    LOG_JOB *jb;

    const char *line;
    uint32_t pos;
    uint32_t key_start;

    char key[LOGFMT_KEY_MAX];
    char msg[LOGFMT_ERROR_LINE_MAX];
};

#define logfmt_current_pos(lfs) &(lfs)->line[(lfs)->pos]
#define logfmt_consume_char(lfs) ++(lfs)->pos

static inline void logfmt_process_key_value(LOGFMT_STATE *lfs, const char *value, size_t len) {
    log_job_send_extracted_key_value(lfs->jb, lfs->key, value, len);
}

static inline void logfmt_skip_spaces(LOGFMT_STATE *lfs) {
    const char *s = logfmt_current_pos(lfs);
    const char *start = s;

    while(isspace(*s)) s++;

    lfs->pos += s - start;
}

static inline void copy_newline(LOGFMT_STATE *lfs __maybe_unused, char **d, size_t *remaining) {
    if(*remaining > 3) {
        *(*d)++ = '\\';
        *(*d)++ = 'n';
        (*remaining) -= 2;
    }
}

static inline void copy_tab(LOGFMT_STATE *lfs __maybe_unused, char **d, size_t *remaining) {
    if(*remaining > 3) {
        *(*d)++ = '\\';
        *(*d)++ = 't';
        (*remaining) -= 2;
    }
}

static inline bool logftm_parse_value(LOGFMT_STATE *lfs) {
    static __thread char value[JOURNAL_MAX_VALUE_LEN];

    char quote = '\0';
    const char *s = logfmt_current_pos(lfs);
    if(*s == '\"' || *s == '\'') {
        quote = *s;
        logfmt_consume_char(lfs);
    }

    value[0] = '\0';
    char *d = value;
    s = logfmt_current_pos(lfs);
    size_t remaining = sizeof(value);

    char end_char = (char)(quote == '\0' ? ' ' : quote);
    while (*s && *s != end_char) {
        char c;

        if (*s == '\\') {
            s++;

            switch (*s) {
                case 'n':
                    copy_newline(lfs, &d, &remaining);
                    s++;
                    continue;

                case 't':
                    copy_tab(lfs, &d, &remaining);
                    s++;
                    continue;

                case 'f':
                case 'b':
                case 'r':
                    c = ' ';
                    s++;
                    break;

                default:
                    c = *s++;
                    break;
            }
        }
        else
            c = *s++;

        if(remaining < 2) {
            snprintf(lfs->msg, sizeof(lfs->msg),
                     "LOGFMT PARSER: truncated string value at position %u", lfs->pos);
            return false;
        }
        else {
            *d++ = c;
            remaining--;
        }
    }
    *d = '\0';
    lfs->pos += s - logfmt_current_pos(lfs);

    s = logfmt_current_pos(lfs);

    if(quote != '\0') {
        if (*s != quote) {
            snprintf(lfs->msg, sizeof(lfs->msg),
                     "LOGFMT PARSER: missing quote at position %u: '%s'",
                     lfs->pos, s);
            return false;
        }
        else
            logfmt_consume_char(lfs);
    }

    if(d > value)
        logfmt_process_key_value(lfs, value, d - value);

    return true;
}

static inline bool logfmt_parse_key(LOGFMT_STATE *lfs) {
    logfmt_skip_spaces(lfs);

    char *d = &lfs->key[lfs->key_start];

    size_t remaining = sizeof(lfs->key) - (d - lfs->key);

    const char *s = logfmt_current_pos(lfs);
    char last_c = '\0';
    while(*s && *s != '=') {
        char c;

        if (*s == '\\')
            s++;

        c = journal_key_characters_map[(unsigned char)*s++];

        if(c == '_' && last_c == '_')
            continue;
        else {
            if(remaining < 2) {
                snprintf(lfs->msg, sizeof(lfs->msg),
                         "LOGFMT PARSER: key buffer full - keys are too long, at position %u", lfs->pos);
                return false;
            }
            *d++ = c;
            remaining--;
        }

        last_c = c;
    }
    *d = '\0';
    lfs->pos += s - logfmt_current_pos(lfs);

    s = logfmt_current_pos(lfs);
    if(*s != '=') {
        snprintf(lfs->msg, sizeof(lfs->msg),
                 "LOGFMT PARSER: key is missing the equal sign, at position %u", lfs->pos);
        return false;
    }

    logfmt_consume_char(lfs);

    return true;
}

LOGFMT_STATE *logfmt_parser_create(LOG_JOB *jb) {
    LOGFMT_STATE *lfs = mallocz(sizeof(LOGFMT_STATE));
    memset(lfs, 0, sizeof(LOGFMT_STATE));
    lfs->jb = jb;

    if(jb->prefix)
        lfs->key_start = copy_to_buffer(lfs->key, sizeof(lfs->key), lfs->jb->prefix, strlen(lfs->jb->prefix));

    return lfs;
}

void logfmt_parser_destroy(LOGFMT_STATE *lfs) {
    if(lfs)
        freez(lfs);
}

const char *logfmt_parser_error(LOGFMT_STATE *lfs) {
    return lfs->msg;
}

bool logfmt_parse_document(LOGFMT_STATE *lfs, const char *txt) {
    lfs->line = txt;
    lfs->pos = 0;
    lfs->msg[0] = '\0';

    const char *s;
    do {
        if(!logfmt_parse_key(lfs))
            return false;

        if(!logftm_parse_value(lfs))
            return false;

        logfmt_skip_spaces(lfs);

        s = logfmt_current_pos(lfs);
    } while(*s);

    return true;
}


void logfmt_test(void) {
    LOG_JOB jb = { .prefix = "NIGNX_" };
    LOGFMT_STATE *logfmt = logfmt_parser_create(&jb);

    logfmt_parse_document(logfmt, "x=1 y=2 z=\"3 \\ 4\" 5  ");

    logfmt_parser_destroy(logfmt);
}