-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
116 lines (103 loc) · 2.73 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
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
/**
* @file log.h
* @brief Logging functions
*/
/* log.h v0.0.1
* simple logging library - sleepntsheep 2022
* logging should be simple and easy,
* here you put in argument just as how you would printf it
* panic(...) : log at panic level and abort
* warn(...) : log at warn level
* info(...) : log at info level
* the *err(...) counterpart of each function
* do the same thing except it call perror at the end
* causing error from errno to be printed too
*/
#ifdef __cplusplus
#include <cstdio>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#else
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#ifndef SHEEP_LOG_H
#define SHEEP_LOG_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief log at panic level and abort
*/
#define panic(...) do { \
__stderr_log("PANIC", __FILE__, __LINE__, __VA_ARGS__); \
exit(1); \
} while (0)
/**
* @brief log at panic level, print errno and abort
*/
#define panicerr(...) do { \
__stderr_log("PANIC", __FILE__, __LINE__, __VA_ARGS__); \
perror(""); \
exit(1); \
} while (0)
/**
* @brief log at warn level
*/
#define warn(...) do { \
__stderr_log("WARN", __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
/**
* @brief log at warn level, print errno
*/
#define warnerr(...) do { \
__stderr_log("WARN", __FILE__, __LINE__, __VA_ARGS__); \
perror(""); \
} while (0)
/**
* @brief log at info level
*/
#define info(...) do { \
__stderr_log("INFO", __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
/**
* @brief log at info level, print errno
*/
#define infoerr(...) do { \
__stderr_log("INFO", __FILE__, __LINE__, __VA_ARGS__); \
perror(""); \
} while (0)
static void
__stderr_log(const char *type, const char *file,
const int line, const char *fmt, ...);
#ifdef __cplusplus
}
#endif
#endif /* SHEEP_LOG_H */
#ifdef SHEEP_LOG_IMPLEMENTATION
#ifdef __cplusplus
extern "C" {
#include <cstdarg>
#else
#include <stdarg.h>
#endif
static void
__stderr_log(const char *type, const char *file
, const int line, const char *fmt
, ...)
{
fprintf(stderr, "%s: %s:%d: ", type, file, line);
va_list a;
va_start(a, fmt);
vfprintf(stderr, fmt, a);
va_end(a);
fprintf(stderr, "\n");
fflush(stderr);
}
#ifdef __cplusplus
}
#endif
#endif /* SHEEP_LOG_IMPLEMENTATION */