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 #ifdef ENABLE_NLS
28 # include "gettext.h"
29 # include <libintl.h>
30 # define _(string) dgettext(LIBDVBV5_DOMAIN, string)
31
32 #else
33 # define _(string) string
34 #endif
35
36 # define N_(string) string
37
38
39 static const struct loglevel {
40 const char *name;
41 const char *color;
42 int fd;
43 } loglevels[9] = {
44 {N_("EMERG "), "\033[31m", STDERR_FILENO },
45 {N_("ALERT "), "\033[31m", STDERR_FILENO },
46 {N_("CRITICAL "), "\033[31m", STDERR_FILENO },
47 {N_("ERROR "), "\033[31m", STDERR_FILENO },
48 {N_("WARNING "), "\033[33m", STDOUT_FILENO },
49 {NULL, "\033[36m", STDOUT_FILENO }, /* NOTICE */
50 {NULL, NULL, STDOUT_FILENO }, /* INFO */
51 {N_("DEBUG "), "\033[32m", STDOUT_FILENO },
52 {NULL, "\033[0m", STDOUT_FILENO }, /* reset*/
53 };
54 #define LOG_COLOROFF 8
55
dvb_default_log(int level,const char * fmt,...)56 void dvb_default_log(int level, const char *fmt, ...)
57 {
58 if(level > sizeof(loglevels) / sizeof(struct loglevel) - 2) // ignore LOG_COLOROFF as well
59 level = LOG_INFO;
60 va_list ap;
61 va_start(ap, fmt);
62 FILE *out = stdout;
63 if (STDERR_FILENO == loglevels[level].fd)
64 out = stderr;
65 if (loglevels[level].color && isatty(loglevels[level].fd))
66 fputs(loglevels[level].color, out);
67 if (loglevels[level].name)
68 fprintf(out, "%s", _(loglevels[level].name));
69 vfprintf(out, fmt, ap);
70 fprintf(out, "\n");
71 if(loglevels[level].color && isatty(loglevels[level].fd))
72 fputs(loglevels[LOG_COLOROFF].color, out);
73 va_end(ap);
74 }
75
76