]>
Commit | Line | Data |
---|---|---|
7b35c02d MW |
1 | /* |
2 | * Copyright (C) 2014 Martin Willi | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
7b35c02d MW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
15 | */ | |
16 | ||
17 | #include "command.h" | |
18 | ||
19 | #include <errno.h> | |
20 | #include <unistd.h> | |
21 | ||
22 | CALLBACK(log_cb, void, | |
dacb75f5 | 23 | command_format_options_t *format, char *name, vici_res_t *msg) |
7b35c02d | 24 | { |
dacb75f5 | 25 | if (*format & COMMAND_FORMAT_RAW) |
7b35c02d | 26 | { |
dacb75f5 | 27 | vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout); |
7b35c02d MW |
28 | } |
29 | else | |
30 | { | |
31 | char *current, *next; | |
32 | ||
33 | current = vici_find_str(msg, NULL, "msg"); | |
34 | while (current) | |
35 | { | |
36 | next = strchr(current, '\n'); | |
37 | printf("%.2d[%s] ", vici_find_int(msg, 0, "thread"), | |
38 | vici_find_str(msg, " ", "group")); | |
39 | if (next == NULL) | |
40 | { | |
41 | printf("%s\n", current); | |
42 | break; | |
43 | } | |
44 | printf("%.*s\n", (int)(next - current), current); | |
45 | current = next + 1; | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
50 | static int logcmd(vici_conn_t *conn) | |
51 | { | |
dacb75f5 | 52 | command_format_options_t format = COMMAND_FORMAT_NONE; |
7b35c02d | 53 | char *arg; |
67f9f09d | 54 | int ret; |
7b35c02d MW |
55 | |
56 | while (TRUE) | |
57 | { | |
58 | switch (command_getopt(&arg)) | |
59 | { | |
60 | case 'h': | |
61 | return command_usage(NULL); | |
dacb75f5 AS |
62 | case 'P': |
63 | format |= COMMAND_FORMAT_PRETTY; | |
64 | /* fall through to raw */ | |
7b35c02d | 65 | case 'r': |
dacb75f5 | 66 | format |= COMMAND_FORMAT_RAW; |
7b35c02d MW |
67 | continue; |
68 | case EOF: | |
69 | break; | |
70 | default: | |
71 | return command_usage("invalid --log option"); | |
72 | } | |
73 | break; | |
74 | } | |
75 | ||
dacb75f5 | 76 | if (vici_register(conn, "log", log_cb, &format) != 0) |
7b35c02d | 77 | { |
67f9f09d | 78 | ret = errno; |
7b35c02d | 79 | fprintf(stderr, "registering for log failed: %s\n", strerror(errno)); |
67f9f09d | 80 | return ret; |
7b35c02d | 81 | } |
f59e2b7b MW |
82 | |
83 | wait_sigint(); | |
84 | ||
85 | fprintf(stderr, "disconnecting...\n"); | |
86 | ||
7b35c02d MW |
87 | return 0; |
88 | } | |
89 | ||
90 | /** | |
91 | * Register the command. | |
92 | */ | |
93 | static void __attribute__ ((constructor))reg() | |
94 | { | |
95 | command_register((command_t) { | |
96 | logcmd, 'T', "log", "trace logging output", | |
dacb75f5 | 97 | {"[--raw|--pretty]"}, |
7b35c02d MW |
98 | { |
99 | {"help", 'h', 0, "show usage information"}, | |
100 | {"raw", 'r', 0, "dump raw response message"}, | |
dacb75f5 | 101 | {"pretty", 'P', 0, "dump raw response message in pretty print"}, |
7b35c02d MW |
102 | } |
103 | }); | |
104 | } |