Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

poslib/server/log.h

Go to the documentation of this file.

  
00001 /* 00002 Posadis - A DNS Server 00003 Logging functions 00004 Copyright (C) 2002 Meilof Veeningen <> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #ifndef __POSLIB_LOG_H 00022 #define __POSLIB_LOG_H 00023 00024 #include 00025 00026 #include <poslib/postime.h> 00027 #include 00028 00029 #include 00030 #include 00031 #include 00032 00066 #ifdef HAVE_SYSLOG_H 00067 #include 00068 #else 00069 #define LOG_INFO 0 00070 #define LOG_WARNING 1 00071 #define LOG_ERR 2 00072 #define LOG_CRIT 3 00073 #endif 00074 00081 enum log_context { 00082 context_none = 0, 00083 context_threads = 1, 00084 context_socket = 2, 00085 context_server = 3, 00086 context_query = 4, 00087 context_conf = 5, 00088 context_zonedata = 6, 00089 context_system = 7, 00090 }; 00091 00097 enum log_severity { 00098 log_info = LOG_INFO, 00099 log_warning = LOG_WARNING, 00100 log_error = LOG_ERR, 00101 log_panic = LOG_CRIT, 00102 }; 00103 00104 00105 #if defined(_WIN32) && !defined(BUILD_POSD_DLL) 00106 extern __declspec(dllimport) const char log_contexts[][16]; 00107 #else 00110 extern const char log_contexts[][16]; 00111 #endif 00112 00113 #if defined(_WIN32) && !defined(BUILD_POSD_DLL) 00114 extern __declspec(dllimport) void (*pos_log)(log_context context, log_severity severity, 00115 char *log_fmt, ...); 00116 #else 00125 extern void (*pos_log)(log_context context, log_severity severity, 00126 char *log_fmt, ...); 00127 #endif 00128 00129 #ifdef POS_DEFAULTLOG 00130 00131 #ifdef POS_DEFAULTLOG_FILE 00132 FILE *outfile = NULL; 00133 #endif 00134 00135 void l_pos_log(log_context context, log_severity severity, 00136 char *log_fmt, ...); 00137 00138 class __static_log_initializer { 00139 public: 00140 __static_log_initializer() { 00141 pos_log = l_pos_log; 00142 #if defined(POS_DEFAULTLOG_SYSLOG) && defined(HAVE_SYSLOG_H) 00143 openlog("posadis", LOG_PID, LOG_DAEMON); 00144 #endif 00145 } 00146 ~__static_log_initializer() { 00147 #if defined(POS_DEFAULTLOG_SYSLOG) && defined(HAVE_SYSLOG_H) 00148 closelog(); 00149 #endif 00150 #ifdef POS_DEFAULTLOG_FILE 00151 if (outfile) fclose(outfile); 00152 #endif 00153 } 00154 } ___static_log_intializer; 00155 00156 #ifdef POS_DEFAULTLOG_FPTR 00157 void(*pos_log_fptr)(log_context context, log_severity severity, char *message) = NULL; 00158 #endif 00159 00160 #ifdef POS_DEFAULTLOG_STDERR 00161 00167 bool do_stderr_log = true; 00168 #endif 00169 00170 void l_pos_log(log_context context, log_severity severity, 00171 char *log_fmt, ...) { 00172 char buff[4096]; 00173 struct tm *tstruct; 00174 va_list args; 00175 time_t tsecs; 00176 int pos; 00177 00178 tsecs = time(NULL); 00179 tstruct = localtime(&tsecs); 00180 sprintf(buff, "%04d/%02d/%02d %02d:%02d:%02d|", tstruct->tm_year + 1900, tstruct->tm_mon + 1, tstruct->tm_mday, tstruct->tm_hour, tstruct->tm_min, tstruct->tm_sec); 00181 switch (severity) { 00182 case log_info: strcat(buff, "info: "); break; 00183 case log_warning: strcat(buff, "warning: "); break; 00184 case log_error: strcat(buff, "error: "); break; 00185 case log_panic: strcat(buff, "panic: "); break; 00186 } 00187 00188 if (context != context_none) { 00189 strcat(buff, "["); 00190 strcat(buff, log_contexts[context]); 00191 strcat(buff, "] "); 00192 } 00193 00194 pos = strlen(buff); 00195 va_start(args, log_fmt); 00196 vsnprintf(&buff[strlen(buff)], sizeof(buff) - strlen(buff) - 1, log_fmt, args); 00197 va_end(args); 00198 00199 #ifdef POS_DEFAULTLOG_FILE 00200 if (outfile) fprintf(outfile, "%s\n", buff); 00201 #ifndef POS_DEFAULTLOG_FILE_NOFLUSH 00202 fflush(outfile); 00203 #endif 00204 #endif 00205 #ifdef POS_DEFAULTLOG_STDERR 00206 if (do_stderr_log) fprintf(stderr, "%s\n", buff); 00207 #endif 00208 #if defined(POS_DEFAULTLOG_SYSLOG) && defined(HAVE_SYSLOG_H) 00209 syslog(severity, "%s", strchr(buff, '|') + 1); 00210 #endif 00211 #ifdef POS_DEFAULTLOG_FPTR 00212 if (pos_log_fptr) pos_log_fptr(context, severity, buff + pos); 00213 #endif 00214 } 00215 00216 #else /* POS_DEFAULTLOG */ 00217 00227 extern FILE *outfile; 00237 extern void(*pos_log_fptr)(log_context context, log_severity severity, char *message); 00247 extern bool do_stderr_log; 00248 #endif 00249 00250 #endif /* __POSLIB_LOG_H */

  
Generated on Fri Dec 24 19:55:14 2004 for Poslib by doxygen 1.3.7