Log context is actually a log
[helenos.git] / uspace / srv / logger / main.c
blob2cf5a3ff89f331027378cd6de5154482e322a337
1 /*
2 * Copyright (c) 2012 Vojtech Horky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /**
30 * @defgroup logger Logging service.
31 * @brief HelenOS logging service.
32 * @{
35 /** @file
38 #include <ipc/services.h>
39 #include <ipc/logger.h>
40 #include <io/log.h>
41 #include <io/logctl.h>
42 #include <ns.h>
43 #include <async.h>
44 #include <stdio.h>
45 #include <errno.h>
46 #include <str_error.h>
47 #include <malloc.h>
48 #include "logger.h"
50 static int handle_namespace_level_change(sysarg_t new_level)
52 void *namespace_name;
53 int rc = async_data_write_accept(&namespace_name, true, 0, 0, 0, NULL);
54 if (rc != EOK) {
55 return rc;
58 logging_namespace_t *namespace = namespace_writer_attach((const char *) namespace_name);
59 free(namespace_name);
60 if (namespace == NULL)
61 return ENOENT;
63 rc = namespace_change_level(namespace, (log_level_t) new_level);
64 namespace_writer_detach(namespace);
66 return rc;
69 static int handle_context_level_change(sysarg_t new_level)
71 void *namespace_name;
72 int rc = async_data_write_accept(&namespace_name, true, 0, 0, 0, NULL);
73 if (rc != EOK) {
74 return rc;
77 logging_namespace_t *namespace = namespace_writer_attach((const char *) namespace_name);
78 free(namespace_name);
79 if (namespace == NULL)
80 return ENOENT;
82 void *context_name;
83 rc = async_data_write_accept(&context_name, true, 0, 0, 0, NULL);
84 if (rc != EOK) {
85 namespace_writer_detach(namespace);
86 return rc;
89 rc = namespace_change_context_level(namespace, context_name, new_level);
90 free(context_name);
91 namespace_writer_detach(namespace);
93 return rc;
96 static void connection_handler_control(void)
98 printf(NAME "/control: new client.\n");
100 while (true) {
101 ipc_call_t call;
102 ipc_callid_t callid = async_get_call(&call);
104 if (!IPC_GET_IMETHOD(call))
105 break;
107 int rc;
109 switch (IPC_GET_IMETHOD(call)) {
110 case LOGGER_CTL_GET_DEFAULT_LEVEL:
111 async_answer_1(callid, EOK, get_default_logging_level());
112 break;
113 case LOGGER_CTL_SET_DEFAULT_LEVEL:
114 rc = set_default_logging_level(IPC_GET_ARG1(call));
115 async_answer_0(callid, rc);
116 break;
117 case LOGGER_CTL_SET_NAMESPACE_LEVEL:
118 rc = handle_namespace_level_change(IPC_GET_ARG1(call));
119 async_answer_0(callid, rc);
120 break;
121 case LOGGER_CTL_SET_CONTEXT_LEVEL:
122 rc = handle_context_level_change(IPC_GET_ARG1(call));
123 async_answer_0(callid, rc);
124 break;
125 default:
126 async_answer_0(callid, EINVAL);
127 break;
131 printf(NAME "/control: client terminated.\n");
134 static logging_namespace_t *find_namespace_and_attach_writer(void)
136 ipc_call_t call;
137 ipc_callid_t callid = async_get_call(&call);
139 if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
140 async_answer_0(callid, EINVAL);
141 return NULL;
144 void *name;
145 int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
146 async_answer_0(callid, rc);
148 if (rc != EOK) {
149 return NULL;
152 logging_namespace_t *result = namespace_writer_attach((const char *) name);
154 free(name);
156 return result;
159 static int handle_receive_message(logging_namespace_t *namespace, sysarg_t context, int level)
161 bool skip_message = !namespace_has_reader(namespace, context, level);
162 if (skip_message) {
163 /* Abort the actual message buffer transfer. */
164 ipc_callid_t callid;
165 size_t size;
166 int rc = ENAK;
167 if (!async_data_write_receive(&callid, &size))
168 rc = EINVAL;
170 async_answer_0(callid, rc);
171 return rc;
174 void *message;
175 int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
176 if (rc != EOK) {
177 return rc;
180 namespace_add_message(namespace, message, context, level);
182 free(message);
184 return EOK;
187 static int handle_create_context(logging_namespace_t *namespace, sysarg_t *idx)
189 void *name;
190 int rc = async_data_write_accept(&name, true, 0, 0, 0, NULL);
191 if (rc != EOK) {
192 return rc;
195 rc = namespace_create_context(namespace, name);
197 free(name);
199 if (rc < 0)
200 return rc;
202 *idx = (sysarg_t) rc;
203 return EOK;
206 static void connection_handler_sink(logging_namespace_t *namespace)
208 printf(NAME "/sink: new client %s.\n", namespace_get_name(namespace));
210 while (true) {
211 ipc_call_t call;
212 ipc_callid_t callid = async_get_call(&call);
214 if (!IPC_GET_IMETHOD(call))
215 break;
217 int rc;
218 sysarg_t arg = 0;
220 switch (IPC_GET_IMETHOD(call)) {
221 case LOGGER_CREATE_CONTEXT:
222 rc = handle_create_context(namespace, &arg);
223 async_answer_1(callid, rc, arg);
224 break;
225 case LOGGER_MESSAGE:
226 rc = handle_receive_message(namespace, IPC_GET_ARG1(call), IPC_GET_ARG2(call));
227 async_answer_0(callid, rc);
228 break;
229 default:
230 async_answer_0(callid, EINVAL);
231 break;
235 printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
236 namespace_writer_detach(namespace);
239 static void connection_handler(ipc_callid_t iid, ipc_call_t *icall, void *arg)
241 logger_interface_t iface = IPC_GET_ARG1(*icall);
243 logging_namespace_t *namespace = NULL;
245 switch (iface) {
246 case LOGGER_INTERFACE_CONTROL:
247 async_answer_0(iid, EOK);
248 connection_handler_control();
249 break;
250 case LOGGER_INTERFACE_SINK:
251 /* First call has to be the registration. */
252 async_answer_0(iid, EOK);
253 namespace = find_namespace_and_attach_writer();
254 if (namespace == NULL) {
255 fprintf(stderr, NAME ": failed to register namespace.\n");
256 break;
258 connection_handler_sink(namespace);
259 break;
260 default:
261 async_answer_0(iid, EINVAL);
262 break;
266 int main(int argc, char *argv[])
268 printf(NAME ": HelenOS Logging Service\n");
270 /* Get default logging level from sysinfo (if available). */
271 log_level_t boot_logging_level = LVL_NOTE;
272 int rc = logctl_get_boot_level(&boot_logging_level);
273 if (rc == EOK)
274 set_default_logging_level(boot_logging_level);
275 else
276 printf(NAME ": Warn: failed to get logging level from sysinfo: %s.\n",
277 str_error(rc));
279 async_set_client_connection(connection_handler);
281 rc = service_register(SERVICE_LOGGER);
282 if (rc != EOK) {
283 printf(NAME ": failed to register: %s.\n", str_error(rc));
284 return -1;
287 printf(NAME ": Accepting connections\n");
288 async_manager();
290 /* Never reached */
291 return 0;
295 * @}