open logfile in relevant dav-sync commands

Thu, 20 Jul 2023 21:16:19 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 20 Jul 2023 21:16:19 +0200
changeset 782
3cfe65695a8c
parent 781
08d887af3971
child 783
36a7f2ea7d12

open logfile in relevant dav-sync commands

dav/sync.c file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Wed Jul 19 20:48:20 2023 +0200
+++ b/dav/sync.c	Thu Jul 20 21:16:19 2023 +0200
@@ -105,6 +105,43 @@
     va_end(ap);
 }
 
+void log_error(const char *fmt, ...) {
+    va_list ap;
+    va_start(ap, fmt);
+    cxmutstr str = cx_vasprintf(fmt, ap);
+    
+    fprintf(stderr, "%s", str.ptr);
+    if(synclog) {
+        fprintf(synclog, "%s", str.ptr);
+    }
+    free(str.ptr);
+    
+    va_end(ap);
+}
+
+
+int logfile_open(SyncDirectory *dir) {
+    int ret = 0;
+    if(dir && dir->logfile) {
+        char *lf_path = dir->logfile;
+        char *lf_path_fr = NULL;
+        if(dir->logfile[0] != '/') {
+            lf_path = config_file_path(dir->logfile);
+            lf_path_fr = lf_path;
+        }
+        
+        synclog = fopen(lf_path, "a");
+        if(!synclog) {
+            fprintf(stderr, "Cannot open logfile %s: %s\n", lf_path, strerror(errno));
+            ret = 1;
+        }
+        if(lf_path_fr) {
+            free(lf_path_fr);
+        }
+    }
+    return ret;
+}
+
 /*
  * strcmp version that works with NULL pointers
  */
@@ -625,6 +662,9 @@
     if(scfg_check_dir(dir)) {
         return -1;
     }
+    if(logfile_open(dir)) {
+        return -1;
+    }
     
     if((dir->allow_cmd & SYNC_CMD_PULL) != SYNC_CMD_PULL) {
         fprintf(stderr, "Command 'pull' is not allowed for this sync dir\n");
@@ -1952,6 +1992,9 @@
     if(scfg_check_dir(dir)) {
         return -1;
     }
+    if(logfile_open(dir)) {
+        return -1;
+    }
     if(cmd_getoption(a, "snapshot")) {
         if(dir->versioning) {
             dir->versioning->always = TRUE;
@@ -2541,6 +2584,9 @@
     if(syncdir) {
         dir = scfg_get_dir(syncdir);
     }
+    if(logfile_open(dir)) {
+        return -1;
+    }
     
     LocalResource nres;
     if(a->argc > 0) {
@@ -4757,6 +4803,9 @@
     if(scfg_check_dir(dir)) {
         return -1;
     }
+    if(logfile_open(dir)) {
+        return -1;
+    }
     
     SyncDatabase *db = load_db(dir->database);
     if(!db) {
@@ -4806,6 +4855,9 @@
     if(scfg_check_dir(dir)) {
         return -1;
     }
+    if(logfile_open(dir)) {
+        return -1;
+    }
     
     SyncDatabase *db = load_db(dir->database);
     if(!db) {
@@ -5178,6 +5230,9 @@
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
         return -1;
     }
+    if(logfile_open(syncdir)) {
+        return -1;
+    }
     
     if(!syncdir->trash) {
         fprintf(stderr, "trash not configured for %s\n", syncdir->name);

mercurial