-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathutils.h
228 lines (195 loc) · 5.51 KB
/
utils.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#if !defined(UTILS_H)
#define UTILS_H
#include <stdio.h>
#include <syslog.h>
#include <stdbool.h>
#include <unistd.h>
#include <glib.h>
#include <glib-unix.h>
#include <sys/uio.h>
#include <string.h>
/* stdpipe_t represents one of the std pipes (or NONE).
* Sync with const in container_attach.go */
typedef enum {
NO_PIPE,
STDIN_PIPE, /* unused */
STDOUT_PIPE,
STDERR_PIPE,
} stdpipe_t;
/* Different levels of logging */
typedef enum {
EXIT_LEVEL,
WARN_LEVEL,
INFO_LEVEL,
DEBUG_LEVEL,
TRACE_LEVEL,
} log_level_t;
// Default log level is Warning, This will be configured before any logging
// should happen
extern log_level_t log_level;
extern char *log_cid;
extern gboolean use_syslog;
#define _pexit(s) \
do { \
fprintf(stderr, "[conmon:e]: %s %s\n", s, strerror(errno)); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: %s %s\n", log_cid, s, strerror(errno)); \
_exit(EXIT_FAILURE); \
} while (0)
#define _pexitf(fmt, ...) \
do { \
fprintf(stderr, "[conmon:e]: " fmt " %s\n", ##__VA_ARGS__, strerror(errno)); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt ": %s\n", log_cid, ##__VA_ARGS__, strerror(errno)); \
_exit(EXIT_FAILURE); \
} while (0)
#define pexit(s) \
do { \
fprintf(stderr, "[conmon:e]: %s %s\n", s, strerror(errno)); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: %s %s\n", log_cid, s, strerror(errno)); \
exit(EXIT_FAILURE); \
} while (0)
#define pexitf(fmt, ...) \
do { \
fprintf(stderr, "[conmon:e]: " fmt " %s\n", ##__VA_ARGS__, strerror(errno)); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt ": %s\n", log_cid, ##__VA_ARGS__, strerror(errno)); \
exit(EXIT_FAILURE); \
} while (0)
#define pwarn(s) \
do { \
fprintf(stderr, "[conmon:w]: %s %s\n", s, strerror(errno)); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <pwarn>: %s %s\n", log_cid, s, strerror(errno)); \
} while (0)
#define nexit(s) \
do { \
fprintf(stderr, "[conmon:e] %s\n", s); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: %s\n", log_cid, s); \
exit(EXIT_FAILURE); \
} while (0)
#define nexitf(fmt, ...) \
do { \
fprintf(stderr, "[conmon:e]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt " \n", log_cid, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
#define nwarn(s) \
if (log_level >= WARN_LEVEL) { \
do { \
fprintf(stderr, "[conmon:w]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <nwarn>: %s\n", log_cid, s); \
} while (0); \
}
#define nwarnf(fmt, ...) \
if (log_level >= WARN_LEVEL) { \
do { \
fprintf(stderr, "[conmon:w]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <nwarn>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}
#define ninfo(s) \
if (log_level >= INFO_LEVEL) { \
do { \
fprintf(stderr, "[conmon:i]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ninfo>: %s\n", log_cid, s); \
} while (0); \
}
#define ninfof(fmt, ...) \
if (log_level >= INFO_LEVEL) { \
do { \
fprintf(stderr, "[conmon:i]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ninfo>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}
#define ndebug(s) \
if (log_level >= DEBUG_LEVEL) { \
do { \
fprintf(stderr, "[conmon:d]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ndebug>: %s\n", log_cid, s); \
} while (0); \
}
#define ndebugf(fmt, ...) \
if (log_level >= DEBUG_LEVEL) { \
do { \
fprintf(stderr, "[conmon:d]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ndebug>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}
#define ntrace(s) \
if (log_level >= TRACE_LEVEL) { \
do { \
fprintf(stderr, "[conmon:d]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ntrace>: %s\n", log_cid, s); \
} while (0); \
}
#define ntracef(fmt, ...) \
if (log_level >= TRACE_LEVEL) { \
do { \
fprintf(stderr, "[conmon:d]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ntrace>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}
/* Set the log level for this call. log level defaults to warning.
parse the string value of level_name to the appropriate log_level_t enum value
*/
void set_conmon_logs(char *level_name, char *cid_, gboolean syslog_, char *tag);
#define _cleanup_(x) __attribute__((cleanup(x)))
static inline void freep(void *p)
{
free(*(void **)p);
}
static inline void closep(int *fd)
{
if (*fd >= 0)
close(*fd);
*fd = -1;
}
static inline void fclosep(FILE **fp)
{
if (*fp)
fclose(*fp);
*fp = NULL;
}
static inline void gstring_free_cleanup(GString **string)
{
if (*string)
g_string_free(*string, TRUE);
}
static inline void gerror_free_cleanup(GError **err)
{
if (*err)
g_error_free(*err);
}
static inline void strv_cleanup(char ***strv)
{
if (strv)
g_strfreev(*strv);
}
static inline void hashtable_free_cleanup(GHashTable **tbl)
{
if (tbl)
g_hash_table_destroy(*tbl);
}
#define _cleanup_free_ _cleanup_(freep)
#define _cleanup_close_ _cleanup_(closep)
#define _cleanup_fclose_ _cleanup_(fclosep)
#define _cleanup_gstring_ _cleanup_(gstring_free_cleanup)
#define _cleanup_gerror_ _cleanup_(gerror_free_cleanup)
#define _cleanup_strv_ _cleanup_(strv_cleanup)
#define _cleanup_hashtable_ _cleanup_(hashtable_free_cleanup)
#define WRITEV_BUFFER_N_IOV 128
ssize_t write_all(int fd, const void *buf, size_t count);
#endif /* !defined(UTILS_H) */