get rid of non-ANSI function declarations missing (void)
[osmocom-bb.git] / src / vty / logging_vty.c
1 /* OpenBSC logging helper for the VTY */
2 /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
3  * (C) 2009-2010 by Holger Hans Peter Freyther
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "../../config.h"
26
27 #include <osmocom/core/talloc.h>
28 #include <osmocom/core/logging.h>
29 #include <osmocom/core/utils.h>
30
31 //#include <openbsc/vty.h>
32
33 #include <osmocom/vty/command.h>
34 #include <osmocom/vty/buffer.h>
35 #include <osmocom/vty/vty.h>
36 #include <osmocom/vty/telnet_interface.h>
37 #include <osmocom/vty/logging.h>
38
39 #define LOG_STR "Configure logging sub-system\n"
40
41 extern const struct log_info *osmo_log_info;
42
43 static void _vty_output(struct log_target *tgt,
44                         unsigned int level, const char *line)
45 {
46         struct vty *vty = tgt->tgt_vty.vty;
47         vty_out(vty, "%s", line);
48         /* This is an ugly hack, but there is no easy way... */
49         if (strchr(line, '\n'))
50                 vty_out(vty, "\r");
51 }
52
53 struct log_target *log_target_create_vty(struct vty *vty)
54 {
55         struct log_target *target;
56
57         target = log_target_create();
58         if (!target)
59                 return NULL;
60
61         target->tgt_vty.vty = vty;
62         target->output = _vty_output;
63         return target;
64 }
65
66 DEFUN(enable_logging,
67       enable_logging_cmd,
68       "logging enable",
69         LOGGING_STR
70       "Enables logging to this vty\n")
71 {
72         struct telnet_connection *conn;
73
74         conn = (struct telnet_connection *) vty->priv;
75         if (conn->dbg) {
76                 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
77                 return CMD_WARNING;
78         }
79
80         conn->dbg = log_target_create_vty(vty);
81         if (!conn->dbg)
82                 return CMD_WARNING;
83
84         log_add_target(conn->dbg);
85         return CMD_SUCCESS;
86 }
87
88 struct log_target *osmo_log_vty2tgt(struct vty *vty)
89 {
90         struct telnet_connection *conn;
91
92         if (vty->node == CFG_LOG_NODE)
93                 return vty->index;
94
95
96         conn = (struct telnet_connection *) vty->priv;
97         if (!conn->dbg)
98                 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
99
100         return conn->dbg;
101 }
102
103 DEFUN(logging_fltr_all,
104       logging_fltr_all_cmd,
105       "logging filter all (0|1)",
106         LOGGING_STR FILTER_STR
107         "Do you want to log all messages?\n"
108         "Only print messages matched by other filters\n"
109         "Bypass filter and print all messages\n")
110 {
111         struct log_target *tgt = osmo_log_vty2tgt(vty);
112
113         if (!tgt)
114                 return CMD_WARNING;
115
116         log_set_all_filter(tgt, atoi(argv[0]));
117         return CMD_SUCCESS;
118 }
119
120 DEFUN(logging_use_clr,
121       logging_use_clr_cmd,
122       "logging color (0|1)",
123         LOGGING_STR "Configure color-printing for log messages\n"
124       "Don't use color for printing messages\n"
125       "Use color for printing messages\n")
126 {
127         struct log_target *tgt = osmo_log_vty2tgt(vty);
128
129         if (!tgt)
130                 return CMD_WARNING;
131
132         log_set_use_color(tgt, atoi(argv[0]));
133         return CMD_SUCCESS;
134 }
135
136 DEFUN(logging_prnt_timestamp,
137       logging_prnt_timestamp_cmd,
138       "logging timestamp (0|1)",
139         LOGGING_STR "Configure log message timestamping\n"
140         "Don't prefix each log message\n"
141         "Prefix each log message with current timestamp\n")
142 {
143         struct log_target *tgt = osmo_log_vty2tgt(vty);
144
145         if (!tgt)
146                 return CMD_WARNING;
147
148         log_set_print_timestamp(tgt, atoi(argv[0]));
149         return CMD_SUCCESS;
150 }
151
152 DEFUN(logging_level,
153       logging_level_cmd,
154       NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
155       NULL) /* same thing for helpstr. */
156 {
157         int category = log_parse_category(argv[0]);
158         int level = log_parse_level(argv[1]);
159         struct log_target *tgt = osmo_log_vty2tgt(vty);
160
161         if (!tgt)
162                 return CMD_WARNING;
163
164         if (level < 0) {
165                 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
166                 return CMD_WARNING;
167         }
168
169         /* Check for special case where we want to set global log level */
170         if (!strcmp(argv[0], "all")) {
171                 log_set_log_level(tgt, level);
172                 return CMD_SUCCESS;
173         }
174
175         if (category < 0) {
176                 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
177                 return CMD_WARNING;
178         }
179
180         tgt->categories[category].enabled = 1;
181         tgt->categories[category].loglevel = level;
182
183         return CMD_SUCCESS;
184 }
185
186 DEFUN(logging_set_category_mask,
187       logging_set_category_mask_cmd,
188       "logging set log mask MASK",
189         LOGGING_STR
190       "Decide which categories to output.\n")
191 {
192         struct log_target *tgt = osmo_log_vty2tgt(vty);
193
194         if (!tgt)
195                 return CMD_WARNING;
196
197         log_parse_category_mask(tgt, argv[0]);
198         return CMD_SUCCESS;
199 }
200
201 DEFUN(diable_logging,
202       disable_logging_cmd,
203       "logging disable",
204         LOGGING_STR
205       "Disables logging to this vty\n")
206 {
207         struct log_target *tgt = osmo_log_vty2tgt(vty);
208         struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
209
210         if (!tgt)
211                 return CMD_WARNING;
212
213         log_del_target(tgt);
214         talloc_free(tgt);
215         conn->dbg = NULL;
216
217         return CMD_SUCCESS;
218 }
219
220 static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
221                                 const struct log_target *tgt)
222 {
223         unsigned int i;
224
225         vty_out(vty, " Global Loglevel: %s%s",
226                 log_level_str(tgt->loglevel), VTY_NEWLINE);
227         vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
228                 tgt->use_color ? "On" : "Off",
229                 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
230
231         vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
232
233         for (i = 0; i < info->num_cat; i++) {
234                 const struct log_category *cat = &tgt->categories[i];
235                 vty_out(vty, "  %-10s %-10s %-8s %s%s",
236                         info->cat[i].name+1, log_level_str(cat->loglevel),
237                         cat->enabled ? "Enabled" : "Disabled",
238                         info->cat[i].description,
239                         VTY_NEWLINE);
240         }
241 }
242
243 #define SHOW_LOG_STR "Show current logging configuration\n"
244
245 DEFUN(show_logging_vty,
246       show_logging_vty_cmd,
247       "show logging vty",
248         SHOW_STR SHOW_LOG_STR
249         "Show current logging configuration for this vty\n")
250 {
251         struct log_target *tgt = osmo_log_vty2tgt(vty);
252
253         if (!tgt)
254                 return CMD_WARNING;
255
256         vty_print_logtarget(vty, osmo_log_info, tgt);
257
258         return CMD_SUCCESS;
259 }
260
261 gDEFUN(cfg_description, cfg_description_cmd,
262         "description .TEXT",
263         "Save human-readable decription of the object\n")
264 {
265         char **dptr = vty->index_sub;
266
267         if (!dptr) {
268                 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
269                 return CMD_WARNING;
270         }
271
272         if (*dptr)
273                 talloc_free(*dptr);
274         *dptr = argv_concat(argv, argc, 0);
275         if (!dptr)
276                 return CMD_WARNING;
277
278         return CMD_SUCCESS;
279 }
280
281 gDEFUN(cfg_no_description, cfg_no_description_cmd,
282         "no description",
283         NO_STR
284         "Remove description of the object\n")
285 {
286         char **dptr = vty->index_sub;
287
288         if (!dptr) {
289                 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
290                 return CMD_WARNING;
291         }
292
293         if (*dptr) {
294                 talloc_free(*dptr);
295                 *dptr = NULL;
296         }
297
298         return CMD_SUCCESS;
299 }
300
301 /* Support for configuration of log targets != the current vty */
302
303 struct cmd_node cfg_log_node = {
304         CFG_LOG_NODE,
305         "%s(config-log)# ",
306         1
307 };
308
309 #ifdef HAVE_SYSLOG_H
310
311 #include <syslog.h>
312
313 static const int local_sysl_map[] = {
314         [0] = LOG_LOCAL0,
315         [1] = LOG_LOCAL1,
316         [2] = LOG_LOCAL2,
317         [3] = LOG_LOCAL3,
318         [4] = LOG_LOCAL4,
319         [5] = LOG_LOCAL5,
320         [6] = LOG_LOCAL6,
321         [7] = LOG_LOCAL7
322 };
323
324 /* From VTY core code */
325 extern struct host host;
326
327 static int _cfg_log_syslog(struct vty *vty, int facility)
328 {
329         struct log_target *tgt;
330
331         /* First delete the old syslog target, if any */
332         tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
333         if (tgt)
334                 log_target_destroy(tgt);
335
336         tgt = log_target_create_syslog(host.app_info->name, 0, facility);
337         if (!tgt) {
338                 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
339                 return CMD_WARNING;
340         }
341         log_add_target(tgt);
342
343         vty->index = tgt;
344         vty->node = CFG_LOG_NODE;
345
346         return CMD_SUCCESS;
347 }
348
349 DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
350       "log syslog local <0-7>",
351         LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
352         "Local facility number\n")
353 {
354         int local = atoi(argv[0]);
355         int facility = local_sysl_map[local];
356
357         return _cfg_log_syslog(vty, facility);
358 }
359
360 static struct value_string sysl_level_names[] = {
361         { LOG_AUTHPRIV, "authpriv" },
362         { LOG_CRON,     "cron" },
363         { LOG_DAEMON,   "daemon" },
364         { LOG_FTP,      "ftp" },
365         { LOG_LPR,      "lpr" },
366         { LOG_MAIL,     "mail" },
367         { LOG_NEWS,     "news" },
368         { LOG_USER,     "user" },
369         { LOG_UUCP,     "uucp" },
370         /* only for value -> string conversion */
371         { LOG_LOCAL0,   "local 0" },
372         { LOG_LOCAL1,   "local 1" },
373         { LOG_LOCAL2,   "local 2" },
374         { LOG_LOCAL3,   "local 3" },
375         { LOG_LOCAL4,   "local 4" },
376         { LOG_LOCAL5,   "local 5" },
377         { LOG_LOCAL6,   "local 6" },
378         { LOG_LOCAL7,   "local 7" },
379         { 0, NULL }
380 };
381
382 DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
383       "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
384         LOG_STR "Logging via syslog\n")
385 {
386         int facility = get_string_value(sysl_level_names, argv[0]);
387
388         return _cfg_log_syslog(vty, facility);
389 }
390
391 DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
392         "no log syslog",
393         NO_STR LOG_STR "Logging via syslog\n")
394 {
395         struct log_target *tgt;
396
397         tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
398         if (!tgt) {
399                 vty_out(vty, "%% No syslog target found%s",
400                         VTY_NEWLINE);
401                 return CMD_WARNING;
402         }
403
404         log_target_destroy(tgt);
405
406         return CMD_SUCCESS;
407 }
408 #endif /* HAVE_SYSLOG_H */
409
410 DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
411         "log stderr",
412         LOG_STR "Logging via STDERR of the process\n")
413 {
414         struct log_target *tgt;
415
416         tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
417         if (!tgt) {
418                 tgt = log_target_create_stderr();
419                 if (!tgt) {
420                         vty_out(vty, "%% Unable to create stderr log%s",
421                                 VTY_NEWLINE);
422                         return CMD_WARNING;
423                 }
424                 log_add_target(tgt);
425         }
426
427         vty->index = tgt;
428         vty->node = CFG_LOG_NODE;
429
430         return CMD_SUCCESS;
431 }
432
433 DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
434         "no log stderr",
435         NO_STR LOG_STR "Logging via STDERR of the process\n")
436 {
437         struct log_target *tgt;
438
439         tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
440         if (!tgt) {
441                 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
442                 return CMD_WARNING;
443         }
444
445         log_target_destroy(tgt);
446
447         return CMD_SUCCESS;
448 }
449
450 DEFUN(cfg_log_file, cfg_log_file_cmd,
451         "log file .FILENAME",
452         LOG_STR "Logging to text file\n" "Filename\n")
453 {
454         const char *fname = argv[0];
455         struct log_target *tgt;
456
457         tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
458         if (!tgt) {
459                 tgt = log_target_create_file(fname);
460                 if (!tgt) {
461                         vty_out(vty, "%% Unable to create file `%s'%s",
462                                 fname, VTY_NEWLINE);
463                         return CMD_WARNING;
464                 }
465                 log_add_target(tgt);
466         }
467
468         vty->index = tgt;
469         vty->node = CFG_LOG_NODE;
470
471         return CMD_SUCCESS;
472 }
473
474
475 DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
476         "no log file .FILENAME",
477         NO_STR LOG_STR "Logging to text file\n" "Filename\n")
478 {
479         const char *fname = argv[0];
480         struct log_target *tgt;
481
482         tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
483         if (!tgt) {
484                 vty_out(vty, "%% No such log file `%s'%s",
485                         fname, VTY_NEWLINE);
486                 return CMD_WARNING;
487         }
488
489         log_target_destroy(tgt);
490
491         return CMD_SUCCESS;
492 }
493
494 static int config_write_log_single(struct vty *vty, struct log_target *tgt)
495 {
496         int i;
497         char level_lower[32];
498
499         switch (tgt->type) {
500         case LOG_TGT_TYPE_VTY:
501                 return 1;
502                 break;
503         case LOG_TGT_TYPE_STDERR:
504                 vty_out(vty, "log stderr%s", VTY_NEWLINE);
505                 break;
506         case LOG_TGT_TYPE_SYSLOG:
507 #ifdef HAVE_SYSLOG_H
508                 vty_out(vty, "log syslog %s%s",
509                         get_value_string(sysl_level_names,
510                                          tgt->tgt_syslog.facility),
511                         VTY_NEWLINE);
512 #endif
513                 break;
514         case LOG_TGT_TYPE_FILE:
515                 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
516                 break;
517         }
518
519         vty_out(vty, "  logging color %u%s", tgt->use_color ? 1 : 0,
520                 VTY_NEWLINE);
521         vty_out(vty, "  logging timestamp %u%s", tgt->print_timestamp ? 1 : 0,
522                 VTY_NEWLINE);
523
524         /* stupid old osmo logging API uses uppercase strings... */
525         osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
526         vty_out(vty, "  logging level all %s%s", level_lower, VTY_NEWLINE);
527
528         for (i = 0; i < osmo_log_info->num_cat; i++) {
529                 const struct log_category *cat = &tgt->categories[i];
530                 char cat_lower[32];
531
532                 /* stupid old osmo logging API uses uppercase strings... */
533                 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
534                 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
535
536                 vty_out(vty, "  logging level %s %s%s", cat_lower, level_lower,
537                         VTY_NEWLINE);
538         }
539
540         /* FIXME: levels */
541
542         return 1;
543 }
544
545 static int config_write_log(struct vty *vty)
546 {
547         struct log_target *dbg = vty->index;
548
549         llist_for_each_entry(dbg, &osmo_log_target_list, entry)
550                 config_write_log_single(vty, dbg);
551
552         return 1;
553 }
554
555 void logging_vty_add_cmds(const struct log_info *cat)
556 {
557         install_element_ve(&enable_logging_cmd);
558         install_element_ve(&disable_logging_cmd);
559         install_element_ve(&logging_fltr_all_cmd);
560         install_element_ve(&logging_use_clr_cmd);
561         install_element_ve(&logging_prnt_timestamp_cmd);
562         install_element_ve(&logging_set_category_mask_cmd);
563
564         /* Logging level strings are generated dynamically. */
565         logging_level_cmd.string = log_vty_command_string(cat);
566         logging_level_cmd.doc = log_vty_command_description(cat);
567         install_element_ve(&logging_level_cmd);
568         install_element_ve(&show_logging_vty_cmd);
569
570         install_node(&cfg_log_node, config_write_log);
571         install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
572         install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
573         install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
574         install_element(CFG_LOG_NODE, &logging_level_cmd);
575
576         install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
577         install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
578         install_element(CONFIG_NODE, &cfg_log_file_cmd);
579         install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
580 #ifdef HAVE_SYSLOG_H
581         install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
582         install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
583         install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
584 #endif
585 }