File: tcommon.c

package info (click to toggle)
sysrepo 4.5.1-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 6,824 kB
  • sloc: ansic: 86,774; sh: 145; makefile: 29
file content (87 lines) | stat: -rw-r--r-- 1,855 bytes parent folder | download
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
/**
 * @file tcommon.c
 * @author Irfan
 * @brief common header file for all tests to facilitate uniform logging format
 *
 * @copyright
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */
#define _GNU_SOURCE

#include <assert.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>

#include "sysrepo.h"
#include "tests/tcommon.h"

static void
_test_log_msg(sr_log_level_t level, const char *message, const char *prefix)
{
    const char *severity = NULL;
    struct timespec ts;

    switch (level) {
    case SR_LL_ERR:
        severity = "ERR";
        break;
    case SR_LL_WRN:
        severity = "WRN";
        break;
    case SR_LL_INF:
        severity = "INF";
        break;
    case SR_LL_VRB:
        /*severity = "VRB";
        break;*/
        return;
    case SR_LL_DBG:
        /*severity = "DBG";
        break;*/
        return;
    case SR_LL_NONE:
        assert(0);
        return;
    }

    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec %= 1000;
    ts.tv_nsec /= 1000;
    fprintf(stderr, "%03lld.%06ld [%ld][%lu][%s]%s: %s\n", (long long)ts.tv_sec, ts.tv_nsec,
            (long)getpid(), (unsigned long)pthread_self(), severity,
            prefix, message);
}

static void
_test_sr_log_cb(sr_log_level_t level, const char *message)
{
    _test_log_msg(level, message, "");
}

void
test_log_init(void)
{
    sr_log_set_cb(_test_sr_log_cb);
}

void
_test_log(sr_log_level_t ll, ...)
{
    va_list ap;
    char msg[1024] = "";
    char *fmt = NULL;

    va_start(ap, ll);
    fmt = va_arg(ap, char *);

    vsnprintf(msg, sizeof(msg), fmt, ap);
    va_end(ap);
    _test_log_msg(ll, msg, "[TestLog]");
}