Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

log.cc

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright (c) 1997 Michael Graff.
00016  * Copyright (c) 2001 James Hess <mysidia@sorcery.net>
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms, with or without
00020  * modification, are permitted provided that the following conditions
00021  * are met:
00022  * 1. Redistributions of source code must retain the above copyright
00023  *    notice, this list of conditions and the following disclaimer.
00024  * 2. Redistributions in binary form must reproduce the above copyright
00025  *    notice, this list of conditions and the following disclaimer in the
00026  *    documentation and/or other materials provided with the distribution.
00027  * 3. Neither the name of the authors nor the names of its contributors
00028  *    may be used to endorse or promote products derived from this software
00029  *    without specific prior written permission.
00030  *
00031  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
00032  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00034  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
00035  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00036  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00037  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00038  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00039  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00040  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00041  * SUCH DAMAGE.
00042  */
00043 
00044 #include "services.h"
00045 #include "log.h"
00046 
00047 #ifndef NUM_LOG_ENTRIES
00048 
00049 #define NUM_LOG_ENTRIES     100 /* number of log entries */
00050 #endif
00051 
00052 #ifndef LOG_ENTRY_SIZE
00053 
00054 #define LOG_ENTRY_SIZE      200 /* size of each entry */
00055 #endif
00056 
00058 const char *nullFmtHack = (const char *)0;
00059 
00061 static char *log_entry[NUM_LOG_ENTRIES];
00062 
00064 static int next_entry;
00065 
00067 static int num_entries;
00068 #ifdef C_DEBUG2
00069 
00071 FILE *fp1;
00072 #endif
00073 
00081 int
00082 SLogfile::log(UserList *sender,
00083               interp::services_cmd_id cmd,
00084               const char *target,
00085               u_int32_t flags, const char *extra, ...) {
00086     va_list ap;
00087 
00088     va_start(ap, extra);
00089     logx(sender, 0, cmd, target, flags, extra, ap);
00090     va_end(ap);
00091 
00092     va_start(ap, extra);
00093     logx(sender, 1, cmd, target, flags, extra, ap);
00094     va_end(ap);
00095 }
00096 
00100 int
00101 SLogfile::logw(UserList *sender,
00102               interp::services_cmd_id cmd,
00103               const char *target,
00104               u_int32_t flags, const char *extra, ...) {
00105     va_list ap;
00106 
00107     va_start(ap, extra);
00108     logx(sender, 1, cmd, target, flags, extra, ap);
00109     va_end(ap);
00110 }
00111 
00112 int
00113 SLogfile::logx(UserList *sender, int zb,
00114               interp::services_cmd_id cmd,
00115               const char *target,
00116               u_int32_t flags, const char *extra, va_list ap)
00117 {
00118     static char buffer[IRCBUF * 2];
00119     char senderblock[IRCBUF];
00120 
00121     if (extra && *extra && ap != NULL) {
00122         vsnprintf(buffer, IRCBUF, extra, ap);
00123     } else
00124         *buffer = '\0';
00125 
00126     if (sender)
00127         sprintf(senderblock, "%s!%s@%s", sender->nick, sender->user,
00128                 sender->host);
00129     else
00130         strcpy(senderblock, "-");
00131     fprintf(zb ? fpw : fp, "%s %lu %s %lu %s",
00132             interp::cmd_name(cmd), time(NULL),
00133             *senderblock ? senderblock : "<???>",
00134             (unsigned long int)flags, target ? target : "");
00135     if (*buffer)
00136         fprintf(zb ? fpw : fp, " %s\n", buffer);
00137     else
00138         putc('\n', zb ? fpw : fp);
00139     return 0;
00140 }
00141 
00142 // Quick reference, remove this comment later
00143 // NS_SETFLAG //        logDump(operlog, "%s!%s@%s set flag(s): %s on %s", from, tmp->user, tmp->host, args[2], args[1]);
00144 // NS_SETOP   //
00145 // NS_GETREALPASS // logDump(operlog, "%s!%s@%s tries to getreal pass %s.", from, tmp->user, tmp->host, args[1]);
00146 // NS_GETREALPASS // logDump(operlog, "%s!%s@%s GETREALPASS %s%s", from, tmp->user, tmp->host, args[1], isRoot(tmp) ? " +" : "");
00147 // CS_DROP // logDump(chanlog, "%s!%s@%s failed DROP on %s", from, nick->user, nick->host, args[1]);
00148 // CS_DELETE //             logDump(chanlog, "%s!%s@%s (oper cmd) FAILED DROP %s", from, tmp->user, tmp->host, args[1]);
00149 // NS_IDENTIFY // "%s!%s@%s: Bypassed AHURT ban with registered nick %s\n", from, tmp->user, tmp->host, tonick->nick);
00150 
00151 
00152 
00156 void
00157 dlogInit(void)
00158 {
00159     int i;
00160 
00161     for (i = 0; i < NUM_LOG_ENTRIES; i++) {
00162         log_entry[i] = (char *)oalloc(LOG_ENTRY_SIZE);
00163         if (log_entry[i] == NULL) {
00164             fprintf(stderr,
00165                     "Could not allocate memory initializing the log\n");
00166             exit(1);
00167         }
00168     }
00169 
00170     next_entry = 0;
00171     num_entries = 0;
00172 }
00173 
00177 void
00178 dlogEntry(char *format, ...)
00179 {
00180     va_list ap;
00181 
00182     va_start(ap, format);
00183     (void)vsnprintf(log_entry[next_entry], LOG_ENTRY_SIZE, format, ap);
00184     va_end(ap);
00185 
00186     next_entry++;
00187     if (next_entry >= NUM_LOG_ENTRIES)
00188         next_entry = 0;
00189 
00190     if (num_entries < NUM_LOG_ENTRIES)
00191         num_entries++;
00192 }
00193 
00197 void
00198 dlogDump(FILE *fp)
00199 {
00200     int i;
00201 
00202     /*
00203      * Null fp is bad
00204      */
00205     if (fp == NULL)
00206         return;
00207 
00208     /*
00209      * Nothing to dump
00210      */
00211     if (num_entries == 0)
00212         return;
00213 
00214     /*
00215      * Start the log dump
00216      */
00217     fprintf(fp, "Dump of log [%d entries]\n", num_entries);
00218 
00219     /*
00220      * print out the individual entries.
00221      */
00222     if (num_entries < NUM_LOG_ENTRIES) {
00223         for (i = 0; i < num_entries; i++)
00224             fprintf(fp, "%d: %s\n", i, log_entry[i]);
00225     } else {
00226         for (i = 0; i < num_entries; i++) {
00227             fprintf(fp, "%d: %s\n", i, log_entry[next_entry++]);
00228 
00229             if (next_entry >= NUM_LOG_ENTRIES)
00230                 next_entry = 0;
00231         }
00232     }
00233 }
00234 
00238 void
00239 logDump(FILE *fp, char *format, ...)
00240 {
00241     char buffer[512];
00242     char stuff[80];
00243     time_t doot = time(NULL);
00244     va_list crud;
00245 
00246     va_start(crud, format);
00247     vsprintf(buffer, format, crud);
00248     va_end(crud);
00249 
00250     strftime(stuff, 80, "%H:%M[%d/%m/%Y]", localtime(&doot));
00251     fprintf(fp, "(%s) %s\n", stuff, buffer);
00252     return;
00253 }
00254 
00259 const char *
00260 fullhost1(const UserList *nick)
00261 {
00262     static char buf[NICKLEN + USERLEN + HOSTLEN + 5];
00263 
00264     if (!nick)
00265         return NULL;
00266     sprintf(buf, "%s!%s@%s", nick->nick, nick->user, nick->host);
00267     return buf;
00268 }

Generated at Sat Oct 25 20:56:08 2003 for Services using Doxygen.
Services Copyr. 1996-2001 Chip Norkus, Max Byrd, Greg Poma, Michael Graff, James Hess, Dafydd James. All rights reserved See LICENSE for licensing information.