mirror of
https://github.com/pevans/erc-c.git
synced 2024-11-23 08:31:55 +00:00
Better documentation; log to stdout
This commit is contained in:
parent
4199637338
commit
ffd2550364
@ -5,7 +5,7 @@
|
||||
|
||||
extern void log_write(int, const char *, ...);
|
||||
extern void log_close();
|
||||
extern void log_open();
|
||||
extern void log_open(FILE *);
|
||||
|
||||
#define log_critical(...) log_write(0, __VA_ARGS__)
|
||||
#define log_error(...) log_write(0, __VA_ARGS__)
|
||||
|
29
src/log.c
29
src/log.c
@ -4,11 +4,28 @@
|
||||
|
||||
#include "log.h"
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
static FILE *log_stream = NULL;
|
||||
|
||||
/*
|
||||
* This function will assign the log_stream variable to either a given
|
||||
* file stream, or if none given, to the default location (which is a
|
||||
* filename defined by the `LOG_FILENAME` macro).
|
||||
*/
|
||||
void
|
||||
log_open()
|
||||
log_open(FILE *stream)
|
||||
{
|
||||
// Oh, you're telling _me_ what the stream is? Neat!
|
||||
if (stream != NULL) {
|
||||
log_stream = stream;
|
||||
return;
|
||||
}
|
||||
|
||||
// Oh, well. I'll just open this dang thing.
|
||||
log_stream = fopen(LOG_FILENAME, "w");
|
||||
if (log_stream == NULL) {
|
||||
perror("Couldn't open log file (" LOG_FILENAME ")");
|
||||
@ -16,6 +33,11 @@ log_open()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 (!).
|
||||
*/
|
||||
void
|
||||
log_close()
|
||||
{
|
||||
@ -24,6 +46,11 @@ log_close()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
void
|
||||
log_write(int level, const char *fmt, ...)
|
||||
{
|
||||
|
19
src/main.c
19
src/main.c
@ -3,25 +3,40 @@
|
||||
|
||||
#include "log.h"
|
||||
|
||||
/*
|
||||
* This function will establish the base environment that we want to use
|
||||
* while we execute.
|
||||
*/
|
||||
static void
|
||||
init()
|
||||
{
|
||||
log_open();
|
||||
// We're literally using stdout in this heavy phase of development.
|
||||
log_open(stdout);
|
||||
}
|
||||
|
||||
/*
|
||||
* And this is the teardown function.
|
||||
*/
|
||||
static void
|
||||
finish()
|
||||
{
|
||||
log_close();
|
||||
}
|
||||
|
||||
/*
|
||||
* This is what will run when the program begins, if you were new to how
|
||||
* C works.
|
||||
*/
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
init();
|
||||
|
||||
// When we exit, we want to wrap up a few loose ends.
|
||||
// When we exit, we want to wrap up a few loose ends. This syscall
|
||||
// will ensure that `finish()` runs whether we return from main
|
||||
// successfully or if we run `exit()` from elsewhere in the program.
|
||||
atexit(finish);
|
||||
|
||||
// ha ha ha ha #nervous #laughter
|
||||
printf("Hello, world\n");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user