From 6d802496590fc04f74df6224d7a6824c62dd62a2 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 30 Apr 2019 02:16:19 -0300 Subject: [PATCH] add nicer formatting to output files --- src/journal/emotion.c | 4 +--- src/journal/format.h | 0 src/journal/journal.c | 21 ++++++++++++++++++++- src/journal/journal.h | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/journal/format.h diff --git a/src/journal/emotion.c b/src/journal/emotion.c index 143c84b..a8c8d50 100644 --- a/src/journal/emotion.c +++ b/src/journal/emotion.c @@ -4,8 +4,6 @@ void handle_emotion(FILE *journal, char* msg) { - char formatted[512]; - sprintf(formatted, "%s\n", msg); - journal_write(journal, formatted); + journal_write_topic(journal, "emotion", msg); return; } diff --git a/src/journal/format.h b/src/journal/format.h new file mode 100644 index 0000000..e69de29 diff --git a/src/journal/journal.c b/src/journal/journal.c index 5aaee17..fa8137f 100644 --- a/src/journal/journal.c +++ b/src/journal/journal.c @@ -1,8 +1,11 @@ -// functions to deal with the journal file #include #include #include #include +#include + +// functions to deal with the journal file + FILE *journal_open(char *topic) { @@ -36,3 +39,19 @@ void journal_close(FILE* journal_fd) { fclose(journal_fd); } + +void journal_write_topic(FILE *journal_fd, char *topic, char *message) +{ + char *tstamp = malloc(128 * sizeof(char)); + char fmt_msg[512]; + + time_t rawtime; + time(&rawtime); + const struct tm *cur_time = gmtime(&rawtime); + strftime(tstamp, 128, "%c", cur_time); + + sprintf(fmt_msg, "[%s] [%s]: %s\n", tstamp, topic, message); + free(tstamp); + + journal_write(journal_fd, fmt_msg); +} diff --git a/src/journal/journal.h b/src/journal/journal.h index 5151fef..1f21989 100644 --- a/src/journal/journal.h +++ b/src/journal/journal.h @@ -5,6 +5,7 @@ FILE *journal_open(char *topic); void journal_write(FILE*, char* message); +void journal_write_topic(FILE*, char *topic, char *message); void journal_close(FILE*); #endif