1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <assert.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #include <fibril_synch.h>
|
---|
37 | #include <stdarg.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <ipc/logger.h>
|
---|
43 | #include <ns.h>
|
---|
44 |
|
---|
45 | /** Log messages are printed under this name. */
|
---|
46 | static const char *log_prog_name;
|
---|
47 |
|
---|
48 | /** IPC session with the logger service. */
|
---|
49 | static async_sess_t *logger_session;
|
---|
50 |
|
---|
51 | /** Maximum length of a single log message (in bytes). */
|
---|
52 | #define MESSAGE_BUFFER_SIZE 4096
|
---|
53 |
|
---|
54 | static int logger_register(async_sess_t *session, const char *prog_name)
|
---|
55 | {
|
---|
56 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
57 | if (exchange == NULL) {
|
---|
58 | return ENOMEM;
|
---|
59 | }
|
---|
60 |
|
---|
61 | aid_t reg_msg = async_send_0(exchange, LOGGER_REGISTER, NULL);
|
---|
62 | int rc = async_data_write_start(exchange, prog_name, str_size(prog_name));
|
---|
63 | sysarg_t reg_msg_rc;
|
---|
64 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
65 |
|
---|
66 | async_exchange_end(exchange);
|
---|
67 |
|
---|
68 | if (rc != EOK) {
|
---|
69 | return rc;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return reg_msg_rc;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static int logger_message(async_sess_t *session, log_level_t level, const char *message)
|
---|
76 | {
|
---|
77 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
78 | if (exchange == NULL) {
|
---|
79 | return ENOMEM;
|
---|
80 | }
|
---|
81 |
|
---|
82 | aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL);
|
---|
83 | int rc = async_data_write_start(exchange, message, str_size(message));
|
---|
84 | sysarg_t reg_msg_rc;
|
---|
85 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
86 |
|
---|
87 | async_exchange_end(exchange);
|
---|
88 |
|
---|
89 | if (rc != EOK) {
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return reg_msg_rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Initialize the logging system.
|
---|
97 | *
|
---|
98 | * @param prog_name Program name, will be printed as part of message
|
---|
99 | * @param level Minimum message level to print
|
---|
100 | */
|
---|
101 | int log_init(const char *prog_name, log_level_t level)
|
---|
102 | {
|
---|
103 | assert(level < LVL_LIMIT);
|
---|
104 |
|
---|
105 | log_prog_name = str_dup(prog_name);
|
---|
106 | if (log_prog_name == NULL)
|
---|
107 | return ENOMEM;
|
---|
108 |
|
---|
109 | logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
|
---|
110 | if (logger_session == NULL) {
|
---|
111 | return ENOMEM;
|
---|
112 | }
|
---|
113 |
|
---|
114 | int rc = logger_register(logger_session, log_prog_name);
|
---|
115 |
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Write an entry to the log.
|
---|
120 | *
|
---|
121 | * @param level Message verbosity level. Message is only printed
|
---|
122 | * if verbosity is less than or equal to current
|
---|
123 | * reporting level.
|
---|
124 | * @param fmt Format string (no traling newline).
|
---|
125 | */
|
---|
126 | void log_msg(log_level_t level, const char *fmt, ...)
|
---|
127 | {
|
---|
128 | va_list args;
|
---|
129 |
|
---|
130 | va_start(args, fmt);
|
---|
131 | log_msgv(level, fmt, args);
|
---|
132 | va_end(args);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Write an entry to the log (va_list variant).
|
---|
136 | *
|
---|
137 | * @param level Message verbosity level. Message is only printed
|
---|
138 | * if verbosity is less than or equal to current
|
---|
139 | * reporting level.
|
---|
140 | * @param fmt Format string (no trailing newline)
|
---|
141 | */
|
---|
142 | void log_msgv(log_level_t level, const char *fmt, va_list args)
|
---|
143 | {
|
---|
144 | assert(level < LVL_LIMIT);
|
---|
145 |
|
---|
146 | char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
|
---|
147 | if (message_buffer == NULL) {
|
---|
148 | return;
|
---|
149 | }
|
---|
150 |
|
---|
151 | vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
|
---|
152 | logger_message(logger_session, level, message_buffer);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** @}
|
---|
156 | */
|
---|