1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright 2006-2008 Roy Marples <roy@marples.name>
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <syslog.h>
34
35 #include "common.h"
36 #include "logger.h"
37
38 #ifdef ANDROID
39 #include <android/log.h>
40 #endif
41
42 static int loglevel = LOG_INFO;
43 static char logprefix[12] = {0};
44
45 void
setloglevel(int level)46 setloglevel(int level)
47 {
48 loglevel = level;
49 }
50
51 void
setlogprefix(const char * prefix)52 setlogprefix(const char *prefix)
53 {
54 strlcpy(logprefix, prefix, sizeof(logprefix));
55 }
56
57 void
logger(int level,const char * fmt,...)58 logger(int level, const char *fmt, ...)
59 {
60 va_list p, p2;
61 FILE *f = stderr;
62 size_t len, fmt2len;
63 char *fmt2, *pf;
64
65 va_start(p, fmt);
66 #ifdef ANDROID
67 if (level <= loglevel) {
68 if (level <= LOG_ERR) {
69 level = ANDROID_LOG_ERROR;
70 } else {
71 level = ANDROID_LOG_DEBUG;
72 }
73 __android_log_vprint(level, "dhcpcd", fmt, p);
74 }
75 #else
76 va_copy(p2, p);
77
78 if (level <= LOG_ERR || level <= loglevel) {
79 fprintf(f, "%s", logprefix);
80 vfprintf(f, fmt, p);
81 fputc('\n', f);
82 }
83
84 if (level < LOG_DEBUG || level <= loglevel) {
85 len = strlen(logprefix);
86 fmt2len = strlen(fmt) + len + 1;
87 fmt2 = pf = malloc(sizeof(char) * fmt2len);
88 if (fmt2) {
89 strlcpy(pf, logprefix, fmt2len);
90 pf += len;
91 strlcpy(pf, fmt, fmt2len - len);
92 vsyslog(level, fmt2, p2);
93 free(fmt2);
94 } else {
95 vsyslog(level, fmt, p2);
96 syslog(LOG_ERR, "logger: memory exhausted");
97 exit(EXIT_FAILURE);
98 }
99 }
100
101 va_end(p2);
102 #endif
103 va_end(p);
104 }
105