summaryrefslogtreecommitdiff
path: root/logging.c
diff options
context:
space:
mode:
Diffstat (limited to 'logging.c')
-rw-r--r--logging.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/logging.c b/logging.c
index e0f5f1f..569a243 100644
--- a/logging.c
+++ b/logging.c
@@ -3,11 +3,13 @@
// Write the line 'str' to the relevant log file such as
// '#channel.log' or 'nickname.log'. 'ournick' is our own
// nick and is used to determine which log file to write to.
+// 'basedir' is the directory in which the 'logs' directory
+// will be created in which logs are to be written.
// Expects a string in the format:
// :from!bar@baz PRIVMSG to :hello world
// With the ":foo!bar@baz "prefix being important.
// Returns 1 on success or 0 on failure.
-int logprivmsg(char *str, char *ournick) {
+int logprivmsg(char *str, char *ournick, char *basedir) {
// First, extract the "from" nick and the "to" nick or channel by splitting up the string
// Track which space-separated token within this response we're on
@@ -41,14 +43,27 @@ int logprivmsg(char *str, char *ournick) {
char filename[MAXCHAR];
// If the message was sent to us, then log it in the sender's log file
if (strncmp(tokens[2], ournick, strlen(tokens[0])) == 0) {
- snprintf(filename, MAXCHAR, "%s.log", tokens[0]);
+ snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, tokens[0]);
} else {
// Otherwise log it in the "to" log file
- snprintf(filename, MAXCHAR, "%s.log", tokens[2]);
+ snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, tokens[2]);
}
printf("logprivmsg(): Logging from '%s' to '%s' message '%s' in filename '%s'.\n", tokens[0], tokens[2], str, filename);
+ // Make sure the log directory exists
+ char logdir[PATH_MAX];
+ snprintf(logdir, PATH_MAX, "%s/logs/", basedir);
+ struct stat st = {0};
+ if (stat(logdir, &st) == -1) {
+ if (mkdir(logdir, 0700)) {
+ printf("Error creating log directory '%s'.\n", logdir);
+ exit(1);
+ } else {
+ printf("Created log directory '%s'.\n", logdir);
+ }
+ }
+
FILE *fp;
char line[MAXCHAR];