summaryrefslogtreecommitdiff
path: root/src/lib/dlt_env_ll.c
blob: 6cf7ded477cb5e1cf439ab44634785318b3d0251 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2015  Intel Corporation
 *
 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 */

/*!
 * \author Stefan Vacek <stefan.vacek@intel.com> Intel Corporation
 *
 * \copyright Copyright © 2015 Intel Corporation. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt_env_ll.c
 */

#include "dlt_user.h"
#include <string.h>
#include <stdlib.h>

#define DLT_ENV_LL_SET_INCREASE 10


/* a generic entry looks like:
 * ll_item ::= apid:ctid:ll
 * ll_set  ::= ll_item |
 *             ll_set;ll_item
 */

/**
 * @brief extract id out of given string
 *
 * Extract 4-byte string out of given environment string, the pointer of the
 * environment string is moved to the next un-used character and the extracted
 * id is copied into \param id
 *
 * Example:
 * env[] = "abcd:1234:3"
 * char res[4u];
 * char * tmp = &env[0];
 * int ret = extract_id(&tmp, res);
 * assert(ret == 0);
 * assert(*tmp == ':');
 * assert(res[3] == 'd');
 *
 * @param env    Environment variable
 * @param id     Extracted ID
 * @return 0 if successful, -1 else
 */
int dlt_env_extract_id(char **const env, char *id)
{
    int i;

    if (!env || !id)
        return -1;

    if (!(*env))
        return -1;

    memset(id, 0, 4);

    for (i = 0; (i < 4) && (**env != ':') && (**env != 0); ++i)
        *id++ = *((*env)++);

    /* the next/last character must be ':' */
    if ((0 != **env) && (':' == **env))
        return 0;

    return -1;
}


/**
 * @brief convert a given string to lower-case
 *
 * Stops end of string or if ';' is detected
 */
int dlt_env_helper_to_lower(char **const env, char *result, int const res_len)
{
    int count = 0;
    char ch;

    if (!env || !result)
        return -1;

    if (!(*env))
        return -1;

    ch = *(*env);

    while (ch && (count < res_len - 1) && (ch != ';')) {
        if ((ch >= 'A') && (ch <= 'Z'))
            result[count] = ch + 'a' - 'A';
        else
            result[count] = ch;

        ch = *(++(*env));
        ++count;
    }

    result[count] = 0;

    if (!ch || (ch == ';')) /* full input was parsed */
        return 0;
    else
        return -1;
}


int dlt_env_extract_symbolic_ll(char **const env, int8_t *ll)
{
    char result[strlen("verbose") + 1];

    if (!env || !ll)
        return -1;

    if (!(*env))
        return -1;

    if (dlt_env_helper_to_lower(env, &result[0], (int) sizeof(result)) == 0) {
        if (strncmp("default", result, sizeof(result)) == 0)
            *ll = -1;
        else if (strncmp("off", result, sizeof(result)) == 0)
            *ll = 0;
        else if (strncmp("fatal", result, sizeof(result)) == 0)
            *ll = 1;
        else if (strncmp("error", result, sizeof(result)) == 0)
            *ll = 2;
        else if (strncmp("warning", result, sizeof(result)) == 0)
            *ll = 3;
        else if (strncmp("info", result, sizeof(result)) == 0)
            *ll = 4;
        else if (strncmp("debug", result, sizeof(result)) == 0)
            *ll = 5;
        else if (strncmp("verbose", result, sizeof(result)) == 0)
            *ll = 6;
        else
            return -1;

        if (**env != 0)
            (*env)++;

        return 0;
    }
    else {
        return -1;
    }
}


/**
 * @brief extract log-level out of given string
 *
 * A valid log-level is a numeric value in the range of -1 .. 6, with:
 * -1: default
 *  0: off
 *  1: fatal
 *  2: error
 *  3: warning
 *  4: info
 *  5: debug
 *  6: verbose
 * During parsing, the environment string is moved to the next un-used character and the extracted
 * log-level is written into \param ll
 *
 * Example:
 * env[] = "abcd:1234:6"
 * int ll;
 * char ** tmp = &env[10]; // tmp points to '6'!
 * int ret = extract_ll(&tmp, &ll);
 * assert(ret == 0);
 * assert(*tmp == NULL);
 * assert(ll == 6);
 *
 * @param env    Environment variable
 * @param ll     Extracted log level
 * @return 0 if successful, -1 else
 */
int dlt_env_extract_ll(char **const env, int8_t *ll)
{
    if (!env || !ll)
        return -1;

    if (!(*env))
        return -1;

    /* extract number */
    if (**env == '-') {
        (*env)++;

        if (**env == '1') {
            *ll = -1;
            (*env)++;
        }
    }
    else {
        if ((**env >= '0') && (**env < '7')) {
            *ll = (int8_t) (**env - '0');
            (*env)++;
        }
        else if (dlt_env_extract_symbolic_ll(env, ll) != 0)
            return -1;
    }

    /* check end, either next char is NULL or ';' */
    if ((**env == ';') || (**env == 0))
        return 0;

    return -1;
}


/**
 * @brief extract one item out of string
 *
 * @return 0 if successful, -1 else
 */
int dlt_env_extract_ll_item(char **const env, dlt_env_ll_item *const item)
{
    int ret = -1;

    if (!env || !item)
        return -1;

    if (!(*env))
        return -1;

    memset(item, 0, sizeof(dlt_env_ll_item));
    ret = dlt_env_extract_id(env, item->appId);

    if (ret == -1)
        return -1;

    (*env)++;
    ret = dlt_env_extract_id(env, item->ctxId);

    if (ret == -1)
        return -1;

    (*env)++;
    ret = dlt_env_extract_ll(env, &item->ll);

    if (ret == -1)
        return -1;

    return 0;
}


/**
 * @brief initialize ll_set
 *
 * Must call release_ll_set before exit to release all memory
 *
 * @return -1 if memory could not be allocated
 * @return 0 on success
 */
int dlt_env_init_ll_set(dlt_env_ll_set *const ll_set)
{
    if (!ll_set)
        return -1;

    ll_set->array_size = DLT_ENV_LL_SET_INCREASE;
    ll_set->item = (dlt_env_ll_item *)malloc(sizeof(dlt_env_ll_item) * ll_set->array_size);

    if (!ll_set->item) {
        /* should trigger a warning: no memory left */
        ll_set->array_size = 0;
        return -1;
    }

    ll_set->num_elem = 0u;
    return 0;
}


/**
 * @brief release ll_set
 */
void dlt_env_free_ll_set(dlt_env_ll_set *const ll_set)
{
    if (!ll_set)
        return;

    if (ll_set->item != NULL) {
        free(ll_set->item);
        ll_set->item = NULL;
    }

    ll_set->array_size = 0u;
    ll_set->num_elem = 0u;
}


/**
 * @brief increase size of ll_set by LL_SET_INCREASE elements
 *
 * @return -1 if memory could not be allocated
 * @return 0 on success
 */
int dlt_env_increase_ll_set(dlt_env_ll_set *const ll_set)
{
    dlt_env_ll_item *old_set;
    size_t old_size;

    if (!ll_set)
        return -1;

    old_set = ll_set->item;
    old_size = ll_set->array_size;

    ll_set->array_size += DLT_ENV_LL_SET_INCREASE;
    ll_set->item = (dlt_env_ll_item *)malloc(sizeof(dlt_env_ll_item) * ll_set->array_size);

    if (!ll_set->item) {
        /* should trigger a warning: no memory left */
        ll_set->array_size -= DLT_ENV_LL_SET_INCREASE;
        return -1;
    }
    else {
        memcpy(ll_set->item, old_set, sizeof(dlt_env_ll_item) * old_size);
        free(old_set);
        return 0;
    }
}


/**
 * @brief extract all items out of string
 *
 * The given set is initialized within this function (memory is allocated).
 * Make sure, that the caller frees this memory when it is no longer needed!
 *
 * @return 0 if successful, -1 else
 */
int dlt_env_extract_ll_set(char **const env, dlt_env_ll_set *const ll_set)
{
    if (!env || !ll_set)
        return -1;

    if (!(*env))
        return -1;

    if (dlt_env_init_ll_set(ll_set) == -1)
        return -1;

    do {
        if (ll_set->num_elem == ll_set->array_size) {
            if (dlt_env_increase_ll_set(ll_set) == -1)
                return -1;
        }

        if (dlt_env_extract_ll_item(env, &ll_set->item[ll_set->num_elem++]) == -1)
            return -1;

        if (**env == ';')
            (*env)++;
    } while (**env != 0);

    return 0;
}


/**
 * @brief check if two ids match
 *
 * @return 1 if matching, 0 if not
 */
int dlt_env_ids_match(char const *const a, char const *const b)
{
    if (a[0] != b[0])
        return 0;

    if (a[1] != b[1])
        return 0;

    if (a[2] != b[2])
        return 0;

    if (a[3] != b[3])
        return 0;

    return 1;
}


/**
 * @brief check if (and how) apid and ctid match with given item
 *
 * Resulting priorities:
 * - no apid, no ctid only ll given in item: use ll with prio 1
 * - no apid, ctid matches: use ll with prio 2
 * - no ctid, apid matches: use ll with prio 3
 * - apid, ctid matches: use ll with prio 4
 *
 * In case of error, -1 is returned.
 */
int dlt_env_ll_item_get_matching_prio(dlt_env_ll_item const *const item, char const *const apid, char const *const ctid)
{
    if ((!item) || (!apid) || (!ctid))
        return -1;

    if (item->appId[0] == 0) {
        if (item->ctxId[0] == 0) {
            return 1;
        }
        else if (dlt_env_ids_match(item->ctxId, ctid))
            return 2;
    }
    else if (dlt_env_ids_match(item->appId, apid)) {
        if (item->ctxId[0] == 0)
            return 3;
        else if (dlt_env_ids_match(item->ctxId, ctid))
            return 4;
    }

    return 0;
}


/**
 * @brief adjust log-level based on values given through environment
 *
 * Iterate over the set of items, and find the best match (\see ll_item_get_matching_prio)
 * For any item that matches, the one with the highest priority is selected and that
 * log-level is returned.
 *
 * If no item matches or in case of error, the original log-level (\param ll) is returned
 */
int dlt_env_adjust_ll_from_env(dlt_env_ll_set const *const ll_set,
                               char const *const apid,
                               char const *const ctid,
                               int const ll)
{
    if ((!ll_set) || (!apid) || (!ctid))
        return ll;

    int res = ll;
    int prio = 0; /* no match so far */
    size_t i;

    for (i = 0; i < ll_set->num_elem; ++i) {
        int p = dlt_env_ll_item_get_matching_prio(&ll_set->item[i], apid, ctid);

        if (p > prio) {
            prio = p;
            res = ll_set->item[i].ll;

            if (p == 4) /* maximum reached, immediate return */
                return res;
        }
    }

    return res;
}