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 | /** @addtogroup logview
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Print logs.
|
---|
33 | */
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <async.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <ipc/logger.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <ns.h>
|
---|
41 | #include <io/console.h>
|
---|
42 |
|
---|
43 | #define MAX_MESSAGE_LENGTH 8192
|
---|
44 |
|
---|
45 | static async_exch_t *init_ipc_with_server(const char *namespace)
|
---|
46 | {
|
---|
47 | async_sess_t *logger_session = service_connect_blocking(
|
---|
48 | EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SOURCE, 0);
|
---|
49 | if (logger_session == NULL) {
|
---|
50 | fprintf(stderr, "Failed to connect to logger service.\n");
|
---|
51 | return NULL;
|
---|
52 | }
|
---|
53 |
|
---|
54 | async_exch_t *exchange = async_exchange_begin(logger_session);
|
---|
55 | if (exchange == NULL) {
|
---|
56 | fprintf(stderr, "Failed to start exchange with logger service.\n");
|
---|
57 | return NULL;
|
---|
58 | }
|
---|
59 |
|
---|
60 | aid_t reg_msg = async_send_0(exchange, LOGGER_CONNECT, NULL);
|
---|
61 | int rc = async_data_write_start(exchange, namespace, str_size(namespace));
|
---|
62 | sysarg_t reg_msg_rc;
|
---|
63 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
64 |
|
---|
65 | if ((rc != EOK) || (reg_msg_rc != EOK)) {
|
---|
66 | fprintf(stderr, "Failed to register with logger service: %s.\n",
|
---|
67 | str_error(rc == EOK ? (int) reg_msg_rc : rc));
|
---|
68 | async_exchange_end(exchange);
|
---|
69 | return NULL;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return exchange;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static bool quit_pressed(kbd_event_t event)
|
---|
76 | {
|
---|
77 | return (event.type == KEY_PRESS) && (event.c == 'q');
|
---|
78 | }
|
---|
79 |
|
---|
80 | static int parse_message_level(const char *str)
|
---|
81 | {
|
---|
82 | if (str == NULL) {
|
---|
83 | return 99;
|
---|
84 | }
|
---|
85 | char *tmp;
|
---|
86 | int result = strtol(str, &tmp, 10);
|
---|
87 | if (result < 0) {
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | int main(int argc, char *argv[])
|
---|
94 | {
|
---|
95 | if (argc < 2) {
|
---|
96 | fprintf(stderr, "Usage: %s <service-name> [max log level]\n", argv[0]);
|
---|
97 | return 1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | async_exch_t *exchange = init_ipc_with_server(argv[1]);
|
---|
101 | if (exchange == NULL) {
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | int display_message_level = parse_message_level(argv[2]);
|
---|
106 |
|
---|
107 | console_ctrl_t *console = console_init(stdin, stdout);
|
---|
108 |
|
---|
109 | bool terminate = false;
|
---|
110 | while (!terminate) {
|
---|
111 | ipc_call_t req_msg_data;
|
---|
112 | aid_t req_msg = async_send_0(exchange, LOGGER_GET_MESSAGE, &req_msg_data);
|
---|
113 | char message[MAX_MESSAGE_LENGTH];
|
---|
114 | aid_t data_msg = async_data_read(exchange, &message, MAX_MESSAGE_LENGTH, NULL);
|
---|
115 |
|
---|
116 | while (true) {
|
---|
117 | sysarg_t retval;
|
---|
118 | int answer_arrived = async_wait_timeout(data_msg, &retval, 1);
|
---|
119 | if (answer_arrived == EOK) {
|
---|
120 | async_wait_for(req_msg, &retval);
|
---|
121 | if (retval == EOK) {
|
---|
122 | int level = (int) IPC_GET_ARG1(req_msg_data);
|
---|
123 | if (display_message_level >= level) {
|
---|
124 | printf("%2d: %s\n", level, message);
|
---|
125 | }
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | kbd_event_t kbd_event;
|
---|
131 | suseconds_t timeout = 1;
|
---|
132 | bool key_pressed = console_get_kbd_event_timeout(console, &kbd_event, &timeout);
|
---|
133 | if (key_pressed && quit_pressed(kbd_event)) {
|
---|
134 | printf("Terminating (q pressed)...\n");
|
---|
135 | terminate = true;
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | async_exchange_end(exchange);
|
---|
142 | console_done(console);
|
---|
143 |
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** @}
|
---|
148 | */
|
---|