1 /*
2  * Copyright (c) 2011-2012 - Mauro Carvalho Chehab
3  * Copyright (c) 2012 - Andre Roth <neolynx@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation version 2.1 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18  *
19  */
20 
21 #include <libdvbv5/dvb-log.h>
22 
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdarg.h>
26 
27 #include <config.h>
28 
29 #ifdef ENABLE_NLS
30 # include <stdio.h>
31 # include <libintl.h>
32 # define _(string) dgettext(LIBDVBV5_DOMAIN, string)
33 
34 #else
35 # define _(string) string
36 #endif
37 
38 # define N_(string) string
39 
40 
41 static const struct loglevel {
42 	const char *name;
43 	const char *color;
44 	int fd;
45 } loglevels[9] = {
46 	{N_("EMERG    "), "\033[31m", STDERR_FILENO },
47 	{N_("ALERT    "), "\033[31m", STDERR_FILENO },
48 	{N_("CRITICAL "), "\033[31m", STDERR_FILENO },
49 	{N_("ERROR    "), "\033[31m", STDERR_FILENO },
50 	{N_("WARNING  "), "\033[33m", STDOUT_FILENO },
51 	{NULL,            "\033[36m", STDOUT_FILENO }, /* NOTICE */
52 	{NULL,            NULL,       STDOUT_FILENO }, /* INFO */
53 	{N_("DEBUG    "), "\033[32m", STDOUT_FILENO },
54 	{NULL,            "\033[0m",  STDOUT_FILENO }, /* reset*/
55 };
56 #define LOG_COLOROFF 8
57 
dvb_default_log(int level,const char * fmt,...)58 void dvb_default_log(int level, const char *fmt, ...)
59 {
60 	if(level > sizeof(loglevels) / sizeof(struct loglevel) - 2) // ignore LOG_COLOROFF as well
61 		level = LOG_INFO;
62 	va_list ap;
63 	va_start(ap, fmt);
64 	FILE *out = stdout;
65 	if (STDERR_FILENO == loglevels[level].fd)
66 		out = stderr;
67 	if (loglevels[level].color && isatty(loglevels[level].fd))
68 		fputs(loglevels[level].color, out);
69 	if (loglevels[level].name)
70 		fprintf(out, "%s", _(loglevels[level].name));
71 	vfprintf(out, fmt, ap);
72 	fprintf(out, "\n");
73 	if(loglevels[level].color && isatty(loglevels[level].fd))
74 		fputs(loglevels[LOG_COLOROFF].color, out);
75 	va_end(ap);
76 }
77 
78