wvlogfile.cc

00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * A "Log Receiver" that logs messages to a file
00006  */
00007 #include "wvlogfile.h"
00008 #include "wvtimeutils.h"
00009 #include "wvdiriter.h"
00010 #include "strutils.h"
00011 #include "wvdailyevent.h"
00012 #include "wvfork.h"
00013 #include <time.h>
00014 #include <sys/types.h>
00015 #ifndef _WIN32
00016 #include <sys/wait.h>
00017 #endif
00018 
00019 #define MAX_LOGFILE_SZ  1024*1024*100   // 100 Megs
00020 
00021 static time_t gmtoffset()
00022 {
00023     time_t nowgmt = time(NULL);
00024     struct tm gmt = *gmtime(&nowgmt);
00025     struct tm local = *localtime(&nowgmt);
00026     time_t nowantilocal = mktime(&gmt); // mktime assumes gmt
00027     return nowgmt - nowantilocal;
00028 }
00029 
00030 
00031 //----------------------------------- WvLogFileBase ------------------
00032 
00033 WvLogFileBase::WvLogFileBase(WvStringParm _filename, WvLog::LogLevel _max_level)
00034     : WvLogRcv(_max_level),
00035       WvFile(_filename, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644)
00036 {
00037     fsync_every = fsync_count = 0;
00038 }
00039 
00040 
00041 WvLogFileBase::WvLogFileBase(WvLog::LogLevel _max_level) 
00042     : WvLogRcv(_max_level) 
00043 { 
00044     fsync_every = fsync_count = 0;
00045 }
00046 
00047 
00048 void WvLogFileBase::_mid_line(const char *str, size_t len)
00049 {
00050     WvFile::write(str, len);
00051 }
00052 
00053 
00054 void WvLogFileBase::_end_line()
00055 {
00056     if (fsync_every)
00057     {
00058         fsync_count--;
00059         if (fsync_count <= 0 || fsync_count > fsync_every)
00060         {
00061             fsync_count = fsync_every;
00062             //WvFile::print("tick!\n");
00063             WvFile::flush(1000);
00064             fsync(getwfd());
00065         }
00066     }
00067 }
00068 
00069 #ifdef _WIN32
00070 #define TIME_FORMAT "%b %d %H:%M:%S" // timezones in win32 look stupid
00071 #else
00072 #define TIME_FORMAT "%b %d %H:%M:%S %Z"
00073 #endif
00074 
00075 void WvLogFileBase::_make_prefix(time_t timenow)
00076 {
00077     struct tm* tmstamp = localtime(&timenow);
00078     char timestr[30];
00079     strftime(&timestr[0], 30, TIME_FORMAT, tmstamp);
00080 
00081     prefix = WvString("%s: %s<%s>: ", timestr, last_source,
00082         loglevels[last_level]);
00083     prelen = prefix.len();
00084 }
00085 
00086 //----------------------------------- WvLogFile ----------------------
00087 
00088 WvLogFile::WvLogFile(WvStringParm _filename, WvLog::LogLevel _max_level,
00089                      int _keep_for, bool _force_new_line, bool _allow_append)
00090     : WvLogFileBase(_max_level), keep_for(_keep_for), filename(_filename),
00091       allow_append(_allow_append)
00092 {
00093     WvLogRcv::force_new_line = _force_new_line;
00094     start_log();
00095 }
00096 
00097 void WvLogFile::_make_prefix(time_t timenow)
00098 {
00099     // struct tm *tmstamp = localtime(&timenow);
00100     struct stat statbuf;
00101 
00102     // Get the filesize
00103     if (fstat(getfd(), &statbuf) == -1)
00104         statbuf.st_size = 0;
00105 
00106     // Make sure we are calculating last_day in the current time zone.
00107     if (last_day != ((timenow + gmtoffset())/86400) 
00108         || statbuf.st_size > MAX_LOGFILE_SZ)
00109         start_log();
00110 
00111     WvLogFileBase::_make_prefix(timenow);
00112 }
00113 
00114 static void trim_old_logs(WvStringParm filename, WvStringParm base,
00115                           int keep_for)
00116 {
00117     if (!keep_for) return;
00118     WvDirIter i(getdirname(filename), false);
00119     for (i.rewind(); i.next(); )
00120     {
00121         // if it begins with the base name
00122         if (!strncmp(i.ptr()->name, base, strlen(base)))
00123         {
00124             // and it's older than 'keep_for' days
00125             if (i.ptr()->st_mtime < wvtime().tv_sec - keep_for*86400)
00126                 ::unlink(i.ptr()->fullname);
00127         }
00128     }
00129 }
00130 
00131 
00132 void WvLogFile::start_log()
00133 {
00134     WvFile::close();
00135 
00136     int num = 0;
00137     struct stat statbuf;
00138     time_t timenow = wvtime().tv_sec;
00139     last_day = (timenow + gmtoffset()) / 86400;
00140     struct tm* tmstamp = localtime(&timenow);
00141     char buf[20];
00142     WvString fullname;
00143     strftime(buf, 20, "%Y-%m-%d", tmstamp);
00144 
00145     // Get the next filename
00146     do
00147         fullname = WvString("%s.%s.%s", filename, buf, num++);
00148     while (stat(fullname, &statbuf) != -1
00149                 && (statbuf.st_size >= MAX_LOGFILE_SZ || !allow_append));
00150 
00151     WvString curname("%s.current", filename);
00152     WvString base = getfilename(filename);
00153 
00154     WvFile::open(fullname, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644);
00155 
00156 #ifndef _WIN32 // no symlinks in win32
00157     // Don't delete the file, unless it's a symlink!
00158     int sym = readlink(curname, buf, 20);
00159     if (sym > 0 || errno == ENOENT)
00160     {
00161         unlink(curname);
00162         symlink(getfilename(fullname), curname);
00163     }
00164 #endif
00165 
00166 #ifndef _WIN32
00167     // We fork here because this can be really slow when the directory has
00168     // (oh, say 32,000 files)
00169     pid_t forky = wvfork();
00170     if (!forky)
00171     {
00172         // ForkTwiceSoTheStupidThingWorksRight
00173         if (!wvfork())
00174         {
00175             // Child will Look for old logs and purge them
00176             trim_old_logs(filename, base, keep_for);
00177             _exit(0);
00178         }
00179         _exit(0);
00180     }
00181     // In case a signal is in the process of being delivered...
00182     pid_t rv;
00183     while ((rv = waitpid(forky, NULL, 0)) != forky)
00184         if (rv == -1 && errno != EINTR)
00185             break;
00186 #else
00187     // just do it in the foreground on Windows
00188     trim_old_logs(filename, base, keep_for);
00189 #endif
00190 }

Generated on Fri Oct 5 18:20:28 2007 for WvStreams by  doxygen 1.5.3