replace printf with log_printf in sync.c

Wed, 19 Jul 2023 20:48:20 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 19 Jul 2023 20:48:20 +0200
changeset 781
08d887af3971
parent 780
cd1c869720e0
child 782
3cfe65695a8c

replace printf with log_printf in sync.c

dav/sync.c file | annotate | diff | comparison | revisions
--- a/dav/sync.c	Wed Jul 19 20:47:10 2023 +0200
+++ b/dav/sync.c	Wed Jul 19 20:48:20 2023 +0200
@@ -671,7 +671,7 @@
         }
         DavLock *lock = dav_get_lock(sn, "/");
         if(lock) {
-            printf("Lock-Token: %s\n", lock->token);
+            log_printf("Lock-Token: %s\n", lock->token);
         }
         locked = TRUE;
         locktokenfile = create_locktoken_file(dir->name, lock->token);
@@ -958,7 +958,7 @@
         
         LocalResource *local = cxMapGet(db->resources, cx_hash_key_str(res->path));
         if(local) {
-            printf("update: %s\n", res->path);
+            log_printf("update: %s\n", res->path);
             char *res_path = resource_local_path(res);
             char *local_path = create_local_path(dir, res->path);
             free(res_path);
@@ -1047,7 +1047,7 @@
         char *str_delete = sync_delete == 1 ? "file" : "files";
         char *str_error = sync_error == 1 ? "error" : "errors";
         char *str_conflict = sync_conflict == 1 ? "conflict" : "conflicts";
-        printf("Result: %d %s pulled, %d %s deleted, %d %s, %d %s\n",
+        log_printf("Result: %d %s pulled, %d %s deleted, %d %s, %d %s\n",
                 sync_success, str_success,
                 sync_delete,str_delete,
                 sync_conflict, str_conflict,
@@ -1449,7 +1449,7 @@
     char *new_path = util_concat_path(dir->path, res->path);
     char *old_path = util_concat_path(dir->path, local_resource_path(content));
     
-    printf("%s: %s -> %s\n", copy?"copy":"move", content->path, res->path);
+    log_printf("%s: %s -> %s\n", copy?"copy":"move", content->path, res->path);
     if(fn(old_path, new_path)) {
         free(new_path);
         free(old_path);
@@ -1581,7 +1581,7 @@
     
     int64_t truncate_file = -1;
     if(!link) {
-        printf("get: %s\n", path);
+        log_printf("get: %s\n", path);
         if(issplit) {
             part_updates = sync_download_changed_parts(res, local, out, blocksize, &blockcount, &truncate_file, &ret);
         } else {
@@ -1589,7 +1589,7 @@
         }
         fclose(out);
     } else {
-        printf("link: %s -> %s\n", path, link);
+        log_printf("link: %s -> %s\n", path, link);
         if(sys_symlink(link, local_path)) {
             perror("symlink");
             ret = 1;
@@ -1706,7 +1706,7 @@
     char *local_path = create_local_path(dir, res->path);
     free(res_path);
      
-    printf("get: %s\n", res->path);
+    log_printf("get: %s\n", res->path);
     // create directory
     // ignore error if it already exists
     if(sys_mkdir(local_path) && errno != EEXIST) {
@@ -1779,7 +1779,7 @@
         return -2;
     }
     
-    printf("delete: %s\n", res->path);
+    log_printf("delete: %s\n", res->path);
     int ret = 0;
     if(dir->trash) {
         move_to_trash(dir, local_path);
@@ -1796,7 +1796,7 @@
     int ret = 0;
     char *local_path = create_local_path(dir, res->path);
     
-    printf("delete: %s\n", res->path);
+    log_printf("delete: %s\n", res->path);
     if(rmdir(local_path)) {
         // don't print error when dirs are not empty
         // because that can regulary happen, because the pull conflict
@@ -1840,7 +1840,7 @@
         if(sys_stat(new_path.ptr, &s)) {
             if(errno == ENOENT) {
                 loop = 0;
-                printf("conflict: %s\n", local_path);
+                log_printf("conflict: %s\n", local_path);
                 if(fn(local_path, new_path.ptr)) {
                     //printf("errno: %d\n", errno);
                     fprintf(
@@ -2024,7 +2024,7 @@
         }
         DavLock *lock = dav_get_lock(sn, "/");
         if(lock) {
-            printf("Lock-Token: %s\n", lock->token);
+            log_printf("Lock-Token: %s\n", lock->token);
         }
         locked = TRUE;
         locktokenfile = create_locktoken_file(dir->name, lock->token);
@@ -2248,7 +2248,7 @@
         if(sn->error == DAV_NOT_FOUND) {
             // create collection
             // TODO: show 405
-            printf("mkcol: %s\n", local_res->path);
+            log_printf("mkcol: %s\n", local_res->path);
             if(sync_mkdir(dir, res, local_res) && sn->error != DAV_METHOD_NOT_ALLOWED) {
                 print_resource_error(sn, res->path);
                 ret = -1;
@@ -2297,7 +2297,7 @@
             int err = 0;
             DavResource *res = dav_resource_new(sn, local->path);
             if(dav_exists(res)) {
-                printf("conflict: %s\n", local->path);
+                log_printf("conflict: %s\n", local->path);
                 local->last_modified = 0;
                 nullfree(local->etag);
                 local->etag = NULL;
@@ -2318,7 +2318,7 @@
                     // upload with put
                     cxListInsert(ls_modified, 0, local);               
                 } else {
-                    printf("%s: %s -> %s\n", copy ? "copy":"move", local->origin->path, local->path);
+                    log_printf("%s: %s -> %s\n", copy ? "copy":"move", local->origin->path, local->path);
                     err = sync_move_remote_resource(
                             dir,
                             db,
@@ -2375,7 +2375,7 @@
                         local_res->etag = strdup(etag);
                     }
                 } else if(cdt && changed) {
-                    printf("conflict: %s\n", local_res->path);
+                    log_printf("conflict: %s\n", local_res->path);
                     local_res->last_modified = 0;
                     nullfree(local_res->etag);
                     local_res->etag = NULL;
@@ -2393,12 +2393,12 @@
                     res_conflict = TRUE;
                 } else {
                     if(local_res->link_target) {
-                        printf(
+                        log_printf(
                                 "link: %s -> %s\n",
                                 local_res->path,
                                 local_res->link_target);
                     } else {
-                        printf("put: %s\n", local_res->path);
+                        log_printf("put: %s\n", local_res->path);
                     }
                     if(sync_put_resource(dir, res, local_res, &sync_success)) {
                         sync_error++;
@@ -2434,7 +2434,7 @@
         
         DavResource *res = dav_resource_new(sn, local_res->path);       
         if(local_res->metadata_updated) {
-            printf("update: %s\n", local_res->path);
+            log_printf("update: %s\n", local_res->path);
             if(!sync_update_metadata(dir, sn, res, local_res)) {
                 LocalResource *dbres = cxMapGet(db->resources, cx_hash_key_str(local_res->path));
                 cxMapPut(db->resources, cx_hash_key_str(local_res->path), local_res);
@@ -2509,11 +2509,11 @@
         char *str_conflict = sync_conflict == 1 ? "conflict" : "conflicts";
         char *str_error = sync_error == 1 ? "error" : "errors";
         
-        printf("Result: %d %s pushed, ", sync_success, str_success);
+        log_printf("Result: %d %s pushed, ", sync_success, str_success);
         if(!archive) {
-            printf("%d %s deleted, ", sync_delete, str_delete);
-        }
-        printf("%d %s, %d %s\n",
+            log_printf("%d %s deleted, ", sync_delete, str_delete);
+        }
+        log_printf("%d %s, %d %s\n",
                 sync_conflict, str_conflict, sync_error, str_error);
     }
     
@@ -2670,7 +2670,7 @@
         }
         DavLock *lock = dav_get_lock(sn, "/");
         if(lock) {
-            printf("Lock-Token: %s\n", lock->token);
+            log_printf("Lock-Token: %s\n", lock->token);
         }
         locked = TRUE;
         locktokenfile = create_locktoken_file(dir->name, lock->token);
@@ -2689,7 +2689,7 @@
         cx_foreach(LocalResource *, resource, iter) {
             DavResource *res = dav_get(sn, resource->path, "D:getetag,idav:status,idav:version-collection,idav:split,`idav:content-hash`,idavprops:tags,idavprops:finfo,idavprops:xattributes,idavprops:link");
             if(!res) {
-                printf("skip: %s\n", resource->path);
+                log_printf("skip: %s\n", resource->path);
                 continue;
             }
             char *status = dav_get_string_property(res, "idav:status");
@@ -2776,7 +2776,7 @@
     if(ret != -2) {
         char *str_success = sync_success == 1 ? "file" : "files";
         char *str_error = sync_error == 1 ? "error" : "errors";
-        printf("Result: %d %s pulled, %d %s\n",
+        log_printf("Result: %d %s pulled, %d %s\n",
                 sync_success, str_success,
                 sync_error, str_error);
     }
@@ -2787,7 +2787,7 @@
 static void print_outgoging_file(LocalResource *res) {
     char *lastmodified = util_date_str(res->last_modified);
     char *size = util_size_str(FALSE, res->size);
-    printf(" %-49s  %12s  %10s\n", res->path+1, lastmodified, size);
+    log_printf(" %-49s  %12s  %10s\n", res->path+1, lastmodified, size);
     free(lastmodified);
     free(size);
 }
@@ -2816,23 +2816,23 @@
     
     size_t total = len_new + len_mod + len_cnf + len_upd + len_del + len_mov + len_cpy + len_mkc;
     if(total == 0) {
-        printf("no changes\n");
+        log_printf("no changes\n");
         return;
     }
     
-    printf("%s\n", "File                                                Last Modified    Size");
-    printf("%s\n", "==============================================================================");
+    log_printf("%s\n", "File                                                Last Modified    Size");
+    log_printf("%s\n", "==============================================================================");
        
     if(ls_mkcol) {
-        printf("Directories:\n");
+        log_printf("Directories:\n");
         CxIterator i = cxListIterator(ls_mkcol);
         cx_foreach(LocalResource *, res, i) {
-            printf(" %-49s\n", res->path+1);
+            log_printf(" %-49s\n", res->path+1);
             total_size += res->size;
         }
     }
     if(ls_new) {
-        printf("New:\n");
+        log_printf("New:\n");
         CxIterator i = cxListIterator(ls_new);
         cx_foreach(LocalResource *, res, i) {
             print_outgoging_file(res);
@@ -2840,7 +2840,7 @@
         }
     }
     if(ls_modified) {
-        printf("Modified:\n");
+        log_printf("Modified:\n");
         CxIterator i = cxListIterator(ls_modified);
         cx_foreach(LocalResource *, res, i) {
             print_outgoging_file(res);
@@ -2848,54 +2848,54 @@
         }
     }
     if(ls_update) {
-        printf("Update:\n");
+        log_printf("Update:\n");
         CxIterator i = cxListIterator(ls_update);
         cx_foreach(LocalResource *, res, i) {
             char *lastmodified = util_date_str(res->last_modified);
-            printf(" %-49s  %12s\n", res->path+1, lastmodified);
+            log_printf(" %-49s  %12s\n", res->path+1, lastmodified);
             free(lastmodified);
         }
     }
     if(ls_delete) {
-        printf("Delete:\n");
+        log_printf("Delete:\n");
         CxIterator i = cxListIterator(ls_delete);
         cx_foreach(LocalResource *, res, i) {
-            printf(" %s\n", res->path+1);
+            log_printf(" %s\n", res->path+1);
         }
     }
     if(ls_copy) {
-        printf("Copy:\n");
+        log_printf("Copy:\n");
         CxIterator i = cxListIterator(ls_copy);
         cx_foreach(LocalResource *, res, i) {
-            printf("%s -> %s\n", res->origin->path+1, res->path);
+            log_printf("%s -> %s\n", res->origin->path+1, res->path);
         }
     }
     if(ls_move) {
-        printf("Move:\n");
+        log_printf("Move:\n");
         CxIterator i = cxListIterator(ls_move);
         cx_foreach(LocalResource *, res, i) {
-            printf("%s -> %s\n", res->origin->path+1, res->path);
+            log_printf("%s -> %s\n", res->origin->path+1, res->path);
         }
     }
     if(ls_conflict) {
-        printf("Conflict\n");
+        log_printf("Conflict\n");
         CxIterator i = cxListIterator(ls_conflict);
         cx_foreach(LocalResource *, res, i) {
-            printf(" %s\n", res->path+1);
+            log_printf(" %s\n", res->path+1);
         }
     }
     
     char *total_size_str = util_size_str(FALSE, total_size);
     
-    printf("\n");
-    if(len_new > 0) printf("new: %zu, ", len_new);
-    if(len_mod > 0) printf("modified: %zu, ", len_mod);
-    if(len_upd > 0) printf("updated: %zu, ", len_upd);
-    if(len_cpy > 0) printf("copied: %zu, ", len_cpy);
-    if(len_mov > 0) printf("moved: %zu, ", len_mov);
-    if(len_del > 0) printf("deleted: %zu, ", len_del);
-    if(len_cnf > 0) printf("conflicts: %zu, ", len_cnf);
-    printf("total size: %s\n", total_size_str);
+    log_printf("\n");
+    if(len_new > 0) log_printf("new: %zu, ", len_new);
+    if(len_mod > 0) log_printf("modified: %zu, ", len_mod);
+    if(len_upd > 0) log_printf("updated: %zu, ", len_upd);
+    if(len_cpy > 0) log_printf("copied: %zu, ", len_cpy);
+    if(len_mov > 0) log_printf("moved: %zu, ", len_mov);
+    if(len_del > 0) log_printf("deleted: %zu, ", len_del);
+    if(len_cnf > 0) log_printf("conflicts: %zu, ", len_cnf);
+    log_printf("total size: %s\n", total_size_str);
     
     free(total_size_str);
 }
@@ -4505,7 +4505,7 @@
         if(cols) {
             cxListAdd(cols, local_res);
         } else if(split || !res->children) {
-            printf("delete: %s\n", res->path);
+            log_printf("delete: %s\n", res->path);
             if(dav_delete(res)) {
                 ret = 1;
                 fprintf(stderr, "Cannot delete collection %s\n", res->path);
@@ -4524,7 +4524,7 @@
         if(!nullstrcmp(etag, local_res->etag)) {
             // local resource metadata == remote resource metadata
             // resource can be deleted
-            printf("delete: %s\n", res->path);
+            log_printf("delete: %s\n", res->path);
             int exists = 1;
             int vend_required = 0;
             if(dir->versioning && dir->versioning->always) {
@@ -4786,7 +4786,7 @@
     // Report
     if(ret != -2) {
         char *str_conflict = num_conflict == 1 ? "conflict" : "conflicts";
-        printf("Result: %d %s resolved\n", num_conflict, str_conflict);
+        log_printf("Result: %d %s resolved\n", num_conflict, str_conflict);
     }
     
     return ret;
@@ -4823,7 +4823,7 @@
     // delete all conflict files
     CxIterator i = cxMapIteratorValues(db->conflict);
     cx_foreach(LocalResource*, res, i) {
-        printf("delete: %s\n", res->path);
+        log_printf("delete: %s\n", res->path);
         char *path = create_local_path(dir, res->path);
         if(sys_unlink(path)) {
             if(errno != ENOENT) {
@@ -4852,7 +4852,7 @@
     if(ret == 0) {
         char *str_delete = num_del == 1 ? "file" : "files";
         char *str_error = num_err == 1 ? "error" : "errors";
-        printf("Result: %d conflict %s deleted, %d %s\n",
+        log_printf("Result: %d conflict %s deleted, %d %s\n",
                 num_del, str_delete,
                 num_err, str_error);
     }
@@ -4898,12 +4898,12 @@
     char *prev = "";
     cx_foreach(char *, path, i) {
         // TODO: implement verbose print   if(cmd_getoption(a, "verbose"))
-        //       printf("%s (%d)\n", path, confl_count);
+        //       log_printf("%s (%d)\n", path, confl_count);
         if(!strcmp(path, prev)) {
             continue;
         }
         
-        printf("%s\n", path);
+        log_printf("%s\n", path);
         
         prev = path;
     }
@@ -4988,11 +4988,11 @@
     putenv("LC_TIME=C");
     size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT", date);
 
-    printf("name: %s\n", name);
-    printf("lastmodified: %s\n", str);
+    log_printf("name: %s\n", name);
+    log_printf("lastmodified: %s\n", str);
     char *server = util_url_base(res->session->base_url);
     char *url = util_concat_path(server, res->href);
-    printf("url: %s\n", url);
+    log_printf("url: %s\n", url);
     free(server);
     free(url);
 }
@@ -5124,7 +5124,7 @@
     }
     
     if(!syncdir->trash) {
-        printf("trash not configured for %s\n", syncdir->name);
+        log_printf("trash not configured for %s\n", syncdir->name);
         return 0;
     }
     
@@ -5157,10 +5157,10 @@
     }
     sys_closedir(dir);
     
-    printf("path: %s\n", syncdir->trash);
-    printf("%d %s\n", count, count == 1 ? "file" : "files");
+    log_printf("path: %s\n", syncdir->trash);
+    log_printf("%d %s\n", count, count == 1 ? "file" : "files");
     char *sizestr = size_str(trashsize);
-    printf("%s\n", sizestr);
+    log_printf("%s\n", sizestr);
     free(sizestr);
     
     return 0;
@@ -5198,7 +5198,7 @@
         }
         
         char *path = util_concat_path(syncdir->trash, ent->name);
-        printf("delete: %s\n", path);
+        log_printf("delete: %s\n", path);
         
         SYS_STAT s;
         if(sys_stat(path, &s)) {
@@ -5300,7 +5300,7 @@
         int x = -1;
         cx_foreach(DavTag *, t, i) {
             if(cmd == CMD_TAG_LIST) {
-                printf("%s\n", t->name);
+                log_printf("%s\n", t->name);
             } else if(!strcmp(t->name, tag)) {
                 x = i.index;
                 break;
@@ -5502,7 +5502,7 @@
     return 1;
 #if 0
     
-    printf("Each sync directory must have an unique name.\n");
+    log_printf("Each sync directory must have an unique name.\n");
     char *name = assistant_getcfg("name");
     if(!name) {
         fprintf(stderr, "Abort\n");
@@ -5513,18 +5513,18 @@
         return -1;
     }
     
-    printf("Enter local directory path.\n");
+    log_printf("Enter local directory path.\n");
     char *path = assistant_getcfg("path");
     if(!path) {
         fprintf(stderr, "Abort\n");
         return -1;
     }
     
-    printf("Specify webdav repository.\n");
+    log_printf("Specify webdav repository.\n");
     CxIterator repos = get_repositories();
     int i = 0;
     cx_foreach(Repository *, r, repos) {
-        printf("%d) %s\n", i, r->name);
+        log_printf("%d) %s\n", i, r->name);
         i++;
     }
     char *repository = assistant_getcfg("repository");
@@ -5556,7 +5556,7 @@
         }
     }
     
-    printf("Enter collection relative to the repository base url.\n");
+    log_printf("Enter collection relative to the repository base url.\n");
     char *collection = assistant_getdefcfg("collection", "/");
     
     char *db = generate_db_name(name);
@@ -5575,7 +5575,7 @@
         fprintf(stderr, "Cannot write sync.xml\n");
         ret = -1;
     } else {
-        printf("\nAdded directory: %s (%s)\n", name, path);
+        log_printf("\nAdded directory: %s (%s)\n", name, path);
     }
     
     free(name);
@@ -5591,7 +5591,7 @@
 int cmd_list_dirs() {
     CxIterator iter = scfg_directory_iterator();
     cx_foreach(SyncDirectory *, dir, iter) {
-        printf("%s\n", dir->name);
+        log_printf("%s\n", dir->name);
     }
     return 0;
 }
@@ -5609,10 +5609,10 @@
     
     CxIterator iter = cxListIterator(reponames);
     cx_foreach(char *, reponame, iter) {
-        printf("Checking %s... ", reponame);
+        log_printf("Checking %s... ", reponame);
         Repository* repo = get_repository(cx_str(reponame));
         if (!repo) {
-            printf(" not found in config.xml!\n");
+            log_printf(" not found in config.xml!\n");
             ret = EXIT_FAILURE;
         } else {
             DavSession *sn = create_session(a, ctx, repo, repo->url);
@@ -5620,15 +5620,15 @@
                 DavResource *res = dav_query(sn,
                         "select - from / with depth = 0");
                 if (res) {
-                    printf("OK.\n");
+                    log_printf("OK.\n");
                     dav_resource_free(res);
                 } else {
-                    printf("unavailable!\n");
+                    log_printf("unavailable!\n");
                     ret = EXIT_FAILURE;
                 }
                 dav_session_destroy(sn);
             } else {
-                printf("cannot create session!\n");
+                log_printf("cannot create session!\n");
                 ret = EXIT_FAILURE;
             }
         }

mercurial