* Add some logging and forking tweaks to work better with systemd.

This commit is contained in:
Justin Maggard 2012-10-04 21:04:36 +00:00
parent de15926f76
commit 2e120e83a0
5 changed files with 43 additions and 25 deletions

37
log.c
View File

@ -25,6 +25,7 @@
#include <string.h>
#include <time.h>
#include "upnpglobalvars.h"
#include "log.h"
static FILE *log_fp = NULL;
@ -123,11 +124,7 @@ log_init(const char *fname, const char *debug)
void
log_err(int level, enum _log_facility facility, char *fname, int lineno, char *fmt, ...)
{
//char errbuf[1024];
char * errbuf;
va_list ap;
time_t t;
struct tm *tm;
if (level && level>log_level[facility] && level>E_FATAL)
return;
@ -135,29 +132,33 @@ log_err(int level, enum _log_facility facility, char *fname, int lineno, char *f
if (!log_fp)
log_fp = stdout;
// timestamp
if (!GETFLAG(SYSTEMD_MASK))
{
time_t t;
struct tm *tm;
t = time(NULL);
tm = localtime(&t);
fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
if (level)
fprintf(log_fp, "%s:%d: %s: ", fname, lineno, level_name[level]);
else
fprintf(log_fp, "%s:%d: ", fname, lineno);
// user log
va_start(ap, fmt);
//vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
if (vasprintf(&errbuf, fmt, ap) == -1)
if (vfprintf(log_fp, fmt, ap) == -1)
{
va_end(ap);
return;
}
va_end(ap);
// timestamp
t = time(NULL);
tm = localtime(&t);
fprintf(log_fp, "[%04d/%02d/%02d %02d:%02d:%02d] ",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
if (level)
fprintf(log_fp, "%s:%d: %s: %s", fname, lineno, level_name[level], errbuf);
else
fprintf(log_fp, "%s:%d: %s", fname, lineno, errbuf);
fflush(log_fp);
free(errbuf);
if (level==E_FATAL)
exit(-1);

View File

@ -668,7 +668,7 @@ no_exifdata:
m.dlna_pn = strdup("JPEG_SM");
else if( width <= 1024 && height <= 768 )
m.dlna_pn = strdup("JPEG_MED");
else if( (width <= 4096 && height <= 4096) || !(GETFLAG(DLNA_STRICT_MASK)) )
else if( (width <= 4096 && height <= 4096) || !GETFLAG(DLNA_STRICT_MASK) )
m.dlna_pn = strdup("JPEG_LRG");
xasprintf(&m.resolution, "%dx%d", width, height);

View File

@ -930,6 +930,11 @@ init(int argc, char * * argv)
}
}
break;
#ifdef __linux__
case 'S':
SETFLAG(SYSTEMD_MASK);
break;
#endif
case 'V':
printf("Version " MINIDLNA_VERSION "\n");
exit(0);
@ -970,6 +975,9 @@ init(int argc, char * * argv)
"\t-h displays this text\n"
"\t-R forces a full rescan\n"
"\t-L do note create playlists\n"
#ifdef __linux__
"\t-S changes behaviour for systemd\n"
#endif
"\t-V print the version number\n",
argv[0], pidfilename);
return 1;
@ -984,26 +992,33 @@ init(int argc, char * * argv)
{
log_level = log_str;
}
/* Set the default log file path to NULL (stdout) */
path = NULL;
if(debug_flag)
{
pid = getpid();
strcpy(log_str+65, "maxdebug");
log_level = log_str;
log_init(NULL, log_level);
}
else if(GETFLAG(SYSTEMD_MASK))
{
pid = getpid();
}
else
{
pid = daemonize();
#ifdef READYNAS
unlink("/ramfs/.upnp-av_scan");
log_init("/var/log/upnp-av.log", log_level);
path = "/var/log/upnp-av.log";
#else
if( access(db_path, F_OK) != 0 )
make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
sprintf(buf, "%s/minidlna.log", log_path);
log_init(buf, log_level);
snprintf(buf, sizeof(buf), "%s/minidlna.log", log_path);
path = buf;
#endif
}
log_init(path, log_level);
if (checkforrunning(pidfilename) < 0)
{

View File

@ -252,7 +252,8 @@ _asf_load_string(FILE *fp, int type, int size, char *buf, int len)
case ASF_VT_UNICODE:
for(j = 0; j < size; j += 2)
{
wc = *(__s16*)&data[j];
wd16 = (__s16 *) &data[j];
wc = (__u16)*wd16;
i += utf16le_to_utf8(&buf[i], len - i, wc);
}
break;

View File

@ -188,9 +188,10 @@ extern uint32_t runtime_flags;
#define TIVO_MASK 0x0002
#define DLNA_STRICT_MASK 0x0004
#define NO_PLAYLIST_MASK 0x0008
#define SYSTEMD_MASK 0x0010
#define SETFLAG(mask) runtime_flags |= mask
#define GETFLAG(mask) runtime_flags & mask
#define GETFLAG(mask) (runtime_flags & mask)
#define CLEARFLAG(mask) runtime_flags &= ~mask
extern const char * pidfilename;