* Rework how we do some very early-stage logging, and clean up a few warnings.

This commit is contained in:
Justin Maggard 2012-01-10 02:50:33 +00:00
parent 6eb9cc7054
commit 1aa540c4b5

View File

@ -351,7 +351,7 @@ init(int argc, char * * argv)
char * string, * word; char * string, * word;
enum media_types type; enum media_types type;
char * path; char * path;
char real_path[PATH_MAX]; char buf[PATH_MAX];
char ip_addr[INET_ADDRSTRLEN + 3] = {'\0'}; char ip_addr[INET_ADDRSTRLEN + 3] = {'\0'};
char log_str[72] = "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn"; char log_str[72] = "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn";
@ -387,7 +387,7 @@ init(int argc, char * * argv)
{ {
/* only error if file exists or using -f */ /* only error if file exists or using -f */
if(access(optionsfile, F_OK) == 0 || options_flag) if(access(optionsfile, F_OK) == 0 || options_flag)
fprintf(stderr, "Error reading configuration file %s\n", optionsfile); DPRINTF(E_ERROR, L_GENERAL, "Error reading configuration file %s\n", optionsfile);
} }
else else
{ {
@ -406,12 +406,10 @@ init(int argc, char * * argv)
if(n_lan_addr < MAX_LAN_ADDR) if(n_lan_addr < MAX_LAN_ADDR)
n_lan_addr++; n_lan_addr++;
} }
else
fprintf(stderr, "Interface %s not found, ignoring.\n", word);
} }
else else
{ {
fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
MAX_LAN_ADDR, word); MAX_LAN_ADDR, word);
} }
} }
@ -425,7 +423,7 @@ init(int argc, char * * argv)
} }
else else
{ {
fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
MAX_LAN_ADDR, ary_options[i].value); MAX_LAN_ADDR, ary_options[i].value);
} }
break; break;
@ -473,13 +471,13 @@ init(int argc, char * * argv)
type = IMAGES_ONLY; type = IMAGES_ONLY;
myval = index(ary_options[i].value, '/'); myval = index(ary_options[i].value, '/');
case '/': case '/':
path = realpath(myval ? myval:ary_options[i].value, real_path); path = realpath(myval ? myval:ary_options[i].value, buf);
if( !path ) if( !path )
path = (myval ? myval:ary_options[i].value); path = (myval ? myval:ary_options[i].value);
if( access(path, F_OK) != 0 ) if( access(path, F_OK) != 0 )
{ {
fprintf(stderr, "Media directory not accessible! [%s]\n", DPRINTF(E_ERROR, L_GENERAL, "Media directory \"%s\" not accessible! [%s]\n",
path); path, strerror(errno));
break; break;
} }
struct media_dir_s * this_dir = calloc(1, sizeof(struct media_dir_s)); struct media_dir_s * this_dir = calloc(1, sizeof(struct media_dir_s));
@ -498,7 +496,7 @@ init(int argc, char * * argv)
} }
break; break;
default: default:
fprintf(stderr, "Media directory entry not understood! [%s]\n", DPRINTF(E_ERROR, L_GENERAL, "Media directory entry not understood! [%s]\n",
ary_options[i].value); ary_options[i].value);
break; break;
} }
@ -528,7 +526,7 @@ init(int argc, char * * argv)
} }
break; break;
case UPNPDBDIR: case UPNPDBDIR:
path = realpath(ary_options[i].value, real_path); path = realpath(ary_options[i].value, buf);
if( !path ) if( !path )
path = (ary_options[i].value); path = (ary_options[i].value);
make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
@ -540,7 +538,7 @@ init(int argc, char * * argv)
strncpy(db_path, path, PATH_MAX); strncpy(db_path, path, PATH_MAX);
break; break;
case UPNPLOGDIR: case UPNPLOGDIR:
path = realpath(ary_options[i].value, real_path); path = realpath(ary_options[i].value, buf);
if( !path ) if( !path )
path = (ary_options[i].value); path = (ary_options[i].value);
make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
@ -586,7 +584,7 @@ init(int argc, char * * argv)
runtime_vars.root_container = IMAGE_ID; runtime_vars.root_container = IMAGE_ID;
break; break;
default: default:
fprintf(stderr, "Invalid root container! [%s]\n", DPRINTF(E_ERROR, L_GENERAL, "Invalid root container! [%s]\n",
ary_options[i].value); ary_options[i].value);
break; break;
} }
@ -595,7 +593,7 @@ init(int argc, char * * argv)
minissdpdsocketpath = ary_options[i].value; minissdpdsocketpath = ary_options[i].value;
break; break;
default: default:
fprintf(stderr, "Unknown option in file %s\n", DPRINTF(E_ERROR, L_GENERAL, "Unknown option in file %s\n",
optionsfile); optionsfile);
} }
} }
@ -615,7 +613,7 @@ init(int argc, char * * argv)
{ {
if(argv[i][0]!='-') if(argv[i][0]!='-')
{ {
fprintf(stderr, "Unknown option: %s\n", argv[i]); DPRINTF(E_ERROR, L_GENERAL, "Unknown option: %s\n", argv[i]);
} }
else if(strcmp(argv[i], "--help")==0) else if(strcmp(argv[i], "--help")==0)
{ {
@ -628,20 +626,20 @@ init(int argc, char * * argv)
if(i+1 < argc) if(i+1 < argc)
runtime_vars.notify_interval = atoi(argv[++i]); runtime_vars.notify_interval = atoi(argv[++i]);
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break; break;
case 's': case 's':
if(i+1 < argc) if(i+1 < argc)
strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN); strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN);
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0'; serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
break; break;
case 'm': case 'm':
if(i+1 < argc) if(i+1 < argc)
strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN); strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN);
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
modelnumber[MODELNUMBER_MAX_LEN-1] = '\0'; modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
break; break;
/*case 'l': /*case 'l':
@ -651,13 +649,13 @@ init(int argc, char * * argv)
if(i+1 < argc) if(i+1 < argc)
runtime_vars.port = atoi(argv[++i]); runtime_vars.port = atoi(argv[++i]);
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break; break;
case 'P': case 'P':
if(i+1 < argc) if(i+1 < argc)
pidfilename = argv[++i]; pidfilename = argv[++i];
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break; break;
case 'd': case 'd':
debug_flag = 1; debug_flag = 1;
@ -671,7 +669,7 @@ init(int argc, char * * argv)
if(i+1 < argc) if(i+1 < argc)
presurl = argv[++i]; presurl = argv[++i];
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break; break;
case 'a': case 'a':
if(i+1 < argc) if(i+1 < argc)
@ -695,12 +693,12 @@ init(int argc, char * * argv)
} }
else else
{ {
fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
MAX_LAN_ADDR, argv[i]); MAX_LAN_ADDR, argv[i]);
} }
} }
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break; break;
case 'i': case 'i':
if(i+1 < argc) if(i+1 < argc)
@ -710,9 +708,8 @@ init(int argc, char * * argv)
i++; i++;
if( getifaddr(argv[i], ip_addr, sizeof(ip_addr)) < 0 ) if( getifaddr(argv[i], ip_addr, sizeof(ip_addr)) < 0 )
{ {
fprintf(stderr, "Network interface '%s' not found.\n", DPRINTF(E_FATAL, L_GENERAL, "Required network interface '%s' not found.\n",
argv[i]); argv[i]);
exit(-1);
} }
for(j=0; j<n_lan_addr; j++) for(j=0; j<n_lan_addr; j++)
{ {
@ -730,12 +727,12 @@ init(int argc, char * * argv)
} }
else else
{ {
fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
MAX_LAN_ADDR, argv[i]); MAX_LAN_ADDR, argv[i]);
} }
} }
else else
fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
break; break;
case 'f': case 'f':
i++; /* discarding, the config file is already read */ i++; /* discarding, the config file is already read */
@ -744,15 +741,16 @@ init(int argc, char * * argv)
runtime_vars.port = 0; // triggers help display runtime_vars.port = 0; // triggers help display
break; break;
case 'R': case 'R':
snprintf(real_path, sizeof(real_path), "rm -rf %s/files.db %s/art_cache", db_path, db_path); snprintf(buf, sizeof(buf), "rm -rf %s/files.db %s/art_cache", db_path, db_path);
system(real_path); if( system(buf) != 0 )
DPRINTF(E_WARN, L_GENERAL, "Failed to clean old file cache.\n");
break; break;
case 'V': case 'V':
printf("Version " MINIDLNA_VERSION "\n"); printf("Version " MINIDLNA_VERSION "\n");
exit(0); exit(0);
break; break;
default: default:
fprintf(stderr, "Unknown option: %s\n", argv[i]); DPRINTF(E_ERROR, L_GENERAL, "Unknown option: %s\n", argv[i]);
} }
} }
/* If no IP was specified, try to detect one */ /* If no IP was specified, try to detect one */
@ -772,7 +770,7 @@ init(int argc, char * * argv)
if( (n_lan_addr==0) || (runtime_vars.port<=0) ) if( (n_lan_addr==0) || (runtime_vars.port<=0) )
{ {
fprintf(stderr, "Usage:\n\t" DPRINTF(E_ERROR, L_GENERAL, "Usage:\n\t"
"%s [-d] [-v] [-f config_file]\n" "%s [-d] [-v] [-f config_file]\n"
"\t\t[-a listening_ip] [-p port]\n" "\t\t[-a listening_ip] [-p port]\n"
/*"[-l logfile] " not functionnal */ /*"[-l logfile] " not functionnal */
@ -806,8 +804,8 @@ init(int argc, char * * argv)
#else #else
if( access(db_path, F_OK) != 0 ) if( access(db_path, F_OK) != 0 )
make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
sprintf(real_path, "%s/minidlna.log", log_path); sprintf(buf, "%s/minidlna.log", log_path);
log_init(real_path, log_str); log_init(buf, log_str);
#endif #endif
} }
@ -837,7 +835,6 @@ init(int argc, char * * argv)
} }
/* set signal handler */ /* set signal handler */
signal(SIGCHLD, SIG_IGN);
memset(&sa, 0, sizeof(struct sigaction)); memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = sigterm; sa.sa_handler = sigterm;
if (sigaction(SIGTERM, &sa, NULL)) if (sigaction(SIGTERM, &sa, NULL))
@ -886,6 +883,8 @@ main(int argc, char * * argv)
struct timeval lastbeacontime = {0, 0}; struct timeval lastbeacontime = {0, 0};
#endif #endif
for (i = 0; i < L_MAX; i++)
log_level[i] = E_WARN;
#ifdef ENABLE_NLS #ifdef ENABLE_NLS
setlocale(LC_MESSAGES, ""); setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "en_US.utf8"); setlocale(LC_CTYPE, "en_US.utf8");
@ -931,8 +930,15 @@ main(int argc, char * * argv)
} }
sqlite3_close(db); sqlite3_close(db);
char *cmd; char *cmd;
asprintf(&cmd, "rm -rf %s/files.db %s/art_cache", db_path, db_path); i = asprintf(&cmd, "rm -rf %s/files.db %s/art_cache", db_path, db_path);
system(cmd); if( i > 0 )
i = system(cmd);
else
cmd = NULL;
if( i != 0 )
{
DPRINTF(E_WARN, L_GENERAL, "Failed to clean old file cache.\n");
}
free(cmd); free(cmd);
open_db(); open_db();
if( CreateDatabase() != 0 ) if( CreateDatabase() != 0 )
@ -971,6 +977,7 @@ main(int argc, char * * argv)
start_scanner(); start_scanner();
#endif #endif
} }
signal(SIGCHLD, SIG_IGN);
#ifdef HAVE_INOTIFY #ifdef HAVE_INOTIFY
if( sqlite3_threadsafe() && sqlite3_libversion_number() >= 3005001 && if( sqlite3_threadsafe() && sqlite3_libversion_number() >= 3005001 &&
GETFLAG(INOTIFY_MASK) && pthread_create(&inotify_thread, NULL, start_inotify, NULL) ) GETFLAG(INOTIFY_MASK) && pthread_create(&inotify_thread, NULL, start_inotify, NULL) )