source: mainline/uspace/srv/logger/main.c@ 0a6a996

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0a6a996 was 6e9e12b, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Get rid of logview for now

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2012 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/**
30 * @defgroup logger Logging service.
31 * @brief HelenOS logging service.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <ipc/services.h>
39#include <ipc/logger.h>
40#include <io/log.h>
41#include <ns.h>
42#include <async.h>
43#include <stdio.h>
44#include <errno.h>
45#include <str_error.h>
46#include <malloc.h>
47#include "logger.h"
48
49static void connection_handler_control(void)
50{
51 printf(NAME "/control: new client.\n");
52
53 while (true) {
54 ipc_call_t call;
55 ipc_callid_t callid = async_get_call(&call);
56
57 if (!IPC_GET_IMETHOD(call))
58 break;
59
60 int rc;
61
62 switch (IPC_GET_IMETHOD(call)) {
63 case LOGGER_CTL_GET_DEFAULT_LEVEL:
64 async_answer_1(callid, EOK, get_default_logging_level());
65 break;
66 case LOGGER_CTL_SET_DEFAULT_LEVEL:
67 rc = set_default_logging_level(IPC_GET_ARG1(call));
68 async_answer_0(callid, rc);
69 break;
70 default:
71 async_answer_0(callid, EINVAL);
72 break;
73 }
74 }
75
76 printf(NAME "/control: client terminated.\n");
77}
78
79static logging_namespace_t *find_namespace_and_attach_writer(void)
80{
81 ipc_call_t call;
82 ipc_callid_t callid = async_get_call(&call);
83
84 if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
85 async_answer_0(callid, EINVAL);
86 return NULL;
87 }
88
89 void *name;
90 int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
91 async_answer_0(callid, rc);
92
93 if (rc != EOK) {
94 return NULL;
95 }
96
97 logging_namespace_t *result = namespace_writer_attach((const char *) name);
98
99 free(name);
100
101 return result;
102}
103
104static int handle_receive_message(logging_namespace_t *namespace, int level)
105{
106 bool skip_message = !namespace_has_reader(namespace, level);
107 if (skip_message) {
108 /* Abort the actual message buffer transfer. */
109 ipc_callid_t callid;
110 size_t size;
111 int rc = ENAK;
112 if (!async_data_write_receive(&callid, &size))
113 rc = EINVAL;
114
115 async_answer_0(callid, rc);
116 return rc;
117 }
118
119 void *message;
120 int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
121 if (rc != EOK) {
122 return rc;
123 }
124
125 namespace_add_message(namespace, message, level);
126
127 free(message);
128
129 return EOK;
130}
131
132static void connection_handler_sink(logging_namespace_t *namespace)
133{
134 printf(NAME "/sink: new client %s.\n", namespace_get_name(namespace));
135
136 while (true) {
137 ipc_call_t call;
138 ipc_callid_t callid = async_get_call(&call);
139
140 if (!IPC_GET_IMETHOD(call))
141 break;
142
143 int rc;
144
145 switch (IPC_GET_IMETHOD(call)) {
146 case LOGGER_MESSAGE:
147 rc = handle_receive_message(namespace, IPC_GET_ARG1(call));
148 async_answer_0(callid, rc);
149 break;
150 default:
151 async_answer_0(callid, EINVAL);
152 break;
153 }
154 }
155
156 printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
157 namespace_writer_detach(namespace);
158}
159
160static void connection_handler(ipc_callid_t iid, ipc_call_t *icall, void *arg)
161{
162 logger_interface_t iface = IPC_GET_ARG1(*icall);
163
164 logging_namespace_t *namespace = NULL;
165
166 switch (iface) {
167 case LOGGER_INTERFACE_CONTROL:
168 async_answer_0(iid, EOK);
169 connection_handler_control();
170 break;
171 case LOGGER_INTERFACE_SINK:
172 /* First call has to be the registration. */
173 async_answer_0(iid, EOK);
174 namespace = find_namespace_and_attach_writer();
175 if (namespace == NULL) {
176 fprintf(stderr, NAME ": failed to register namespace.\n");
177 break;
178 }
179 connection_handler_sink(namespace);
180 break;
181 default:
182 async_answer_0(iid, EINVAL);
183 break;
184 }
185}
186
187int main(int argc, char *argv[])
188{
189 printf(NAME ": HelenOS Logging Service\n");
190
191 async_set_client_connection(connection_handler);
192
193 int rc = service_register(SERVICE_LOGGER);
194 if (rc != EOK) {
195 printf(NAME ": failed to register: %s.\n", str_error(rc));
196 return -1;
197 }
198
199 printf(NAME ": Accepting connections\n");
200 async_manager();
201
202 /* Never reached */
203 return 0;
204}
205
206/**
207 * @}
208 */
Note: See TracBrowser for help on using the repository browser.