aboutsummaryrefslogtreecommitdiff
path: root/src/common.h
blob: 4f580b983bb9f5a95b219cf2dc16ec3406147935 (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
#ifndef COMMON_H
#define COMMON_H

// LOGGING

#include <stdio.h>
#include <time.h>

#define _RED    "\033[0;31m"
#define _GREEN  "\033[0;32m"
#define _YELLOW "\033[0;33m"
#define _RST    "\033[0m"

#define _log_print(...) fprintf(stderr, __VA_ARGS__)

#ifdef DEBUG
#define _log(color, message) _log_print("[%s] %s%-7s"_RST" ", timenow(), color, message)
#define _log_file() _log_print(__FILE__":%d: ", __LINE__)
#else
#define _log(color, message) _log_print("%s%-7s"_RST" ", color, message)
#define _log_file()
#endif

#define info(...) do {                              \
        _log(_GREEN, "[INFO]");                     \
        _log_print(__VA_ARGS__); _log_print("\n");  \
    } while(0)
#define err(...)  do {                              \
        _log(_RED,   "[ERROR]");                    \
        _log_file();                                \
        _log_print(__VA_ARGS__); _log_print("\n");  \
    } while(0)
#define die(...) do {     \
        err(__VA_ARGS__); \
        abort();          \
    } while(0)

static inline char *timenow(void)
{
    static char buffer[64];
    time_t timer = time(NULL);
    strftime(buffer, 64, "%H:%M:%S", localtime(&timer));
    return buffer;
}

// ERROR MANAGEMENT

#include <errno.h>

#define NOT_IMPLEMENTED() die("Not Implemented. ABORTING")

#define ERR_NZ(e, r, on_fail) do {                \
        int r;                                    \
        if((r = e)) { on_fail; }                  \
    } while(0)

#define ERR_NZ_RET(e)                             \
    ERR_NZ(e, r, return r)                        \

#define ERR_ERRNO_SET(e, on_fail)                 \
    ERR_NZ(e, _r, errno = -_r; on_fail)

#define ERR_Z(e, on_fail) do {                    \
        if(!(e)) { on_fail; }                     \
    } while(0)

#endif

// MEMORY MANAGEMENT

#ifdef ENABLE_MEMDEBUG
#include "memdebug.h"
#define DISABLE_DIE_ALLOC
#endif

#ifndef DISABLE_DIE_ALLOC

#include <stdlib.h>
#include <string.h>
#include <errno.h>

// #ifdef __GNUC__
// #define __die_alloc(e) ({void *ptr = e; if(!v) die("%s", strerror(errno)); ptr;})
// #define malloc(...)  __die_alloc(malloc(__VA_ARGS__))
// #define calloc(...)  __die_alloc(calloc(__VA_ARGS__))
// #define realloc(...) __die_alloc(realloc(__VA_ARGS__))
// #else

#define DIE_ALLOC_BUILDER(name, typed_args, args)   \
    static inline void *__die_##name typed_args     \
    {                                               \
        void *r = name args;                        \
        if(!r) die("%s", strerror(errno));          \
        return r;                                   \
    }

DIE_ALLOC_BUILDER(malloc,  (size_t size), (size))
DIE_ALLOC_BUILDER(calloc,  (size_t nmemb, size_t size), (nmemb, size))
DIE_ALLOC_BUILDER(realloc, (void *ptr, size_t size), (ptr, size))

#define malloc(size)        __die_malloc (       size)
#define calloc(nmemb, size) __die_calloc (nmemb, size)
#define realloc(ptr, size)  __die_realloc(ptr,   size)

// #endif
#endif