0a5cd202252fa8af4ab74cd3e5c26152ff7d3c82
[vpp.git] / vlib / vlib / elog_samples.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vlib/vlib.h>
17 #include <vppinfra/elog.h>
18
19 static inline void elog_four_int_sample (u32 *data)
20 {
21   ELOG_TYPE_DECLARE(e) = 
22     {
23       .format = "four int: first %d second %d third %d fourth %d",
24       .format_args = "i4i4i4i4",
25     };
26   struct { u32 data[4];} * ed;
27   ed = ELOG_DATA (&vlib_global_main.elog_main, e);
28   ed->data[0] = data[0];
29   ed->data[1] = data[1];
30   ed->data[2] = data[2];
31   ed->data[3] = data[3];
32 }
33
34 static inline void elog_four_int_track_sample (u32 *data)
35 {
36   ELOG_TYPE_DECLARE(e) = 
37     {
38       .format = "four_int_track: first %d second %d third %d fourth %d",
39       .format_args = "i4i4i4i4",
40     };
41   struct { u32 data[4];} * ed;
42   ELOG_TRACK(sample_track);
43   ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, sample_track);
44   ed->data[0] = data[0];
45   ed->data[1] = data[1];
46   ed->data[2] = data[2];
47   ed->data[3] = data[3];
48 }
49
50 static inline void elog_enum_sample (u8 which)
51 {
52   ELOG_TYPE_DECLARE (e) = 
53     {
54       .format = "my enum: %s",
55       .format_args = "t1",
56       .n_enum_strings = 2,
57       .enum_strings = 
58       {
59         "string 1",
60         "string 2", 
61       },
62     };
63   struct { u8 which;} * ed;
64   ed = ELOG_DATA (&vlib_global_main.elog_main, e);
65   ed->which = which;
66 }
67
68 static inline void elog_one_datum_sample (u32 data)
69 {
70   ELOG_TYPE_DECLARE (e) = 
71     {
72       .format = "one datum: %d",
73       .format_args = "i4",
74     };
75   
76   elog (&vlib_global_main.elog_main, &e, data);
77 }
78
79 static clib_error_t *
80 test_elog_command_fn (vlib_main_t * vm,
81                  unformat_input_t * input,
82                  vlib_cli_command_t * cmd)
83 {
84   int i;
85   u32 samples[4];
86
87   for (i = 0; i < 10; i++)
88     {
89       samples[0] = i;
90       samples[1] = i+1;
91       samples[2] = i+2;
92       samples[3] = i+3;
93
94       elog_four_int_sample (samples);
95       elog_four_int_track_sample (samples);
96       elog_enum_sample (0);
97       elog_enum_sample (1);
98       elog_one_datum_sample (i);
99     }
100
101   return 0;
102 }
103
104 VLIB_CLI_COMMAND (test_elog_command, static) = {
105   .path = "test elog sample",
106   .short_help = "test elog sample",
107   .function = test_elog_command_fn,
108 };