improved debug messages

This commit is contained in:
Fufu Fang 2021-08-22 02:26:09 +01:00
parent 67ec1ad7e5
commit 6d5267089f
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
10 changed files with 272 additions and 189 deletions

View File

@ -1,5 +1,6 @@
#include "cache.h"
#include "config.h"
#include "log.h"
#include <sys/stat.h>
@ -52,13 +53,13 @@ static char *CacheSystem_calc_dir(const char *url)
}
if (mkdir(xdg_cache_home, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_calc_dir(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n",
strerror(errno));
}
char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/");
if (mkdir(cache_dir_root, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_calc_dir(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n",
strerror(errno));
}
@ -71,15 +72,15 @@ static char *CacheSystem_calc_dir(const char *url)
# For information about cache directory tags, see:\n\
# http://www.brynosaurus.com/cachedir/\n");
} else {
fprintf(stderr, "CacheSystem_calc_dir(): fopen(%s): %s", fn,
lprintf(debug, "CacheSystem_calc_dir(): fopen(%s): %s", fn,
strerror(errno));
}
if (ferror(fp)) {
fprintf(stderr,
lprintf(debug,
"CacheSystem_calc_dir(): fwrite(): encountered error!\n");
}
if (fclose(fp)) {
fprintf(stderr, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn,
lprintf(debug, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn,
strerror(errno));
}
CURL* c = curl_easy_init();
@ -87,7 +88,7 @@ static char *CacheSystem_calc_dir(const char *url)
char *full_path = path_append(cache_dir_root, escaped_url);
if (mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_calc_dir(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n",
strerror(errno));
}
free(fn);
@ -100,7 +101,7 @@ static char *CacheSystem_calc_dir(const char *url)
void CacheSystem_init(const char *path, int url_supplied)
{
if (pthread_mutex_init(&cf_lock, NULL) != 0) {
fprintf(stderr,
lprintf(debug,
"CacheSystem_init(): cf_lock initialisation failed!\n");
exit_failure();
}
@ -109,12 +110,12 @@ void CacheSystem_init(const char *path, int url_supplied)
path = CacheSystem_calc_dir(path);
}
fprintf(stderr, "CacheSystem_init(): directory: %s\n", path);
lprintf(debug, "CacheSystem_init(): directory: %s\n", path);
DIR* dir;
dir = opendir(path);
if (!dir) {
fprintf(stderr,
lprintf(debug,
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
exit_failure();
}
@ -124,14 +125,14 @@ void CacheSystem_init(const char *path, int url_supplied)
/* Check if directories exist, if not, create them */
if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
strerror(errno));
exit_failure();
}
if (mkdir(DATA_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
strerror(errno));
exit_failure();
}
@ -142,7 +143,7 @@ void CacheSystem_init(const char *path, int url_supplied)
sonic_path = path_append(META_DIR, "rest/");
if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
strerror(errno));
exit_failure();
}
@ -152,7 +153,7 @@ void CacheSystem_init(const char *path, int url_supplied)
sonic_path = path_append(DATA_DIR, "rest/");
if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
strerror(errno));
exit_failure();
}
@ -178,7 +179,7 @@ static int Meta_read(Cache *cf)
if (!fp) {
/* The metadata file does not exist */
fprintf(stderr, "Meta_read(): fopen(): %s\n", strerror(errno));
lprintf(debug, "Meta_read(): fopen(): %s\n", strerror(errno));
return EFREAD;
}
@ -189,25 +190,25 @@ static int Meta_read(Cache *cf)
/* Error checking for fread */
if (ferror(fp)) {
fprintf(stderr,
lprintf(debug,
"Meta_read(): error reading core metadata!\n");
}
/* These things really should not be zero!!! */
if (!cf->content_length || !cf->blksz || !cf->segbc) {
fprintf(stderr,
lprintf(debug,
"Meta_read(): corrupt metadata: %s, content_length: %ld, \
blksz: %d, segbc: %ld\n", cf->path, cf->content_length, cf->blksz, cf->segbc);
return EZERO;
}
if (cf->blksz != CONFIG.data_blksz) {
fprintf(stderr, "Meta_read(): Warning: cf->blksz != CONFIG.data_blksz\n");
lprintf(debug, "Meta_read(): Warning: cf->blksz != CONFIG.data_blksz\n");
}
/* Allocate some memory for the segment */
if (cf->segbc > CONFIG.max_segbc) {
fprintf(stderr, "Meta_read(): Error: segbc: %ld\n", cf->segbc);
lprintf(debug, "Meta_read(): Error: segbc: %ld\n", cf->segbc);
return EMEM;
}
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
@ -218,21 +219,21 @@ blksz: %d, segbc: %ld\n", cf->path, cf->content_length, cf->blksz, cf->segbc);
/* We shouldn't have gone past the end of the file */
if (feof(fp)) {
/* reached EOF */
fprintf(stderr,
lprintf(debug,
"Meta_read(): attempted to read past the end of the file!\n");
return EINCONSIST;
}
/* Error checking for fread */
if (ferror(fp)) {
fprintf(stderr,
lprintf(debug,
"Meta_read(): error reading bitmap!\n");
return EFREAD;
}
/* Check for inconsistent metadata file */
if (nmemb != cf-> segbc) {
fprintf(stderr,
lprintf(debug,
"Meta_read(): corrupted metadata!\n");
return EINCONSIST;
}
@ -253,13 +254,13 @@ static int Meta_write(Cache *cf)
if (!fp) {
/* Cannot create the metadata file */
fprintf(stderr, "Meta_write(): fopen(): %s\n", strerror(errno));
lprintf(debug, "Meta_write(): fopen(): %s\n", strerror(errno));
return -1;
}
/* These things really should not be zero!!! */
if (!cf->content_length || !cf->blksz || !cf->segbc) {
fprintf(stderr,
lprintf(debug,
"Meta_write(): Warning: content_length: %ld, blksz: %d, segbc: \
%ld\n", cf->content_length, cf->blksz, cf->segbc);
}
@ -274,7 +275,7 @@ static int Meta_write(Cache *cf)
/* Error checking for fwrite */
if (ferror(fp)) {
fprintf(stderr,
lprintf(debug,
"Meta_write(): fwrite(): encountered error!\n");
return -1;
}
@ -300,14 +301,14 @@ static int Data_create(Cache *cf)
fd = open(datafn, O_WRONLY | O_CREAT, mode);
free(datafn);
if (fd == -1) {
fprintf(stderr, "Data_create(): open(): %s\n", strerror(errno));
lprintf(debug, "Data_create(): open(): %s\n", strerror(errno));
return -1;
}
if (ftruncate(fd, cf->content_length)) {
fprintf(stderr, "Data_create(): ftruncate(): %s\n", strerror(errno));
lprintf(debug, "Data_create(): ftruncate(): %s\n", strerror(errno));
}
if (close(fd)) {
fprintf(stderr, "Data_create(): close:(): %s\n", strerror(errno));
lprintf(debug, "Data_create(): close:(): %s\n", strerror(errno));
}
return 0;
}
@ -324,7 +325,7 @@ static long Data_size(const char *fn)
if (!s) {
return st.st_size;
}
fprintf(stderr, "Data_size(): stat(): %s\n", strerror(errno));
lprintf(debug, "Data_size(): stat(): %s\n", strerror(errno));
return -1;
}
@ -341,12 +342,12 @@ static long Data_size(const char *fn)
static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
{
if (len == 0) {
fprintf(stderr, "Data_read(): requested to read 0 byte!\n");
lprintf(debug, "Data_read(): requested to read 0 byte!\n");
return -EINVAL;
}
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Data_read(): thread %lu: locking seek_lock;\n",
lprintf(debug, "Data_read(): thread %lu: locking seek_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
@ -355,7 +356,7 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
if (fseeko(cf->dfp, offset, SEEK_SET)) {
/* fseeko failed */
fprintf(stderr, "Data_read(): fseeko(): %s\n", strerror(errno));
lprintf(debug, "Data_read(): fseeko(): %s\n", strerror(errno));
byte_read = -EIO;
goto end;
}
@ -369,24 +370,24 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp);
if (byte_read != len) {
fprintf(stderr,
lprintf(debug,
"Data_read(): fread(): requested %ld, returned %ld!\n",
len, byte_read);
if (feof(cf->dfp)) {
/* reached EOF */
fprintf(stderr,
lprintf(debug,
"Data_read(): fread(): reached the end of the file!\n");
}
if (ferror(cf->dfp)) {
/* filesystem error */
fprintf(stderr,
lprintf(debug,
"Data_read(): fread(): encountered error!\n");
}
}
end:
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Data_read(): thread %lu: unlocking seek_lock;\n",
lprintf(debug, "Data_read(): thread %lu: unlocking seek_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
@ -409,12 +410,12 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
off_t offset)
{
if (len == 0) {
fprintf(stderr, "Data_write(): requested to write 0 byte!\n");
lprintf(debug, "Data_write(): requested to write 0 byte!\n");
return -EINVAL;
}
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Data_write(): thread %lu: locking seek_lock;\n",
lprintf(debug, "Data_write(): thread %lu: locking seek_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
@ -423,27 +424,27 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
if (fseeko(cf->dfp, offset, SEEK_SET)) {
/* fseeko failed */
fprintf(stderr, "Data_write(): fseeko(): %s\n", strerror(errno));
lprintf(debug, "Data_write(): fseeko(): %s\n", strerror(errno));
byte_written = -EIO;
goto end;
}
byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp);
if (byte_written != len) {
fprintf(stderr,
lprintf(debug,
"Data_write(): fwrite(): requested %ld, returned %ld!\n",
len, byte_written);
exit_failure();
if (ferror(cf->dfp)) {
/* filesystem error */
fprintf(stderr,
lprintf(debug,
"Data_write(): fwrite(): encountered error!\n");
}
}
end:
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Data_write(): thread %lu: unlocking seek_lock;\n",
lprintf(debug, "Data_write(): thread %lu: unlocking seek_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
@ -458,12 +459,12 @@ int CacheDir_create(const char *dirn)
i = -mkdir(metadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (i && (errno != EEXIST)) {
fprintf(stderr, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
lprintf(debug, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
}
i |= -mkdir(datadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) << 1;
if (i && (errno != EEXIST)) {
fprintf(stderr, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
lprintf(debug, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
}
free(datadirn);
free(metadirn);
@ -478,25 +479,25 @@ static Cache *Cache_alloc()
Cache *cf = CALLOC(1, sizeof(Cache));
if (pthread_mutex_init(&cf->seek_lock, NULL)) {
fprintf(stderr, "Cache_alloc(): seek_lock initialisation failed!\n");
lprintf(debug, "Cache_alloc(): seek_lock initialisation failed!\n");
}
if (pthread_mutex_init(&cf->w_lock, NULL)) {
fprintf(stderr, "Cache_alloc(): w_lock initialisation failed!\n");
lprintf(debug, "Cache_alloc(): w_lock initialisation failed!\n");
}
if (pthread_mutexattr_init(&cf->bgt_lock_attr)) {
fprintf(stderr,
lprintf(debug,
"Cache_alloc(): bgt_lock_attr initialisation failed!\n");
}
if (pthread_mutexattr_setpshared(&cf->bgt_lock_attr,
PTHREAD_PROCESS_SHARED)) {
fprintf(stderr, "Cache_alloc(): could not set bgt_lock_attr!\n");
lprintf(debug, "Cache_alloc(): could not set bgt_lock_attr!\n");
}
if (pthread_mutex_init(&cf->bgt_lock, &cf->bgt_lock_attr)) {
fprintf(stderr, "Cache_alloc(): bgt_lock initialisation failed!\n");
lprintf(debug, "Cache_alloc(): bgt_lock initialisation failed!\n");
}
return cf;
@ -508,19 +509,19 @@ static Cache *Cache_alloc()
static void Cache_free(Cache *cf)
{
if (pthread_mutex_destroy(&cf->seek_lock)) {
fprintf(stderr, "Cache_free(): could not destroy seek_lock!\n");
lprintf(debug, "Cache_free(): could not destroy seek_lock!\n");
}
if (pthread_mutex_destroy(&cf->w_lock)) {
fprintf(stderr, "Cache_free(): could not destroy w_lock!\n");
lprintf(debug, "Cache_free(): could not destroy w_lock!\n");
}
if (pthread_mutex_destroy(&cf->bgt_lock)) {
fprintf(stderr, "Cache_free(): could not destroy bgt_lock!\n");
lprintf(debug, "Cache_free(): could not destroy bgt_lock!\n");
}
if (pthread_mutexattr_destroy(&cf->bgt_lock_attr)) {
fprintf(stderr, "Cache_alloc(): could not destroy bgt_lock_attr!\n");
lprintf(debug, "Cache_alloc(): could not destroy bgt_lock_attr!\n");
}
if (cf->path) {
@ -566,13 +567,13 @@ static int Cache_exist(const char *fn)
if (meta_exists ^ data_exists) {
if (meta_exists) {
if(unlink(metafn)) {
fprintf(stderr, "Cache_exist(): unlink(): %s\n",
lprintf(debug, "Cache_exist(): unlink(): %s\n",
strerror(errno));
}
}
if (data_exists) {
if(unlink(datafn)) {
fprintf(stderr, "Cache_exist(): unlink(): %s\n",
lprintf(debug, "Cache_exist(): unlink(): %s\n",
strerror(errno));
}
}
@ -598,14 +599,14 @@ void Cache_delete(const char *fn)
char *datafn = path_append(DATA_DIR, fn);
if (!access(metafn, F_OK)) {
if(unlink(metafn)) {
fprintf(stderr, "Cache_delete(): unlink(): %s\n",
lprintf(debug, "Cache_delete(): unlink(): %s\n",
strerror(errno));
}
}
if (!access(datafn, F_OK)) {
if(unlink(datafn)) {
fprintf(stderr, "Cache_delete(): unlink(): %s\n",
lprintf(debug, "Cache_delete(): unlink(): %s\n",
strerror(errno));
}
}
@ -626,7 +627,7 @@ static int Data_open(Cache *cf)
free(datafn);
if (!cf->dfp) {
/* Failed to open the data file */
fprintf(stderr, "Data_open(): fopen(%s): %s\n", datafn,
lprintf(debug, "Data_open(): fopen(%s): %s\n", datafn,
strerror(errno));
return -1;
}
@ -645,7 +646,7 @@ static int Meta_open(Cache *cf)
cf->mfp = fopen(metafn, "r+");
if (!cf->mfp) {
/* Failed to open the data file */
fprintf(stderr, "Meta_open(): fopen(%s): %s\n", metafn,
lprintf(debug, "Meta_open(): fopen(%s): %s\n", metafn,
strerror(errno));
free(metafn);
return -1;
@ -666,7 +667,7 @@ static int Meta_create(Cache *cf)
cf->mfp = fopen(metafn, "w");
if (!cf->mfp) {
/* Failed to open the data file */
fprintf(stderr, "Meta_create(): fopen(%s): %s\n", metafn,
lprintf(debug, "Meta_create(): fopen(%s): %s\n", metafn,
strerror(errno));
free(metafn);
return -1;
@ -686,7 +687,7 @@ int Cache_create(const char *path)
} else {
fn = this_link->sonic_id;
}
fprintf(stderr, "Cache_create(): Creating cache files for %s.\n", fn);
lprintf(debug, "Cache_create(): Creating cache files for %s.\n", fn);
Cache *cf = Cache_alloc();
cf->path = strndup(fn, MAX_PATH_LEN);
@ -697,33 +698,33 @@ int Cache_create(const char *path)
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
if (Meta_create(cf)) {
fprintf(stderr, "Cache_create(): cannot create metadata.\n");
lprintf(debug, "Cache_create(): cannot create metadata.\n");
exit_failure();
}
if (fclose(cf->mfp)) {
fprintf(stderr,
lprintf(debug,
"Cache_create(): cannot close metadata after creation: %s.\n",
strerror(errno));
}
if (Meta_open(cf)) {
Cache_free(cf);
fprintf(stderr, "Cache_create(): cannot open metadata file, %s.\n", fn);
lprintf(debug, "Cache_create(): cannot open metadata file, %s.\n", fn);
}
if (Meta_write(cf)) {
fprintf(stderr, "Cache_create(): Meta_write() failed!\n");
lprintf(debug, "Cache_create(): Meta_write() failed!\n");
}
if (fclose(cf->mfp)) {
fprintf(stderr,
lprintf(debug,
"Cache_create(): cannot close metadata after write, %s.\n",
strerror(errno));
}
if (Data_create(cf)) {
fprintf(stderr, "Cache_create(): Data_create() failed!\n");
lprintf(debug, "Cache_create(): Data_create() failed!\n");
}
Cache_free(cf);
@ -752,7 +753,7 @@ Cache *Cache_open(const char *fn)
/*---------------- Cache_open() critical section -----------------*/
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_open(): thread %lu: locking cf_lock;\n",
lprintf(debug, "Cache_open(): thread %lu: locking cf_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf_lock);
@ -760,7 +761,7 @@ Cache *Cache_open(const char *fn)
if (link->cache_opened) {
link->cache_opened++;
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_open(): thread %lu: unlocking cf_lock;\n",
lprintf(debug, "Cache_open(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf_lock);
@ -768,7 +769,7 @@ Cache *Cache_open(const char *fn)
}
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_open(): thread %lu: unlocking cf_lock;\n",
lprintf(debug, "Cache_open(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf_lock);
@ -804,7 +805,7 @@ Cache *Cache_open(const char *fn)
if (Meta_open(cf)) {
Cache_free(cf);
fprintf(stderr, "Cache_open(): cannot open metadata file %s.\n", fn);
lprintf(debug, "Cache_open(): cannot open metadata file %s.\n", fn);
return NULL;
}
@ -815,7 +816,7 @@ Cache *Cache_open(const char *fn)
*/
if ((rtn == EINCONSIST) || (rtn == EMEM)) {
Cache_free(cf);
fprintf(stderr, "Cache_open(): metadata error: %s, %d.\n", fn, rtn);
lprintf(debug, "Cache_open(): metadata error: %s, %d.\n", fn, rtn);
return NULL;
}
@ -825,7 +826,7 @@ Cache *Cache_open(const char *fn)
* allocation policy.
*/
if (cf->content_length > Data_size(fn)) {
fprintf(stderr, "Cache_open(): metadata inconsistency %s, \
lprintf(debug, "Cache_open(): metadata inconsistency %s, \
cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
Data_size(fn));
Cache_free(cf);
@ -834,14 +835,14 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
/* Check if the cache files are not outdated */
if (cf->time != cf->link->time) {
fprintf(stderr, "Cache_open(): outdated cache file: %s.\n", fn);
lprintf(debug, "Cache_open(): outdated cache file: %s.\n", fn);
Cache_free(cf);
return NULL;
}
if (Data_open(cf)) {
Cache_free(cf);
fprintf(stderr, "Cache_open(): cannot open data file %s.\n", fn);
lprintf(debug, "Cache_open(): cannot open data file %s.\n", fn);
return NULL;
}
@ -857,7 +858,7 @@ void Cache_close(Cache *cf)
/*--------------- Cache_close() critical section -----------------*/
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_close(): thread %lu: locking cf_lock;\n",
lprintf(debug, "Cache_close(): thread %lu: locking cf_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf_lock);
@ -866,7 +867,7 @@ void Cache_close(Cache *cf)
if (cf->link->cache_opened > 0) {
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_close(): thread %lu: unlocking cf_lock;\n",
lprintf(debug, "Cache_close(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf_lock);
@ -874,7 +875,7 @@ void Cache_close(Cache *cf)
}
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"Cache_close(): thread %lu: locking and unlocking bgt_lock;\n",
pthread_self());
#endif
@ -883,7 +884,7 @@ void Cache_close(Cache *cf)
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_close(): thread %lu: unlocking cf_lock;\n",
lprintf(debug, "Cache_close(): thread %lu: unlocking cf_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf_lock);
@ -891,16 +892,16 @@ void Cache_close(Cache *cf)
/*----------------------------------------------------------------*/
if (Meta_write(cf)) {
fprintf(stderr, "Cache_close(): Meta_write() error.");
lprintf(debug, "Cache_close(): Meta_write() error.");
}
if (fclose(cf->mfp)) {
fprintf(stderr, "Cache_close(): cannot close metadata: %s.\n",
lprintf(debug, "Cache_close(): cannot close metadata: %s.\n",
strerror(errno));
}
if (fclose(cf->dfp)) {
fprintf(stderr, "Cache_close(): cannot close data file %s.\n",
lprintf(debug, "Cache_close(): cannot close data file %s.\n",
strerror(errno));
}
@ -940,16 +941,16 @@ static void *Cache_bgdl(void *arg)
{
Cache *cf = (Cache *) arg;
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_bgdl(): thread %lu: locking w_lock;\n",
lprintf(debug, "Cache_bgdl(): thread %lu: locking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf->w_lock);
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
fprintf(stderr, "Cache_bgdl(): thread %lu: ", pthread_self());
lprintf(debug, "Cache_bgdl(): thread %lu: ", pthread_self());
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
cf->next_dl_offset);
if (recv < 0) {
fprintf(stderr, "\nCache_bgdl(): received %lu bytes, \
lprintf(debug, "\nCache_bgdl(): received %lu bytes, \
which does't make sense\n", recv);
exit_failure();
}
@ -960,17 +961,17 @@ which does't make sense\n", recv);
Data_write(cf, recv_buf, recv, cf->next_dl_offset);
Seg_set(cf, cf->next_dl_offset, 1);
} else {
fprintf(stderr,
lprintf(debug,
"Cache_bgdl(): received %ld, possible network error.\n", recv);
}
free(recv_buf);
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n",
lprintf(debug, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_bgdl(): thread %lu: unlocking w_lock;\n",
lprintf(debug, "Cache_bgdl(): thread %lu: unlocking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
@ -993,7 +994,7 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
} else {
/* Wait for any other download thread to finish*/
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_read(): thread %ld: locking w_lock;\n",
lprintf(debug, "Cache_read(): thread %ld: locking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf->w_lock);
@ -1002,7 +1003,7 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
* download thread. Send it off and unlock the I/O */
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_read(): thread %lu: unlocking w_lock;\n",
lprintf(debug, "Cache_read(): thread %lu: unlocking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
@ -1013,11 +1014,11 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
/* ------------------------Download the segment -------------------------*/
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
fprintf(stderr, "Cache_read(): thread %lu: ", pthread_self());
lprintf(debug, "Cache_read(): thread %lu: ", pthread_self());
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
dl_offset);
if (recv < 0) {
fprintf(stderr, "\nCache_read(): received %ld bytes, \
lprintf(debug, "\nCache_read(): received %ld bytes, \
which does't make sense\n", recv);
exit_failure();
}
@ -1033,14 +1034,14 @@ which does't make sense\n", recv);
Data_write(cf, recv_buf, recv, dl_offset);
Seg_set(cf, dl_offset, 1);
} else {
fprintf(stderr,
lprintf(debug,
"Cache_read(): received %ld, possible network error.\n", recv);
}
free(recv_buf);
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_read(): thread %lu: unlocking w_lock;\n",
lprintf(debug, "Cache_read(): thread %lu: unlocking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
@ -1055,12 +1056,12 @@ which does't make sense\n", recv);
/* Stop the spawning of multiple background pthreads */
if(!pthread_mutex_trylock(&cf->bgt_lock)) {
#ifdef CACHE_LOCK_DEBUG
fprintf(stderr, "Cache_read(): thread %lu: trylocked bgt_lock;\n",
lprintf(debug, "Cache_read(): thread %lu: trylocked bgt_lock;\n",
pthread_self());
#endif
cf->next_dl_offset = next_dl_offset;
if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) {
fprintf(stderr,
lprintf(debug,
"Cache_read(): Error creating background download thread\n"
);
}

View File

@ -68,5 +68,7 @@ void Config_init(void)
CONFIG.sonic_insecure = 0;
/*--------------- Log related -------------*/
CONFIG.log_level = log_init();
CONFIG.log_level = log_level_init();
CONFIG.log_verbosity = log_verbosity_init();
}

View File

@ -73,6 +73,14 @@ typedef struct {
/*--------------- Log related -------------*/
/** \brief Current log level */
int log_level;
/**
* \brief Whether we print the filename and line number in log
* \details
* - 0 : No filename and line nubmer
* - 1 : Filename only
* - 2 : Filename and line number
*/
int log_verbosity;
} ConfigStruct;
/**

View File

@ -3,6 +3,7 @@
#include "cache.h"
#include "network.h"
#include "sonic.h"
#include "log.h"
#include <gumbo.h>
@ -38,7 +39,7 @@ LinkTable *LinkSystem_init(const char *raw_url)
}
if (pthread_mutex_init(&link_lock, NULL) != 0) {
fprintf(stderr,
lprintf(debug,
"link_system_init(): link_lock initialisation failed!\n");
exit_failure();
}
@ -80,7 +81,7 @@ void LinkTable_add(LinkTable *linktbl, Link *link)
linktbl->num++;
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) {
fprintf(stderr, "LinkTable_add(): realloc failure!\n");
lprintf(debug, "LinkTable_add(): realloc failure!\n");
exit_failure();
}
linktbl->links[linktbl->num - 1] = link;
@ -189,7 +190,7 @@ static CURL *Link_to_curl(Link *link)
{
CURL *curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Link_to_curl(): curl_easy_init() failed!\n");
lprintf(debug, "Link_to_curl(): curl_easy_init() failed!\n");
}
/* set up some basic curl stuff */
curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent);
@ -235,7 +236,7 @@ static CURL *Link_to_curl(Link *link)
void Link_req_file_stat(Link *this_link)
{
if (this_link->type != LINK_UNINITIALISED_FILE) {
fprintf(stderr, "Link_req_file_stat(), invalid request, LinkType: %c",
lprintf(debug, "Link_req_file_stat(), invalid request, LinkType: %c",
this_link->type);
exit_failure();
}
@ -274,12 +275,12 @@ void Link_set_file_stat(Link* this_link, CURL *curl)
this_link->content_length = cl;
}
} else {
fprintf(stderr, "Link_set_file_stat(): HTTP %ld", http_resp);
lprintf(debug, "Link_set_file_stat(): HTTP %ld", http_resp);
if (HTTP_temp_failure(http_resp)) {
fprintf(stderr, ", retrying later.\n");
lprintf(debug, ", retrying later.\n");
} else {
this_link->type = LINK_INVALID;
fprintf(stderr, ".\n");
lprintf(debug, ".\n");
}
}
}
@ -293,7 +294,7 @@ static void LinkTable_uninitialised_fill(LinkTable *linktbl)
{
int u;
char s[STATUS_LEN];
fprintf(stderr, "LinkTable_uninitialised_fill(): ... ");
lprintf(debug, "LinkTable_uninitialised_fill(): ... ");
do {
u = 0;
for (int i = 0; i < linktbl->num; i++) {
@ -312,12 +313,12 @@ static void LinkTable_uninitialised_fill(LinkTable *linktbl)
erase_string(stderr, STATUS_LEN, s);
}
snprintf(s, STATUS_LEN, "%d / %d", n-i, n);
fprintf(stderr, "%s", s);
lprintf(debug, "%s", s);
j++;
}
} while (u);
erase_string(stderr, STATUS_LEN, s);
fprintf(stderr, "Done!\n");
lprintf(debug, "Done!\n");
}
static void LinkTable_fill(LinkTable *linktbl)
@ -353,7 +354,7 @@ static void LinkTable_invalid_reset(LinkTable *linktbl)
j++;
}
}
fprintf(stderr, "LinkTable_invalid_reset(): %d invalid links\n", j);
lprintf(debug, "LinkTable_invalid_reset(): %d invalid links\n", j);
}
void LinkTable_free(LinkTable *linktbl)
@ -368,13 +369,13 @@ void LinkTable_free(LinkTable *linktbl)
void LinkTable_print(LinkTable *linktbl)
{
int j = 0;
fprintf(stderr, "--------------------------------------------\n");
fprintf(stderr, " LinkTable %p for %s\n", linktbl,
lprintf(debug, "--------------------------------------------\n");
lprintf(debug, " LinkTable %p for %s\n", linktbl,
linktbl->links[0]->f_url);
fprintf(stderr, "--------------------------------------------\n");
lprintf(debug, "--------------------------------------------\n");
for (int i = 0; i < linktbl->num; i++) {
Link *this_link = linktbl->links[i];
fprintf(stderr, "%d %c %lu %s %s\n",
lprintf(debug, "%d %c %lu %s %s\n",
i,
this_link->type,
this_link->content_length,
@ -387,9 +388,9 @@ void LinkTable_print(LinkTable *linktbl)
j++;
}
}
fprintf(stderr, "--------------------------------------------\n");
fprintf(stderr, "LinkTable_print(): Invalid link count: %d\n", j);
fprintf(stderr, "--------------------------------------------\n");
lprintf(debug, "--------------------------------------------\n");
lprintf(debug, "LinkTable_print(): Invalid link count: %d\n", j);
lprintf(debug, "--------------------------------------------\n");
}
DataStruct Link_to_DataStruct(Link *head_link)
@ -409,12 +410,12 @@ DataStruct Link_to_DataStruct(Link *head_link)
transfer_blocking(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (HTTP_temp_failure(http_resp)) {
fprintf(stderr,
lprintf(debug,
"LinkTable_new(): URL: %s, HTTP %ld, retrying later.\n",
url, http_resp);
sleep(CONFIG.http_wait_sec);
} else if (http_resp != HTTP_OK) {
fprintf(stderr,
lprintf(debug,
"LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n",
url, http_resp);
buf.size = 0;
@ -443,7 +444,7 @@ LinkTable *LinkTable_alloc(const char *url)
LinkTable *LinkTable_new(const char *url)
{
#ifdef LINK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"LinkTable_new(): thread %lu: locking link_lock;\n",
pthread_self());
#endif
@ -474,7 +475,7 @@ LinkTable *LinkTable_new(const char *url)
disk_linktbl = LinkTable_disk_open(unescaped_path);
if (disk_linktbl) {
/* Check if we need to update the link table */
fprintf(stderr,
lprintf(debug,
"LinkTable_new(): disk_linktbl->num: %d, linktbl->num: %d\n",
disk_linktbl->num, linktbl->num);
if (disk_linktbl->num == linktbl->num) {
@ -508,7 +509,7 @@ LinkTable *LinkTable_new(const char *url)
LinkTable_print(linktbl);
#ifdef LINK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"LinkTable_new(): thread %lu: unlocking link_lock;\n",
pthread_self());
#endif
@ -526,7 +527,7 @@ static void LinkTable_disk_delete(const char *dirn)
path = path_append(metadirn, "/.LinkTable");
}
if(unlink(path)) {
fprintf(stderr, "LinkTable_disk_delete(): unlink(%s): %s\n", path,
lprintf(debug, "LinkTable_disk_delete(): unlink(%s): %s\n", path,
strerror(errno));
}
free(path);
@ -546,7 +547,7 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
free(metadirn);
if (!fp) {
fprintf(stderr, "LinkTable_disk_save(): fopen(%s): %s\n", path,
lprintf(debug, "LinkTable_disk_save(): fopen(%s): %s\n", path,
strerror(errno));
free(path);
return -1;
@ -565,12 +566,12 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
int res = 0;
if (ferror(fp)) {
fprintf(stderr, "LinkTable_disk_save(): encountered ferror!\n");
lprintf(debug, "LinkTable_disk_save(): encountered ferror!\n");
res = -1;
}
if (fclose(fp)) {
fprintf(stderr,
lprintf(debug,
"LinkTable_disk_save(): cannot close the file pointer, %s\n",
strerror(errno));
res = -1;
@ -609,21 +610,21 @@ LinkTable *LinkTable_disk_open(const char *dirn)
fread(&linktbl->links[i]->time, sizeof(long), 1, fp);
if (feof(fp)) {
/* reached EOF */
fprintf(stderr,
lprintf(debug,
"LinkTable_disk_open(): reached EOF!\n");
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
return NULL;
}
if (ferror(fp)) {
fprintf(stderr, "LinkTable_disk_open(): encountered ferror!\n");
lprintf(debug, "LinkTable_disk_open(): encountered ferror!\n");
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
return NULL;
}
}
if (fclose(fp)) {
fprintf(stderr,
lprintf(debug,
"LinkTable_disk_open(): cannot close the file pointer, %s\n",
strerror(errno));
}
@ -717,7 +718,7 @@ Link *path_to_Link(const char *path)
{
char *new_path = strndup(path, MAX_PATH_LEN);
if (!new_path) {
fprintf(stderr, "path_to_Link(): cannot allocate memory\n");
lprintf(debug, "path_to_Link(): cannot allocate memory\n");
exit_failure();
}
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
@ -729,7 +730,7 @@ long path_download(const char *path, char *output_buf, size_t size,
off_t offset)
{
if (!path) {
fprintf(stderr, "\npath_download(): NULL path supplied\n");
lprintf(debug, "\npath_download(): NULL path supplied\n");
exit_failure();
}
Link *link;
@ -742,7 +743,7 @@ long path_download(const char *path, char *output_buf, size_t size,
size_t end = start + size;
char range_str[64];
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
fprintf(stderr, "path_download(%s, %s);\n", path, range_str);
lprintf(debug, "path_download(%s, %s);\n", path, range_str);
DataStruct buf;
buf.size = 0;
@ -753,7 +754,7 @@ long path_download(const char *path, char *output_buf, size_t size,
curl_easy_setopt(curl, CURLOPT_RANGE, range_str);
#ifdef LINK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"path_download(): thread %lu: locking and unlocking link_lock;\n",
pthread_self());
#endif
@ -771,7 +772,7 @@ long path_download(const char *path, char *output_buf, size_t size,
/* Check for range seek support */
if (!CONFIG.no_range_check) {
if (!strcasestr((header.data), "Accept-Ranges: bytes")) {
fprintf(stderr, "Error: This web server does not support HTTP \
lprintf(debug, "Error: This web server does not support HTTP \
range requests\n");
exit(EXIT_FAILURE);
}
@ -786,7 +787,7 @@ range requests\n");
(http_resp != HTTP_PARTIAL_CONTENT) ||
(http_resp != HTTP_RANGE_NOT_SATISFIABLE)
)) {
fprintf(stderr, "path_download(): Could not download %s, HTTP %ld\n",
lprintf(debug, "path_download(): Could not download %s, HTTP %ld\n",
link->f_url, http_resp);
return -ENOENT;
}

View File

@ -1,22 +1,60 @@
#include "log.h"
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
int log_init()
int log_level_init()
{
char *env = getenv("HTTPDIRFS_DEBUG_LEVEL");
char *env = getenv("HTTPDIRFS_LOG_LEVEL");
if (env) {
return atoi(env);
}
return DEFAULT_LOG_LEVEL;
}
void LOG_PRINTF(int level, const char *file, int line, const char *format, ...)
int log_verbosity_init()
{
fprintf(stderr, "(%s:%d): ", file, line);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
char *env = getenv("HTTPDIRFS_LOG_VERBOSITY");
if (env) {
return atoi(env);
}
return DEFAULT_LOG_VERBOSITY;
}
void log_printf(int type, const char *file, int line, const char *format, ...)
{
if (type & CONFIG.log_level) {
switch (type) {
case notice:
goto print_actual_message;
break;
case error:
fprintf(stderr, "Error: ");
break;
case debug:
fprintf(stderr, "Debug: ");
break;
default:
fprintf(stderr, "Unknown (%x):", type);
break;
}
switch (CONFIG.log_verbosity) {
case LOG_SHOW_FILENAME:
fprintf(stderr, "(%s): ", file);
break;
case LOG_SHOW_FILENAME_LINE_NUM:
fprintf(stderr, "(%s:%d): ", file, line);
break;
}
print_actual_message:
/* A label can only be part of a statement, this is a statement. lol*/
{}
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
}

View File

@ -1,30 +1,60 @@
#ifndef LOG_H
#define LOG_H
/**
* \brief log level: notice
* \brief Log type: Notice
*/
#define LOG_LEVEL_NOTICE 1
#define notice 1 << 0
/**
* \brief the default log level
* \brief Log type: Error
*/
#define DEFAULT_LOG_LEVEL LOG_LEVEL_NOTICE
#define error 1 << 1
/**
* \brief Log type: Debug
*/
#define debug 1 << 2
/**
* \brief The default log level
*/
#define DEFAULT_LOG_LEVEL notice
/**
* \brief Display filename in log
*/
#define LOG_SHOW_FILENAME 1
/**
* \brief Display line number in log
*/
#define LOG_SHOW_FILENAME_LINE_NUM 2
/**
* \brief The default log verbosity
*/
#define DEFAULT_LOG_VERBOSITY LOG_SHOW_FILENAME_LINE_NUM
/**
* \brief Get the log level from the environment.
*/
int log_init();
int log_level_init();
/**
* \brief Get the log verbosity from the environment
*/
int log_verbosity_init();
/**
* \brief log printf
* \details This is for printing nice log messages
*/
void LOG_PRINTF(int level, const char *file, int line, const char *format, ...);
void log_printf(int type, const char *file, int line, const char *format, ...);
/**
* \brief log printf
* \details This macro automatically prints out the filename and line number
*/
#define log_printf(level, ...) \
LOG_PRINTF (level, __FILE__, __LINE__, __VA_ARGS__);
#define lprintf(type, ...) \
log_printf(type, __FILE__, __LINE__, __VA_ARGS__);
#endif

View File

@ -2,6 +2,7 @@
#include "cache.h"
#include "fuse_local.h"
#include "network.h"
#include "log.h"
#include <getopt.h>
#include <stdlib.h>
@ -22,7 +23,7 @@ int main(int argc, char **argv)
/* Automatically print help if not enough arguments are supplied */
if (argc < 2) {
print_help(argv[0], 0);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
lprintf(debug, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
}
@ -70,20 +71,20 @@ int main(int argc, char **argv)
/* The second last remaining argument is the URL */
char *base_url = argv[argc-2];
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
fprintf(stderr, "Error: Please supply a valid URL.\n");
lprintf(debug, "Error: Please supply a valid URL.\n");
print_help(argv[0], 0);
exit(EXIT_FAILURE);
} else {
if (CONFIG.sonic_username && CONFIG.sonic_password) {
CONFIG.sonic_mode = 1;
} else if (CONFIG.sonic_username || CONFIG.sonic_password) {
fprintf(stderr,
lprintf(debug,
"Error: You have to supply both username and password to \
activate Sonic mode.\n");
exit(EXIT_FAILURE);
}
if(!LinkSystem_init(base_url)) {
fprintf(stderr, "Error: Network initialisation failed.\n");
lprintf(debug, "Error: Network initialisation failed.\n");
exit(EXIT_FAILURE);
}
}
@ -257,12 +258,12 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
/* This is for --config, we don't need to do anything */
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
lprintf(debug, "see httpdirfs -h for usage\n");
return 1;
}
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
lprintf(debug, "see httpdirfs -h for usage\n");
return 1;
}
};
@ -283,7 +284,7 @@ void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string)
static void print_help(char *program_name, int long_help)
{
fprintf(stderr,
lprintf(debug,
"usage: %s [options] URL mountpoint\n", program_name);
if (long_help) {
print_long_help();
@ -292,15 +293,15 @@ static void print_help(char *program_name, int long_help)
static void print_version()
{
fprintf(stderr, "HTTPDirFS version " VERSION "\n");
lprintf(debug, "HTTPDirFS version " VERSION "\n");
/* --------- Print off SSL engine version --------- */
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
fprintf(stderr, "libcurl SSL engine: %s\n", data->ssl_version);
lprintf(debug, "libcurl SSL engine: %s\n", data->ssl_version);
}
static void print_long_help()
{
fprintf(stderr,
lprintf(debug,
"\n\
general options:\n\
--config Specify a configuration file \n\

View File

@ -2,6 +2,7 @@
#include "cache.h"
#include "config.h"
#include "log.h"
#include <openssl/crypto.h>
@ -118,7 +119,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (HTTP_temp_failure(http_resp)) {
if (!slept) {
fprintf(stderr,
lprintf(debug,
"curl_process_msgs(): HTTP %ld, sleeping for %d sec\n",
http_resp, CONFIG.http_wait_sec);
sleep(CONFIG.http_wait_sec);
@ -134,7 +135,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
Link_set_file_stat(transfer->link, curl);
}
} else {
fprintf(stderr, "curl_process_msgs(): %d - %s <%s>\n",
lprintf(debug, "curl_process_msgs(): %d - %s <%s>\n",
curl_msg->data.result,
curl_easy_strerror(curl_msg->data.result),
url);
@ -146,7 +147,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
free(transfer);
}
} else {
fprintf(stderr, "curl_process_msgs(): curl_msg->msg: %d\n",
lprintf(debug, "curl_process_msgs(): curl_msg->msg: %d\n",
curl_msg->msg);
}
}
@ -158,7 +159,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
int curl_multi_perform_once(void)
{
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"curl_multi_perform_once(): thread %lu: locking transfer_lock;\n",
pthread_self());
#endif
@ -167,7 +168,7 @@ int curl_multi_perform_once(void)
int n_running_curl;
CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl);
if(mc > 0) {
fprintf(stderr, "curl_multi_perform(): %s\n", curl_multi_strerror(mc));
lprintf(debug, "curl_multi_perform(): %s\n", curl_multi_strerror(mc));
}
fd_set fdread;
@ -201,14 +202,14 @@ int curl_multi_perform_once(void)
mc = curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
if (mc > 0) {
fprintf(stderr, "curl_multi_fdset(): %s.\n", curl_multi_strerror(mc));
lprintf(debug, "curl_multi_fdset(): %s.\n", curl_multi_strerror(mc));
}
if (maxfd == -1) {
usleep(100*1000);
} else {
if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout) < 0) {
fprintf(stderr, "curl_multi_perform_once(): select(): %s.\n",
lprintf(debug, "curl_multi_perform_once(): select(): %s.\n",
strerror(errno));
}
}
@ -220,7 +221,7 @@ int curl_multi_perform_once(void)
curl_process_msgs(curl_msg, n_running_curl, n_mesgs);
}
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"curl_multi_perform_once(): thread %lu: unlocking transfer_lock;\n",
pthread_self());
#endif
@ -232,14 +233,14 @@ void NetworkSystem_init(void)
{
/* ------- Global related ----------*/
if (curl_global_init(CURL_GLOBAL_ALL)) {
fprintf(stderr, "network_init(): curl_global_init() failed!\n");
lprintf(debug, "network_init(): curl_global_init() failed!\n");
exit_failure();
}
/* -------- Share related ----------*/
CURL_SHARE = curl_share_init();
if (!(CURL_SHARE)) {
fprintf(stderr, "network_init(): curl_share_init() failed!\n");
lprintf(debug, "network_init(): curl_share_init() failed!\n");
exit_failure();
}
@ -248,7 +249,7 @@ void NetworkSystem_init(void)
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
if (pthread_mutex_init(&curl_lock, NULL) != 0) {
fprintf(stderr, "network_init(): curl_lock initialisation failed!\n");
lprintf(debug, "network_init(): curl_lock initialisation failed!\n");
exit_failure();
}
curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
@ -257,7 +258,7 @@ void NetworkSystem_init(void)
/* ------------- Multi related -----------*/
curl_multi = curl_multi_init();
if (!curl_multi) {
fprintf(stderr, "network_init(): curl_multi_init() failed!\n");
lprintf(debug, "network_init(): curl_multi_init() failed!\n");
exit_failure();
}
curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
@ -267,7 +268,7 @@ void NetworkSystem_init(void)
/* ------------ Initialise locks ---------*/
if (pthread_mutex_init(&transfer_lock, NULL)) {
fprintf(stderr,
lprintf(debug,
"network_init(): transfer_lock initialisation failed!\n");
exit_failure();
}
@ -290,21 +291,21 @@ void transfer_blocking(CURL *curl)
transfer.transferring = 1;
curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer);
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"transfer_blocking(): thread %lu: locking transfer_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"transfer_blocking(): thread %lu: unlocking transfer_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
if(res > 0) {
fprintf(stderr, "transfer_blocking(): %d, %s\n",
lprintf(debug, "transfer_blocking(): %d, %s\n",
res, curl_multi_strerror(res));
exit_failure();
}
@ -317,21 +318,21 @@ void transfer_blocking(CURL *curl)
void transfer_nonblocking(CURL *curl)
{
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"transfer_nonblocking(): thread %lu: locking transfer_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
#ifdef NETWORK_LOCK_DEBUG
fprintf(stderr,
lprintf(debug,
"transfer_nonblocking(): thread %lu: unlocking transfer_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
if(res > 0) {
fprintf(stderr, "transfer_nonblocking(): %s\n",
lprintf(debug, "transfer_nonblocking(): %s\n",
curl_multi_strerror(res));
}
}
@ -345,7 +346,7 @@ size_t write_memory_callback(void *contents, size_t size, size_t nmemb,
mem->data = realloc(mem->data, mem->size + realsize + 1);
if(!mem->data) {
/* out of memory! */
fprintf(stderr, "write_memory_callback(): realloc failure!\n");
lprintf(debug, "write_memory_callback(): realloc failure!\n");
exit_failure();
return 0;
}

View File

@ -4,6 +4,7 @@
#include "util.h"
#include "link.h"
#include "network.h"
#include "log.h"
#include <expat.h>
@ -166,9 +167,9 @@ static void XMLCALL XML_parser_general(void *data, const char *elem,
{
/* Error checking */
if (!strcmp(elem, "error")) {
fprintf(stderr, "XML_parser_general() error:\n");
lprintf(debug, "XML_parser_general() error:\n");
for (int i = 0; attr[i]; i += 2) {
fprintf(stderr, "%s: %s\n", attr[i], attr[i+1]);
lprintf(debug, "%s: %s\n", attr[i], attr[i+1]);
}
exit(EXIT_FAILURE);
}
@ -321,7 +322,7 @@ static LinkTable *sonic_url_to_LinkTable(const char *url,
XML_SetStartElementHandler(parser, handler);
if (XML_Parse(parser, xml.data, xml.size, 1) == XML_STATUS_ERROR) {
fprintf(stderr,
lprintf(debug,
"sonic_XML_to_LinkTable(): Parse error at line %lu: %s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
@ -355,9 +356,9 @@ static void XMLCALL XML_parser_id3_root(void *data, const char *elem,
const char **attr)
{
if (!strcmp(elem, "error")) {
fprintf(stderr, "XML_parser_id3_root() error:\n");
lprintf(debug, "XML_parser_id3_root() error:\n");
for (int i = 0; attr[i]; i += 2) {
fprintf(stderr, "%s: %s\n", attr[i], attr[i+1]);
lprintf(debug, "%s: %s\n", attr[i], attr[i+1]);
}
exit(EXIT_FAILURE);
}
@ -453,7 +454,7 @@ LinkTable *sonic_LinkTable_new_id3(int depth, const char *id)
/*
* We shouldn't reach here.
*/
fprintf(stderr, "sonic_LinkTable_new_id3(): case %d.\n", depth);
lprintf(debug, "sonic_LinkTable_new_id3(): case %d.\n", depth);
exit_failure();
break;
}

View File

@ -56,7 +56,7 @@ void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x)
int i;
i = pthread_mutex_unlock(x);
if (i) {
fprintf(stderr, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n",
lprintf(debug, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n",
pthread_self(), i, strerror(i));
exit_failure();
}
@ -67,7 +67,7 @@ void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
int i;
i = pthread_mutex_lock(x);
if (i) {
fprintf(stderr, "thread %lu: pthread_mutex_lock() failed, %d, %s\n",
lprintf(debug, "thread %lu: pthread_mutex_lock() failed, %d, %s\n",
pthread_self(), i, strerror(i));
exit_failure();
}
@ -79,8 +79,8 @@ void exit_failure(void)
void *buffer[BT_BUF_SIZE];
nptrs = backtrace(buffer, BT_BUF_SIZE);
fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n");
fprintf(stderr, "backtrace() returned the following %d addresses:\n",
lprintf(debug, "\nOops! HTTPDirFS crashed! :(\n");
lprintf(debug, "backtrace() returned the following %d addresses:\n",
nptrs);
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
@ -132,7 +132,7 @@ void *CALLOC(size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
if (!ptr) {
fprintf(stderr, "calloc() failed, %s!\n", strerror(errno));
lprintf(debug, "calloc() failed, %s!\n", strerror(errno));
exit_failure();
}
return ptr;