2017-12-09 04:12:31 +00:00
|
|
|
/*
|
|
|
|
* log.c
|
|
|
|
*
|
|
|
|
* The functions here support our logging mechanism.
|
|
|
|
*/
|
|
|
|
|
2017-11-22 05:24:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2017-12-02 19:05:53 +00:00
|
|
|
#include "log.h"
|
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
/*
|
|
|
|
* This is the file stream we will write to for our logging. It can be
|
|
|
|
* any number of things, from a literal file on the filesystem, to
|
|
|
|
* stdout.
|
|
|
|
*/
|
2018-01-07 21:05:20 +00:00
|
|
|
static FILE *_stream = NULL;
|
2017-11-22 05:24:51 +00:00
|
|
|
|
2017-12-26 22:44:28 +00:00
|
|
|
/*
|
|
|
|
* Close the file stream we opened (or were given) in `log_open()`.
|
|
|
|
* Nota bene: if you passed stdout into log_open(), this will actually
|
|
|
|
* _close_ the stdout stream (!).
|
|
|
|
*/
|
2018-01-07 21:05:20 +00:00
|
|
|
int
|
2017-12-26 22:44:28 +00:00
|
|
|
log_close()
|
|
|
|
{
|
2018-01-07 21:05:20 +00:00
|
|
|
int rval = 0;
|
|
|
|
|
|
|
|
if (_stream != NULL) {
|
|
|
|
rval = fclose(_stream);
|
|
|
|
_stream = NULL;
|
2017-12-26 22:44:28 +00:00
|
|
|
}
|
2018-01-07 21:05:20 +00:00
|
|
|
|
|
|
|
return rval;
|
2017-12-26 22:44:28 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
/*
|
2018-01-07 21:05:20 +00:00
|
|
|
* This function will assign the _stream variable to either a given
|
2017-12-06 22:43:30 +00:00
|
|
|
* file stream, or if none given, to the default location (which is a
|
|
|
|
* filename defined by the `LOG_FILENAME` macro).
|
|
|
|
*/
|
2017-11-22 05:24:51 +00:00
|
|
|
void
|
2017-12-06 22:43:30 +00:00
|
|
|
log_open(FILE *stream)
|
2017-11-22 05:24:51 +00:00
|
|
|
{
|
2017-12-06 22:43:30 +00:00
|
|
|
// Oh, you're telling _me_ what the stream is? Neat!
|
|
|
|
if (stream != NULL) {
|
2018-01-07 21:05:20 +00:00
|
|
|
_stream = stream;
|
2017-12-06 22:43:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Oh, well. I'll just open this dang thing.
|
2018-01-07 21:05:20 +00:00
|
|
|
_stream = fopen(LOG_FILENAME, "w");
|
|
|
|
if (_stream == NULL) {
|
2017-12-02 19:05:53 +00:00
|
|
|
perror("Couldn't open log file (" LOG_FILENAME ")");
|
2017-11-22 05:24:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
/*
|
|
|
|
* Write to the log stream. This function can accept variable arguments,
|
|
|
|
* ala `printf()`. There is some support for different log levels, but
|
|
|
|
* as you may note, we do not use them at present.
|
|
|
|
*/
|
2017-11-22 05:24:51 +00:00
|
|
|
void
|
|
|
|
log_write(int level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2018-01-07 21:05:20 +00:00
|
|
|
if (_stream == NULL) {
|
|
|
|
_stream = stdout;
|
2017-12-15 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 05:24:51 +00:00
|
|
|
va_start(ap, fmt);
|
2018-01-07 21:05:20 +00:00
|
|
|
vfprintf(_stream, fmt, ap);
|
|
|
|
fprintf(_stream, "\n");
|
2017-11-22 05:24:51 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2018-01-07 21:05:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the file stream we're currently using for logging.
|
|
|
|
*/
|
|
|
|
FILE *
|
|
|
|
log_stream()
|
|
|
|
{
|
|
|
|
return _stream;
|
|
|
|
}
|