summaryrefslogtreecommitdiffstats
path: root/src/vty/logging_vty.c
blob: 88ee330a76815917e5b48f310d80f918d8ffb294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
/*
 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
 * (C) 2009-2014 by Holger Hans Peter Freyther
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdlib.h>
#include <string.h>

#include "../../config.h"

#include <osmocom/core/talloc.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/logging_internal.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/strrb.h>
#include <osmocom/core/loggingrb.h>
#include <osmocom/core/gsmtap.h>

#include <osmocom/vty/command.h>
#include <osmocom/vty/buffer.h>
#include <osmocom/vty/vty.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/vty/logging.h>

#define LOG_STR "Configure logging sub-system\n"
#define LEVEL_STR "Set the log level for a specified category\n"

#define CATEGORY_ALL_STR "Deprecated alias for 'force-all'\n"
#define FORCE_ALL_STR \
	"Globally force all logging categories to a specific level. This is released by the" \
	" 'no logging level force-all' command. Note: any 'logging level <category> <level>'" \
	" commands will have no visible effect after this, until the forced level is released.\n"
#define NO_FORCE_ALL_STR \
	"Release any globally forced log level set with 'logging level force-all <level>'\n"

#define LOG_LEVEL_ARGS "(debug|info|notice|error|fatal)"
#define LOG_LEVEL_STRS \
	"Log debug messages and higher levels\n" \
	"Log informational messages and higher levels\n" \
	"Log noticeable messages and higher levels\n" \
	"Log error messages and higher levels\n" \
	"Log only fatal messages\n"

#define EVERYTHING_STR "Deprecated alias for 'no logging level force-all'\n"

/*! \file logging_vty.c
 *  Configuration of logging from VTY
 *
 *  This module implements
 *  - functions that permit configuration of the libosmocore logging
 *    framework from VTY commands in the configure -> logging node.
 *
 *  - functions that permit logging *to* a VTY session.  Basically each
 *    VTY session gets its own log target, with configurable
 *    per-subsystem log levels.  This is performed internally via the
 *    \ref log_target_create_vty function.
 *
 *  You have to call \ref logging_vty_add_cmds from your application
 *  once to enable both of the above.
 *
 */

static void _vty_output(struct log_target *tgt,
			unsigned int level, const char *line)
{
	struct vty *vty = tgt->tgt_vty.vty;
	vty_out(vty, "%s", line);
	/* This is an ugly hack, but there is no easy way... */
	if (strchr(line, '\n'))
		vty_out(vty, "\r");
}

struct log_target *log_target_create_vty(struct vty *vty)
{
	struct log_target *target;

	target = log_target_create();
	if (!target)
		return NULL;

	target->tgt_vty.vty = vty;
	target->output = _vty_output;
	return target;
}

/*! Get tgt with log lock acquired, return and release lock with warning if tgt
 *  is not found. Lock must be released later with log_tgt_mutex_unlock().
 */
#define ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt) \
	do { \
		log_tgt_mutex_lock(); \
		tgt = osmo_log_vty2tgt(vty); \
		if (!(tgt)) { \
			log_tgt_mutex_unlock(); \
			return CMD_WARNING; \
		} \
	} while (0)

#define RET_WITH_UNLOCK(ret) \
	do { \
		log_tgt_mutex_unlock(); \
		return (ret); \
	} while (0)

DEFUN(enable_logging,
      enable_logging_cmd,
      "logging enable",
	LOGGING_STR
      "Enables logging to this vty\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (conn->dbg) {
		vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	conn->dbg = log_target_create_vty(vty);
	if (!conn->dbg)
		return CMD_WARNING;
	log_tgt_mutex_lock();
	log_add_target(conn->dbg);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

/*! Get log target associated to VTY console.
 *  \param[in] vty Log target type
 *  \returns Log target (if logging enabled), NULL otherwise
 *  Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
 */
struct log_target *osmo_log_vty2tgt(struct vty *vty)
{
	struct telnet_connection *conn;

	if (vty->node == CFG_LOG_NODE)
		return vty->index;


	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg)
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);

	return conn->dbg;
}

DEFUN(logging_fltr_all,
      logging_fltr_all_cmd,
      "logging filter all (0|1)",
	LOGGING_STR FILTER_STR
	"Do you want to log all messages?\n"
	"Only print messages matched by other filters\n"
	"Bypass filter and print all messages\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_all_filter(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_use_clr,
      logging_use_clr_cmd,
      "logging color (0|1)",
	LOGGING_STR "Configure color-printing for log messages\n"
      "Don't use color for printing messages\n"
      "Use color for printing messages\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_use_color(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_prnt_timestamp,
      logging_prnt_timestamp_cmd,
      "logging timestamp (0|1)",
	LOGGING_STR "Configure log message timestamping\n"
	"Don't prefix each log message\n"
	"Prefix each log message with current timestamp\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_print_timestamp(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_prnt_ext_timestamp,
      logging_prnt_ext_timestamp_cmd,
      "logging print extended-timestamp (0|1)",
	LOGGING_STR "Log output settings\n"
	"Configure log message timestamping\n"
	"Don't prefix each log message\n"
	"Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_print_extended_timestamp(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_prnt_cat,
      logging_prnt_cat_cmd,
      "logging print category (0|1)",
	LOGGING_STR "Log output settings\n"
	"Configure log message\n"
	"Don't prefix each log message\n"
	"Prefix each log message with category/subsystem name\n")
{
	struct log_target *tgt;
	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_print_category(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_prnt_cat_hex,
      logging_prnt_cat_hex_cmd,
      "logging print category-hex (0|1)",
	LOGGING_STR "Log output settings\n"
	"Configure log message\n"
	"Don't prefix each log message\n"
	"Prefix each log message with category/subsystem nr in hex ('<000b>')\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_print_category_hex(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_prnt_level,
      logging_prnt_level_cmd,
      "logging print level (0|1)",
      LOGGING_STR "Log output settings\n"
      "Configure log message\n"
      "Don't prefix each log message\n"
      "Prefix each log message with the log level name\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_print_level(tgt, atoi(argv[0]));
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

static const struct value_string logging_print_file_args[] = {
	{ LOG_FILENAME_NONE, "0" },
	{ LOG_FILENAME_PATH, "1" },
	{ LOG_FILENAME_BASENAME, "basename" },
	{ 0, NULL }
};

DEFUN(logging_prnt_file,
      logging_prnt_file_cmd,
      "logging print file (0|1|basename) [last]",
      LOGGING_STR "Log output settings\n"
      "Configure log message\n"
      "Don't prefix each log message\n"
      "Prefix each log message with the source file and line\n"
      "Prefix each log message with the source file's basename (strip leading paths) and line\n"
      "Log source file info at the end of a log line. If omitted, log source file info just"
      " before the log text.\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_print_filename2(tgt, get_string_value(logging_print_file_args, argv[0]));
	if (argc > 1)
		log_set_print_filename_pos(tgt, LOG_FILENAME_POS_LINE_END);
	else
		log_set_print_filename_pos(tgt, LOG_FILENAME_POS_HEADER_END);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

static void add_category_strings(char **cmd_str_p, char **doc_str_p,
				 const struct log_info *categories)
{
	char buf[128];
	int i;
	for (i = 0; i < categories->num_cat; i++) {
		if (categories->cat[i].name == NULL)
			continue;
		/* skip the leading 'D' in each category name, hence '+ 1' */
		osmo_str_tolower_buf(buf, sizeof(buf), categories->cat[i].name + 1);
		osmo_talloc_asprintf(tall_log_ctx, *cmd_str_p, "%s%s",
				     i ? "|" : "", buf);
		osmo_talloc_asprintf(tall_log_ctx, *doc_str_p, "%s\n",
				     categories->cat[i].description);
	}
}

static void gen_logging_level_cmd_strs(struct cmd_element *cmd,
				       const char *level_args, const char *level_strs)
{
	char *cmd_str = NULL;
	char *doc_str = NULL;

	assert_loginfo(__func__);

	OSMO_ASSERT(cmd->string == NULL);
	OSMO_ASSERT(cmd->doc == NULL);

	osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logging level (");
	osmo_talloc_asprintf(tall_log_ctx, doc_str,
			     LOGGING_STR
			     LEVEL_STR);
	add_category_strings(&cmd_str, &doc_str, osmo_log_info);
	osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", level_args);
	osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", level_strs);

	cmd->string = cmd_str;
	cmd->doc = doc_str;
}

/* logging level (<categories>) (debug|...|fatal) */
DEFUN(logging_level,
      logging_level_cmd,
      NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
      NULL) /* same thing for helpstr. */
{
	struct log_target *tgt;
	int category = log_parse_category(argv[0]);
	int level = log_parse_level(argv[1]);

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	if (level < 0) {
		vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	if (category < 0) {
		vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	tgt->categories[category].enabled = 1;
	tgt->categories[category].loglevel = level;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(logging_level_set_all, logging_level_set_all_cmd,
      "logging level set-all " LOG_LEVEL_ARGS,
      LOGGING_STR LEVEL_STR
      "Once-off set all categories to the given log level. There is no single command"
      " to take back these changes -- each category is set to the given level, period.\n"
      LOG_LEVEL_STRS)
{
	struct log_target *tgt;
	int level = log_parse_level(argv[0]);
	int i;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	for (i = 0; i < osmo_log_info->num_cat; i++) {
		struct log_category *cat = &tgt->categories[i];
		/* skip empty entries in the array */
		if (!osmo_log_info->cat[i].name)
			continue;

		cat->enabled = 1;
		cat->loglevel = level;
	}
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

/* logging level (<categories>) everything */
DEFUN_DEPRECATED(deprecated_logging_level_everything, deprecated_logging_level_everything_cmd,
		 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
		 NULL) /* same thing for helpstr. */
{
	vty_out(vty, "%% Ignoring deprecated logging level 'everything' keyword%s", VTY_NEWLINE);
	return CMD_SUCCESS;
}

DEFUN(logging_level_force_all, logging_level_force_all_cmd,
      "logging level force-all " LOG_LEVEL_ARGS,
      LOGGING_STR LEVEL_STR FORCE_ALL_STR LOG_LEVEL_STRS)
{
	struct log_target *tgt;
	int level = log_parse_level(argv[0]);

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_log_level(tgt, level);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(no_logging_level_force_all, no_logging_level_force_all_cmd,
      "no logging level force-all",
      NO_STR LOGGING_STR LEVEL_STR NO_FORCE_ALL_STR)
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_set_log_level(tgt, 0);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

/* 'logging level all (debug|...|fatal)' */
ALIAS_DEPRECATED(logging_level_force_all, deprecated_logging_level_all_cmd,
		 "logging level all " LOG_LEVEL_ARGS,
		 LOGGING_STR LEVEL_STR CATEGORY_ALL_STR LOG_LEVEL_STRS);

/* 'logging level all everything' */
ALIAS_DEPRECATED(no_logging_level_force_all, deprecated_logging_level_all_everything_cmd,
		 "logging level all everything",
		 LOGGING_STR LEVEL_STR CATEGORY_ALL_STR EVERYTHING_STR);

DEFUN(logging_set_category_mask,
      logging_set_category_mask_cmd,
      "logging set-log-mask MASK",
	LOGGING_STR
      "Set the logmask of this logging target\n"
      "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
      " application, refer to the 'logging level' command. Optionally add individual log levels like"
      " 'abc,1:mno,3:xyz,5', where the level numbers are"
      " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
      " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
      " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
      " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
      " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
      "\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_parse_category_mask(tgt, argv[0]);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

ALIAS_DEPRECATED(logging_set_category_mask,
		 logging_set_category_mask_old_cmd,
		 "logging set log mask MASK",
		 LOGGING_STR
		 "Decide which categories to output.\n"
		 "Log commands\n" "Mask commands\n"
		 "'set log mask' is deprecated, please refer to the docs of 'set-log-mask' instead\n");


DEFUN(diable_logging,
      disable_logging_cmd,
      "logging disable",
	LOGGING_STR
      "Disables logging to this vty\n")
{
	struct log_target *tgt;
	struct telnet_connection *conn = (struct telnet_connection *) vty->priv;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	log_del_target(tgt);
	talloc_free(tgt);
	conn->dbg = NULL;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
				const struct log_target *tgt)
{
	unsigned int i;

	vty_out(vty, " Global Loglevel: %s%s",
		log_level_str(tgt->loglevel), VTY_NEWLINE);
	vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
		tgt->use_color ? "On" : "Off",
		tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);

	vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);

	for (i = 0; i < info->num_cat; i++) {
		const struct log_category *cat = &tgt->categories[i];
		/* Skip categories that were not initialized */
		if (!info->cat[i].name)
			continue;
		vty_out(vty, "  %-10s %-10s %-8s %s%s",
			info->cat[i].name+1, log_level_str(cat->loglevel),
			cat->enabled ? "Enabled" : "Disabled",
 			info->cat[i].description,
			VTY_NEWLINE);
	}

	vty_out(vty, " Log Filter 'ALL': %s%s",
		tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
		VTY_NEWLINE);

	/* print application specific filters */
	if (info->print_fn)
		info->print_fn(vty, info, tgt);
}

#define SHOW_LOG_STR "Show current logging configuration\n"

DEFUN(show_logging_vty,
	show_logging_vty_cmd,
	"show logging vty",
	SHOW_STR SHOW_LOG_STR
	"Show current logging configuration for this vty\n")
{
	struct log_target *tgt;

	ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);

	vty_print_logtarget(vty, osmo_log_info, tgt);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(show_alarms,
	show_alarms_cmd,
	"show alarms",
	SHOW_STR SHOW_LOG_STR
	"Show the contents of the logging ringbuffer\n")
{
	int i, num_alarms;
	struct osmo_strrb *rb;
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
	if (!tgt) {
		vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
			VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	rb = tgt->tgt_rb.rb;
	num_alarms = osmo_strrb_elements(rb);

	vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);

	for (i = 0; i < num_alarms; i++)
		vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
			VTY_NEWLINE);
	RET_WITH_UNLOCK(CMD_SUCCESS);
}

gDEFUN(cfg_description, cfg_description_cmd,
	"description .TEXT",
	"Save human-readable description of the object\n"
	"Text until the end of the line\n")
{
	char **dptr = vty->index_sub;

	if (!dptr) {
		vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	if (*dptr)
		talloc_free(*dptr);
	*dptr = argv_concat(argv, argc, 0);
	if (!*dptr)
		return CMD_WARNING;

	return CMD_SUCCESS;
}

gDEFUN(cfg_no_description, cfg_no_description_cmd,
	"no description",
	NO_STR
	"Remove description of the object\n")
{
	char **dptr = vty->index_sub;

	if (!dptr) {
		vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	if (*dptr) {
		talloc_free(*dptr);
		*dptr = NULL;
	}

	return CMD_SUCCESS;
}

/* Support for configuration of log targets != the current vty */

struct cmd_node cfg_log_node = {
	CFG_LOG_NODE,
	"%s(config-log)# ",
	1
};

#ifdef HAVE_SYSLOG_H

#include <syslog.h>

static const int local_sysl_map[] = {
	[0] = LOG_LOCAL0,
	[1] = LOG_LOCAL1,
	[2] = LOG_LOCAL2,
	[3] = LOG_LOCAL3,
	[4] = LOG_LOCAL4,
	[5] = LOG_LOCAL5,
	[6] = LOG_LOCAL6,
	[7] = LOG_LOCAL7
};

/* From VTY core code */
extern struct host host;

static int _cfg_log_syslog(struct vty *vty, int facility)
{
	struct log_target *tgt;

	log_tgt_mutex_lock();
	/* First delete the old syslog target, if any */
	tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
	if (tgt)
		log_target_destroy(tgt);

	tgt = log_target_create_syslog(host.app_info->name, 0, facility);
	if (!tgt) {
		vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}
	log_add_target(tgt);

	vty->index = tgt;
	vty->node = CFG_LOG_NODE;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
      "log syslog local <0-7>",
	LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
	"Local facility number\n")
{
	int local = atoi(argv[0]);
	int facility = local_sysl_map[local];

	return _cfg_log_syslog(vty, facility);
}

static struct value_string sysl_level_names[] = {
	{ LOG_AUTHPRIV, "authpriv" },
	{ LOG_CRON, 	"cron" },
	{ LOG_DAEMON,	"daemon" },
	{ LOG_FTP,	"ftp" },
	{ LOG_LPR,	"lpr" },
	{ LOG_MAIL,	"mail" },
	{ LOG_NEWS,	"news" },
	{ LOG_USER,	"user" },
	{ LOG_UUCP,	"uucp" },
	/* only for value -> string conversion */
	{ LOG_LOCAL0,	"local 0" },
	{ LOG_LOCAL1,	"local 1" },
	{ LOG_LOCAL2,	"local 2" },
	{ LOG_LOCAL3,	"local 3" },
	{ LOG_LOCAL4,	"local 4" },
	{ LOG_LOCAL5,	"local 5" },
	{ LOG_LOCAL6,	"local 6" },
	{ LOG_LOCAL7,	"local 7" },
	{ 0, NULL }
};

DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
      "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
	LOG_STR "Logging via syslog\n"
	"Security/authorization messages facility\n"
	"Clock daemon (cron/at) facility\n"
	"General system daemon facility\n"
	"Ftp daemon facility\n"
	"Line printer facility\n"
	"Mail facility\n"
	"News facility\n"
	"Generic facility\n"
	"UUCP facility\n")
{
	int facility = get_string_value(sysl_level_names, argv[0]);

	return _cfg_log_syslog(vty, facility);
}

DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
	"no log syslog",
	NO_STR LOG_STR "Logging via syslog\n")
{
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
	if (!tgt) {
		vty_out(vty, "%% No syslog target found%s",
			VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	log_target_destroy(tgt);

	RET_WITH_UNLOCK(CMD_SUCCESS);
}
#endif /* HAVE_SYSLOG_H */

DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
	"log gsmtap [HOSTNAME]",
	LOG_STR "Logging via GSMTAP\n"
	"Host name to send the GSMTAP logging to (UDP port 4729)\n")
{
	const char *hostname = argc ? argv[0] : "127.0.0.1";
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
	if (!tgt) {
		tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
					       host.app_info->name, false,
					       true);
		if (!tgt) {
			vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
				hostname, VTY_NEWLINE);
			RET_WITH_UNLOCK(CMD_WARNING);
		}
		log_add_target(tgt);
	}

	vty->index = tgt;
	vty->node = CFG_LOG_NODE;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
	"log stderr",
	LOG_STR "Logging via STDERR of the process\n")
{
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
	if (!tgt) {
		tgt = log_target_create_stderr();
		if (!tgt) {
			vty_out(vty, "%% Unable to create stderr log%s",
				VTY_NEWLINE);
			RET_WITH_UNLOCK(CMD_WARNING);
		}
		log_add_target(tgt);
	}

	vty->index = tgt;
	vty->node = CFG_LOG_NODE;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
	"no log stderr",
	NO_STR LOG_STR "Logging via STDERR of the process\n")
{
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
	if (!tgt) {
		vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	log_target_destroy(tgt);

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(cfg_log_file, cfg_log_file_cmd,
	"log file .FILENAME",
	LOG_STR "Logging to text file\n" "Filename\n")
{
	const char *fname = argv[0];
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
	if (!tgt) {
		tgt = log_target_create_file(fname);
		if (!tgt) {
			vty_out(vty, "%% Unable to create file `%s'%s",
				fname, VTY_NEWLINE);
			RET_WITH_UNLOCK(CMD_WARNING);
		}
		log_add_target(tgt);
	}

	vty->index = tgt;
	vty->node = CFG_LOG_NODE;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}


DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
	"no log file .FILENAME",
	NO_STR LOG_STR "Logging to text file\n" "Filename\n")
{
	const char *fname = argv[0];
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
	if (!tgt) {
		vty_out(vty, "%% No such log file `%s'%s",
			fname, VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	log_target_destroy(tgt);

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
	"log alarms <2-32700>",
	LOG_STR "Logging alarms to osmo_strrb\n"
	"Maximum number of messages to log\n")
{
	struct log_target *tgt;
	unsigned int rbsize = atoi(argv[0]);


	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
	if (tgt)
		log_target_destroy(tgt);

	tgt = log_target_create_rb(rbsize);
	if (!tgt) {
		vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
			rbsize, VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}
	log_add_target(tgt);

	vty->index = tgt;
	vty->node = CFG_LOG_NODE;

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
	"no log alarms",
	NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
{
	struct log_target *tgt;

	log_tgt_mutex_lock();
	tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
	if (!tgt) {
		vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
		RET_WITH_UNLOCK(CMD_WARNING);
	}

	log_target_destroy(tgt);

	RET_WITH_UNLOCK(CMD_SUCCESS);
}

stat