diff options
author | Luke Bratch <luke@bratch.co.uk> | 2019-05-11 19:49:27 +0100 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2019-05-11 19:49:27 +0100 |
commit | 231656bf59da65036b171ce5ccc06eeadd64a05a (patch) | |
tree | 350e2c0fdbf1e368b26e90a59331e48044f4387d /replay.c | |
parent | 0559bff00a6be2054194632c3543bf62af1fb56f (diff) |
Implement relay log writing.
Diffstat (limited to 'replay.c')
-rw-r--r-- | replay.c | 46 |
1 files changed, 44 insertions, 2 deletions
@@ -154,7 +154,8 @@ int relaylines(int seconds) { if (fp == NULL) { printf("error: could not open replay log '%s'.\n", filename); - exit(1); + // Assume the file just doesn't exist yet - TODO - Interpret error codes to see what happened. + return 0; } // Get the current time for comparison later @@ -196,7 +197,7 @@ int readrelayline(int seconds, int linenum, char *str) { if (fp == NULL) { printf("error: could not open replay log '%s'.\n", filename); - return 1; + exit(1); } // Get the current time for comparison later @@ -229,3 +230,44 @@ int readrelayline(int seconds, int linenum, char *str) { // If we got here something went wrong return 0; } + +// Write the line 'str' to the relay log file after prepending it with +// the current unixtime timestamp. +// Expects a string in the format: +// :foo!bar@baz PRIVMSG foo :[17:41:41] hello world +// With the ":foo!bar@baz "prefix being important. +// Returns 1 on success or 0 on failure. +int writerelayline(char *str) { + FILE *fp; + char line[MAXCHAR]; + char* filename = "replay.log"; + + int bytes = 0; + + fp = fopen(filename, "a"); + + if (fp == NULL) { + printf("error: could not open replay log '%s' for writing.\n", filename); + exit(1); + } + + // Get the current time and manipulate it into a C string + time_t timenow = time(NULL); + int timenowlen = snprintf(NULL, 0, "%ld", timenow); + char timenowstr[timenowlen + 1]; // TODO - Make this Year 2038 proof. + snprintf(timenowstr, timenowlen + 1, "%ld", timenow); + + // Prepend the unixtime timestamp + snprintf(line, MAXCHAR, "%s %s", timenowstr, str); + + printf("Complete replay log string to write: '%s', length '%ld'.\n", line, strlen(line)); + + // Write complete line to file + if ((bytes = fprintf(fp, line)) < 0) { + printf("error: could not write to replay log file.\n"); + exit(1); + } + + fclose(fp); + return bytes; +} |