source: mainline/uspace/srv/logger/writer.c@ e005f92

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

Split logger into more files

  • Property mode set to 100644
File size: 4.2 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 <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"
49
50
51static logging_namespace_t *find_namespace_and_attach_writer(void)
52{
53 ipc_call_t call;
54 ipc_callid_t callid = async_get_call(&call);
55
56 if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
57 async_answer_0(callid, EINVAL);
58 return NULL;
59 }
60
61 void *name;
62 int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
63 async_answer_0(callid, rc);
64
65 if (rc != EOK) {
66 return NULL;
67 }
68
69 logging_namespace_t *result = namespace_writer_attach((const char *) name);
70
71 free(name);
72
73 return result;
74}
75
76static int handle_receive_message(logging_namespace_t *namespace, sysarg_t context, int level)
77{
78 bool skip_message = !namespace_has_reader(namespace, context, level);
79 if (skip_message) {
80 /* Abort the actual message buffer transfer. */
81 ipc_callid_t callid;
82 size_t size;
83 int rc = ENAK;
84 if (!async_data_write_receive(&callid, &size))
85 rc = EINVAL;
86
87 async_answer_0(callid, rc);
88 return rc;
89 }
90
91 void *message;
92 int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
93 if (rc != EOK) {
94 return rc;
95 }
96
97 namespace_add_message(namespace, message, context, level);
98
99 free(message);
100
101 return EOK;
102}
103
104static int handle_create_context(logging_namespace_t *namespace, sysarg_t *idx)
105{
106 void *name;
107 int rc = async_data_write_accept(&name, true, 0, 0, 0, NULL);
108 if (rc != EOK) {
109 return rc;
110 }
111
112 rc = namespace_create_context(namespace, name);
113
114 free(name);
115
116 if (rc < 0)
117 return rc;
118
119 *idx = (sysarg_t) rc;
120 return EOK;
121}
122
123void logger_connection_handler_writer(ipc_callid_t callid)
124{
125 /* First call has to be the registration. */
126 async_answer_0(callid, EOK);
127 logging_namespace_t *namespace = find_namespace_and_attach_writer();
128 if (namespace == NULL) {
129 fprintf(stderr, NAME ": failed to register namespace.\n");
130 return;
131 }
132
133 printf(NAME "/writer: new client %s.\n", namespace_get_name(namespace));
134
135 while (true) {
136 ipc_call_t call;
137 ipc_callid_t callid = async_get_call(&call);
138
139 if (!IPC_GET_IMETHOD(call))
140 break;
141
142 int rc;
143 sysarg_t arg = 0;
144
145 switch (IPC_GET_IMETHOD(call)) {
146 case LOGGER_CREATE_CONTEXT:
147 rc = handle_create_context(namespace, &arg);
148 async_answer_1(callid, rc, arg);
149 break;
150 case LOGGER_MESSAGE:
151 rc = handle_receive_message(namespace, IPC_GET_ARG1(call), IPC_GET_ARG2(call));
152 async_answer_0(callid, rc);
153 break;
154 default:
155 async_answer_0(callid, EINVAL);
156 break;
157 }
158 }
159
160 printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
161 namespace_writer_detach(namespace);
162}
163
164
165/**
166 * @}
167 */
Note: See TracBrowser for help on using the repository browser.