00001 /* 00002 * $Id: syslog_async.h 5072 2008-10-14 16:47:42Z henningw $ 00003 * 00004 * syslog_async is Copyright (c) 2007 Simon Kelley 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; version 2 dated June, 1991, or 00009 * (at your option) version 3 dated 29 June, 2007. 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 * You should have received a copy of the GNU General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 /*! 00020 * \file 00021 * \brief A non-blocking syslog() replacement 00022 * 00023 * Syslog_async is a non-blocking replacement for the 00024 * POSIX-standard syslog() system call. Instead of blocking, 00025 * log-lines are buffered in memory. The buffer size is limited 00026 * and if the buffer overflows log lines are lost. When lines are 00027 * lost this fact is logged with a message of the form: 00028 * 00029 * async_syslog overflow: 5 log entries lost 00030 * 00031 * In order to limit the probability of buffer overflow 00032 * short delays are added to syslog_async() calls when the 00033 * queue is getting full. The delay added is strictly 00034 * bounded and tunable. 00035 * 00036 * The API is very close to the standard syslog(), with an 00037 * additional call the tune buffer parameters and a couple 00038 * of calls into the event loop. 00039 * 00040 * The code has been tested under Linux and BSD, and with both 00041 * the syslog and syslog-ng log daemons. 00042 */ 00043 00044 #ifdef SYSLOG_ASYNC 00045 00046 #ifndef _SYSLOG_ASYNC_H 00047 #define _SYSLOG_ASYNC_H 1 00048 00049 #include <syslog.h> 00050 #include <stdarg.h> 00051 00052 00053 /* 00054 openlog_async(), closelog_async() and setlogmask_async() are 00055 identical to the POSIX equivalents. 00056 */ 00057 00058 void openlog_async(const char *ident, int option, int facility); 00059 void closelog_async(void); 00060 int setlogmask_async(int mask); 00061 00062 00063 00064 /* 00065 syslog_async() and vsyslog_async() are identical to syslog() and vsyslog() 00066 except for their blocking behaviour. The formatting is done using printf(), 00067 so the additional format operator %m is available only if the system 00068 printf() provides it. (GNU printf() does.) 00069 */ 00070 00071 void syslog_async(int priority, const char *format, ...); 00072 void vsyslog_async(int priority, const char *format, va_list ap); 00073 00074 00075 00076 /* 00077 log_fd_async() and log_write_async() are the interface between the library 00078 and the daemon event loop. 00079 00080 log_fd_async() returns a file descriptor which the library needs to write, 00081 or -1 if no write is queued. log_write_async() does the write. 00082 00083 The result of log_fd_async() is only valid until [v]syslog_async() or 00084 log_write_async() is called, so it should be called each time around the 00085 event loop, just before the call to select() or poll(). 00086 00087 A typical event loop looks like this: 00088 00089 while (1) 00090 { 00091 int log_fd; 00092 fd_set read_set, write_set; 00093 00094 FD_ZERO(&read_set); 00095 FD_ZERO(&write_set); 00096 00097 ...other stuff.. 00098 00099 if ((log_fd = log_fd_async()) != -1) 00100 FD_SET(log_fd, &write_set); 00101 00102 select(..., &read_set, &write_set, ...); 00103 00104 if (log_fd != -1 && FD_ISSET(log_fd, &write)) 00105 log_write_async(); 00106 00107 00108 ...other stuff.... 00109 } 00110 */ 00111 00112 int log_fd_async(void); 00113 void log_write_async(void); 00114 00115 00116 00117 /* 00118 tunelog_async() tunes the log-line buffer. Backlog is the limit 00119 on the number of queued log-lines. These are stored in malloc'ed memory 00120 and each line is stored in a fixed-size buffer which is just over 1K bytes. 00121 The library maintains a buffer pool to avoid heap fragmentation. Delay 00122 is the upper bound on the time taken to run syslog_async, in milliseconds. 00123 This delay is added when syslog is busy in order to reduce the probability 00124 of buffer overflow. Backlog is constrained between 1 and 99 and delay 00125 between 1 millisecond and 1000 millisconds. The default for backlog 00126 is 5 and for delay 1000. Note that delay is calculated from queue size as 00127 2^queue_size (in milliseconds) therefore the maximum delay for the default 00128 queue size is 64ms. Setting delay to zero is allowed, and inhibits the delay 00129 completely. 00130 */ 00131 00132 void tunelog_async(int backlog, int delay); 00133 00134 #endif 00135 00136 #endif
1.5.6