summaryrefslogtreecommitdiff
path: root/archive/Podium/p_log.h
blob: 0f6ce6c8f39222b309e89f6a561448e72a8285c6 (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
/*
 * Logging!
 */

#include <stdlib.h>

#ifdef DEBUG
#define LOG_DEBUG(...) PRINTF(__VA_ARGS__)
#else
#define LOG_DEBUG(...) ((void)0)
#endif

/* fatal and error always enabled */
#define PFATAL(message, ...) p_log_printf(P_LOG_LEVEL_FATAL, message, ##__VA_ARGS__)
#define PERROR(message, ...) p_log_printf(P_LOG_LEVEL_ERROR, message, ##__VA_ARGS__)

/* warn and info enabled by default but can be disabled */
#define P_LOG_WARN_ENABLED 1
#define P_LOG_INFO_ENABLED 1

#if P_LOG_WARN_ENABLED == 1
#define PWARN(message, ...) p_log_printf(P_LOG_LEVEL_WARN, message, ##__VA_ARGS__)
#else
#define PWARN(message, ...)
#endif

#if P_LOG_INFO_ENABLED == 1
#define PINFO(message, ...) p_log_printf(P_LOG_LEVEL_INFO, message, ##__VA_ARGS__)
#else 
#define PINFO(message, ...)
#endif

#if P_LOG_DEBUG_ENABLED == 1
#define PDEBUG(message, ...) p_log_printf(P_LOG_LEVEL_DEBUG, message, ##__VA_ARGS__)
#else 
#define PDEBUG(message, ...)
#endif

#if P_LOG_TRACE_ENABLED == 1
#define PTRACE(message, ...) p_log_printf(P_LOG_LEVEL_TRACE, message, ##__VA_ARGS__)
#else 
#define PTRACE(message, ...)
#endif

typedef enum P_Log_Level {
    P_LOG_LEVEL_FATAL = 0,
    P_LOG_LEVEL_ERROR,
    P_LOG_LEVEL_WARN,
    P_LOG_LEVEL_INFO,
    P_LOG_LEVEL_DEBUG,
    P_LOG_LEVEL_TRACE,
    P_COUNT_LOG_LEVEL
} P_Log_Level;

#define P_PREFIX_LEN 7
static const char *p_prefix[P_COUNT_LOG_LEVEL] = {
    [P_LOG_LEVEL_FATAL] = "[FATAL]",
    [P_LOG_LEVEL_ERROR] = "[ERROR]",
    [P_LOG_LEVEL_WARN]  = "[WARNI]",
    [P_LOG_LEVEL_INFO]  = "[INFOR]",
    [P_LOG_LEVEL_DEBUG] = "[DEBUG]",
    [P_LOG_LEVEL_TRACE] = "[TRACE]",
}; 

#define P_MAX_ALLOCS 512

typedef struct P_DebugMemoryInfo {
    void *ptr;
    size_t size;
    const char *file;
    const char *func;
    int line;
} P_DebugMemoryInfo;

static uint64_t p_alloc_count = 0;
static P_DebugMemoryInfo p_ptr_array[P_MAX_ALLOCS];

static inline void *
p_debug_malloc_impl(size_t size, const char *file, int line, const char *func)
{
    void *ptr = malloc(size);
    printf("[ALLOC] %p (%zu bytes) -> %s:%d %s()\n", ptr, size, file, line, func);

    if (ptr) {
        if (p_alloc_count < P_MAX_ALLOCS) {
            p_ptr_array[p_alloc_count++] = (P_DebugMemoryInfo){
                .ptr = ptr,
                .size = size,
                .file = file,
                .func = func,
                .line = line
            };
        } else {
            fprintf(stderr, "[ERROR] Debug allocator tracking capacity (%d) exceeded!\n", P_MAX_ALLOCS);
        }
    }
    return ptr;
}

static inline void
p_debug_free_impl(void *ptr, const char *file, int line, const char *func)
{
    printf("[FREE]  %p -> %s:%d %s()\n", ptr, file, line, func);

    if (!ptr) return; // Standard C allows free(NULL)

    bool found = false;
    uint64_t index = 0;

    /* Find allocation metadata */
    for (index = 0; index < p_alloc_count; ++index) {
        if (p_ptr_array[index].ptr == ptr) {
            found = true;
            break;
        }
    }

    if (found) {
        /* Shift array left to remove tracked struct */
        for (uint64_t j = index; j < p_alloc_count - 1; ++j) {
            p_ptr_array[j] = p_ptr_array[j + 1];
        }
        p_alloc_count--;
    } else {
        fprintf(stderr, "[WARNING] Attempted to free untracked/double-freed pointer %p at %s:%d %s()\n", 
                ptr, file, line, func);
    }

    free(ptr);
}

static inline void *
p_debug_realloc_impl(void *ptr, size_t size, const char *file, int line, const char *func)
{
    if (!ptr) {
        return p_debug_malloc_impl(size, file, line, func);
    }
    if (size == 0) {
        p_debug_free_impl(ptr, file, line, func);
        return NULL;
    }

    void *new_ptr = realloc(ptr, size);
    printf("[REALLOC] %p -> %p (%zu bytes) -> %s:%d %s()\n", ptr, new_ptr, size, file, line, func);

    if (new_ptr) {
        bool found = false;
        for (uint64_t i = 0; i < p_alloc_count; ++i) {
            if (p_ptr_array[i].ptr == ptr) {
                p_ptr_array[i].ptr = new_ptr;
                p_ptr_array[i].size = size;
                p_ptr_array[i].file = file;
                p_ptr_array[i].line = line;
                p_ptr_array[i].func = func;
                found = true;
                break;
            }
        }
        if (!found) {
            if (p_alloc_count < P_MAX_ALLOCS) {
                p_ptr_array[p_alloc_count++] = (P_DebugMemoryInfo){
                    .ptr = new_ptr,
                    .size = size,
                    .file = file,
                    .func = func,
                    .line = line
                };
            }
        }
    }
    return new_ptr;
}

static void
p_debug_memory_report(void) 
{
    printf("\n==================== MEMORY REPORT ====================\n");
    printf("Remaining unfreed allocations: %lu\n", (unsigned long)p_alloc_count);
    
    for (uint64_t i = 0; i < p_alloc_count; ++i) {
        P_DebugMemoryInfo *info = &p_ptr_array[i];
        printf("[LEAK] %p (%zu bytes) allocated at %s:%d in %s()\n", 
               info->ptr, info->size, info->file, info->line, info->func);
    }
    printf("=======================================================\n");
}