| [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>
|
|---|
| 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 |
|
|---|
| [f039dba] | 51 | static logger_log_t *handle_create_log(sysarg_t parent)
|
|---|
| [e005f92] | 52 | {
|
|---|
| 53 | void *name;
|
|---|
| [cba45af] | 54 | int rc = async_data_write_accept(&name, true, 1, 0, 0, NULL);
|
|---|
| 55 | if (rc != EOK)
|
|---|
| [e005f92] | 56 | return NULL;
|
|---|
| 57 |
|
|---|
| [90dc458] | 58 | logger_log_t *log = NULL;
|
|---|
| 59 | rc = find_or_create_log_and_acquire(name, parent, &log);
|
|---|
| 60 | if (rc)
|
|---|
| [e005f92] | 61 |
|
|---|
| 62 | free(name);
|
|---|
| 63 |
|
|---|
| [cba45af] | 64 | return log;
|
|---|
| [e005f92] | 65 | }
|
|---|
| 66 |
|
|---|
| [f039dba] | 67 | static int handle_receive_message(sysarg_t log_id, sysarg_t level)
|
|---|
| [e005f92] | 68 | {
|
|---|
| [3cf862f] | 69 | logger_log_t *log = find_log_by_id_and_acquire(log_id);
|
|---|
| [cba45af] | 70 | if (log == NULL)
|
|---|
| 71 | return ENOENT;
|
|---|
| 72 |
|
|---|
| [3cf862f] | 73 | void *message = NULL;
|
|---|
| [cba45af] | 74 | int rc = async_data_write_accept(&message, true, 1, 0, 0, NULL);
|
|---|
| 75 | if (rc != EOK)
|
|---|
| [3cf862f] | 76 | goto leave;
|
|---|
| [cba45af] | 77 |
|
|---|
| [f039dba] | 78 | if (!shall_log_message(log, level)) {
|
|---|
| [3cf862f] | 79 | rc = EOK;
|
|---|
| 80 | goto leave;
|
|---|
| [e005f92] | 81 | }
|
|---|
| 82 |
|
|---|
| [f039dba] | 83 | printf("[%s] %s: %s\n",
|
|---|
| 84 | log->full_name, log_level_str(level),
|
|---|
| [cba45af] | 85 | (const char *) message);
|
|---|
| [90dc458] | 86 | write_to_log(log, level, message);
|
|---|
| [e005f92] | 87 |
|
|---|
| [3cf862f] | 88 | rc = EOK;
|
|---|
| 89 |
|
|---|
| 90 | leave:
|
|---|
| 91 | log_release(log);
|
|---|
| [e005f92] | 92 | free(message);
|
|---|
| 93 |
|
|---|
| [3cf862f] | 94 | return rc;
|
|---|
| [e005f92] | 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void logger_connection_handler_writer(ipc_callid_t callid)
|
|---|
| 98 | {
|
|---|
| [cba45af] | 99 | /* Acknowledge the connection. */
|
|---|
| [e005f92] | 100 | async_answer_0(callid, EOK);
|
|---|
| 101 |
|
|---|
| [cba45af] | 102 | printf(NAME "/writer: new client.\n");
|
|---|
| [e005f92] | 103 |
|
|---|
| 104 | while (true) {
|
|---|
| 105 | ipc_call_t call;
|
|---|
| 106 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 107 |
|
|---|
| 108 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 109 | break;
|
|---|
| 110 |
|
|---|
| 111 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| [f039dba] | 112 | case LOGGER_WRITER_CREATE_LOG: {
|
|---|
| 113 | logger_log_t *log = handle_create_log(IPC_GET_ARG1(call));
|
|---|
| [cba45af] | 114 | if (log == NULL) {
|
|---|
| 115 | async_answer_0(callid, ENOMEM);
|
|---|
| 116 | break;
|
|---|
| 117 | }
|
|---|
| [3cf862f] | 118 | log_release(log);
|
|---|
| [cba45af] | 119 | async_answer_1(callid, EOK, (sysarg_t) log);
|
|---|
| [e005f92] | 120 | break;
|
|---|
| [cba45af] | 121 | }
|
|---|
| 122 | case LOGGER_WRITER_MESSAGE: {
|
|---|
| 123 | int rc = handle_receive_message(IPC_GET_ARG1(call),
|
|---|
| [f039dba] | 124 | IPC_GET_ARG2(call));
|
|---|
| [e005f92] | 125 | async_answer_0(callid, rc);
|
|---|
| 126 | break;
|
|---|
| [cba45af] | 127 | }
|
|---|
| [e005f92] | 128 | default:
|
|---|
| 129 | async_answer_0(callid, EINVAL);
|
|---|
| 130 | break;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| [cba45af] | 134 | // FIXME: destroy created logs
|
|---|
| [e005f92] | 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * @}
|
|---|
| 140 | */
|
|---|