File: log.h

package info (click to toggle)
ga 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,472 kB
  • sloc: ansic: 192,963; fortran: 53,761; f90: 11,218; cpp: 5,784; makefile: 2,248; sh: 1,945; python: 1,734; perl: 534; csh: 134; asm: 106
file content (122 lines) | stat: -rw-r--r-- 4,944 bytes parent folder | download | duplicates (3)
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
/*
 *  * Copyright (c) 2016 Intel Corporation.  All rights reserved.
 */

#ifndef LOG_H_
#define LOG_H_

#include <assert.h>
#include <ctype.h>
#include <execinfo.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <time.h>
#ifndef __USE_MISC
#define __USE_MISC
#endif
#ifndef __USE_XOPEN2K
#define __USE_XOPEN2K
#endif
#include <unistd.h>

#include "env.h"

typedef enum log_level_t
{
    ERROR = 0,
    WARN,
    INFO,
    DEBUG,
    TRACE
} log_level_t;

#define COMEX_OFI_LOG(log_lvl, fmt, ...)                                               \
  do {                                                                                 \
        if (log_lvl <= env_data.log_level)                                             \
        {                                                                              \
            char time_buf[20]; /*2016:07:21 14:47:39*/                                 \
            get_time(time_buf, 20);                                                    \
            switch (log_lvl)                                                           \
            {                                                                          \
                case ERROR:                                                            \
                {                                                                      \
                    printf("%s: ERROR: (%d): %s:%u " fmt "\n", time_buf, get_tid(),    \
                            __FUNCTION__, __LINE__, ##__VA_ARGS__);                    \
                    print_backtrace();                                                 \
                    break;                                                             \
                }                                                                      \
                case WARN:                                                             \
                {                                                                      \
                    printf("WARNING: (%d): " fmt "\n", get_tid(), ##__VA_ARGS__);      \
                    break;                                                             \
                }                                                                      \
                case INFO:                                                             \
                {                                                                      \
                    printf("(%d):" fmt "\n", get_tid(), ##__VA_ARGS__);                \
                    break;                                                             \
                }                                                                      \
                case DEBUG:                                                            \
                case TRACE:                                                            \
                {                                                                      \
                    printf("%s: (%d): %s:%u " fmt "\n", time_buf, get_tid(),           \
                            __FUNCTION__, __LINE__, ##__VA_ARGS__);                    \
                    break;                                                             \
                }                                                                      \
                default:                                                               \
                {                                                                      \
                    assert(0);                                                         \
                }                                                                      \
            }                                                                          \
            fflush(stdout);                                                            \
        }                                                                              \
  } while (0)

static int get_tid()
{
#if defined(__APPLE__) && defined(__MACH__)
    uint64_t tid;
    pthread_threadid_np(pthread_self(), &tid);
    return (int)tid;
#else
    return (int)syscall(SYS_gettid);
#endif
}

static void get_time(char* buf, size_t buf_size)
{
    time_t timer;
    struct tm* time_info = 0;
    time(&timer);
    time_info = localtime(&timer);
    assert(time_info);
    strftime(buf, buf_size, "%Y:%m:%d %H:%M:%S", time_info);
}

static void print_backtrace(void)
{
    int j, nptrs;
    void* buffer[100];
    char** strings;

    nptrs = backtrace(buffer, 100);
    printf("backtrace() returned %d addresses\n", nptrs);
    fflush(stdout);

    strings = backtrace_symbols(buffer, nptrs);
    if (strings == NULL)
    {
        perror("backtrace_symbols");
        exit(EXIT_FAILURE);
    }

    for (j = 0; j < nptrs; j++)
    {
        printf("%s\n", strings[j]);
        fflush(stdout);
    }
    free(strings);
}

#endif /* LOG_H_ */