log.c 2.2 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* the Music Player Daemon (MPD)
 * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
 * This project's homepage is: http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "log.h"

#include "conf.h"
#include "myfprintf.h"
23
#include "utils.h"
Warren Dukes's avatar
Warren Dukes committed
24

25
#include <stdlib.h>
Warren Dukes's avatar
Warren Dukes committed
26
#include <string.h>
27
#include <stdarg.h>
Warren Dukes's avatar
Warren Dukes committed
28 29

int logLevel = LOG_LEVEL_LOW;
30 31 32
short warningFlushed = 0;

static char * warningBuffer = NULL;
Warren Dukes's avatar
Warren Dukes committed
33 34

void initLog() {
35 36 37 38 39
	ConfigParam * param = getConfigParam(CONF_LOG_LEVEL);

	if(!param) return;

	if(0 == strcmp(param->value, "default")) {
Warren Dukes's avatar
Warren Dukes committed
40 41
		if(logLevel<LOG_LEVEL_LOW) logLevel = LOG_LEVEL_LOW;
	}
42
	else if(0 == strcmp(param->value, "secure")) {
Warren Dukes's avatar
Warren Dukes committed
43 44
		if(logLevel<LOG_LEVEL_SECURE) logLevel = LOG_LEVEL_SECURE;
	}
45
	else if(0 == strcmp(param->value, "verbose")) {
Warren Dukes's avatar
Warren Dukes committed
46 47
		if(logLevel<LOG_LEVEL_DEBUG) logLevel = LOG_LEVEL_DEBUG;
	}
48 49 50 51 52
	else {
		ERROR("unknown log level \"%s\" at line %i\n",
				param->value, param->line);
		exit(EXIT_FAILURE);
	}
Warren Dukes's avatar
Warren Dukes committed
53
}
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

#define BUFFER_LENGTH	4096

void bufferWarning(char * format, ... ) {
	va_list arglist;
	char temp[BUFFER_LENGTH+1];

	memset(temp, 0, BUFFER_LENGTH+1);

	va_start(arglist, format);

	vsnprintf(temp, BUFFER_LENGTH, format, arglist);

	warningBuffer = appendToString(warningBuffer, temp);

	va_end(arglist);
}
71

72 73 74
void flushWarningLog() {
	char * s;

75 76
	DEBUG("flushing warning messages\n");

77 78 79 80 81 82 83 84 85 86 87 88 89 90
	if(warningBuffer == NULL) return;

	s = strtok(warningBuffer, "\n");
	while ( s != NULL ) {
		myfprintf(stderr, "%s\n", s);

		s = strtok(NULL, "\n");
	}

	free(warningBuffer);
	warningBuffer = NULL;

	warningFlushed = 1;
}