From bb4c595810ccd9b849be2d20f5c16d6076d083bb Mon Sep 17 00:00:00 2001 From: Xavier Roche Date: Mon, 13 May 2013 14:31:20 +0000 Subject: Introducing the hts_log_print() logging function * cleaned up logging --- src/htsback.c | 375 +++++++++----------------------- src/htsbase.h | 32 +-- src/htscache.c | 117 +++------- src/htscore.c | 350 ++++++++---------------------- src/htscore.h | 12 +- src/htscoremain.c | 30 +-- src/htslib.c | 64 ++++++ src/htslib.h | 1 + src/htsmodules.c | 6 +- src/htsname.c | 79 ++----- src/htsopt.h | 13 ++ src/htsparse.c | 573 ++++++++++++------------------------------------- src/htsstrings.h | 2 + src/htswizard.c | 206 ++++-------------- src/httrack-library.h | 13 ++ src/proxy/proxytrack.c | 2 - src/proxy/store.c | 2 +- 17 files changed, 525 insertions(+), 1352 deletions(-) (limited to 'src') diff --git a/src/htsback.c b/src/htsback.c index 7da2cf8..2e07121 100644 --- a/src/htsback.c +++ b/src/htsback.c @@ -70,9 +70,6 @@ Please visit our Website: http://www.httrack.com #include "htsmms.h" #endif -#undef test_flush -#define test_flush if (opt->flush) { if (opt->log) { fflush(opt->log); } if (opt->log) { fflush(opt->log); } } - #define VT_CLREOL "\33[K" /* Slot operations */ @@ -203,19 +200,11 @@ static int back_index_ready(httrackp* opt, struct_back* sback, char* adr, char* freet(itemback); itemback = NULL; } - if (opt->log != NULL) { - int last_errno = errno; - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: unserialize error for %s%s (%s): %s"LF,adr,fil,sav,strerror(last_errno)); - test_flush; - } + hts_log_print(opt, LOG_INFO | LOG_ERRNO, "engine: warning: unserialize error for %s%s (%s)",adr,fil,sav); } fclose(fp); } else { - if (opt->log != NULL) { - int last_errno = errno; - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: unserialize error for %s%s (%s), file disappeared: %s"LF,adr,fil,sav,strerror(last_errno)); - test_flush; - } + hts_log_print(opt, LOG_INFO | LOG_ERRNO, "engine: warning: unserialize error for %s%s (%s), file disappeared",adr,fil,sav); } (void) UNLINK(fileback); #else @@ -235,10 +224,7 @@ static int back_index_ready(httrackp* opt, struct_back* sback, char* adr, char* back_set_locked(sback, q); /* locked */ return q; } else { - if (opt->log != NULL) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: unserialize error for %s%s (%s): no more space to wakeup frozen slots"LF,adr,fil,sav); - test_flush; - } + hts_log_print(opt, LOG_INFO, "engine: warning: unserialize error for %s%s (%s): no more space to wakeup frozen slots",adr,fil,sav); } } } @@ -270,13 +256,10 @@ int back_cleanup_background(httrackp* opt,cache_back* cache,struct_back* sback) /* Security check */ int checkIndex = back_index_ready(opt, sback, back[i].url_adr, back[i].url_fil, back[i].url_sav, 1); if (checkIndex != -1) { - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"engine: unexpected duplicate file entry: %s%s -> %s (%d '%s') / %s%s -> %s (%d '%s')"LF, - back[checkIndex].url_adr, back[checkIndex].url_fil, back[checkIndex].url_sav, back[checkIndex].r.statuscode, back[checkIndex].r.msg, - back[i].url_adr, back[i].url_fil, back[i].url_sav, back[i].r.statuscode, back[i].r.msg - ); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "engine: unexpected duplicate file entry: %s%s -> %s (%d '%s') / %s%s -> %s (%d '%s')", + back[checkIndex].url_adr, back[checkIndex].url_fil, back[checkIndex].url_sav, back[checkIndex].r.statuscode, back[checkIndex].r.msg, + back[i].url_adr, back[i].url_fil, back[i].url_sav, back[i].r.statuscode, back[i].r.msg + ); back_delete(NULL, NULL, sback, checkIndex); #ifdef _DEBUG /* This should NOT happend! */ @@ -297,10 +280,7 @@ int back_cleanup_background(httrackp* opt,cache_back* cache,struct_back* sback) } /* Security check */ if (fexist_utf8(filename)) { - if (opt->log != NULL) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: temporary file %s already exists"LF, filename); - test_flush; - } + hts_log_print(opt, LOG_INFO, "engine: warning: temporary file %s already exists", filename); } /* Create file and serialize slot */ if ((fp = filecreate(NULL, filename)) != NULL) @@ -313,28 +293,16 @@ int back_cleanup_background(httrackp* opt,cache_back* cache,struct_back* sback) nclean++; back_clear_entry(&back[i]); /* entry is now recycled */ } else { - if (opt->log != NULL) { - int last_errno = errno; - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: serialize error for %s%s to %s: write error: %s"LF,back[i].url_adr,back[i].url_fil,filename,strerror(last_errno)); - test_flush; - } + hts_log_print(opt, LOG_INFO | LOG_ERRNO, "engine: warning: serialize error for %s%s to %s: write error",back[i].url_adr,back[i].url_fil,filename); } fclose(fp); } else { - if (opt->log != NULL) { - int last_errno = errno; - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: serialize error for %s%s to %s: open error: %s (%s, %s)"LF, back[i].url_adr, back[i].url_fil, filename, strerror(last_errno), dir_exists(filename) ? "directory exists" : "directory does NOT exist!", fexist_utf8(filename) ? "file already exists!" : "file does not exist"); - test_flush; - } + hts_log_print(opt, LOG_INFO | LOG_ERRNO, "engine: warning: serialize error for %s%s to %s: open error (%s, %s)", back[i].url_adr, back[i].url_fil, filename, dir_exists(filename) ? "directory exists" : "directory does NOT exist!", fexist_utf8(filename) ? "file already exists!" : "file does not exist"); } if (filename != NULL) free(filename); } else { - if (opt->log != NULL) { - int last_errno = errno; - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: serialize error for %s%s to %s: memory full: %s"LF,back[i].url_adr,back[i].url_fil,filename,strerror(last_errno)); - test_flush; - } + hts_log_print(opt, LOG_INFO | LOG_ERRNO, "engine: warning: serialize error for %s%s to %s: memory full",back[i].url_adr,back[i].url_fil,filename); } } #else @@ -462,19 +430,12 @@ int back_finalize(httrackp* opt, cache_back* cache, struct_back* sback, const in if (back[p].r.totalsize >= 0 && back[p].r.statuscode > 0 && back[p].r.size != back[p].r.totalsize && ! opt->tolerant) { if (back[p].status == STATUS_READY) { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); - fprintf(opt->log, "file not stored in cache due to bogus state (broken size, expected "LLintP" got "LLintP"): %s%s"LF, - back[p].r.totalsize, back[p].r.size, back[p].url_adr,back[p].url_fil); - } + hts_log_print(opt, LOG_WARNING, "file not stored in cache due to bogus state (broken size, expected "LLintP" got "LLintP"): %s%s", + back[p].r.totalsize, back[p].r.size, back[p].url_adr,back[p].url_fil); } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_INFO); - fprintf(opt->log, "incomplete file not yet stored in cache (expected "LLintP" got "LLintP"): %s%s"LF, - back[p].r.totalsize, back[p].r.size, back[p].url_adr,back[p].url_fil); - } + hts_log_print(opt, LOG_INFO, "incomplete file not yet stored in cache (expected "LLintP" got "LLintP"): %s%s", + back[p].r.totalsize, back[p].r.size, back[p].url_adr,back[p].url_fil); } - test_flush; return -1; } @@ -687,14 +648,10 @@ int back_finalize(httrackp* opt, cache_back* cache, struct_back* sback, const in } if ( (!back[p].r.notmodified) && (opt->is_update) ) { HTS_STAT.stat_updated_files++; // page modifiée - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_INFO); - if (back[p].is_update) { - fprintf(opt->log,"engine: transfer-status: link updated: %s%s -> %s"LF,back[p].url_adr,back[p].url_fil,back[p].url_sav); - } else { - fprintf(opt->log,"engine: transfer-status: link added: %s%s -> %s"LF,back[p].url_adr,back[p].url_fil,back[p].url_sav); - } - test_flush; + if (back[p].is_update) { + hts_log_print(opt, LOG_INFO, "engine: transfer-status: link updated: %s%s -> %s",back[p].url_adr,back[p].url_fil,back[p].url_sav); + } else { + hts_log_print(opt, LOG_INFO, "engine: transfer-status: link added: %s%s -> %s",back[p].url_adr,back[p].url_fil,back[p].url_sav); } if (cache->txt) { if (back[p].is_update) { @@ -704,10 +661,7 @@ int back_finalize(httrackp* opt, cache_back* cache, struct_back* sback, const in } } } else { - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: transfer-status: link recorded: %s%s -> %s"LF,back[p].url_adr,back[p].url_fil,back[p].url_sav); - test_flush; - } + hts_log_print(opt, LOG_INFO, "engine: transfer-status: link recorded: %s%s -> %s",back[p].url_adr,back[p].url_fil,back[p].url_sav); if (cache->txt) { if (opt->is_update) state="untouched"; @@ -716,18 +670,13 @@ int back_finalize(httrackp* opt, cache_back* cache, struct_back* sback, const in } } } else { - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: transfer-status: empty file? (%d, '%s'): %s%s"LF,back[p].r.statuscode,back[p].r.msg,back[p].url_adr,back[p].url_fil); - test_flush; - } + hts_log_print(opt, LOG_INFO, "engine: transfer-status: empty file? (%d, '%s'): %s%s",back[p].r.statuscode,back[p].r.msg,back[p].url_adr,back[p].url_fil); if (cache->txt) { state="empty"; } } } else { - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: transfer-status: link error (%d, '%s'): %s%s"LF,back[p].r.statuscode,back[p].r.msg,back[p].url_adr,back[p].url_fil); - } + hts_log_print(opt, LOG_INFO, "engine: transfer-status: link error (%d, '%s'): %s%s",back[p].r.statuscode,back[p].r.msg,back[p].url_adr,back[p].url_fil); if (cache->txt) { state="error"; } @@ -758,16 +707,12 @@ int back_finalize(httrackp* opt, cache_back* cache, struct_back* sback, const in } else { /* error */ if (!HTTP_IS_OK(back[p].r.statuscode)) { - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"redirect to %s%s"LF,back[p].url_adr,back[p].url_fil); - } + hts_log_print(opt, LOG_DEBUG, "redirect to %s%s",back[p].url_adr,back[p].url_fil); /* Store only header reference */ cache_mayadd(opt,cache,&back[p].r,back[p].url_adr,back[p].url_fil,NULL); } else { /* Partial file, but marked as "ok" ? */ - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"file not stored in cache due to bogus state (incomplete type with %s (%d), size "LLintP"): %s%s"LF,back[p].r.msg,back[p].r.statuscode,(LLint)back[p].r.size,back[p].url_adr,back[p].url_fil); - } + hts_log_print(opt, LOG_WARNING, "file not stored in cache due to bogus state (incomplete type with %s (%d), size "LLintP"): %s%s",back[p].r.msg,back[p].r.statuscode,(LLint)back[p].r.size,back[p].url_adr,back[p].url_fil); } } @@ -996,11 +941,9 @@ int back_maydelete(httrackp* opt,cache_back* cache,struct_back* sback, const int if (back_letlive(opt, cache, sback, p)) { strcpybuff(back[p].url_adr, tmp.url_adr); back[p].status = STATUS_ALIVE; // alive & waiting - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Keep-Alive): successfully saved #%d (%s)"LF, - back[p].r.debugid, - back[p].url_adr); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(Keep-Alive): successfully saved #%d (%s)", + back[p].r.debugid, + back[p].url_adr); return 1; } } @@ -1048,11 +991,9 @@ void back_maydeletehttp(httrackp* opt, cache_back* cache, struct_back* sback, co back[p].r.soc = INVALID_SOCKET; strcpybuff(back[q].url_adr, tmp.url_adr); // address back[q].status = STATUS_ALIVE; // alive & waiting - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Keep-Alive): successfully preserved #%d (%s)"LF, - back[q].r.debugid, - back[q].url_adr); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(Keep-Alive): successfully preserved #%d (%s)", + back[q].r.debugid, + back[q].url_adr); } else { deletehttp(&back[p].r); back[p].r.soc = INVALID_SOCKET; @@ -1233,9 +1174,7 @@ int back_delete(httrackp* opt, cache_back* cache, struct_back* sback, const int && (back[p].r.statuscode>0) // not internal error ) { - if (opt != NULL && opt->debug>1 && opt->log!=NULL) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File '%s%s' -> %s not yet saved in cache - saving now"LF, back[p].url_adr, back[p].url_fil, back[p].url_sav); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File '%s%s' -> %s not yet saved in cache - saving now", back[p].url_adr, back[p].url_fil, back[p].url_sav); } if (cache != NULL) { back_finalize(opt, cache, sback, p); @@ -1345,23 +1284,17 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* #if (defined(_DEBUG) || defined(DEBUG)) if (!test && back_exist(sback,opt,adr,fil,save)) { int already_there = 0; - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"error: back_add(%s,%s,%s) duplicate"LF, adr, fil, save); - } + hts_log_print(opt, LOG_ERROR, "error: back_add(%s,%s,%s) duplicate", adr, fil, save); } #endif // vérifier cohérence de adr et fil (non vide!) if (strnotempty(adr)==0) { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"error: adr is empty for back_add"LF); - } + hts_log_print(opt, LOG_WARNING, "error: adr is empty for back_add"); return -1; // erreur! } if (strnotempty(fil)==0) { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"error: fil is empty for back_add"LF); - } + hts_log_print(opt, LOG_WARNING, "error: fil is empty for back_add"); return -1; // erreur! } // FIN vérifier cohérence de adr et fil (non vide!) @@ -1424,9 +1357,7 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* strcpybuff(back[p].r.msg,"mirror stopped by user"); back[p].status=STATUS_READY; // terminé back_set_finished(sback, p); - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"File not added due to mirror cancel: %s%s"LF,adr,fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "File not added due to mirror cancel: %s%s",adr,fil); return 0; } @@ -1508,13 +1439,9 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* rename(fconv(catbuff, previous_save), fconv(catbuff2,save)); if (fexist_utf8(fconv(catbuff,save))) { found = 1; - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File '%s' has been renamed since last mirror to '%s' ; applying changes"LF, previous_save, save); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File '%s' has been renamed since last mirror to '%s' ; applying changes", previous_save, save); } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Could not rename '%s' to '%s' ; will have to retransfer it"LF, previous_save, save); test_flush; - } + hts_log_print(opt, LOG_ERROR, "Could not rename '%s' to '%s' ; will have to retransfer it", previous_save, save); } } } @@ -1535,14 +1462,10 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* if (!fexist_utf8(fconv(catbuff,save))) { // fichier existe pas mais déclaré: on l'a effacé FILE* fp=FOPEN(fconv(catbuff,save),"wb"); if (fp) fclose(fp); - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Previous file '%s' not found (erased by user ?), ignoring: %s%s"LF,save,back[p].url_adr,back[p].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Previous file '%s' not found (erased by user ?), ignoring: %s%s",save,back[p].url_adr,back[p].url_fil); } } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Previous file '%s' not found (erased by user ?), recatching: %s%s"LF,save,back[p].url_adr,back[p].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Previous file '%s' not found (erased by user ?), recatching: %s%s",save,back[p].url_adr,back[p].url_fil); } } } // fsize() <= 0 @@ -1593,12 +1516,10 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* } if (back[p].r.statuscode != -1 || IS_DELAYED_EXT(save)) { // pas d'erreur de lecture ou test retardé - if ((opt->debug>0) && (opt->log!=NULL)) { - if (!test) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File immediately loaded from cache: %s%s"LF,back[p].url_adr,back[p].url_fil); test_flush; - } else { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File immediately tested from cache: %s%s"LF,back[p].url_adr,back[p].url_fil); test_flush; - } + if (!test) { + hts_log_print(opt, LOG_DEBUG, "File immediately loaded from cache: %s%s",back[p].url_adr,back[p].url_fil); + } else { + hts_log_print(opt, LOG_DEBUG, "File immediately tested from cache: %s%s",back[p].url_adr,back[p].url_fil); } back[p].r.notmodified=1; // fichier non modifié back[p].status=STATUS_READY; // OK prêt @@ -1723,9 +1644,7 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* #if DEBUGCA printf("..if unmodified since %s size "LLintP"\n", lastmodified, (LLint)sz); #endif - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File partially present ("LLintP" bytes): %s%s"LF,(LLint)sz,back[p].url_adr,back[p].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File partially present ("LLintP" bytes): %s%s",(LLint)sz,back[p].url_adr,back[p].url_fil); /* impossible - don't have etag or date if (strnotempty(back[p].r.etag)) { // ETag (RFC2616) @@ -1746,22 +1665,11 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* back[p].r.req.range_used=1; back[p].r.req.nocompression=1; } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Could not find timestamp for partially present file, restarting (lost "LLintP" bytes): %s%s"LF,(LLint)sz,back[p].url_adr,back[p].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Could not find timestamp for partially present file, restarting (lost "LLintP" bytes): %s%s",(LLint)sz,back[p].url_adr,back[p].url_fil); } } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); - /* - if (opt->http10) - fprintf(opt->log,"File partially present (%d bytes) retransfered due to HTTP/1.0 settings: %s%s"LF,sz,back[p].url_adr,back[p].url_fil); - else - */ - fprintf(opt->log,"File partially present ("LLintP" bytes) retransfered due to lack of cache: %s%s"LF,(LLint)sz,back[p].url_adr,back[p].url_fil); - test_flush; - } + hts_log_print(opt, LOG_NOTICE, "File partially present ("LLintP" bytes) retransfered due to lack of cache: %s%s",(LLint)sz,back[p].url_adr,back[p].url_fil); /* Sinon requête normale... */ back[p].http11=0; } @@ -1775,11 +1683,7 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* return 0; } } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); - fprintf(opt->log,"HTML file ("LLintP" bytes) retransfered due to lack of cache: %s%s"LF,(LLint)sz,back[p].url_adr,back[p].url_fil); - test_flush; - } + hts_log_print(opt, LOG_NOTICE, "HTML file ("LLintP" bytes) retransfered due to lack of cache: %s%s",(LLint)sz,back[p].url_adr,back[p].url_fil); /* Sinon requête normale... */ back[p].http11=0; } @@ -1792,9 +1696,7 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* strcpybuff(back[p].r.msg,"mirror stopped by user"); back[p].status=STATUS_READY; // terminé back_set_finished(sback, p); - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"File not added due to mirror cancel: %s%s"LF,adr,fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "File not added due to mirror cancel: %s%s",adr,fil); return 0; } @@ -1829,9 +1731,7 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* // mode ftp, court-circuit! if (strfield(back[p].url_adr,"ftp://")) { if (back[p].testmode) { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"error: forbidden test with ftp link for back_add"LF); - } + hts_log_print(opt, LOG_DEBUG, "error: forbidden test with ftp link for back_add"); return -1; // erreur pas de test permis } if (!(back[p].r.req.proxy.active && opt->ftp_proxy)) { // connexion directe, gérée en thread @@ -1852,13 +1752,11 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* else if (strfield(back[p].url_adr,"mms://")) { MMSDownloadStruct str; if (back[p].testmode) { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"error: forbidden test with mms link for back_add"LF); - } + hts_log_print(opt, LOG_DEBUG, "error: forbidden test with mms link for back_add"); return -1; // erreur pas de test permis } if (back[p].r.req.proxy.active) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"warning: direct connection for mms links (proxy settings ignored)"LF); + hts_log_print(opt, LOG_WARNING, "warning: direct connection for mms links (proxy settings ignored)"); } back[p].status=STATUS_FTP_TRANSFER; // connexion externe str.pBack = &back[p]; @@ -1916,11 +1814,9 @@ int back_add(struct_back* sback,httrackp* opt,cache_back* cache,char* adr,char* } else { soc = back[p].r.soc; - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Keep-Alive): successfully linked #%d (for %s%s)"LF, - back[p].r.debugid, - back[p].url_adr, back[p].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(Keep-Alive): successfully linked #%d (for %s%s)", + back[p].r.debugid, + back[p].url_adr, back[p].url_fil); } if (opt->timeout>0) { // gestion du opt->timeout @@ -1986,10 +1882,10 @@ printf("Xfopen ok, poll..\n"); return 0; } else { if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"error: no space left in stack for back_add"LF); + hts_log_print(opt, LOG_WARNING, "error: no space left in stack for back_add"); if ( ( opt->state.debug_state & 1 ) == 0 ) { /* debug_state<0> == debug 'no space left in stack' */ int i; - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"debug: DUMPING %d BLOCKS"LF, back_max); + hts_log_print(opt, LOG_WARNING, "debug: DUMPING %d BLOCKS", back_max); opt->state.debug_state |= 1; /* once */ /* OUTPUT FULL DEBUG INFORMATION THE FIRST TIME WE SEE THIS VERY ANNOYING BUG, HOPING THAT SOME USER REPORT WILL QUICKLY SOLVE THIS PROBLEM :p */ @@ -1998,12 +1894,11 @@ printf("Xfopen ok, poll..\n"); int may_clean = slot_can_be_cleaned(&back[i]); int may_finalize = may_clean && slot_can_be_finalized(opt, &back[i]); int may_serialize = slot_can_be_cached_on_disk(&back[i]); - HTS_LOG(opt,LOG_INFO); - fprintf(opt->log, - "debug: back[%03d]: may_clean=%d, may_finalize_disk=%d, may_serialize=%d:"LF + hts_log_print(opt, LOG_DEBUG, + "back[%03d]: may_clean=%d, may_finalize_disk=%d, may_serialize=%d:"LF "\t" "finalized(%d), status(%d), locked(%d), delayed(%d), test(%d), "LF "\t" "statuscode(%d), size(%d), is_write(%d), may_hypertext(%d), "LF - "\t" "contenttype(%s), url(%s%s), save(%s)"LF, + "\t" "contenttype(%s), url(%s%s), save(%s)", i, may_clean, may_finalize, may_serialize, back[i].finalized, @@ -2222,25 +2117,18 @@ void back_clean(httrackp* opt,cache_back* cache,struct_back* sback) { if (index >= 0) { opt->hash->liens[index]->pass2 = -1; /* DONE! */ } else { - if (opt->log != NULL) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: warning: entry cleaned up, but no trace on heap: %s%s (%s)"LF,back[i].url_adr, back[i].url_fil,back[i].url_sav); - test_flush; - } + hts_log_print(opt, LOG_INFO, "engine: warning: entry cleaned up, but no trace on heap: %s%s (%s)",back[i].url_adr, back[i].url_fil,back[i].url_sav); } } HTS_STAT.stat_background++; - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"File successfully written in background: %s"LF,back[i].url_sav); test_flush; - } + hts_log_print(opt, LOG_INFO, "File successfully written in background: %s",back[i].url_sav); back_maydelete(opt,cache,sback,i); // May delete backing entry } else { if (!back[i].finalized) { if (1) { /* Ensure deleted or recycled socket */ /* BUT DO NOT YET WIPE back[i].r.adr */ - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"file %s%s validated (cached, left in memory)"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "file %s%s validated (cached, left in memory)",back[i].url_adr,back[i].url_fil); back_maydeletehttp(opt, cache, sback, i); } else { /* @@ -2261,14 +2149,10 @@ void back_clean(httrackp* opt,cache_back* cache,struct_back* sback) { /* Delete buffer and sockets */ deleteaddr(&back[i].r); deletehttp(&back[i].r); - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"file %s%s temporarily left in cache to spare memory"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "file %s%s temporarily left in cache to spare memory",back[i].url_adr,back[i].url_fil); } } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unexpected html cache lookup error during back clean"LF); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Unexpected html cache lookup error during back clean"); } // xxc xxc } @@ -2282,12 +2166,9 @@ void back_clean(httrackp* opt,cache_back* cache,struct_back* sback) { || back[i].r.keep_alive_max < 1 || time_local() >= back[i].ka_time_start + back[i].r.keep_alive_t ) { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Keep-Alive): live socket closed #%d (%s)"LF, + hts_log_print(opt, LOG_DEBUG, "(Keep-Alive): live socket closed #%d (%s)", back[i].r.debugid, back[i].url_adr); - test_flush; - } back_delete(opt,cache,sback, i); // delete backing entry } } @@ -2305,10 +2186,8 @@ void back_clean(httrackp* opt,cache_back* cache,struct_back* sback) { int max = opt->maxsoc + oneMore; int curr = back_nsoc_overall(sback); if (curr > max) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Keep-Alive): deleting #%d sockets"LF, - curr - max); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(Keep-Alive): deleting #%d sockets", + curr - max); } for(i = 0 ; i < back_max && curr > max ; i++) { if (back[i].status == STATUS_ALIVE) { @@ -2320,9 +2199,8 @@ void back_clean(httrackp* opt,cache_back* cache,struct_back* sback) { /* transfer ready slots to the storage hashtable */ { int nxfr = back_cleanup_background(opt,cache,sback); - if (nxfr > 0 && (opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(htsback): %d slots ready moved to background"LF, nxfr); - test_flush; + if (nxfr > 0) { + hts_log_print(opt, LOG_DEBUG, "(htsback): %d slots ready moved to background", nxfr); } } } @@ -2447,9 +2325,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti strcpybuff(back[i].r.msg,"Receive Error"); back[i].status=STATUS_READY; // terminé back_set_finished(sback, i); - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unexpected socket error during pre-loop"LF); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Unexpected socket error during pre-loop"); } } @@ -2805,26 +2681,19 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti if (back[i].r.out==NULL) { errno = last_errno; if ((fcheck=check_fatal_io_errno())) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Mirror aborted: disk full or filesystem problems"LF); test_flush; + hts_log_print(opt, LOG_ERROR, "Mirror aborted: disk full or filesystem problems"); opt->state.exit_xh=-1; /* fatal error */ } } #if HDEBUG printf("direct-disk: %s\n",back[i].url_sav); #endif - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File received from net to disk: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File received from net to disk: %s%s",back[i].url_adr,back[i].url_fil); if (back[i].r.out==NULL) { - if (opt->log) { - HTS_LOG(opt,LOG_ERROR); - fprintf(opt->log,"Unable to save file %s : %s"LF,back[i].url_sav, strerror(last_errno)); - if (fcheck) { - HTS_LOG(opt,LOG_ERROR); - fprintf(opt->log,"* * Fatal write error, giving up"LF); - } - test_flush; + hts_log_print(opt, LOG_ERROR | LOG_ERRNO, "Unable to save file %s",back[i].url_sav); + if (fcheck) { + hts_log_print(opt, LOG_ERROR, "* * Fatal write error, giving up"); } back[i].r.is_write=0; // erreur, abandonner } else { @@ -2834,16 +2703,12 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti /* create a temporary reference file in case of broken mirror */ if (back[i].r.out != NULL) { if (back_serialize_ref(opt, &back[i]) != 0) { - if (opt->log != NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log, "Could not create temporary reference file for %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Could not create temporary reference file for %s%s",back[i].url_adr,back[i].url_fil); } } } } else { // on coupe tout! - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File cancelled (non HTML): %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File cancelled (non HTML): %s%s",back[i].url_adr,back[i].url_fil); back[i].status=STATUS_READY; // terminé back_set_finished(sback, i); if (!back[i].testmode) @@ -2977,9 +2842,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti } else { //#else // Un warning suffira.. - if (cache->log!=NULL) { - fspc(opt,cache->log,"warning"); fprintf(cache->log,"Incorrect length ("LLintP"!="LLintP" expected) for %s%s"LF,(LLint)back[i].r.size,(LLint)back[i].r.totalsize,back[i].url_adr,back[i].url_fil); - } + hts_log_print(opt, LOG_WARNING, "Incorrect length ("LLintP"!="LLintP" expected) for %s%s",(LLint)back[i].r.size,(LLint)back[i].r.totalsize,back[i].url_adr,back[i].url_fil); //#endif } } @@ -3024,7 +2887,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti back[i].r.adr=(char*) realloct(back[i].r.adr, (size_t)back[i].r.totalsize + 1); if (!back[i].r.adr) { if (cache->log!=NULL) { - fprintf(cache->log,"Error: Not enough memory ("LLintP") for %s%s"LF,(LLint)back[i].r.totalsize,back[i].url_adr,back[i].url_fil); + hts_log_print(opt, LOG_ERROR, "not enough memory ("LLintP") for %s%s",(LLint)back[i].r.totalsize,back[i].url_adr,back[i].url_fil); } } } @@ -3032,9 +2895,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti printf("[%d] chunk length: %d - next total "LLintP":\n",(int)back[i].r.soc,(int)chunk_size,(LLint)back[i].r.totalsize); #endif } else { - if (cache->log!=NULL) { - fprintf(cache->log,"Warning: Illegal chunk (%s) for %s%s"LF,back[i].chunk_adr,back[i].url_adr,back[i].url_fil); - } + hts_log_print(opt, LOG_WARNING, "Illegal chunk (%s) for %s%s",back[i].chunk_adr,back[i].url_adr,back[i].url_fil); } } else { /* back[i].status==STATUS_CHUNK_CR : just receiving ending CRLF after data */ if (chunk_data[0] == '\0') { @@ -3048,18 +2909,14 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti printf("[%d] chunk CRLF seen\n", (int)back[i].r.soc); #endif } else { - if (cache->log!=NULL) { - fprintf(cache->log,"Warning: Illegal chunk CRLF (%s) for %s%s"LF,back[i].chunk_adr,back[i].url_adr,back[i].url_fil); - } + hts_log_print(opt, LOG_WARNING, "illegal chunk CRLF (%s) for %s%s", back[i].chunk_adr,back[i].url_adr,back[i].url_fil); #if CHUNKDEBUG==1 printf("[%d] chunk CRLF ERROR!! : '%s'\n", (int)back[i].r.soc, chunk_data); #endif } } } else { - if (cache->log!=NULL) { - fprintf(cache->log,"Warning: Chunk too big ("LLintP") for %s%s"LF,(LLint)back[i].chunk_size,back[i].url_adr,back[i].url_fil); - } + hts_log_print(opt, LOG_WARNING, "chunk too big ("LLintP") for %s%s",(LLint)back[i].chunk_size,back[i].url_adr,back[i].url_fil); } // ok, continuer sur le body @@ -3095,9 +2952,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti back_finalize(opt,cache,sback,i); } else { if (back[i].r.statuscode == HTTP_OK) { - if (cache->log!=NULL) { - fspc(opt,cache->log,"warning"); fprintf(cache->log,"Unexpected incomplete type with 200 code at %s%s"LF, back[i].url_adr, back[i].url_fil); - } + hts_log_print(opt, LOG_WARNING, "unexpected incomplete type with 200 code at %s%s", back[i].url_adr, back[i].url_fil); } } if (back[i].r.soc!=INVALID_SOCKET) { @@ -3127,9 +2982,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti strcpybuff(back[i].r.msg,"Incorrect length"); } else { // Un warning suffira.. - if (cache->log!=NULL) { - fspc(opt,cache->log,"warning"); fprintf(cache->log,"Incorrect length ("LLintP"!="LLintP" expected) for %s%s"LF,(LLint)back[i].r.size,(LLint)back[i].r.totalsize,back[i].url_adr,back[i].url_fil); - } + hts_log_print(opt, LOG_WARNING, "Incorrect length ("LLintP"!="LLintP" expected) for %s%s",(LLint)back[i].r.size,(LLint)back[i].r.totalsize,back[i].url_adr,back[i].url_fil); } } } @@ -3242,9 +3095,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti int test_head = RUN_CALLBACK6(opt, receivehead, back[i].r.adr, back[i].url_adr, back[i].url_fil, back[i].referer_adr, back[i].referer_fil, &back[i].r); if (test_head!=1) { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"External wrapper aborted transfer, breaking connection: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "External wrapper aborted transfer, breaking connection: %s%s",back[i].url_adr,back[i].url_fil); back[i].status=STATUS_READY; // FINI back_set_finished(sback, i); deletehttp(&back[i].r); back[i].r.soc=INVALID_SOCKET; @@ -3277,9 +3128,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti back[i].chunk_size=0; back[i].r.statuscode=STATUSCODE_INVALID; back[i].r.msg[0]='\0'; - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Status 100 detected for %s%s, continuing headers"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Status 100 detected for %s%s, continuing headers",back[i].url_adr,back[i].url_fil); continue; } @@ -3299,9 +3148,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti back_set_finished(sback, i); back[i].r.size=back[i].r.totalsize=back[i].range_req_size; back[i].r.statuscode=HTTP_NOT_MODIFIED; // NOT MODIFIED - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File seems complete (good 416 message), breaking connection: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File seems complete (good 416 message), breaking connection: %s%s",back[i].url_adr,back[i].url_fil); } } @@ -3316,9 +3163,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti if (!opt->delete_old) { if (HTTP_IS_ERROR(back[i].r.statuscode) && back[i].is_update && !back[i].testmode) { if (back[i].url_sav[0] && fexist_utf8(back[i].url_sav)) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Error ignored %d (%s) because of 'no purge' option for %s%s"LF,back[i].r.statuscode,back[i].r.msg,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Error ignored %d (%s) because of 'no purge' option for %s%s",back[i].r.statuscode,back[i].r.msg,back[i].url_adr,back[i].url_fil); back[i].r.statuscode = HTTP_NOT_MODIFIED; deletehttp(&back[i].r); back[i].r.soc=INVALID_SOCKET; } @@ -3348,15 +3193,11 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti if (len1 == len2) { // tailles identiques back[i].r.statuscode=HTTP_NOT_MODIFIED; // forcer NOT MODIFIED deletehttp(&back[i].r); back[i].r.soc=INVALID_SOCKET; - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File seems complete (same size), breaking connection: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File seems complete (same size), breaking connection: %s%s",back[i].url_adr,back[i].url_fil); } } } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"File seems complete (same size), but there was a cache read error (%u): %s%s"LF, r.statuscode, back[i].url_adr, back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "File seems complete (same size), but there was a cache read error (%u): %s%s", r.statuscode, back[i].url_adr, back[i].url_fil); } if (r.adr) { freet(r.adr); @@ -3383,9 +3224,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti filenote(&opt->state.strc,back[i].url_sav,NULL); file_notify(opt,back[i].url_adr, back[i].url_fil, back[i].url_sav, 0, 0, back[i].r.notmodified); back[i].r.statuscode=HTTP_NOT_MODIFIED; // NOT MODIFIED - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File seems complete (same size file discovered), breaking connection: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File seems complete (same size file discovered), breaking connection: %s%s",back[i].url_adr,back[i].url_fil); } } } @@ -3421,9 +3260,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti filenote(&opt->state.strc,back[i].url_sav,NULL); file_notify(opt,back[i].url_adr, back[i].url_fil, back[i].url_sav, 0, 0, back[i].r.notmodified); back[i].r.statuscode=HTTP_NOT_MODIFIED; // NOT MODIFIED - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File seems complete (reget failed), breaking connection: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File seems complete (reget failed), breaking connection: %s%s",back[i].url_adr,back[i].url_fil); } } } @@ -3462,9 +3299,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti /* if (back[i].r.soc!=INVALID_SOCKET) { // ok récupérer body? */ // head: terminé if (back[i].head_request) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Tested file: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Tested file: %s%s",back[i].url_adr,back[i].url_fil); #if HTS_DEBUG_CLOSESOCK DEBUG_W("back_wait(head request): deletehttp\n"); #endif @@ -3502,9 +3337,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti back[i].r.is_file=1; back[i].r.totalsize = back[i].r.size = fsize_utf8(back[i].url_sav); get_httptype(opt,back[i].r.contenttype, back[i].url_sav, 1); - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Not-modified status without cache guessed: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Not-modified status without cache guessed: %s%s",back[i].url_adr,back[i].url_fil); } } @@ -3513,9 +3346,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti back[i].status=STATUS_READY; // OK prêt back_set_finished(sback, i); back[i].r.notmodified=1; // NON modifié! - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File loaded after test from cache: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File loaded after test from cache: %s%s",back[i].url_adr,back[i].url_fil); // finalize //file_notify(back[i].url_adr, back[i].url_fil, back[i].url_sav, 0, 0, back[i].r.notmodified); // not modified @@ -3586,9 +3417,7 @@ void back_wait(struct_back* sback,httrackp* opt,cache_back* cache,TStamp stat_ti fseek(back[i].r.out,0,SEEK_END); // à la fin /* create a temporary reference file in case of broken mirror */ if (back_serialize_ref(opt, &back[i]) != 0) { - if (opt->log != NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log, "Could not create temporary reference file for %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Could not create temporary reference file for %s%s",back[i].url_adr,back[i].url_fil); } #if HDEBUG printf("continue interrupted file\n"); @@ -3833,10 +3662,7 @@ int back_checkmirror(httrackp* opt) { // Check max size if ((opt->maxsite>0) && (HTS_STAT.stat_bytes >= opt->maxsite)) { if (!opt->state.stop) { /* not yet stopped */ - if (opt->log) { - fprintf(opt->log,"More than "LLintP" bytes have been transfered.. giving up"LF,(LLint)opt->maxsite); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "More than "LLintP" bytes have been transfered.. giving up",(LLint)opt->maxsite); /* cancel mirror smoothly */ hts_request_stop(opt, 0); } @@ -3847,10 +3673,7 @@ int back_checkmirror(httrackp* opt) { // Check max time if ((opt->maxtime>0) && ((time_local()-HTS_STAT.stat_timestart)>opt->maxtime)) { if (!opt->state.stop) { /* not yet stopped */ - if (opt->log) { - fprintf(opt->log,"More than %d seconds passed.. giving up"LF,opt->maxtime); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "More than %d seconds passed.. giving up",opt->maxtime); /* cancel mirror smoothly */ hts_request_stop(opt, 0); } @@ -3983,5 +3806,3 @@ void back_infostr(struct_back* sback,int i,int j,char* s) { } // -- backing -- - -#undef test_flush diff --git a/src/htsbase.h b/src/htsbase.h index e144991..ef01eba 100644 --- a/src/htsbase.h +++ b/src/htsbase.h @@ -71,9 +71,11 @@ extern "C" { #ifdef __GNUC__ #define HTS_UNUSED __attribute__ ((unused)) #define HTS_STATIC static __attribute__ ((unused)) +#define HTS_PRINTF_FUN(fmt, arg) __attribute__ ((format (printf, fmt, arg))) #else #define HTS_UNUSED #define HTS_STATIC static +#define HTS_PRINTF_FUN(fmt, arg) #endif #endif @@ -156,36 +158,6 @@ extern HTSEXT_API t_abortLog abortLog__; } \ } while(0) -/* logging */ -typedef enum { - LOG_DEBUG, - LOG_INFO, - LOG_WARNING, - LOG_ERROR, - LOG_PANIC -} HTS_LogType; -#define HTS_LOG(OPT,TYPE) do { \ - int last_errno = errno; \ - switch(TYPE) { \ - case LOG_DEBUG: \ - fspc(OPT,(OPT)->log, "debug"); \ - break; \ - case LOG_INFO: \ - fspc(OPT,(OPT)->log, "info"); \ - break; \ - case LOG_WARNING: \ - fspc(OPT,(OPT)->log, "warning"); \ - break; \ - case LOG_ERROR: \ - fspc(OPT,(OPT)->log, "error"); \ - break; \ - case LOG_PANIC: \ - fspc(OPT,(OPT)->log, "panic"); \ - break; \ - } \ - errno = last_errno; \ -} while(0) - /* regular malloc's() */ #ifndef HTS_TRACE_MALLOC #define malloct(A) malloc(A) diff --git a/src/htscache.c b/src/htscache.c index fc8a5b8..07e2cf7 100644 --- a/src/htscache.c +++ b/src/htscache.c @@ -49,9 +49,6 @@ Please visit our Website: http://www.httrack.com #include "htszlib.h" /* END specific definitions */ -#undef test_flush -#define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); } - // routines de mise en cache /* @@ -111,18 +108,14 @@ with void cache_mayadd(httrackp* opt,cache_back* cache,htsblk* r,const char* url_adr,const char* url_fil,const char* url_save) { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File checked by cache: %s"LF,url_adr); - } + hts_log_print(opt, LOG_DEBUG, "File checked by cache: %s",url_adr); // ---stockage en cache--- // stocker dans le cache? if (opt->cache) { if (cache_writable(cache)) { // ensure not a temporary filename (should not happend ?!) if (IS_DELAYED_EXT(url_save)) { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log, "aborted cache validation: %s%s still has temporary name %s"LF, url_adr, url_fil, url_save); - } + hts_log_print(opt, LOG_WARNING, "aborted cache validation: %s%s still has temporary name %s", url_adr, url_fil, url_save); return ; } @@ -161,9 +154,7 @@ void cache_mayadd(httrackp* opt,cache_back* cache,htsblk* r,const char* url_adr, strcatbuff(tempo, "\n"); strcatbuff(tempo, r->location); } - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log, "Cached fast-header response: %s%s is %d"LF, url_adr, url_fil, (int)r->statuscode); - } + hts_log_print(opt, LOG_DEBUG, "Cached fast-header response: %s%s is %d", url_adr, url_fil, (int)r->statuscode); inthash_add(cache->cached_tests, concat(OPT_GET_BUFF(opt), url_adr, url_fil), (intptr_t)strdupt(tempo)); } } @@ -709,9 +700,7 @@ static htsblk cache_readex_new(httrackp* opt,cache_back* cache,const char* adr,c if (!dataincache) { /* Data are supposed to be on disk */ if (!fexist_utf8(fconv(catbuff, previous_save))) { // un fichier existe déja if (!opt->norecatch) { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: could not find %s"LF, previous_save); - } + hts_log_print(opt, LOG_DEBUG, "Cache: could not find %s", previous_save); r.statuscode=STATUSCODE_INVALID; strcpybuff(r.msg, "Previous cache file not found"); } @@ -748,16 +737,14 @@ static htsblk cache_readex_new(httrackp* opt,cache_back* cache,const char* adr,c // So far so good ok=1; // plus rien à faire - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log, "File '%s' has been renamed since last mirror to '%s' ; applying changes"LF, previous_save, target_save); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "File '%s' has been renamed since last mirror to '%s' ; applying changes", previous_save, target_save); } else { r.statuscode=STATUSCODE_INVALID; strcpybuff(r.msg, "Unable to rename file on disk"); } } } else { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log, "warning: file size on disk ("LLintP") does not have the expected size ("LLintP"))"LF, (LLint) fsize, (LLint) r.size); + hts_log_print(opt, LOG_WARNING, "warning: file size on disk ("LLintP") does not have the expected size ("LLintP"))", (LLint) fsize, (LLint) r.size); } } // File exists with the target name and not previous one ? @@ -769,7 +756,7 @@ static htsblk cache_readex_new(httrackp* opt,cache_back* cache,const char* adr,c // So far so good ok=1; // plus rien à faire } else { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log, "warning: renamed file size on disk ("LLintP") does not have the expected size ("LLintP"))"LF, (LLint) fsize, (LLint) r.size); + hts_log_print(opt, LOG_WARNING, "warning: renamed file size on disk ("LLintP") does not have the expected size ("LLintP"))", (LLint) fsize, (LLint) r.size); } } @@ -1278,9 +1265,7 @@ int cache_readdata(cache_back* cache,const char* str1,const char* str2,char** in } static int hts_rename(httrackp* opt, const char *a, const char *b) { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: rename %s -> %s (%p %p)"LF, a, b, a, b); - } + hts_log_print(opt, LOG_DEBUG, "Cache: rename %s -> %s (%p %p)", a, b, a, b); return rename(a, b); } @@ -1299,9 +1284,7 @@ htsblk* cache_header(httrackp* opt,cache_back* cache,const char* adr,const char* void cache_init(cache_back* cache,httrackp* opt) { // --- // utilisation du cache: renommer ancien éventuel et charger index - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: enabled=%d, base=%s, ro=%d"LF, (int) opt->cache, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/"), (int) cache->ro); - } + hts_log_print(opt, LOG_DEBUG, "Cache: enabled=%d, base=%s, ro=%d", (int) opt->cache, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/"), (int) cache->ro); if (opt->cache) { #if DEBUGCA printf("cache init: "); @@ -1330,23 +1313,15 @@ void cache_init(cache_back* cache,httrackp* opt) { /* Remove OLD cache */ if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"))) { if (remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip")) != 0) { - int last_errno = errno; - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: error while moving previous cache: %s"LF, strerror(last_errno)); - } + hts_log_print(opt, LOG_WARNING | LOG_ERRNO, "Cache: error while moving previous cache"); } } /* Rename */ if (hts_rename(opt, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"), fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip")) != 0) { - int last_errno = errno; - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: error while moving previous cache: %s"LF, strerror(last_errno)); - } + hts_log_print(opt, LOG_WARNING | LOG_ERRNO, "Cache: error while moving previous cache"); } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: successfully renamed"LF); - } + hts_log_print(opt, LOG_DEBUG, "Cache: successfully renamed"); } } else if ((fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"))) && (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx")))) { // il existe déja un cache précédent.. renommer @@ -1370,13 +1345,9 @@ void cache_init(cache_back* cache,httrackp* opt) { remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx")); } } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: no cache found"LF); - } - } - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: size %d"LF, (int)fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"))); + hts_log_print(opt, LOG_DEBUG, "Cache: no cache found"); } + hts_log_print(opt, LOG_DEBUG, "Cache: size %d", (int)fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"))); // charger index cache précédent if ( @@ -1407,10 +1378,7 @@ void cache_init(cache_back* cache,httrackp* opt) { } else { name = fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"); } - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: damaged cache, trying to repair"LF); - fflush(opt->log); - } + hts_log_print(opt, LOG_WARNING, "Cache: damaged cache, trying to repair"); if (unzRepair(name, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/repair.zip"), fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/repair.tmp"), @@ -1419,16 +1387,10 @@ void cache_init(cache_back* cache,httrackp* opt) { unlink(name); rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/repair.zip"), name); cache->zipInput = unzOpen(name); - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: %d bytes successfully recovered in %d entries"LF, - (int) repairedBytes, (int) repaired); - fflush(opt->log); - } + hts_log_print(opt, LOG_WARNING, "Cache: %d bytes successfully recovered in %d entries", + (int) repairedBytes, (int) repaired); } else { - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: could not repair the cache"LF); - fflush(opt->log); - } + hts_log_print(opt, LOG_WARNING, "Cache: could not repair the cache"); } } @@ -1485,33 +1447,25 @@ void cache_init(cache_back* cache,httrackp* opt) { else inthash_add(cache->hashtable, filenameIndex, -pos); } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Corrupted cache meta entry #%d"LF, (int)entries); - } + hts_log_print(opt, LOG_WARNING, "Corrupted cache meta entry #%d", (int)entries); } } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Corrupted cache entry #%d"LF, (int)entries); - } + hts_log_print(opt, LOG_WARNING, "Corrupted cache entry #%d", (int)entries); } unzCloseCurrentFile((unzFile) cache->zipInput); } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Corrupted cache entry #%d"LF, (int)entries); - } + hts_log_print(opt, LOG_WARNING, "Corrupted cache entry #%d", (int)entries); } } while( unzGoToNextFile((unzFile) cache->zipInput) == Z_OK ); - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache index loaded: %d entries loaded"LF, (int)entries); - } + hts_log_print(opt, LOG_DEBUG, "Cache index loaded: %d entries loaded", (int)entries); opt->is_update=1; // signaler comme update } else { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: error trying to read the cache: %s"LF, hts_get_zerror(zErr)); + hts_log_print(opt, LOG_WARNING, "Cache: error trying to read the cache: %s", hts_get_zerror(zErr)); } } else { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: error trying to open the cache"LF); + hts_log_print(opt, LOG_WARNING, "Cache: error trying to open the cache"); } } else if ( @@ -1568,20 +1522,14 @@ void cache_init(cache_back* cache,httrackp* opt) { a+=cache_brstr(a,firstline); strcpybuff(cache->lastmodified,firstline); } else { - if (opt->log) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Cache: version 1.%d not supported, ignoring current cache"LF,cache->version); - fflush(opt->log); - } + hts_log_print(opt, LOG_ERROR, "Cache: version 1.%d not supported, ignoring current cache",cache->version); fclose(cache->olddat); cache->olddat=NULL; freet(cache->use); cache->use=NULL; } } else { // non supporté - if (opt->log) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Cache: %s not supported, ignoring current cache"LF,firstline); - fflush(opt->log); - } + hts_log_print(opt, LOG_ERROR, "Cache: %s not supported, ignoring current cache",firstline); fclose(cache->olddat); cache->olddat=NULL; freet(cache->use); @@ -1590,10 +1538,7 @@ void cache_init(cache_back* cache,httrackp* opt) { /* */ } else { // Vieille version du cache /* */ - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Cache: importing old cache format"LF); - fflush(opt->log); - } + hts_log_print(opt, LOG_WARNING, "Cache: importing old cache format"); cache->version=0; // cache 1.0 strcpybuff(cache->lastmodified,firstline); } @@ -1626,9 +1571,7 @@ void cache_init(cache_back* cache,httrackp* opt) { } } } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: no cache found in %s"LF, fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/")); - } + hts_log_print(opt, LOG_DEBUG, "Cache: no cache found in %s", fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/")); } #if DEBUGCA @@ -1738,9 +1681,7 @@ void cache_init(cache_back* cache,httrackp* opt) { } } else { - if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Cache: no cache enabled"LF); - } + hts_log_print(opt, LOG_DEBUG, "Cache: no cache enabled"); } } diff --git a/src/htscore.c b/src/htscore.c index 4973479..f77124f 100644 --- a/src/htscore.c +++ b/src/htscore.c @@ -114,10 +114,6 @@ int nsocDEBUG=0; // Début de httpmirror, routines annexes -// version 1 pour httpmirror -// flusher si on doit lire peu à peu le fichier -#define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); } - // pour alléger la syntaxe, des raccourcis sont créés #define urladr (liens[ptr]->adr) #define urlfil (liens[ptr]->fil) @@ -128,9 +124,7 @@ int nsocDEBUG=0; // note: partir de liens_max.. vers 0.. sinon erreur de violation de mémoire: les liens suivants // ne sont plus à nous.. agh! [dur celui-là] #define HTMLCHECK_UNINIT { \ -if ( (opt->debug>0) && (opt->log!=NULL) ) { \ -HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: end"LF); \ -} \ +hts_log_print(opt, LOG_INFO, "engine: end"); \ RUN_CALLBACK0(opt, end); \ } @@ -172,8 +166,7 @@ RUN_CALLBACK0(opt, end); \ if (cache.olddat) { fclose(cache.olddat); cache.olddat=NULL; } \ if (cache.lst) { fclose(cache.lst); cache.lst=opt->state.strc.lst=NULL; } \ if (cache.txt) { fclose(cache.txt); cache.txt=NULL; } \ - if (opt->log) fflush(opt->log); \ - if (opt->log) fflush(opt->log);\ + if (opt->log != NULL) fflush(opt->log); \ if (makestat_fp) { fclose(makestat_fp); makestat_fp=NULL; } \ if (maketrack_fp){ fclose(maketrack_fp); maketrack_fp=NULL; } \ if (opt->accept_cookie) cookie_save(opt->cookie,fconcat(OPT_GET_BUFF(opt),StringBuff(opt->path_log),"cookies.txt")); \ @@ -520,11 +513,8 @@ int httpmirror(char* url1, httrackp* opt) { opt->maxfilter += HTS_FILTERSINC; if (filters_init(&filters, opt->maxfilter, HTS_FILTERSINC) == 0) { printf("PANIC! : Too many filters : >%d [%d]\n",filptr,__LINE__); - if (opt->log) { - fprintf(opt->log,LF"Too many filters, giving up..(>%d)"LF,filptr); - fprintf(opt->log,"To avoid that: use #F option for more filters (example: -#F5000)"LF); - test_flush; - } + hts_log_print(opt, LOG_PANIC, "Too many filters, giving up..(>%d)",filptr); + hts_log_print(opt, LOG_INFO, "To avoid that: use #F option for more filters (example: -#F5000)"); XH_extuninit; return 0; } @@ -592,16 +582,12 @@ int httpmirror(char* url1, httrackp* opt) { } } // fclose(fp); - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"%d links added from %s"LF,n,StringBuff(opt->filelist)); test_flush; - } + hts_log_print(opt, LOG_INFO, "%d links added from %s",n,StringBuff(opt->filelist)); // Free buffer freet(filelist_buff); } else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Could not include URL list: %s"LF,StringBuff(opt->filelist)); test_flush; - } + hts_log_print(opt, LOG_ERROR, "Could not include URL list: %s",StringBuff(opt->filelist)); } } @@ -610,10 +596,7 @@ int httpmirror(char* url1, httrackp* opt) { liens_record("primary","/primary",fslash(OPT_GET_BUFF(opt),fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html_utf8),"index.html")),"","",opt->urlhack); if (liens[lien_tot]==NULL) { // erreur, pas de place réservée printf("PANIC! : Not enough memory [%d]\n",__LINE__); - if (opt->log) { - fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url))); - test_flush; - } + hts_log_print(opt, LOG_PANIC, "Not enough memory, can not re-allocate %d bytes",(int)((add_tab_alloc+1)*sizeof(lien_url))); XH_extuninit; // désallocation mémoire & buffers return 0; } @@ -665,16 +648,11 @@ int httpmirror(char* url1, httrackp* opt) { // --> 1024 entrées + 32 entrées par socket en supplément sback = back_new(opt->maxsoc*32+1024); if (sback == NULL) { - if (opt->log) - fprintf(opt->log,"Not enough memory, can not allocate %d bytes"LF,(int)((opt->maxsoc+1)*sizeof(lien_back))); + hts_log_print(opt, LOG_PANIC, "Not enough memory, can not allocate %d bytes",(int)((opt->maxsoc+1)*sizeof(lien_back))); return 0; } } - - // flush - test_flush; - // statistiques if (opt->makestat) { makestat_fp=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-stats.txt"),"wb"); @@ -695,9 +673,7 @@ int httpmirror(char* url1, httrackp* opt) { // on n'a pas de liens!! (exemple: httrack www.* impossible sans départ..) if (lien_tot<=0) { - if (opt->log) { - fprintf(opt->log,"Error! You MUST specify at least one complete URL, and not only wildcards!"LF); - } + hts_log_print(opt, LOG_ERROR, "You MUST specify at least one complete URL, and not only wildcards!"); } /* Send options to callback functions */ @@ -769,9 +745,7 @@ int httpmirror(char* url1, httrackp* opt) { } /* Info for wrappers */ - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: start"LF); - } + hts_log_print(opt, LOG_INFO, "engine: start"); if (!RUN_CALLBACK0(opt, start)) { XH_extuninit; return 1; @@ -821,14 +795,11 @@ int httpmirror(char* url1, httrackp* opt) { ( (liens[ptr]->pass2 == -1) ) ) ) { // sauter si lien annulé (ou fil vide) - if ((opt->debug>1) && (opt->log!=NULL)) { - if (liens[ptr] != NULL && liens[ptr]->pass2 == -1) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link #%d is ready, skipping: %s%s.."LF,ptr,((urladr != NULL)?(urladr):(" ")),((urlfil != NULL)?(urlfil):(" "))); - } else { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"link #%d seems ready, skipping: %s%s.."LF,ptr,((urladr != NULL)?(urladr):(" ")),((urlfil != NULL)?(urlfil):(" "))); - } - test_flush; - } + if (liens[ptr] != NULL && liens[ptr]->pass2 == -1) { + hts_log_print(opt, LOG_DEBUG, "link #%d is ready, skipping: %s%s..",ptr,((urladr != NULL)?(urladr):(" ")),((urlfil != NULL)?(urlfil):(" "))); + } else { + hts_log_print(opt, LOG_DEBUG, "link #%d seems ready, skipping: %s%s..",ptr,((urladr != NULL)?(urladr):(" ")),((urlfil != NULL)?(urlfil):(" "))); + } // remove from stats if (liens[ptr]->pass2 == -1) { HTS_STAT.stat_background--; @@ -838,15 +809,12 @@ int httpmirror(char* url1, httrackp* opt) { } if (liens[ptr] != NULL) { // on a qq chose à récupérer? - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Wait get: %s%s"LF,urladr,urlfil); - test_flush; + hts_log_print(opt, LOG_DEBUG, "Wait get: %s%s",urladr,urlfil); #if DEBUG_ROBOTS - if (strcmp(urlfil,"/robots.txt") == 0) { - printf("robots.txt detected\n"); - } + if (strcmp(urlfil,"/robots.txt") == 0) { + printf("robots.txt detected\n"); + } #endif - } // ------------------------------------------------------------ // DEBUT --RECUPERATION LIEN--- if (ptr==0) { // premier lien à parcourir: lien primaire construit avant @@ -957,9 +925,7 @@ int httpmirror(char* url1, httrackp* opt) { } else { // lien vide.. - if (opt->log && opt->debug > 0) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Warning, link #%d empty"LF,ptr); test_flush; - } + hts_log_print(opt, LOG_WARNING, "Warning, link #%d empty",ptr); error=1; goto jump_if_done; } // test si url existe (non vide!) @@ -984,10 +950,7 @@ int httpmirror(char* url1, httrackp* opt) { if ((istoobig(opt,r.totalsize,opt->maxfile_html,opt->maxfile_nonhtml,r.contenttype)) || (istoobig(opt,r.totalsize,opt->maxfile_html,opt->maxfile_nonhtml,r.contenttype))) { error=0; - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Big file cancelled according to user's preferences: %s%s"LF,urladr,urlfil); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "Big file cancelled according to user's preferences: %s%s",urladr,urlfil); } // // // error=1; // ne pas traiter la suite -- euhh si finalement.. } @@ -1033,15 +996,9 @@ int httpmirror(char* url1, httrackp* opt) { is_loaded_from_file = 1; \ r.adr = readfile2(savename, &r.size); \ if (r.adr != NULL) { \ - if ( (opt->debug>0) && (opt->log!=NULL) ) { \ - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"File successfully loaded for parsing: %s%s (%d bytes)"LF,urladr,urlfil,(int)r.size); \ - test_flush; \ - } \ + hts_log_print(opt, LOG_INFO, "File successfully loaded for parsing: %s%s (%d bytes)",urladr,urlfil,(int)r.size); \ } else { \ - if ( opt->log != NULL ) { \ - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"File could not be loaded for parsing: %s%s"LF,urladr,urlfil); \ - test_flush; \ - } \ + hts_log_print(opt, LOG_ERROR, "File could not be loaded for parsing: %s%s",urladr,urlfil); \ } \ } \ } while(0) @@ -1187,10 +1144,7 @@ int httpmirror(char* url1, httrackp* opt) { CH_ADD_RNG0( unic, 0x80 ); } } - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"File %s%s converted from UCS2 to UTF-8 (old size: %d bytes, new size: %d bytes)"LF, urladr, urlfil, (int)r.size, new_offs); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "File %s%s converted from UCS2 to UTF-8 (old size: %d bytes, new size: %d bytes)", urladr, urlfil, (int)r.size, new_offs); freet(r.adr); r.adr = NULL; r.size = new_offs; @@ -1203,10 +1157,7 @@ int httpmirror(char* url1, httrackp* opt) { } else if ((nspec > r.size / 100) && (nspec > 10)) { // too many special characters is_binary = 1; strcpybuff(r.contenttype,"application/octet-stream"); - if (opt->log) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"File not parsed, looks like binary: %s%s"LF,urladr,urlfil); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "File not parsed, looks like binary: %s%s",urladr,urlfil); } /* This hack allows to avoid problems with parsing '\0' characters */ @@ -1400,18 +1351,12 @@ int httpmirror(char* url1, httrackp* opt) { if ((!r.notmodified) && (opt->is_update) && (!store_errpage)) { // page modifiée if (strnotempty(savename)) { HTS_STAT.stat_updated_files++; - if (opt->log!=NULL) { - //if ((opt->debug>0) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"File updated: %s%s"LF,urladr,urlfil); - test_flush; - } + //if ((opt->debug>0) && (opt->log!=NULL)) { + hts_log_print(opt, LOG_INFO, "File updated: %s%s",urladr,urlfil); } } else { if (!store_errpage) { - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"File recorded: %s%s"LF,urladr,urlfil); - test_flush; - } + hts_log_print(opt, LOG_INFO, "File recorded: %s%s",urladr,urlfil); } } } @@ -1471,18 +1416,14 @@ int httpmirror(char* url1, httrackp* opt) { } /* Could not detect charset */ if (page_charset[0] == '\0') { - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Warning: could not detect encoding for: %s%s"LF,urladr,urlfil); - } + hts_log_print(opt, LOG_INFO, "Warning: could not detect encoding for: %s%s",urladr,urlfil); /* Fallback to ISO-8859-1 (~== identity) ; accents will look weird */ strcpy(page_charset, "iso-8859-1"); } } /* Info for wrappers */ - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: check-html: %s%s"LF,urladr,urlfil); - } + hts_log_print(opt, LOG_INFO, "engine: check-html: %s%s",urladr,urlfil); { char BIGSTK buff_err_msg[1024]; htsmoduleStruct BIGSTK str; @@ -1596,10 +1537,7 @@ int httpmirror(char* url1, httrackp* opt) { /* Créer message d'erreur ? */ if (create_html_warning) { char* adr=(char*)malloct(strlen(HTS_DATA_ERROR_HTML)+1100); - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Creating HTML warning file (%s)"LF,r.msg); - test_flush; - } + hts_log_print(opt, LOG_INFO, "Creating HTML warning file (%s)",r.msg); if (adr) { if (r.adr) { freet(r.adr); @@ -1610,10 +1548,7 @@ int httpmirror(char* url1, httrackp* opt) { } } else if (create_gif_warning) { char* adr=(char*)malloct(HTS_DATA_UNKNOWN_GIF_LEN); - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Creating GIF dummy file (%s)"LF,r.msg); - test_flush; - } + hts_log_print(opt, LOG_INFO, "Creating GIF dummy file (%s)",r.msg); if (r.adr) { freet(r.adr); r.adr=NULL; @@ -1690,10 +1625,7 @@ int httpmirror(char* url1, httrackp* opt) { } #ifdef IGNORE_RESTRICTIVE_ROBOTS else { - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Note: %s robots.txt rules are too restrictive, ignoring /"LF,urladr); - test_flush; - } + hts_log_print(opt, LOG_INFO, "Note: %s robots.txt rules are too restrictive, ignoring /",urladr); } #endif } @@ -1702,16 +1634,8 @@ int httpmirror(char* url1, httrackp* opt) { } while( (bptrlog!=NULL) { - if (opt->log != opt->log) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Note: robots.txt forbidden links for %s are: %s"LF,urladr,infobuff); - test_flush; - } - } - if (opt->log!=NULL) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Note: due to %s remote robots.txt rules, links beginning with these path will be forbidden: %s (see in the options to disable this)"LF,urladr,infobuff); - test_flush; - } + hts_log_print(opt, LOG_INFO, "Note: robots.txt forbidden links for %s are: %s",urladr,infobuff); + hts_log_print(opt, LOG_INFO, "Note: due to %s remote robots.txt rules, links beginning with these path will be forbidden: %s (see in the options to disable this)",urladr,infobuff); } } } @@ -1763,21 +1687,12 @@ int httpmirror(char* url1, httrackp* opt) { } - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Warning: store %s without scan: %s"LF,r.contenttype,savename); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "Warning: store %s without scan: %s",r.contenttype,savename); } else { if ((opt->getmode & 2)!=0) { // ok autorisé - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Store %s: %s"LF,r.contenttype,savename); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Store %s: %s",r.contenttype,savename); } else { // lien non autorisé! (ex: cgi-bin en html) - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"non-html file ignored after upload at %s : %s"LF,urladr,urlfil); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "non-html file ignored after upload at %s : %s",urladr,urlfil); if (r.adr) { freet(r.adr); r.adr=NULL; } @@ -1792,18 +1707,10 @@ int httpmirror(char* url1, httrackp* opt) { if (filesave(opt,r.adr,(int)r.size,savename,urladr,urlfil)!=0) { int fcheck; if ((fcheck=check_fatal_io_errno())) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Mirror aborted: disk full or filesystem problems"LF); test_flush; + hts_log_print(opt, LOG_ERROR, "Mirror aborted: disk full or filesystem problems"); opt->state.exit_xh=-1; /* fatal error */ } - if (opt->log) { - int last_errno = errno; - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unable to save file %s : %s"LF, savename, strerror(last_errno)); - if (fcheck) { - HTS_LOG(opt,LOG_ERROR); - fprintf(opt->log,"* * Fatal write error, giving up"LF); - } - test_flush; - } + hts_log_print(opt, LOG_ERROR | LOG_ERRNO, "Unable to save file %s", savename); } else { /* if (!ishttperror(r.statuscode)) @@ -1819,9 +1726,7 @@ int httpmirror(char* url1, httrackp* opt) { /* Parsing of other media types (java, ram..) */ /* if (strfield2(r.contenttype,"audio/x-pn-realaudio")) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Real Media): parsing %s"LF,savename); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(Real Media): parsing %s",savename); if (fexist(savename)) { // ok, existe bien! FILE* fp=FOPEN(savename,"r+b"); if (fp) { @@ -1829,9 +1734,7 @@ int httpmirror(char* url1, httrackp* opt) { char BIGSTK line[HTS_URLMAXSIZE*2]; linput(fp,line,HTS_URLMAXSIZE); if (strnotempty(line)) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(Real Media): detected %s"LF,line); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(Real Media): detected %s",line); } } fclose(fp); @@ -1871,14 +1774,10 @@ int httpmirror(char* url1, httrackp* opt) { /* Parse if recognized */ switch(hts_parse_externals(&str)) { case 1: - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(External module): parsed successfully %s"LF,savename); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(External module): parsed successfully %s",savename); break; case 0: - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(External module): couldn't parse successfully %s : %s"LF,savename, str.err_msg); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(External module): couldn't parse successfully %s : %s",savename, str.err_msg); break; } } @@ -1914,10 +1813,7 @@ jump_if_done: } if (ptr>=lien_tot) { // fin de boucle if (!numero_passe) { // première boucle - if ((opt->debug>1) && (opt->log!=NULL)) { - fprintf(opt->log,LF"Now getting non-html files..."LF); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Now getting non-html files..."); numero_passe=1; // seconde boucle ptr=0; // prochain pass2 @@ -1937,15 +1833,11 @@ jump_if_done: if (!back_checkmirror(opt)) { ptr=lien_tot; } else if (opt->state.exit_xh) { // sortir - if (opt->log) { - HTS_LOG(opt,LOG_INFO); - if (opt->state.exit_xh==1) { - fprintf(opt->log,"Exit requested by shell or user"LF); - } else { - fprintf(opt->log,"Exit requested by engine"LF); - } - test_flush; - } + if (opt->state.exit_xh==1) { + hts_log_print(opt, LOG_ERROR, "Exit requested by shell or user"); + } else { + hts_log_print(opt, LOG_ERROR, "Exit requested by engine"); + } ptr=lien_tot; } } while(ptrlog) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"No data seems to have been transfered during this session! : restoring previous one!"LF); - test_flush; - } + hts_log_print(opt, LOG_INFO, "No data seems to have been transfered during this session! : restoring previous one!"); XH_uninit; if ( (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"))) && (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"))) ) { remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")); @@ -2020,9 +1909,7 @@ jump_if_done: strcatbuff(file,line+1); file[strlen(file)-1]='\0'; if (fexist(file)) { // toujours sur disque: virer - if (opt->log) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Purging %s"LF,file); - } + hts_log_print(opt, LOG_INFO, "Purging %s",file); remove(file); purge=1; } } @@ -2044,7 +1931,7 @@ jump_if_done: while ((strnotempty(file)) && (rmdir(file)==0)) { // ok, éliminé (existait) purge=1; if (opt->log) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Purging directory %s/"LF,file); + hts_log_print(opt, LOG_INFO, "Purging directory %s/",file); while(strnotempty(file) && (file[strlen(file)-1]!='/') && (file[strlen(file)-1]!='\\')) { file[strlen(file)-1]='\0'; } @@ -2057,9 +1944,7 @@ jump_if_done: } // if (!purge) { - if (opt->log) { - fprintf(opt->log,"No files purged"LF); - } + hts_log_print(opt, LOG_INFO, "No files purged"); } } freet(adr); @@ -2135,8 +2020,6 @@ jump_if_done: zipClose(cache.zipOutput, finalInfo); cache.zipOutput = NULL; } - - test_flush; } #if DEBUG_HASH // noter les collisions @@ -2173,10 +2056,6 @@ jump_if_done: return 1; // OK } // version 2 pour le reste -// flusher si on doit lire peu à peu le fichier -#undef test_flush -#define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); } - // Estimate transfer rate // a little bit complex, but not too much @@ -2263,11 +2142,8 @@ void host_ban(httrackp* opt,lien_url** liens,int ptr,int lien_tot,struct_back* s opt->maxfilter += HTS_FILTERSINC; if (filters_init(&_FILTERS, opt->maxfilter, HTS_FILTERSINC) == 0) { printf("PANIC! : Too many filters : >%d [%d]\n",*_FILTERS_PTR,__LINE__); - if (opt->log) { - fprintf(opt->log,LF"Too many filters, giving up..(>%d)"LF,*_FILTERS_PTR); - fprintf(opt->log,"To avoid that: use #F option for more filters (example: -#F5000)"LF); - fflush(opt->log); - } + hts_log_print(opt, LOG_PANIC, "Too many filters, giving up..(>%d)",*_FILTERS_PTR); + hts_log_print(opt, LOG_INFO, "To avoid that: use #F option for more filters (example: -#F5000)"); assertf("too many filters - giving up" == NULL); } } @@ -2285,9 +2161,7 @@ void host_ban(httrackp* opt,lien_url** liens,int ptr,int lien_tot,struct_back* s if (strlen(host)<=1) { // euhh?? longueur <= 1 if (strcmp(host,"file://")) { //## if (host[0]!=lOCAL_CHAR) { // pas local - if (opt->log!=NULL) { - fprintf(opt->log,"PANIC! HostCancel detected memory leaks [char %d]"LF,host[0]); test_flush; - } + hts_log_print(opt, LOG_PANIC, "PANIC! HostCancel detected memory leaks [char %d]",host[0]); return; // purée } } @@ -2305,10 +2179,7 @@ void host_ban(httrackp* opt,lien_url** liens,int ptr,int lien_tot,struct_back* s back[i].r.soc=INVALID_SOCKET; back[i].r.statuscode=STATUSCODE_TIMEOUT; // timeout (peu importe si c'est un traffic jam) strcpybuff(back[i].r.msg,"Link Cancelled by host control"); - - if ((opt->debug>1) && (opt->log!=NULL)) { - fprintf(opt->log,"Shutdown: %s%s"LF,back[i].url_adr,back[i].url_fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Shutdown: %s%s",back[i].url_adr,back[i].url_fil); } } @@ -2323,9 +2194,7 @@ void host_ban(httrackp* opt,lien_url** liens,int ptr,int lien_tot,struct_back* s while((liens[i]->adr[l]) && (l<1020)) l++; if ((l > 0) && (l<1020)) { // sécurité if (strfield2(jump_identification(liens[i]->adr),host)) { // host - if ((opt->debug>1) && (opt->log!=NULL)) { - fprintf(opt->log,"Cancel: %s%s"LF,liens[i]->adr,liens[i]->fil); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Cancel: %s%s",liens[i]->adr,liens[i]->fil); strcpybuff(liens[i]->adr,"!"); // cancel (invalide hash) // on efface pas le hash, because si on rencontre le lien, reverif sav.. } @@ -2334,19 +2203,15 @@ void host_ban(httrackp* opt,lien_url** liens,int ptr,int lien_tot,struct_back* s char dmp[1040]; dmp[0]='\0'; strncatbuff(dmp,liens[i]->adr,1024); - fprintf(opt->log,"WARNING! HostCancel detected memory leaks [len %d at %d]"LF,l,i); test_flush; - fprintf(opt->log,"dump 1024 bytes (address %p): "LF"%s"LF,liens[i]->adr,dmp); test_flush; + hts_log_print(opt, LOG_WARNING, "WARNING! HostCancel detected memory leaks [len %d at %d]",l,i); + hts_log_print(opt, LOG_WARNING, "dump 1024 bytes (address %p): "LF"%s",liens[i]->adr,dmp); } } } else { - if (opt->log!=NULL) { - fprintf(opt->log,"WARNING! HostCancel detected memory leaks [adr at %d]"LF,i); test_flush; - } + hts_log_print(opt, LOG_WARNING, "WARNING! HostCancel detected memory leaks [adr at %d]",i); } } else { - if (opt->log!=NULL) { - fprintf(opt->log,"WARNING! HostCancel detected memory leaks [null at %d]"LF,i); test_flush; - } + hts_log_print(opt, LOG_WARNING, "WARNING! HostCancel detected memory leaks [null at %d]",i); } //} } @@ -2400,16 +2265,16 @@ static int mkdir_compat(const char *pathname) { } /* path must end with "/" or with the finename (/tmp/bar/ or /tmp/bar/foo.zip) */ +/* Note: preserve errno */ HTSEXT_API int dir_exists(const char* path) { + const int err = errno; STRUCT_STAT st; char BIGSTK file[HTS_URLMAXSIZE*2]; int i = 0; if (strnotempty(path) == 0) { - errno = EINVAL; return 0; } if (strlen(path) > HTS_URLMAXSIZE) { - errno = EINVAL; return 0; } @@ -2430,11 +2295,10 @@ HTSEXT_API int dir_exists(const char* path) { /* Check the final dir */ if (STAT(file, &st) == 0 && S_ISDIR(st.st_mode)) { - errno = 0; return 1; /* EXISTS */ } - errno = 0; - return 0; /* DOES NOT EXISTS */ + errno = err; + return 0; /* DOES NOT EXIST */ } /* path must end with "/" or with the finename (/tmp/bar/ or /tmp/bar/foo.zip) */ @@ -2814,7 +2678,7 @@ void file_notify(httrackp* opt,const char* adr,const char* fil,const char* save, // executer commande utilisateur static void postprocess_file(httrackp* opt, const char* save, const char* adr, const char* fil); -HTS_INLINE void usercommand(httrackp* opt,int _exe,const char* _cmd,const char* file,const char* adr,const char* fil) { +void usercommand(httrackp* opt,int _exe,const char* _cmd,const char* file,const char* adr,const char* fil) { usercommand_strc* strc = &opt->state.usercmd; /* Callback */ @@ -2904,9 +2768,7 @@ static void postprocess_file(httrackp* opt,const char* save, const char* adr, co opt->state.mimehtml_created = 1; } else { opt->state.mimehtml_created = -1; - if ( opt->log != NULL ) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"unable to create index.mht"LF); - } + hts_log_print(opt, LOG_ERROR, "unable to create index.mht"); } } if (opt->state.mimehtml_created == 1 && opt->state.mimefp != NULL) { @@ -2967,7 +2829,7 @@ static void postprocess_file(httrackp* opt,const char* save, const char* adr, co } // écrire n espaces dans fp -HTS_INLINE int fspc(httrackp *opt,FILE* fp,const char* type) { +int fspc(httrackp *opt,FILE* fp,const char* type) { fspc_strc* const strc = ( opt != NULL ) ? &opt->state.fspc : NULL; if (fp != NULL) { char s[256]; @@ -3146,10 +3008,7 @@ int back_fill(struct_back* sback,httrackp* opt,cache_back* cache,lien_url** lien if (ok) { if (!back_exist(sback, opt, liens[p]->adr,liens[p]->fil,liens[p]->sav)) { if (back_add(sback,opt,cache,liens[p]->adr,liens[p]->fil,liens[p]->sav,liens[liens[p]->precedent]->adr,liens[liens[p]->precedent]->fil,liens[p]->testmode)==-1) { - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"error: unable to add more links through back_add for back_fill"LF); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "error: unable to add more links through back_add for back_fill"); #if BDEBUG==1 printf("error while adding\n"); #endif @@ -3200,7 +3059,7 @@ int read_stdin(char* s,int max) { return i; } #ifdef _WIN32 -HTS_INLINE int check_stdin(void) { +int check_stdin(void) { #ifndef _WIN32_WCE return (_kbhit()); #else @@ -3208,7 +3067,7 @@ HTS_INLINE int check_stdin(void) { #endif } #else -HTS_INLINE int check_flot(T_SOC s) { +int check_flot(T_SOC s) { fd_set fds; struct timeval tv; FD_ZERO(&fds); @@ -3218,7 +3077,7 @@ HTS_INLINE int check_flot(T_SOC s) { select(s+1,&fds,NULL,NULL,&tv); return FD_ISSET(s,&fds); } -HTS_INLINE int check_stdin(void) { +int check_stdin(void) { fflush(stdout); fflush(stdin); if (check_flot(0)) return 1; @@ -3227,7 +3086,7 @@ HTS_INLINE int check_stdin(void) { #endif #endif -HTS_INLINE int check_sockerror(T_SOC s) { +int check_sockerror(T_SOC s) { fd_set fds; struct timeval tv; FD_ZERO(&fds); @@ -3239,7 +3098,7 @@ HTS_INLINE int check_sockerror(T_SOC s) { } /* check incoming data */ -HTS_INLINE int check_sockdata(T_SOC s) { +int check_sockdata(T_SOC s) { fd_set fds; struct timeval tv; FD_ZERO(&fds); @@ -3414,6 +3273,7 @@ HTSEXT_API int hts_setpause(httrackp *opt, int p) { // ask for termination HTSEXT_API int hts_request_stop(httrackp* opt, int force) { if (opt != NULL) { + hts_log_print(opt, LOG_ERROR, "Exit requested by shell or user"); opt->state.stop = 1; } return 0; @@ -3524,24 +3384,16 @@ int htsAddLink(htsmoduleStruct* str, char* link) { codebase[0]='\0'; - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(module): adding link : '%s'"LF, link); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(module): adding link : '%s'", link); // recopie de "creer le lien" // if (!RUN_CALLBACK1(opt, linkdetected, link)) { - if (opt->log) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Link %s refused by external wrapper"LF, link); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "Link %s refused by external wrapper", link); return 0; } if (!RUN_CALLBACK2(opt, linkdetected2, link, NULL)) { - if (opt->log) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Link %s refused by external wrapper(2)"LF, link); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "Link %s refused by external wrapper(2)", link); return 0; } @@ -3569,19 +3421,13 @@ int htsAddLink(htsmoduleStruct* str, char* link) { strcpybuff(tempo,a); strcpybuff(codebase,tempo); // couper host } else { - if (opt->log) { - fprintf(opt->log,"Unexpected strstr error in base %s"LF,codebase); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "Unexpected strstr error in base %s",codebase); } } } if (!((int) strlen(codebase)log) { - fprintf(opt->log,"Codebase too long, parsing skipped (%s)"LF,codebase); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "Codebase too long, parsing skipped (%s)",codebase); } { @@ -3600,10 +3446,7 @@ int htsAddLink(htsmoduleStruct* str, char* link) { NULL, NULL, &set_prio_to, &just_test_it); - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"result for wizard external module link: %d"LF,forbidden_url); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "result for wizard external module link: %d",forbidden_url); /* Link accepted */ if (!forbidden_url) { @@ -3633,12 +3476,9 @@ int htsAddLink(htsmoduleStruct* str, char* link) { if (r != -1 && !forbidden_url) { if (savename) { if (lienrelatif(tempo,save,savename)==0) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(module): relative link at %s build with %s and %s: %s"LF,adr,save,savename,tempo); - test_flush; - if (str->localLink && str->localLinkSize > (int) strlen(tempo) + 1) { - strcpybuff(str->localLink, tempo); - } + hts_log_print(opt, LOG_DEBUG, "(module): relative link at %s build with %s and %s: %s",adr,save,savename,tempo); + if (str->localLink && str->localLinkSize > (int) strlen(tempo) + 1) { + strcpybuff(str->localLink, tempo); } } } @@ -3646,9 +3486,7 @@ int htsAddLink(htsmoduleStruct* str, char* link) { } if (forbidden_url) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(module): file not caught: %s"LF,lien); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(module): file not caught: %s",lien); if (str->localLink && str->localLinkSize > (int) ( strlen(adr) + strlen(fil) + 8 ) ) { str->localLink[0] = '\0'; if (!link_has_authority(adr)) @@ -3661,9 +3499,7 @@ int htsAddLink(htsmoduleStruct* str, char* link) { // if (r != -1) { - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(module): %s%s -> %s (base %s)"LF,adr,fil,save,codebase); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(module): %s%s -> %s (base %s)",adr,fil,save,codebase); // modifié par rapport à l'autre version (cf prio_fix notamment et save2) @@ -3688,10 +3524,7 @@ int htsAddLink(htsmoduleStruct* str, char* link) { liens_record(adr,fil,save,"","",opt->urlhack); if (liens[lien_tot]==NULL) { // erreur, pas de place réservée printf("PANIC! : Not enough memory [%d]\n",__LINE__); - if (opt->log) { - fprintf(opt->log,"Not enough memory, can not re-allocate %d bytes"LF,(int)((add_tab_alloc+1)*sizeof(lien_url))); - test_flush; - } + hts_log_print(opt, LOG_PANIC, "Not enough memory, can not re-allocate %d bytes",(int)((add_tab_alloc+1)*sizeof(lien_url))); opt->state.exit_xh=-1; /* fatal error -> exit */ return 0; } @@ -3719,10 +3552,7 @@ int htsAddLink(htsmoduleStruct* str, char* link) { //strcpybuff(liens[lien_tot]->adr,adr); //strcpybuff(liens[lien_tot]->fil,fil); //strcpybuff(liens[lien_tot]->sav,save); - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(module): OK, NOTE: %s%s -> %s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil,liens[lien_tot]->sav); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(module): OK, NOTE: %s%s -> %s",liens[lien_tot]->adr,liens[lien_tot]->fil,liens[lien_tot]->sav); lien_tot++; // UN LIEN DE PLUS } diff --git a/src/htscore.h b/src/htscore.h index 2272327..8f07cf2 100644 --- a/src/htscore.h +++ b/src/htscore.h @@ -335,7 +335,7 @@ FILE* fileappend(filenote_strc *strct,const char* s); int filecreateempty(filenote_strc *strct, const char* filename); int filenote(filenote_strc *strct,const char* s,filecreate_params* params); void file_notify(httrackp* opt,const char* adr,const char* fil,const char* save,int create,int modify,int wasupdated); -HTS_INLINE void usercommand(httrackp* opt,int exe,const char* cmd,const char* file,const char* adr,const char* fil); +void usercommand(httrackp* opt,int exe,const char* cmd,const char* file,const char* adr,const char* fil); void usercommand_exe(const char* cmd,const char* file); int filters_init(char*** ptrfilters, int maxfilter, int filterinc); #ifndef HTTRACK_DEFLIB @@ -343,7 +343,7 @@ HTSEXT_API int structcheck(const char* path); HTSEXT_API int structcheck_utf8(const char* path); HTSEXT_API int dir_exists(const char* path); #endif -HTS_INLINE int fspc(httrackp *opt,FILE* fp,const char* type); +int fspc(httrackp *opt,FILE* fp,const char* type); char* next_token(char* p,int flag); // char* readfile(char* fil); @@ -382,12 +382,12 @@ int hts_add_file(char* file,int file_position); // Polling #if HTS_POLL -HTS_INLINE int check_flot(T_SOC s); -HTS_INLINE int check_stdin(void); +int check_flot(T_SOC s); +int check_stdin(void); int read_stdin(char* s,int max); #endif -HTS_INLINE int check_sockerror(T_SOC s); -HTS_INLINE int check_sockdata(T_SOC s); +int check_sockerror(T_SOC s); +int check_sockdata(T_SOC s); /* external modules */ int htsAddLink(htsmoduleStruct* str, char* link); diff --git a/src/htscoremain.c b/src/htscoremain.c index 75c34fe..53baa56 100644 --- a/src/htscoremain.c +++ b/src/htscoremain.c @@ -2141,7 +2141,7 @@ HTSEXT_API int hts_main2(int argc, char **argv, httrackp *opt) { } // fichier log - if (opt->log) { + if (opt->log) { int i; fprintf(opt->log,"HTTrack"HTTRACK_VERSION"%s launched on %s at %s"LF, hts_get_version_info(opt), @@ -2186,32 +2186,22 @@ HTSEXT_API int hts_main2(int argc, char **argv, httrackp *opt) { if (!opt->bypass_limits) { if (opt->maxsoc <= 0 || opt->maxsoc > 8) { opt->maxsoc = 8; - if (opt->log != NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: maximum number of simultaneous connections limited to %d to avoid server overload"LF, (int)opt->maxsoc); - } + hts_log_print(opt, LOG_WARNING, "* security warning: maximum number of simultaneous connections limited to %d to avoid server overload", (int)opt->maxsoc); } if (opt->maxrate <= 0 || opt->maxrate > 250000) { opt->maxrate = 250000; - if (opt->log != NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: maximum bandwidth limited to %d to avoid server overload"LF, (int)opt->maxrate); - } + hts_log_print(opt, LOG_WARNING, "* security warning: maximum bandwidth limited to %d to avoid server overload", (int)opt->maxrate); } if (opt->maxconn <= 0 || opt->maxconn > 5.0) { opt->maxconn = 5.0; - if (opt->log != NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: maximum number of connections per second limited to %f to avoid server overload"LF, (float)opt->maxconn); - } + hts_log_print(opt, LOG_WARNING, "* security warning: maximum number of connections per second limited to %f to avoid server overload", (float)opt->maxconn); } } else { - if (opt->log != NULL) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: !!! BYPASSING SECURITY LIMITS - MONITOR THIS SESSION WITH EXTREME CARE !!!"LF); - } + hts_log_print(opt, LOG_WARNING, "* security warning: !!! BYPASSING SECURITY LIMITS - MONITOR THIS SESSION WITH EXTREME CARE !!!"); } /* Info for wrappers */ - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: init"LF); - } + hts_log_print(opt, LOG_INFO, "engine: init"); /* Init external */ RUN_CALLBACK_NOARG(opt, init); @@ -2252,9 +2242,7 @@ HTSEXT_API int hts_main2(int argc, char **argv, httrackp *opt) { if (a) { *a='\0'; hts_buildtopindex(opt,rpath,StringBuff(opt->path_bin)); - if (opt->log) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Top index rebuilt (done)"LF); - } + hts_log_print(opt, LOG_INFO, "Top index rebuilt (done)"); } } @@ -2286,9 +2274,7 @@ HTSEXT_API int hts_main2(int argc, char **argv, httrackp *opt) { } /* Info for wrappers */ - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: free"LF); - } + hts_log_print(opt, LOG_INFO, "engine: free"); /* UnInit */ RUN_CALLBACK_NOARG(opt, uninit); diff --git a/src/htslib.c b/src/htslib.c index 1a95a67..8b20879 100644 --- a/src/htslib.c +++ b/src/htslib.c @@ -76,6 +76,7 @@ Please visit our Website: http://www.httrack.com #include #include +#include #ifndef _WIN32_WCE #include @@ -4346,29 +4347,39 @@ void fprintfio(FILE* fp,char* buff,char* prefix) { /* Le fichier existe-t-il? (ou est-il accessible?) */ /* Note: NOT utf-8 */ +/* Note: preserve errno */ int fexist(const char* s) { char catbuff[CATBUFF_SIZE]; + const int err = errno; struct stat st; memset(&st, 0, sizeof(st)); if (stat(fconv(catbuff,s), &st) == 0) { if (S_ISREG(st.st_mode)) { return 1; + } else { + return 0; } } + errno = err; return 0; } /* Le fichier existe-t-il? (ou est-il accessible?) */ /* Note: utf-8 */ +/* Note: preserve errno */ int fexist_utf8(const char* s) { char catbuff[CATBUFF_SIZE]; + const int err = errno; STRUCT_STAT st; memset(&st, 0, sizeof(st)); if (STAT(fconv(catbuff,s), &st) == 0) { if (S_ISREG(st.st_mode)) { return 1; + } else { + return 0; } } + errno = err; return 0; } @@ -5181,6 +5192,7 @@ HTSEXT_API int hts_uninit_module(void) { return 1; } +// legacy. do not use HTSEXT_API int hts_log(httrackp *opt, const char* prefix, const char *msg) { if (opt->log != NULL) { fspc(opt, opt->log, prefix); @@ -5190,6 +5202,58 @@ HTSEXT_API int hts_log(httrackp *opt, const char* prefix, const char *msg) { return 1; /* Error */ } +HTSEXT_API void hts_log_print(httrackp *opt, int type, const char *format, ...) { + assertf(format != NULL); + if (opt != NULL && opt->log != NULL) { + va_list args; + const int save_errno = errno; + const char *s_type = "unknown"; + switch(type & 0xff) { + case LOG_DEBUG: + // check verbosity + if (opt->debug < 2) { + return ; + } + s_type = "debug"; + break; + case LOG_INFO: + // check verbosity + if (opt->debug < 1) { + return ; + } + s_type = "info"; + break; + case LOG_NOTICE: + // check verbosity + if (opt->debug < 1) { + return ; + } + /* nobreak; */ + case LOG_WARNING: + s_type = "warning"; + break; + case LOG_ERROR: + s_type = "error"; + break; + case LOG_PANIC: + s_type = "panic"; + break; + } + fspc(opt, opt->log, s_type); + va_start(args, format); + (void) vfprintf(opt->log, format, args); + va_end(args); + if ((type & LOG_ERRNO) != 0) { + fprintf(opt->log, ": %s", strerror(save_errno)); + } + fputs(LF, opt->log); + if (opt->flush) { + fflush(opt->log); + } + errno = save_errno; + } +} + HTSEXT_API void set_wrappers(httrackp *opt) { // LEGACY } diff --git a/src/htslib.h b/src/htslib.h index a676af6..cb93367 100644 --- a/src/htslib.h +++ b/src/htslib.h @@ -399,6 +399,7 @@ HTSEXT_API char* fconv(char *catbuff, const char* a); char* fslash(char *catbuff, const char* a); #ifndef HTTRACK_DEFLIB HTSEXT_API int hts_log(httrackp *opt, const char* prefix, const char *msg); +HTSEXT_API void hts_log_print(httrackp *opt, int type, const char *format, ...) HTS_PRINTF_FUN(3, 4); #endif char* convtolower(char *catbuff, const char* a); diff --git a/src/htsmodules.c b/src/htsmodules.c index ea75a7f..4c37047 100644 --- a/src/htsmodules.c +++ b/src/htsmodules.c @@ -362,10 +362,8 @@ void htspe_uninit(void) { static void htspe_log(htsmoduleStruct* str, const char* msg) { const char* savename = str->filename; httrackp* opt = (httrackp*) str->opt; - if ((opt->debug>1) && (opt->log!=NULL)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"(External module): parsing %s using module %s"LF, - savename, msg); - } + hts_log_print(opt, LOG_DEBUG, "(External module): parsing %s using module %s", + savename, msg); } HTSEXT_API const char* hts_is_available(void) { diff --git a/src/htsname.c b/src/htsname.c index 16901a3..bfae34a 100644 --- a/src/htsname.c +++ b/src/htsname.c @@ -46,9 +46,6 @@ Please visit our Website: http://www.httrack.com #include "htscharset.h" #include -#undef test_flush -#define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); } - #define ADD_STANDARD_PATH \ { /* ajout nom */\ char BIGSTK buff[HTS_URLMAXSIZE*2];\ @@ -384,10 +381,7 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, htsblk r = cache_read_including_broken(opt,cache,adr,fil); // test uniquement if (r.statuscode != -1) { // pas d'erreur de lecture cache char s[32]; s[0]='\0'; - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link type (from cache) %s%s"LF,adr_complete,fil_complete); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Testing link type (from cache) %s%s",adr_complete,fil_complete); if (strnotempty(r.cdispo)) { /* filename given */ ext_chg=2; /* change filename */ strcpybuff(ext,r.cdispo); @@ -472,11 +466,8 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, /* Rock'in */ curr_adr[0]=curr_fil[0]='\0'; - opt->state. _hts_in_html_parsing=2; // test - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link type %s%s"LF,adr_complete,fil_complete); - test_flush; - } + opt->state._hts_in_html_parsing=2; // test + hts_log_print(opt, LOG_DEBUG, "Testing link type %s%s",adr_complete,fil_complete); strcpybuff(curr_adr,adr_complete); strcpybuff(curr_fil,fil_complete); // ajouter dans le backing le fichier en mode test @@ -576,18 +567,12 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, methode=BACK_ADD_TEST; // tester avec HEAD else { methode=BACK_ADD_TEST2; // tester avec GET - if ( opt->log!=NULL ) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Loop with HEAD request (during prefetch) at %s%s"LF,curr_adr,curr_fil); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "Loop with HEAD request (during prefetch) at %s%s",curr_adr,curr_fil); } // Ajouter URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET(); if (back_add(sback,opt,cache,mov_adr,mov_fil,methode,referer_adr,referer_fil,1)!=-1) { // OK - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"(during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "(during prefetch) %s (%d) to link %s at %s%s",back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil); // libérer emplacement backing actuel et attendre le prochain back_maydelete(opt,cache,sback,b); @@ -599,26 +584,17 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, petits_tours++; // } else {// sinon on fait rien et on s'en va.. (ftp etc) - if ( (opt->debug>1) && (opt->log)) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Warning: Savename redirect backing error at %s%s"LF,mov_adr,mov_fil); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Warning: Savename redirect backing error at %s%s",mov_adr,mov_fil); } } } else { - if ( opt->log!=NULL ) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop to same filename)"LF,adr_complete,fil_complete); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "Unable to test %s%s (loop to same filename)",adr_complete,fil_complete); } } } } else{ // arrêter les frais - if ( opt->log!=NULL ) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop)"LF,adr_complete,fil_complete); - test_flush; - } + hts_log_print(opt, LOG_WARNING, "Unable to test %s%s (loop)",adr_complete,fil_complete); } } // ok, leaving } @@ -631,13 +607,6 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, strcpybuff(back[b].r.contenttype,"text/html"); // message d'erreur en html // Finalement on, renvoie un erreur, pour ne toucher à rien dans le code // libérer emplacement backing - /*if (opt->log!=NULL) { - fspc(opt->log,0); fprintf(opt->log,"Error: (during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil); - test_flush; - } - back_delete(opt,cache,sback,b); - return -1; // ERREUR (404 par exemple) - */ } { // pas d'erreur, changer type? @@ -687,10 +656,7 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, #if BDEBUG==1 printf("error while savename crash adding\n"); #endif - if (opt->log) { - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unexpected savename backing error at %s%s"LF,adr,fil_complete); - test_flush; - } + hts_log_print(opt, LOG_ERROR, "Unexpected savename backing error at %s%s",adr,fil_complete); } // restaurer @@ -729,12 +695,10 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, if (ext_chg) { // changer ext char* a=fil+strlen(fil)-1; if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); if (ext_chg==1) - fprintf(opt->log,"Changing link extension %s%s to .%s"LF,adr_complete,fil_complete,ext); + hts_log_print(opt, LOG_DEBUG, "Changing link extension %s%s to .%s",adr_complete,fil_complete,ext); else - fprintf(opt->log,"Changing link name %s%s to %s"LF,adr_complete,fil_complete,ext); - test_flush; + hts_log_print(opt, LOG_DEBUG, "Changing link name %s%s to %s",adr_complete,fil_complete,ext); } if (ext_chg==1) { while((a > fil) && (*a!='.') && (*a!='/')) a--; @@ -1196,10 +1160,7 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, if (*a!='.') { // agh pas de point //strcatbuff(save,".none"); // a éviter strcatbuff(save,".html"); // préférable! - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Default HTML type set for %s%s => %s"LF,adr_complete,fil_complete,save); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Default HTML type set for %s%s => %s",adr_complete,fil_complete,save); } } @@ -1348,9 +1309,7 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, if (charset != NULL && charset[0] != '\0') { char *const s = hts_convertStringToUTF8(save, (int) strlen(save), charset); if (s != NULL) { - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: save-name: charset conversion from '%s' to '%s'"LF, save, s); - } + hts_log_print(opt, LOG_DEBUG, "engine: save-name: charset conversion from '%s' to '%s'", save, s); strcpybuff(save, s); free(s); } @@ -1359,10 +1318,7 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, /* callback */ RUN_CALLBACK5(opt, savename, adr_complete,fil_complete,referer_adr,referer_fil,save); - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: save-name: local name: %s%s -> %s"LF,adr,fil,save); - test_flush; - } + hts_log_print(opt, LOG_INFO, "engine: save-name: local name: %s%s -> %s",adr,fil,save); /* Ensure that the MANDATORY "temporary" extension is set */ if (ext_chg_delayed) { @@ -1421,10 +1377,7 @@ int url_savename2(char* adr_complete, char* fil_complete, char* save, // terminating \0 save[sofar + j] = '\0'; // log in debug - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log, "Too long filename shortened: %s%s => %s"LF, adr_complete, fil_complete, save); - test_flush; - } + hts_log_print(opt, LOG_DEBUG, "Too long filename shortened: %s%s => %s", adr_complete, fil_complete, save); } #undef MAX_UTF8_SEQ_CHARS #undef MIN_LAST_SEG_RESERVE @@ -1635,5 +1588,3 @@ void url_savename_refname_remove(httrackp* opt, const char *adr, const char *fil char *filename = url_savename_refname_fullpath(opt, adr, fil); (void) UNLINK(filename); } - -#undef test_flush diff --git a/src/htsopt.h b/src/htsopt.h index 9ad8bf7..dfefed7 100644 --- a/src/htsopt.h +++ b/src/htsopt.h @@ -167,6 +167,19 @@ struct fspc_strc { int info; }; +#ifndef HTS_DEF_DEFSTRUCT_hts_log_type +#define HTS_DEF_DEFSTRUCT_hts_log_type +typedef enum hts_log_type { + LOG_DEBUG, + LOG_INFO, + LOG_NOTICE, + LOG_WARNING, + LOG_ERROR, + LOG_PANIC, + LOG_ERRNO = 1 << 8 +} hts_log_type; +#endif + /* Structure état du miroir */ #ifndef HTS_DEF_FWSTRUCT_htsoptstatecancel #define HTS_DEF_FWSTRUCT_htsoptstatecancel diff --git a/src/htsparse.c b/src/htsparse.c index 41d1797..5a25d88 100644 --- a/src/htsparse.c +++ b/src/htsparse.c @@ -77,8 +77,6 @@ Please visit our Website: http://www.httrack.com #define relativeurlfil ((!parent_relative)?urlfil:parenturlfil) #define relativesavename ((!parent_relative)?savename:parentsavename) -#define test_flush if (opt->flush) { if (opt->log) { fflush(opt->log); } if (opt->log) { fflush(opt->log); } } - // does nothing #define XH_uninit do {} while(0) @@ -163,10 +161,7 @@ Please visit our Website: http://www.httrack.com mbuff[mlen]='\0';\ if ((mlen == 32) && (strcmp(((mbuff!=NULL)?mbuff:""),digest)==0)) {\ ok=1;\ - if ( (opt->debug>1) && (opt->log!=NULL) ) {\ - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"File not re-written (md5): %s"LF,savename);\ - test_flush;\ - }\ + hts_log_print(opt, LOG_DEBUG, "File not re-written (md5): %s",savename);\ } else {\ ok=0;\ } \ @@ -182,13 +177,10 @@ Please visit our Website: http://www.httrack.com opt->state.exit_xh=-1;\ }\ if (opt->log) { \ - int last_errno = errno; \ - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unable to write HTML file %s: %s"LF, savename, strerror(last_errno));\ + hts_log_print(opt, LOG_ERROR | LOG_ERRNO, "Unable to write HTML file %s", savename);\ if (fcheck) {\ - HTS_LOG(opt,LOG_ERROR);\ - fprintf(opt->log,"* * Fatal write error, giving up"LF);\ + hts_log_print(opt, LOG_ERROR, "* * Fatal write error, giving up");\ }\ - test_flush;\ }\ }\ }\ @@ -198,19 +190,12 @@ Please visit our Website: http://www.httrack.com } else {\ int fcheck;\ if ((fcheck=check_fatal_io_errno())) {\ - HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Mirror aborted: disk full or filesystem problems"LF); \ - test_flush; \ + hts_log_print(opt, LOG_ERROR, "Mirror aborted: disk full or filesystem problems"); \ opt->state.exit_xh=-1;\ }\ - if (opt->log) { \ - int last_errno = errno; \ - HTS_LOG(opt,LOG_ERROR);\ - fprintf(opt->log,"Unable to save file %s : %s"LF, savename, strerror(last_errno));\ - if (fcheck) {\ - HTS_LOG(opt,LOG_ERROR);\ - fprintf(opt->log,"* * Fatal write error, giving up"LF);\ - }\ - test_flush;\ + hts_log_print(opt, LOG_ERROR | LOG_ERRNO, "Unable to save file %s", savename);\ + if (fcheck) {\ + hts_log_print(opt, LOG_ERROR, "* * Fatal write error, giving up");\ }\ }\ } else {\ @@ -413,9 +398,7 @@ int htsparse(htsmoduleStruct* str, htsmoduleStructExtended* stre) { { char* cAddr = r->adr; int cSize = (int) r->size; - if ( (opt->debug>0) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: preprocess-html: %s%s"LF, urladr, urlfil); - } + hts_log_print(opt, LOG_DEBUG, "engine: preprocess-html: %s%s", urladr, urlfil); if (RUN_CALLBACK4(opt, preprocess, &cAddr, &cSize, urladr, urlfil) == 1) { r->adr = cAddr; r->size = cSize; @@ -425,22 +408,15 @@ int htsparse(htsmoduleStruct* str, htsmoduleStructExtended* stre) { FILE* fp=NULL; // fichier écrit localement char* adr=r->adr; // pointeur (on parcourt) char* lastsaved; // adresse du dernier octet sauvé + 1 - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"scanning file %s%s (%s).."LF, urladr, urlfil, savename); test_flush; - } - + hts_log_print(opt, LOG_DEBUG, "scanning file %s%s (%s)..", urladr, urlfil, savename); // Indexing! #if HTS_MAKE_KEYWORD_INDEX if (opt->kindex) { if (index_keyword(r->adr,r->size,r->contenttype,savename,StringBuff(opt->path_html_utf8))) { - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"indexing file..done"LF); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "indexing file..done"); } else { - if ( (opt->debug>1) && (opt->log!=NULL) ) { - HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"indexing file..error!"LF); test_flush; - } + hts_log_print(opt, LOG_DEBUG, "indexing file..error!"); } } #endif @@ -545,9 +521,7 @@ int htsparse(htsmoduleStruct* str, htsmoduleStructExtended* stre) { inscript_name="script"; intag=1; // because après