aboutsummaryrefslogtreecommitdiff
path: root/snag/src/util.h
blob: 38139039d861c1894ae87ea09c5eb4aeda4a579f (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
#ifndef UTIL_H
#define UTIL_H

#include <stdarg.h>
#include <stddef.h>

// utility for errors
#if !DEBUG
    void log(const char* format, ...) __attribute__((format(printf, 1, 2)));
    void die(const char* format, ...) __attribute__((format(printf, 1, 2)));
    void* emalloc(size_t size, char* alloc_reason);
#else
    #define log(fmt, ...) _log(__LINE__, __FILE__, fmt, ##__VA_ARGS__)
    void _log(int line, const char* file, const char* format, ...) __attribute__((format(printf, 3, 4)));

    #define die(fmt, ...) _die(__LINE__, __FILE__, fmt, ##__VA_ARGS__)
    void _die(int line, const char* file, const char* format, ...) __attribute__((format(printf, 3, 4)));

    #define emalloc(size, alloc_reason) _emalloc(__LINE__, __FILE__, size, alloc_reason)
    void* _emalloc(int line, const char* file, size_t size, char* alloc_reason);
#endif

// functions for outputting and displaying text
#define printout(fmt, ...) fprintf(stdout, fmt, ##__VA_ARGS__)
#define print(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)

// string functions
char* vastrcat_(int dummy, ...);
#define vastrcat(...) vastrcat_(0, __VA_ARGS__, NULL)

#endif