1 // ---------------------------------------------------------------------------- 2 // debug.h 3 // 4 // Copyright (C) 2008, 2012 5 // Stelios Bounanos, M0GLD; Dave Freese, W1HKJ 6 // 7 // This file is part of FLCLUSTER. 8 // 9 // This is free software; you can redistribute it and/or modify 10 // it under the terms of the GNU General Public License as published by 11 // the Free Software Foundation; either version 3 of the License, or 12 // (at your option) any later version. 13 // 14 // This software is distributed in the hope that it will be useful, 15 // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 // GNU General Public License for more details. 18 // 19 // You should have received a copy of the GNU General Public License 20 // along with this program. If not, see <http://www.gnu.org/licenses/>. 21 // 22 // ---------------------------------------------------------------------------- 23 24 #ifndef _DEBUG_H_ 25 #define _DEBUG_H_ 26 27 #include "util.h" 28 29 class debug 30 { 31 public: 32 enum level_e { QUIET_LEVEL, ERROR_LEVEL, WARN_LEVEL, INFO_LEVEL, DEBUG_LEVEL, LOG_NLEVELS }; 33 enum source_e { LOG_CONTROL = 1 << 0, LOG_OTHER = 1 << 1 }; 34 35 static void start(const char* filename); 36 static void log(level_e level, const char* func, const char* srcf, int line, 37 const char* format, ...) format__(printf, 5, 6); 38 static level_e level; 39 static uint32_t mask; 40 private: 41 debug(const char* filename); 42 debug(const debug&); 43 debug& operator=(const debug&); 44 ~debug(); 45 static debug* inst; 46 }; 47 48 #define LOG(level__, source__, ...) \ 49 do { \ 50 if (level__ <= debug::level && source__ & debug::mask) \ 51 debug::log(level__, __func__, __FILE__, __LINE__, __VA_ARGS__); \ 52 } while (0) 53 54 #define LOG_DEBUG(...) LOG(debug::DEBUG_LEVEL, log_source_, __VA_ARGS__) 55 #define LOG_INFO(...) LOG(debug::INFO_LEVEL, log_source_, __VA_ARGS__) 56 #define LOG_WARN(...) LOG(debug::WARN_LEVEL, log_source_, __VA_ARGS__) 57 #define LOG_ERROR(...) LOG(debug::ERROR_LEVEL, log_source_, __VA_ARGS__) 58 #define LOG_QUIET(...) LOG(debug::QUIET_LEVEL, log_source_, __VA_ARGS__) 59 60 unused__ static uint32_t log_source_ = debug::LOG_OTHER; 61 62 #if defined(__GNUC__) && (__GNUC__ >= 3) 63 # define LOG_FILE_SOURCE(source__) \ 64 __attribute__((constructor)) \ 65 static void log_set_source_(void) { log_source_ = source__; } 66 #else 67 # define LOG_FILE_SOURCE(source__) 68 #endif 69 70 #define LOG_SET_SOURCE(source__) log_source_ = source__ 71 72 #endif // _DEBUG_H_ 73 74 // Local Variables: 75 // mode: c++ 76 // c-file-style: "linux" 77 // End: 78