Revision e78a936f xseg/sys/user/xseg_user.c
b/xseg/sys/user/xseg_user.c | ||
---|---|---|
43 | 43 |
#include <sys/syscall.h> |
44 | 44 |
#include <errno.h> |
45 | 45 |
#include <sys/util.h> |
46 |
#include <sys/types.h> |
|
47 |
#include <sys/stat.h> |
|
48 |
#include <fcntl.h> |
|
46 | 49 |
#include <sys/time.h> |
47 | 50 |
#include <execinfo.h> |
48 | 51 |
#include <sys/domain.h> |
... | ... | |
124 | 127 |
int __renew_logctx(struct log_ctx *lc, char *peer_name, |
125 | 128 |
enum log_level log_level, char *logfile, uint32_t flags) |
126 | 129 |
{ |
127 |
FILE *file;
|
|
130 |
int fd, tmp_fd;
|
|
128 | 131 |
|
129 | 132 |
if (peer_name){ |
130 | 133 |
strncpy(lc->peer_name, peer_name, MAX_PEER_NAME); |
... | ... | |
136 | 139 |
strncpy(lc->filename, logfile, MAX_LOGFILE_LEN); |
137 | 140 |
lc->filename[MAX_LOGFILE_LEN - 1] = 0; |
138 | 141 |
} |
139 |
else if (!(flags & REOPEN_FILE) || lc->logfile == stderr)
|
|
142 |
else if (!(flags & REOPEN_FILE) || lc->logfile == STDERR_FILENO)
|
|
140 | 143 |
return 0; |
141 | 144 |
|
142 |
if (lc->logfile == stderr) |
|
143 |
file = fopen(lc->filename, "a"); |
|
144 |
else |
|
145 |
file = freopen(lc->filename, "a", lc->logfile); |
|
146 |
if (!file) { |
|
145 |
fd = open(lc->filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR); |
|
146 |
if (fd < 1){ |
|
147 | 147 |
return -1; |
148 | 148 |
} |
149 | 149 |
|
150 |
tmp_fd = lc->logfile; |
|
151 |
lc->logfile = fd; |
|
152 |
close(tmp_fd); |
|
153 |
|
|
150 | 154 |
flags &= ~REOPEN_FILE; |
151 |
if ((flags|lc->flags) & REDIRECT_STDOUT) |
|
152 |
if (!freopen(lc->filename, "a", stdout)) |
|
155 |
if ((flags|lc->flags) & REDIRECT_STDOUT){ |
|
156 |
fd = dup2(lc->logfile, STDOUT_FILENO); |
|
157 |
if (fd < 0) |
|
153 | 158 |
return -1; |
154 |
if ((flags|lc->flags) & REDIRECT_STDERR) |
|
155 |
if (!freopen(lc->filename, "a", stderr)) |
|
159 |
} |
|
160 |
if ((flags|lc->flags) & REDIRECT_STDERR){ |
|
161 |
fd = dup2(lc->logfile, STDERR_FILENO); |
|
162 |
if (fd < 0) |
|
156 | 163 |
return -1; |
164 |
} |
|
157 | 165 |
lc->flags |= flags; |
158 | 166 |
|
159 | 167 |
return 0; |
... | ... | |
164 | 172 |
int __init_logctx(struct log_ctx *lc, char *peer_name, |
165 | 173 |
enum log_level log_level, char *logfile, uint32_t flags) |
166 | 174 |
{ |
167 |
FILE *file;
|
|
175 |
int fd;
|
|
168 | 176 |
|
169 | 177 |
if (peer_name){ |
170 | 178 |
strncpy(lc->peer_name, peer_name, MAX_PEER_NAME); |
... | ... | |
174 | 182 |
return -1; |
175 | 183 |
} |
176 | 184 |
|
185 |
/* set logfile to stderr by default */ |
|
186 |
lc->logfile = STDERR_FILENO; |
|
187 |
|
|
188 |
/* duplicate stdout, stderr */ |
|
189 |
fd = dup(STDOUT_FILENO); |
|
190 |
if (fd < 0){ |
|
191 |
return -1; |
|
192 |
} |
|
193 |
lc->stdout_orig = fd; |
|
194 |
|
|
195 |
fd = dup(STDERR_FILENO); |
|
196 |
if (fd < 0){ |
|
197 |
return -1; |
|
198 |
} |
|
199 |
lc->stderr_orig = fd; |
|
200 |
|
|
177 | 201 |
lc->log_level = log_level; |
178 | 202 |
if (!logfile || !logfile[0]) { |
179 |
lc->logfile = stderr;
|
|
203 |
lc->logfile = lc->stderr_orig;
|
|
180 | 204 |
return 0; |
181 | 205 |
} |
182 | 206 |
|
183 | 207 |
strncpy(lc->filename, logfile, MAX_LOGFILE_LEN); |
184 | 208 |
lc->filename[MAX_LOGFILE_LEN - 1] = 0; |
185 |
file = fopen(lc->filename, "a");
|
|
186 |
if (!file) {
|
|
187 |
lc->logfile = stderr;
|
|
209 |
fd = open(lc->filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
|
|
210 |
if (fd < 1){
|
|
211 |
lc->logfile = lc->stderr_orig;
|
|
188 | 212 |
return -1; |
189 | 213 |
} |
190 |
lc->logfile = file;
|
|
214 |
lc->logfile = fd;
|
|
191 | 215 |
|
192 |
if (flags & REDIRECT_STDOUT) |
|
193 |
if (!freopen(lc->filename, "a", stdout)) |
|
216 |
if (flags & REDIRECT_STDOUT){ |
|
217 |
fd = dup2(lc->logfile, STDOUT_FILENO); |
|
218 |
if (fd < 0) |
|
194 | 219 |
return -1; |
195 |
if (flags & REDIRECT_STDERR) |
|
196 |
if (!freopen(lc->filename, "a", stderr)) |
|
220 |
} |
|
221 |
if (flags & REDIRECT_STDERR){ |
|
222 |
fd = dup2(lc->logfile, STDERR_FILENO); |
|
223 |
if (fd < 0) |
|
197 | 224 |
return -1; |
225 |
} |
|
198 | 226 |
lc->flags = flags; |
199 | 227 |
|
200 | 228 |
return 0; |
... | ... | |
209 | 237 |
char timebuf[1024], buffer[4096]; |
210 | 238 |
char *buf = buffer; |
211 | 239 |
char *t = NULL, *pn = NULL; |
240 |
ssize_t r, sum; |
|
241 |
size_t count; |
|
242 |
int fd; |
|
212 | 243 |
|
213 | 244 |
va_start(ap, fmt); |
214 | 245 |
switch (level) { |
... | ... | |
235 | 266 |
buf = buffer + sizeof(buffer) - 2;/* enough to hold \n and \0 */ |
236 | 267 |
buf += sprintf(buf, "\n"); |
237 | 268 |
|
238 |
fprintf(lc->logfile, "%s", buffer); |
|
239 |
fflush(lc->logfile); |
|
269 |
count = buf-buffer; |
|
270 |
sum = 0; |
|
271 |
r = 0; |
|
272 |
fd = *(volatile int *)&lc->logfile; |
|
273 |
do { |
|
274 |
r = write(fd, buffer + sum, count - sum); |
|
275 |
if (r < 0){ |
|
276 |
if (errno == EBADF) |
|
277 |
fd = *(volatile int *)&lc->logfile; |
|
278 |
else |
|
279 |
//XSEGLOG("Error while writing log"); |
|
280 |
break; |
|
281 |
} |
|
282 |
sum += r; |
|
283 |
} while (sum < count); |
|
240 | 284 |
va_end(ap); |
241 | 285 |
|
242 | 286 |
return; |
Also available in: Unified diff