1 /*	$Id: log.c 20894 2012-01-25 12:47:57Z m-oki $	*/
2 
3 /*
4  * Copyright (c) 2012, Internet Initiative Japan, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "config.h"
31 
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <inttypes.h>
35 #include <string.h>
36 #include <time.h>
37 
38 #include <libarms.h>
39 #include <libarms_resource.h>
40 #include <libarms_log.h>
41 
42 /*
43  *
44  */
45 static const char *
libarms_strlog(int type)46 libarms_strlog(int type)
47 {
48 	static char estr[256];
49 	static const char *str = NULL;
50 	static char defstr[256];
51 
52 	switch (type) {
53 		/* Basic log */
54 		case ARMS_LOG_EFALLBACK:
55 			str = "Fallback to previous state";
56 			break;
57 		case ARMS_LOG_ILS_ACCESS_START:
58 			str = "Connecting to LS";
59 			break;
60 		case ARMS_LOG_ILS_ACCESS_END:
61 			str = "LS Access Done";
62 			break;
63 		case ARMS_LOG_ELS_ACCESS_FAIL:
64 			str = "Failed to get location config from LS";
65 			break;
66 		case ARMS_LOG_IRS_ACCESS_START:
67 			str = "Connecting to RS";
68 			break;
69 		case ARMS_LOG_IRS_ACCESS_END:
70 			str = "RS Access Done";
71 			break;
72 		case ARMS_LOG_ERS_ACCESS_FAIL:
73 			str = "Failed to get configuration from RS";
74 			break;
75 		/* Line */
76 		case ARMS_LOG_ELINE_AUTH_FAIL:
77 			str = "Line Authentication Failure";
78 			break;
79 		case ARMS_LOG_ELINE_TIMEOUT:
80 			str = "Line Timeout";
81 			break;
82 		/* HTTP */
83 		case ARMS_LOG_IHTTP_CONNECT_START:
84 			str = "Connecting to ARMS Service";
85 			break;
86 		case ARMS_LOG_IHTTP_CONNECT_END:
87 			str = "Connected to ARMS Service";
88 			break;
89 		case ARMS_LOG_IHTTP_LISTEN_START:
90 			str = "Ready to answer PUSH Request";
91 			break;
92 		case ARMS_LOG_IHTTP_ACCEPT:
93 			str = "Accepting PUSH Request";
94 			break;
95 		case ARMS_LOG_IHTTP_CLOSE:
96 			str = "PUSH Request done.";
97 			break;
98 		/* Network Log */
99 		case ARMS_LOG_EURL:
100 			str = "Invalid URL";
101 			break;
102 		case ARMS_LOG_EHOST:
103 			str = "Unknown HOST";
104 			break;
105 		case ARMS_LOG_ESOCKET:
106 			str = "Socket Level Error";
107 			break;
108 		case ARMS_LOG_ECONNECT:
109 			str = "IP/TCP/SSL Level Error";
110 			break;
111 		case ARMS_LOG_ENETNOMEM:
112 			str = "Memroy Exhausted(Network)";
113 			break;
114 		case ARMS_LOG_EHTTP:
115 			str = "HTTP Level Error";
116 			break;
117 		case ARMS_LOG_ECERTIFICATE:
118 			str = "Invalid Server Certificate";
119 			break;
120 		case ARMS_LOG_ENETTIMEOUT:
121 			str = "Network Timeout";
122 			break;
123 		case ARMS_LOG_ECALLBACK:
124 			str = "Callback Function Error";
125 			break;
126 		case ARMS_LOG_DEBUG:
127 			str = "DEBUG";
128 			break;
129 		default:
130 			memset(estr, 0, sizeof(estr));
131 			snprintf(estr, sizeof(estr),
132 				 "No library default string(%d)", type);
133 			str = estr;
134 			break;
135 	}
136 
137 	if (str == NULL) {
138 		memset(defstr, 0, sizeof(defstr));
139 		snprintf(defstr, sizeof(defstr), "No String(%d)", type);
140 		return defstr;
141 	}
142 
143 	return str;
144 }
145 
146 /*
147  *
148  */
149 int
libarms_log(int type,const char * fmt,...)150 libarms_log(int type, const char *fmt, ...)
151 {
152 	va_list ap;
153 	char buf[LOG_BUFSIZ + 1];
154 	const char *str;
155 	int err = 0;
156 	arms_context_t *res = arms_get_context();
157 
158 	if (res == NULL)
159 		return err;
160 
161 	if (fmt) {
162 		memset(buf, 0, sizeof(buf));
163 		va_start(ap, fmt);
164 		vsnprintf(buf, sizeof(buf), fmt, ap);
165 		va_end(ap);
166 		str = buf;
167 	}
168 	else {
169 		str = libarms_strlog(type);
170 	}
171 
172 	if (res->callbacks.log_cb) {
173 		int log_code;
174 
175 		log_code = ARMS_LOG_CODE(0, 0, type);
176 		err = res->callbacks.log_cb(log_code, str, res->udata);
177 	}
178 
179 	return err;
180 }
181