1 /** 2 @file dlog.h 3 @brief output and logfile abstraction 4 5 This file is part of harvid 6 7 @author Robin Gareus <robin@gareus.org> 8 @copyright 9 10 Copyright (C) 2002,2003,2008-2014 Robin Gareus <robin@gareus.org> 11 12 This program is free software; you can redistribute it and/or modify 13 it under the terms of the GNU General Public License as published by 14 the Free Software Foundation; either version 2, or (at your option) 15 any later version. 16 17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License for more details. 21 22 You should have received a copy of the GNU General Public License 23 along with this program. If not, see <http://www.gnu.org/licenses/>. 24 */ 25 #ifndef _harvid_dlog_H 26 #define _harvid_dlog_H 27 28 /* some common win/posix issues */ 29 #define PRIlld "lld" 30 31 #ifndef WIN32 32 #define mymsleep(ms) usleep((ms) * 1000) 33 #define SNPRINTF snprintf 34 35 #else 36 37 #include <windows.h> 38 #define mymsleep(ms) Sleep(ms) 39 int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...); 40 #define SNPRINTF portable_snprintf 41 #endif 42 43 /* syslog */ 44 #ifndef WIN32 45 #include <syslog.h> 46 #else 47 #define LOG_EMERG 1 48 #define LOG_CRIT 2 49 #define LOG_ERR 3 50 #define LOG_WARNING 4 51 #define LOG_INFO 6 52 #endif 53 54 /* log Levels */ 55 #define DLOG_EMERG LOG_EMERG ///< quiet (system is unusable) 56 #define DLOG_CRIT LOG_CRIT ///< critical conditions -- usually implies exit() or process termination 57 #define DLOG_ERR LOG_ERR ///< error conditions -- recoverable errors 58 #define DLOG_WARNING LOG_WARNING ///< warning conditions 59 #define DLOG_INFO LOG_INFO ///< informational 60 61 enum {DEBUG_SRV=1, DEBUG_HTTP=2, DEBUG_CON=4, DEBUG_DCTL=8, DEBUG_ICS=16}; 62 63 #ifndef DAEMON_LOG_SELF 64 extern int debug_level; ///< global debug_level used by @ref dlog() 65 extern int debug_section; ///< global debug_level used by @ref dlog() 66 #endif 67 68 /** 69 * printf replacement 70 * 71 * @param level log level 0-7 see syslog.h 72 * @param format same as printf(...) 73 */ 74 void dlog(int level, const char *format, ...); 75 76 #ifdef NDEBUG 77 #define debugmsg(section, ...) {} 78 #else 79 #define debugmsg(section, ...) {if (debug_section§ion) printf(__VA_ARGS__);} 80 #endif 81 82 #define raprintf(p, off, siz, ...) \ 83 { \ 84 if (siz - off < 256) { siz *= 2; p = realloc(p, siz * sizeof(char)); } \ 85 off += snprintf(p + off, siz - off, __VA_ARGS__); \ 86 } 87 88 #define rpprintf(p, off, siz, ...) \ 89 { \ 90 while ((*siz) - (*off) <= SNPRINTF((*p) + (*off), 0, __VA_ARGS__)) \ 91 { (*siz) *= 2; (*p) = realloc(*p, (*siz) * sizeof(char)); } \ 92 (*off) += snprintf((*p) + (*off), (*siz) - (*off), __VA_ARGS__); \ 93 assert((*siz) >= (*off)); \ 94 } 95 96 #define rprintf(...) rpprintf(m,o,s, __VA_ARGS__) 97 98 #endif 99 100