[e005f92] | 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>
|
---|
[bd6ff94] | 42 | #include <io/klog.h>
|
---|
[e005f92] | 43 | #include <ns.h>
|
---|
| 44 | #include <async.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 | #include <errno.h>
|
---|
| 47 | #include <str_error.h>
|
---|
| 48 | #include <malloc.h>
|
---|
| 49 | #include "logger.h"
|
---|
| 50 |
|
---|
| 51 |
|
---|
[f039dba] | 52 | static logger_log_t *handle_create_log(sysarg_t parent)
|
---|
[e005f92] | 53 | {
|
---|
| 54 | void *name;
|
---|
[cba45af] | 55 | int rc = async_data_write_accept(&name, true, 1, 0, 0, NULL);
|
---|
| 56 | if (rc != EOK)
|
---|
[e005f92] | 57 | return NULL;
|
---|
| 58 |
|
---|
[1dec7cb] | 59 | logger_log_t *log = find_or_create_log_and_lock(name, parent);
|
---|
[e005f92] | 60 |
|
---|
| 61 | free(name);
|
---|
| 62 |
|
---|
[cba45af] | 63 | return log;
|
---|
[e005f92] | 64 | }
|
---|
| 65 |
|
---|
[f039dba] | 66 | static int handle_receive_message(sysarg_t log_id, sysarg_t level)
|
---|
[e005f92] | 67 | {
|
---|
[1dec7cb] | 68 | logger_log_t *log = find_log_by_id_and_lock(log_id);
|
---|
[cba45af] | 69 | if (log == NULL)
|
---|
| 70 | return ENOENT;
|
---|
| 71 |
|
---|
[3cf862f] | 72 | void *message = NULL;
|
---|
[cba45af] | 73 | int rc = async_data_write_accept(&message, true, 1, 0, 0, NULL);
|
---|
| 74 | if (rc != EOK)
|
---|
[3cf862f] | 75 | goto leave;
|
---|
[cba45af] | 76 |
|
---|
[f039dba] | 77 | if (!shall_log_message(log, level)) {
|
---|
[3cf862f] | 78 | rc = EOK;
|
---|
| 79 | goto leave;
|
---|
[e005f92] | 80 | }
|
---|
| 81 |
|
---|
[bd6ff94] | 82 | KLOG_PRINTF(level, "[%s] %s: %s\n",
|
---|
[f039dba] | 83 | log->full_name, log_level_str(level),
|
---|
[cba45af] | 84 | (const char *) message);
|
---|
[90dc458] | 85 | write_to_log(log, level, message);
|
---|
[e005f92] | 86 |
|
---|
[3cf862f] | 87 | rc = EOK;
|
---|
| 88 |
|
---|
| 89 | leave:
|
---|
[1dec7cb] | 90 | log_unlock(log);
|
---|
[e005f92] | 91 | free(message);
|
---|
| 92 |
|
---|
[3cf862f] | 93 | return rc;
|
---|
[e005f92] | 94 | }
|
---|
| 95 |
|
---|
| 96 | void logger_connection_handler_writer(ipc_callid_t callid)
|
---|
| 97 | {
|
---|
[cba45af] | 98 | /* Acknowledge the connection. */
|
---|
[e005f92] | 99 | async_answer_0(callid, EOK);
|
---|
| 100 |
|
---|
[42bde6a] | 101 | logger_log("writer: new client.\n");
|
---|
[e005f92] | 102 |
|
---|
[131d9a4] | 103 | logger_registered_logs_t registered_logs;
|
---|
| 104 | registered_logs_init(®istered_logs);
|
---|
| 105 |
|
---|
[e005f92] | 106 | while (true) {
|
---|
| 107 | ipc_call_t call;
|
---|
| 108 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 109 |
|
---|
| 110 | if (!IPC_GET_IMETHOD(call))
|
---|
| 111 | break;
|
---|
| 112 |
|
---|
| 113 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[f039dba] | 114 | case LOGGER_WRITER_CREATE_LOG: {
|
---|
| 115 | logger_log_t *log = handle_create_log(IPC_GET_ARG1(call));
|
---|
[cba45af] | 116 | if (log == NULL) {
|
---|
| 117 | async_answer_0(callid, ENOMEM);
|
---|
| 118 | break;
|
---|
| 119 | }
|
---|
[131d9a4] | 120 | if (!register_log(®istered_logs, log)) {
|
---|
| 121 | log_unlock(log);
|
---|
| 122 | async_answer_0(callid, ELIMIT);
|
---|
| 123 | break;
|
---|
| 124 | }
|
---|
[1dec7cb] | 125 | log_unlock(log);
|
---|
[cba45af] | 126 | async_answer_1(callid, EOK, (sysarg_t) log);
|
---|
[e005f92] | 127 | break;
|
---|
[cba45af] | 128 | }
|
---|
| 129 | case LOGGER_WRITER_MESSAGE: {
|
---|
| 130 | int rc = handle_receive_message(IPC_GET_ARG1(call),
|
---|
[f039dba] | 131 | IPC_GET_ARG2(call));
|
---|
[e005f92] | 132 | async_answer_0(callid, rc);
|
---|
| 133 | break;
|
---|
[cba45af] | 134 | }
|
---|
[e005f92] | 135 | default:
|
---|
| 136 | async_answer_0(callid, EINVAL);
|
---|
| 137 | break;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[131d9a4] | 141 | unregister_logs(®istered_logs);
|
---|
[42bde6a] | 142 | logger_log("writer: client terminated.\n");
|
---|
[e005f92] | 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * @}
|
---|
| 148 | */
|
---|