00001
00002
00003
00004
00005
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);
00027 return nowgmt - nowantilocal;
00028 }
00029
00030
00031
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
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(×tr[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
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
00100 struct stat statbuf;
00101
00102
00103 if (fstat(getfd(), &statbuf) == -1)
00104 statbuf.st_size = 0;
00105
00106
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
00122 if (!strncmp(i.ptr()->name, base, strlen(base)))
00123 {
00124
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
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
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
00168
00169 pid_t forky = wvfork();
00170 if (!forky)
00171 {
00172
00173 if (!wvfork())
00174 {
00175
00176 trim_old_logs(filename, base, keep_for);
00177 _exit(0);
00178 }
00179 _exit(0);
00180 }
00181
00182 pid_t rv;
00183 while ((rv = waitpid(forky, NULL, 0)) != forky)
00184 if (rv == -1 && errno != EINTR)
00185 break;
00186 #else
00187
00188 trim_old_logs(filename, base, keep_for);
00189 #endif
00190 }