blob: a94c57a51ebc11730ff21e6560f7d1db6ef73562 [file] [log] [blame]
Damjan Marion07a38572018-01-21 06:44:18 -08001/*
2 * Copyright (c) 2018 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#ifndef included_vlib_log_h
17#define included_vlib_log_h
18
19#include <vppinfra/types.h>
20
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020021#define foreach_vlib_log_level \
22 _(EMERG, emerg) \
23 _(ALERT, alert) \
24 _(CRIT, crit) \
25 _(ERR, error) \
26 _(WARNING, warn) \
27 _(NOTICE, notice) \
28 _(INFO, info) \
29 _(DEBUG, debug) \
30 _(DISABLED, disabled)
Damjan Marion07a38572018-01-21 06:44:18 -080031
32typedef enum
33{
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020034 VLIB_LOG_LEVEL_UNKNOWN = 0,
35#define _(uc,lc) VLIB_LOG_LEVEL_##uc,
Damjan Marion07a38572018-01-21 06:44:18 -080036 foreach_vlib_log_level
37#undef _
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020038 VLIB_LOG_N_LEVELS,
Damjan Marion07a38572018-01-21 06:44:18 -080039} vlib_log_level_t;
40
Paul Vinciguerra03f1af22019-06-25 22:30:19 -040041typedef struct
42{
43 vlib_log_level_t level;
44 vlib_log_class_t class;
45 f64 timestamp;
46 u8 *string;
47} vlib_log_entry_t;
48
49typedef struct
50{
51 u32 index;
52 u8 *name;
53 // level of log messages kept for this subclass
54 vlib_log_level_t level;
55 // level of log messages sent to syslog for this subclass
56 vlib_log_level_t syslog_level;
57 // flag saying whether this subclass is logged to syslog
58 f64 last_event_timestamp;
59 int last_sec_count;
60 int is_throttling;
61 int rate_limit;
62} vlib_log_subclass_data_t;
63
64typedef struct
65{
66 u32 index;
67 u8 *name;
68 vlib_log_subclass_data_t *subclasses;
69} vlib_log_class_data_t;
70
71typedef struct
72{
Damjan Marion055e6402020-10-21 19:43:36 +020073 vlib_log_level_t level;
74 vlib_log_level_t syslog_level;
75 int rate_limit;
76 char *name;
77} vlib_log_class_config_t;
78
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020079
80typedef struct vlib_log_registration
81{
82 char *class_name;
83 char *subclass_name;
84 vlib_log_class_t class;
85 vlib_log_level_t default_level;
86 vlib_log_level_t default_syslog_level;
87
88 /* next */
89 struct vlib_log_registration *next;
90} vlib_log_class_registration_t;
91
Damjan Marion055e6402020-10-21 19:43:36 +020092typedef struct
93{
Paul Vinciguerra03f1af22019-06-25 22:30:19 -040094 vlib_log_entry_t *entries;
95 vlib_log_class_data_t *classes;
96 int size, next, count;
97
Paul Vinciguerra03f1af22019-06-25 22:30:19 -040098 int default_rate_limit;
99 int default_log_level;
100 int default_syslog_log_level;
101 int unthrottle_time;
Damjan Marion06d82262020-10-21 12:43:40 +0200102 u32 max_class_name_length;
Paul Vinciguerra03f1af22019-06-25 22:30:19 -0400103
104 /* time zero */
105 struct timeval time_zero_timeval;
106 f64 time_zero;
107
Damjan Marion055e6402020-10-21 19:43:36 +0200108 /* config */
109 vlib_log_class_config_t *configs;
110 uword *config_index_by_name;
Dave Barachbc867c32020-11-25 10:07:09 -0500111 int add_to_elog;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200112
113 /* registrations */
114 vlib_log_class_registration_t *registrations;
Paul Vinciguerra03f1af22019-06-25 22:30:19 -0400115} vlib_log_main_t;
116
117extern vlib_log_main_t log_main;
Damjan Marion07a38572018-01-21 06:44:18 -0800118
119vlib_log_class_t vlib_log_register_class (char *vlass, char *subclass);
Dave Barach8dc954a2020-02-05 17:31:09 -0500120vlib_log_class_t
121vlib_log_register_class_rate_limit (char *class, char *subclass,
122 u32 rate_limit);
Damjan Marion07a38572018-01-21 06:44:18 -0800123void vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt,
124 ...);
Paul Vinciguerra03f1af22019-06-25 22:30:19 -0400125int last_log_entry ();
126u8 *format_vlib_log_class (u8 * s, va_list * args);
Damjan Marion06d82262020-10-21 12:43:40 +0200127u8 *format_vlib_log_level (u8 * s, va_list * args);
Damjan Marion07a38572018-01-21 06:44:18 -0800128
129#define vlib_log_emerg(...) vlib_log(VLIB_LOG_LEVEL_EMERG, __VA_ARGS__)
130#define vlib_log_alert(...) vlib_log(VLIB_LOG_LEVEL_ALERT, __VA_ARGS__)
131#define vlib_log_crit(...) vlib_log(VLIB_LOG_LEVEL_CRIT, __VA_ARGS__)
132#define vlib_log_err(...) vlib_log(VLIB_LOG_LEVEL_ERR, __VA_ARGS__)
Mohsin Kazmied984e02018-05-17 16:47:08 +0200133#define vlib_log_warn(...) vlib_log(VLIB_LOG_LEVEL_WARNING, __VA_ARGS__)
Damjan Marion07a38572018-01-21 06:44:18 -0800134#define vlib_log_notice(...) vlib_log(VLIB_LOG_LEVEL_NOTICE, __VA_ARGS__)
135#define vlib_log_info(...) vlib_log(VLIB_LOG_LEVEL_INFO, __VA_ARGS__)
136#define vlib_log_debug(...) vlib_log(VLIB_LOG_LEVEL_DEBUG, __VA_ARGS__)
137
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200138#define VLIB_REGISTER_LOG_CLASS(x,...) \
139__VA_ARGS__ vlib_log_class_registration_t x; \
140static void __clib_constructor \
141__vlib_add_log_registration_##x (void) \
142 { \
143 vlib_log_main_t * lm = &log_main; \
144 x.next = lm->registrations; \
145 x.class = ~0; \
146 lm->registrations = &x; \
147 } \
148__VA_ARGS__ vlib_log_class_registration_t x
149
Damjan Marion07a38572018-01-21 06:44:18 -0800150#endif /* included_vlib_log_h */
151
152/*
153 * fd.io coding-style-patch-verification: ON
154 *
155 * Local Variables:
156 * eval: (c-set-style "gnu")
157 * End:
158 */