efficient debug-to-file output (/tmp racy, but hey)

This commit is contained in:
tg
2012-03-23 19:38:12 +00:00
parent 4f043002be
commit c5cc22a13f
3 changed files with 57 additions and 5 deletions

46
main.c
View File

@ -34,7 +34,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.206 2012/03/23 18:58:15 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.207 2012/03/23 19:38:12 tg Exp $");
extern char **environ;
@ -1242,7 +1242,13 @@ can_seek(int fd)
SHF_UNBUF : 0);
}
struct shf shf_iob[3];
#ifdef DF
int shl_dbg_fd;
#define NSHF_IOB 4
#else
#define NSHF_IOB 3
#endif
struct shf shf_iob[NSHF_IOB];
void
initio(void)
@ -1250,8 +1256,22 @@ initio(void)
/* force buffer allocation */
shf_fdopen(1, SHF_WR, shl_stdout);
shf_fdopen(2, SHF_WR, shl_out);
/* force buffer allocation */
shf_fdopen(2, SHF_WR, shl_spare);
#ifdef DF
if ((shl_dbg_fd = open("/tmp/mksh-dbg.txt",
O_WRONLY | O_APPEND | O_CREAT, 0600)) == -1)
errorf("cannot open debug output file");
if (shl_dbg_fd < FDBASE) {
int nfd;
nfd = fcntl(shl_dbg_fd, F_DUPFD, FDBASE);
close(shl_dbg_fd);
if ((shl_dbg_fd = nfd) == -1)
errorf("cannot dup debug output file");
}
shf_fdopen(shl_dbg_fd, SHF_WR, shl_dbg);
DF("=== open ===");
#endif
initio_done = true;
}
@ -1694,3 +1714,23 @@ x_sigwinch(int sig MKSH_A_UNUSED)
got_winch = 1;
}
#endif
#ifdef DF
void
DF(const char *fmt, ...)
{
va_list args;
struct timeval tv;
(void)flock(shl_dbg_fd, LOCK_EX);
gettimeofday(&tv, NULL);
shf_fprintf(shl_dbg, "[%d.%06d:%d] ", (int)tv.tv_sec, (int)tv.tv_usec,
(int)getpid());
va_start(args, fmt);
shf_vfprintf(shl_dbg, fmt, args);
va_end(args);
shf_putc('\n', shl_dbg);
shf_flush(shl_dbg);
(void)flock(shl_dbg_fd, LOCK_UN);
}
#endif