(logerror): Log to stderr as well. Fix potential buffer overflows.
(oops): Let logerror() supply the newline.
This commit is contained in:
parent
8f40f5ad6d
commit
7eb2fe571e
1 changed files with 26 additions and 25 deletions
|
@ -32,7 +32,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include <errno.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#if !defined(_WIN32)
|
#if !defined(_WIN32)
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -57,38 +56,40 @@ loginit(char *program)
|
||||||
sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
|
sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*VARARGS*/
|
/*
|
||||||
|
* Write a line to the log file and to stderr.
|
||||||
|
* Messages are silently truncated after 512 characters or a newline.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
logerror(char *format, ...)
|
logerror(char *format, ...)
|
||||||
{
|
{
|
||||||
|
enum {
|
||||||
|
ctime_len = 24, /* output of ctime() less the newline */
|
||||||
|
msg_space = 512 /* space for formatted message */
|
||||||
|
};
|
||||||
va_list list;
|
va_list list;
|
||||||
time_t now;
|
time_t now;
|
||||||
char buf[512];
|
char buf[ctime_len + 1 + msg_space + 2];
|
||||||
char cbuf[512];
|
|
||||||
char buf1[512];
|
|
||||||
int logf;
|
int logf;
|
||||||
s_char *p;
|
char *msg, *p;
|
||||||
|
|
||||||
va_start(list, format);
|
va_start(list, format);
|
||||||
vsprintf(buf, format, list);
|
msg = buf + ctime_len + 1;
|
||||||
if ((p = strchr(buf, '\n')) != 0)
|
vsnprintf(msg, msg_space, format, list);
|
||||||
*p = 0;
|
buf[sizeof(buf)-2] = 0;
|
||||||
(void)time(&now);
|
p = msg + strlen(msg);
|
||||||
strcpy(cbuf, ctime(&now));
|
p[0] = '\n';
|
||||||
if ((p = strchr(cbuf, '\n')) != 0)
|
p[1] = 0;
|
||||||
*p = 0;
|
p = strchr(msg, '\n');
|
||||||
(void)sprintf(buf1, "%s %s\n", cbuf, buf);
|
p[1] = 0;
|
||||||
|
fputs(msg, stderr);
|
||||||
|
time(&now);
|
||||||
|
memcpy(buf, ctime(&now), ctime_len);
|
||||||
|
buf[ctime_len] = ' ';
|
||||||
if ((logf = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0)
|
if ((logf = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0)
|
||||||
return;
|
return;
|
||||||
(void)write(logf, buf1, strlen(buf1));
|
write(logf, buf, strlen(buf));
|
||||||
(void)close(logf);
|
close(logf);
|
||||||
errno = 0;
|
|
||||||
#ifdef notdef
|
|
||||||
if (player) {
|
|
||||||
pr("A system error has occured; please notify the deity.\n");
|
|
||||||
pr(buf1);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
va_end(list);
|
va_end(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +100,7 @@ logerror(char *format, ...)
|
||||||
int
|
int
|
||||||
oops(char *msg, char *file, int line)
|
oops(char *msg, char *file, int line)
|
||||||
{
|
{
|
||||||
logerror("Oops: %s in %s:%d\n", msg, file, line);
|
logerror("Oops: %s in %s:%d", msg, file, line);
|
||||||
if (debug) abort();
|
if (debug) abort();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue