Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Nathan Moos | bfa0398 | 2021-01-15 14:32:07 -0800 | [diff] [blame] | 19 | #include <sys/time.h> |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 20 | #include <vppinfra/types.h> |
| 21 | |
Damjan Marion | 4d7ad4a | 2020-10-23 21:52:50 +0200 | [diff] [blame] | 22 | #define foreach_vlib_log_level \ |
| 23 | _(EMERG, emerg) \ |
| 24 | _(ALERT, alert) \ |
| 25 | _(CRIT, crit) \ |
| 26 | _(ERR, error) \ |
| 27 | _(WARNING, warn) \ |
| 28 | _(NOTICE, notice) \ |
| 29 | _(INFO, info) \ |
| 30 | _(DEBUG, debug) \ |
| 31 | _(DISABLED, disabled) |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 32 | |
| 33 | typedef enum |
| 34 | { |
Damjan Marion | 4d7ad4a | 2020-10-23 21:52:50 +0200 | [diff] [blame] | 35 | VLIB_LOG_LEVEL_UNKNOWN = 0, |
| 36 | #define _(uc,lc) VLIB_LOG_LEVEL_##uc, |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 37 | foreach_vlib_log_level |
| 38 | #undef _ |
Damjan Marion | 4d7ad4a | 2020-10-23 21:52:50 +0200 | [diff] [blame] | 39 | VLIB_LOG_N_LEVELS, |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 40 | } vlib_log_level_t; |
| 41 | |
Paul Vinciguerra | 03f1af2 | 2019-06-25 22:30:19 -0400 | [diff] [blame] | 42 | typedef struct |
| 43 | { |
| 44 | vlib_log_level_t level; |
| 45 | vlib_log_class_t class; |
| 46 | f64 timestamp; |
| 47 | u8 *string; |
| 48 | } vlib_log_entry_t; |
| 49 | |
| 50 | typedef struct |
| 51 | { |
| 52 | u32 index; |
| 53 | u8 *name; |
| 54 | // level of log messages kept for this subclass |
| 55 | vlib_log_level_t level; |
| 56 | // level of log messages sent to syslog for this subclass |
| 57 | vlib_log_level_t syslog_level; |
| 58 | // flag saying whether this subclass is logged to syslog |
| 59 | f64 last_event_timestamp; |
| 60 | int last_sec_count; |
| 61 | int is_throttling; |
| 62 | int rate_limit; |
| 63 | } vlib_log_subclass_data_t; |
| 64 | |
| 65 | typedef struct |
| 66 | { |
| 67 | u32 index; |
| 68 | u8 *name; |
| 69 | vlib_log_subclass_data_t *subclasses; |
| 70 | } vlib_log_class_data_t; |
| 71 | |
| 72 | typedef struct |
| 73 | { |
Damjan Marion | 055e640 | 2020-10-21 19:43:36 +0200 | [diff] [blame] | 74 | vlib_log_level_t level; |
| 75 | vlib_log_level_t syslog_level; |
| 76 | int rate_limit; |
| 77 | char *name; |
| 78 | } vlib_log_class_config_t; |
| 79 | |
Damjan Marion | 4d7ad4a | 2020-10-23 21:52:50 +0200 | [diff] [blame] | 80 | |
| 81 | typedef struct vlib_log_registration |
| 82 | { |
| 83 | char *class_name; |
| 84 | char *subclass_name; |
| 85 | vlib_log_class_t class; |
| 86 | vlib_log_level_t default_level; |
| 87 | vlib_log_level_t default_syslog_level; |
| 88 | |
| 89 | /* next */ |
| 90 | struct vlib_log_registration *next; |
| 91 | } vlib_log_class_registration_t; |
| 92 | |
Damjan Marion | 055e640 | 2020-10-21 19:43:36 +0200 | [diff] [blame] | 93 | typedef struct |
| 94 | { |
Paul Vinciguerra | 03f1af2 | 2019-06-25 22:30:19 -0400 | [diff] [blame] | 95 | vlib_log_entry_t *entries; |
| 96 | vlib_log_class_data_t *classes; |
| 97 | int size, next, count; |
| 98 | |
Paul Vinciguerra | 03f1af2 | 2019-06-25 22:30:19 -0400 | [diff] [blame] | 99 | int default_rate_limit; |
| 100 | int default_log_level; |
| 101 | int default_syslog_log_level; |
| 102 | int unthrottle_time; |
Damjan Marion | 06d8226 | 2020-10-21 12:43:40 +0200 | [diff] [blame] | 103 | u32 max_class_name_length; |
Paul Vinciguerra | 03f1af2 | 2019-06-25 22:30:19 -0400 | [diff] [blame] | 104 | |
| 105 | /* time zero */ |
| 106 | struct timeval time_zero_timeval; |
| 107 | f64 time_zero; |
| 108 | |
Damjan Marion | 055e640 | 2020-10-21 19:43:36 +0200 | [diff] [blame] | 109 | /* config */ |
| 110 | vlib_log_class_config_t *configs; |
| 111 | uword *config_index_by_name; |
Dave Barach | bc867c3 | 2020-11-25 10:07:09 -0500 | [diff] [blame] | 112 | int add_to_elog; |
Damjan Marion | 4d7ad4a | 2020-10-23 21:52:50 +0200 | [diff] [blame] | 113 | |
| 114 | /* registrations */ |
| 115 | vlib_log_class_registration_t *registrations; |
Paul Vinciguerra | 03f1af2 | 2019-06-25 22:30:19 -0400 | [diff] [blame] | 116 | } vlib_log_main_t; |
| 117 | |
| 118 | extern vlib_log_main_t log_main; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 119 | |
Damjan Marion | 62d656a | 2022-03-09 16:10:54 +0100 | [diff] [blame] | 120 | clib_error_t *vlib_log_init (struct vlib_main_t *vm); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 121 | vlib_log_class_t vlib_log_register_class (char *vlass, char *subclass); |
Dave Barach | 8dc954a | 2020-02-05 17:31:09 -0500 | [diff] [blame] | 122 | vlib_log_class_t |
| 123 | vlib_log_register_class_rate_limit (char *class, char *subclass, |
| 124 | u32 rate_limit); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 125 | void vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, |
| 126 | ...); |
Paul Vinciguerra | 03f1af2 | 2019-06-25 22:30:19 -0400 | [diff] [blame] | 127 | int last_log_entry (); |
| 128 | u8 *format_vlib_log_class (u8 * s, va_list * args); |
Damjan Marion | 06d8226 | 2020-10-21 12:43:40 +0200 | [diff] [blame] | 129 | u8 *format_vlib_log_level (u8 * s, va_list * args); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 130 | |
| 131 | #define vlib_log_emerg(...) vlib_log(VLIB_LOG_LEVEL_EMERG, __VA_ARGS__) |
| 132 | #define vlib_log_alert(...) vlib_log(VLIB_LOG_LEVEL_ALERT, __VA_ARGS__) |
| 133 | #define vlib_log_crit(...) vlib_log(VLIB_LOG_LEVEL_CRIT, __VA_ARGS__) |
| 134 | #define vlib_log_err(...) vlib_log(VLIB_LOG_LEVEL_ERR, __VA_ARGS__) |
Mohsin Kazmi | ed984e0 | 2018-05-17 16:47:08 +0200 | [diff] [blame] | 135 | #define vlib_log_warn(...) vlib_log(VLIB_LOG_LEVEL_WARNING, __VA_ARGS__) |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 136 | #define vlib_log_notice(...) vlib_log(VLIB_LOG_LEVEL_NOTICE, __VA_ARGS__) |
| 137 | #define vlib_log_info(...) vlib_log(VLIB_LOG_LEVEL_INFO, __VA_ARGS__) |
| 138 | #define vlib_log_debug(...) vlib_log(VLIB_LOG_LEVEL_DEBUG, __VA_ARGS__) |
| 139 | |
Damjan Marion | 4d7ad4a | 2020-10-23 21:52:50 +0200 | [diff] [blame] | 140 | #define VLIB_REGISTER_LOG_CLASS(x,...) \ |
| 141 | __VA_ARGS__ vlib_log_class_registration_t x; \ |
| 142 | static void __clib_constructor \ |
| 143 | __vlib_add_log_registration_##x (void) \ |
| 144 | { \ |
| 145 | vlib_log_main_t * lm = &log_main; \ |
| 146 | x.next = lm->registrations; \ |
| 147 | x.class = ~0; \ |
| 148 | lm->registrations = &x; \ |
| 149 | } \ |
| 150 | __VA_ARGS__ vlib_log_class_registration_t x |
| 151 | |
Damjan Marion | bf179a9 | 2023-05-26 19:06:10 +0000 | [diff] [blame] | 152 | static_always_inline vlib_log_class_data_t * |
Vratko Polak | e9ea7d5 | 2023-07-26 13:48:20 +0200 | [diff] [blame] | 153 | vlib_log_get_class_data (vlib_log_class_t ci) |
Damjan Marion | bf179a9 | 2023-05-26 19:06:10 +0000 | [diff] [blame] | 154 | { |
| 155 | vlib_log_main_t *lm = &log_main; |
| 156 | return vec_elt_at_index (lm->classes, (ci >> 16)); |
| 157 | } |
| 158 | |
| 159 | static_always_inline vlib_log_subclass_data_t * |
| 160 | vlib_log_get_subclass_data (vlib_log_class_t ci) |
| 161 | { |
Vratko Polak | e9ea7d5 | 2023-07-26 13:48:20 +0200 | [diff] [blame] | 162 | vlib_log_class_data_t *c = vlib_log_get_class_data (ci); |
Damjan Marion | bf179a9 | 2023-05-26 19:06:10 +0000 | [diff] [blame] | 163 | return vec_elt_at_index (c->subclasses, (ci & 0xffff)); |
| 164 | } |
| 165 | |
| 166 | static_always_inline int |
| 167 | vlib_log_is_enabled (vlib_log_level_t level, vlib_log_class_t class) |
| 168 | { |
| 169 | vlib_log_subclass_data_t *sc = vlib_log_get_subclass_data (class); |
| 170 | |
| 171 | if (level <= sc->level && sc->level != VLIB_LOG_LEVEL_DISABLED) |
| 172 | return 1; |
| 173 | |
| 174 | if (level <= sc->syslog_level && sc->syslog_level != VLIB_LOG_LEVEL_DISABLED) |
| 175 | return 1; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 180 | #endif /* included_vlib_log_h */ |
| 181 | |
| 182 | /* |
| 183 | * fd.io coding-style-patch-verification: ON |
| 184 | * |
| 185 | * Local Variables: |
| 186 | * eval: (c-set-style "gnu") |
| 187 | * End: |
| 188 | */ |