tio/src/log.c
Henner Zeller 763a09e172 Write to logfile as soon as we have the data, don't buffer.
Logfiles are important to see what happend, in particular if something
unexpected happened; so we want to make sure that the logfile is flushed
to disk.

Before this change, the logfile was typically written at the end in
a large chunk as the default (large) buffering applied. Now, characters are
written out ASAP, so it is possible to get a live-view with a
tail -f <logfile>
2018-06-23 11:42:23 -07:00

65 lines
1.5 KiB
C

/*
* tio - a simple TTY terminal I/O application
*
* Copyright (c) 2014-2017 Martin Lund
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "tio/options.h"
#include "tio/print.h"
#include "tio/error.h"
static FILE *fp;
static bool log_error = false;
void log_open(const char *filename)
{
fp = fopen(filename, "w+");
if (fp == NULL)
{
log_error = true;
exit(EXIT_FAILURE);
}
setvbuf(fp, NULL, _IONBF, 0);
}
void log_write(char c)
{
if (fp != NULL)
fputc(c, fp);
}
void log_close(void)
{
if (fp != NULL)
fclose(fp);
}
void log_exit(void)
{
if (option.log)
log_close();
if (log_error)
error_printf("Could not open log file %s (%s)", option.log_filename, strerror(errno));
}