-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.h
231 lines (188 loc) · 8.69 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
229
230
231
/*
* utils.h - Misc utilities
*
* Copyright (C) 2013 - 2016, Max Lv <[email protected]>
*
* This file is part of the shadowsocks-libev.
*
* shadowsocks-libev is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* shadowsocks-libev is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with shadowsocks-libev; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef _UTILS_H
#define _UTILS_H
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define PORTSTRLEN 16
#define SS_ADDRSTRLEN (INET6_ADDRSTRLEN + PORTSTRLEN + 1)
#ifdef ANDROID
#include <android/log.h>
#define USE_TTY()
#define USE_SYSLOG(ident)
#define LOGI(...) \
((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", \
__VA_ARGS__))
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, "shadowsocks", \
__VA_ARGS__))
#else
#define STR(x) # x
#define TOSTR(x) STR(x)
#ifdef LIB_ONLY
extern FILE *logfile;
#define TIME_FORMAT "%Y-%m-%d %H:%M:%S"
#define USE_TTY()
#define USE_SYSLOG(ident)
#define USE_LOGFILE(ident) \
do { \
if (ident != NULL) { logfile = fopen(ident, "w+"); } } \
while (0)
#define CLOSE_LOGFILE \
do { \
if (logfile != NULL) { fclose(logfile); } } \
while (0)
#define LOGI(format, ...) \
do { \
if (logfile != NULL) { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(logfile, " %s INFO: " format "\n", timestr, ## __VA_ARGS__); \
fflush(logfile); } \
} \
while (0)
#define LOGE(format, ...) \
do { \
if (logfile != NULL) { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(logfile, " %s ERROR: " format "\n", timestr, \
## __VA_ARGS__); \
fflush(logfile); } \
} \
while (0)
#elif defined(_WIN32)
#define TIME_FORMAT "%Y-%m-%d %H:%M:%S"
#define USE_TTY()
#define USE_SYSLOG(ident)
#define LOGI(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, " %s INFO: " format "\n", timestr, ## __VA_ARGS__); \
fflush(stderr); } \
while (0)
#define LOGE(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, " %s ERROR: " format "\n", timestr, ## __VA_ARGS__); \
fflush(stderr); } \
while (0)
#else
#include <syslog.h>
extern int use_tty;
#define USE_TTY() \
do { \
use_tty = isatty(STDERR_FILENO); \
} while (0) \
#define HAS_SYSLOG
extern int use_syslog;
#define TIME_FORMAT "%F %T"
#define USE_SYSLOG(ident) \
do { \
use_syslog = 1; \
openlog((ident), LOG_CONS | LOG_PID, 0); } \
while (0)
#define LOGI(format, ...) \
do { \
if (use_syslog) { \
syslog(LOG_INFO, format, ## __VA_ARGS__); \
} else { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
if (use_tty) { \
fprintf(stderr, "\e[01;32m %s INFO: \e[0m" format "\n", timestr, \
## __VA_ARGS__); \
} else { \
fprintf(stderr, " %s INFO: " format "\n", timestr, \
## __VA_ARGS__); \
} \
} \
} \
while (0)
#define LOGE(format, ...) \
do { \
if (use_syslog) { \
syslog(LOG_ERR, format, ## __VA_ARGS__); \
} else { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
if (use_tty) { \
fprintf(stderr, "\e[01;35m %s ERROR: \e[0m" format "\n", timestr, \
## __VA_ARGS__); \
} else { \
fprintf(stderr, " %s ERROR: " format "\n", timestr, \
## __VA_ARGS__); \
} \
} } \
while (0)
#endif
/* _WIN32 */
#endif
#ifdef __MINGW32__
#ifdef ERROR
#undef ERROR
#endif
#define ERROR(s) ss_error(s)
#else
void ERROR(const char *s);
#endif
char *ss_itoa(int i);
int run_as(const char *user);
void FATAL(const char *msg);
void usage(void);
void daemonize(const char *path);
char *ss_strndup(const char *s, size_t n);
#ifdef HAVE_SETRLIMIT
int set_nofile(int nofile);
#endif
#define ss_free(ptr) \
do { \
free(ptr); \
ptr = NULL; \
} while(0)
static inline void *ss_malloc(size_t size)
{
void *tmp = malloc(size);
if (tmp == NULL)
exit(EXIT_FAILURE);
return tmp;
}
static inline void *ss_realloc(void *ptr, size_t new_size)
{
void *new = realloc(ptr, new_size);
if (new == NULL) {
free(ptr);
ptr = NULL;
exit(EXIT_FAILURE);
}
return new;
}
#endif // _UTILS_H