1 /* 2 * Copyright (C) 2015 Adrien Vergé 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef OPENFORTIVPN_LOG_H 19 #define OPENFORTIVPN_LOG_H 20 21 #include <stddef.h> 22 #include <stdint.h> 23 24 // Assign enum values explicitly, we're using them in a lookup 25 enum log_verbosity { 26 OFV_LOG_MUTE = 0, 27 OFV_LOG_ERROR = 1, 28 OFV_LOG_WARN = 2, 29 OFV_LOG_INFO = 3, 30 OFV_LOG_DEBUG = 4, 31 OFV_LOG_DEBUG_DETAILS = 5, 32 OFV_LOG_DEBUG_ALL = 6 33 }; 34 35 extern enum log_verbosity loglevel; 36 37 void init_logging(void); 38 void set_syslog(int do_syslog); 39 40 void increase_verbosity(void); 41 void decrease_verbosity(void); 42 43 void do_log(int verbosity, const char *format, ...); 44 45 #define log_level(verbosity, ...) \ 46 do { \ 47 if (loglevel >= verbosity) \ 48 do_log(verbosity, __VA_ARGS__); \ 49 } while (0) 50 51 #define log_error(...) \ 52 log_level(OFV_LOG_ERROR, __VA_ARGS__) 53 #define log_warn(...) \ 54 log_level(OFV_LOG_WARN, __VA_ARGS__) 55 #define log_info(...) \ 56 log_level(OFV_LOG_INFO, __VA_ARGS__) 57 #define log_debug(...) \ 58 log_level(OFV_LOG_DEBUG, __VA_ARGS__) 59 #define log_debug_details(...) \ 60 log_level(OFV_LOG_DEBUG_DETAILS, __VA_ARGS__) 61 #define log_debug_all(...) \ 62 log_level(OFV_LOG_DEBUG_ALL, __VA_ARGS__) 63 64 #define log_packet(...) \ 65 do { \ 66 if (loglevel >= OFV_LOG_DEBUG_DETAILS) \ 67 do_log_packet(__VA_ARGS__); \ 68 } while (0) 69 70 void do_log_packet(const char *prefix, size_t len, const uint8_t *packet); 71 72 #endif 73