-
Notifications
You must be signed in to change notification settings - Fork 16
/
log.h
42 lines (34 loc) · 1.57 KB
/
log.h
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
#ifndef __LOG_H__
#define __LOG_H__
#include <stdio.h>
#include <stdlib.h> // exit()
#include <stdint.h> // uint8_t
#include <stdbool.h> // bool, true, false
#include <string.h> // strerror()
#include <time.h> // time(), strftime(), localtime()
#include <errno.h> // errno
#define TIME_FORMAT "%Y-%m-%d %H:%M:%S" // e.g. 2021-01-04 01:10:38
#define logger(fmt, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now));\
printf("%s " fmt "\n", timestr, ##__VA_ARGS__); \
} while (0)
#define error(fmt, ...) logger("ERROR: %s:%d %s(): " fmt ": %s", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__, strerror(errno))
#define warn(fmt, ...) logger("WARN: %s:%d %s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define info(fmt, ...) logger("INFO: %s:%d %s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define info_mem(p, l) memdump(p, l);
#ifdef DEBUG
#define debug(fmt, ...) logger("DEBUG: %s:%d %s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define debug_mem(p, l) memdump(p, l);
#else
#define debug(fmt, ...)
#define debug_mem(p, l)
#endif
#define breakpoint() \
do { \
puts("Press any key to continue...");\
getchar(); \
} while (0)
#endif /* __LOG_H__ */