File: debug.h

package info (click to toggle)
yubihsm-shell 2.7.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,020 kB
  • sloc: ansic: 41,745; sh: 2,030; cpp: 528; makefile: 18; xml: 16
file content (74 lines) | stat: -rw-r--r-- 2,813 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
/*
 * Copyright 2015-2018 Yubico AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef DEBUG_H
#define DEBUG_H

#include "../common/platform-config.h"
#include "time_win.h"

#ifdef _MSVC
#include <winsock.h>
#endif

#ifdef __linux__
#define ANSI_RED "\x1b[31m"
#define ANSI_GREEN "\x1b[32m"
#define ANSI_YELLOW "\x1b[33m"
#define ANSI_BLUE "\x1b[34m"
#define ANSI_MAGENTA "\x1b[35m"
#define ANSI_CYAN "\x1b[36m"
#define ANSI_RESET "\x1b[0m"
#else
#define ANSI_RED ""
#define ANSI_GREEN ""
#define ANSI_YELLOW ""
#define ANSI_BLUE ""
#define ANSI_MAGENTA ""
#define ANSI_CYAN ""
#define ANSI_RESET ""
#endif

#define __FILENAME__                                                           \
  (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

#ifdef _MSVC
#define localtime_r(a, b) localtime_s(b, a)
#endif

#define D(var, file, col, who, lev, ...)                                       \
  if (var) {                                                                   \
    struct timeval _tv;                                                        \
    struct tm _tm;                                                             \
    char _tbuf[20];                                                            \
    time_t _tsecs;                                                             \
    gettimeofday(&_tv, NULL);                                                  \
    _tsecs = _tv.tv_sec;                                                       \
    localtime_r(&_tsecs, &_tm);                                                \
    strftime(_tbuf, 20, "%H:%M:%S", &_tm);                                     \
    fprintf(file, "[" col who " - " lev ANSI_RESET " %s.%06ld] ", _tbuf,       \
            (long) _tv.tv_usec);                                               \
    fprintf(file, "%s:%d (%s): ", __FILENAME__, __LINE__, __func__);           \
    fprintf(file, __VA_ARGS__);                                                \
  }

#define DLN(var, file, col, who, lev, ...)                                     \
  if (var) {                                                                   \
    D(var, file != NULL ? file : stderr, col, who, lev, __VA_ARGS__);          \
    fprintf(file != NULL ? file : stderr, "\n");                               \
  }

#endif