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()
00076 {
00077 time_t timenow = wvtime().tv_sec;
00078 struct tm* tmstamp = localtime(&timenow);
00079 char timestr[30];
00080 strftime(×tr[0], 30, TIME_FORMAT, tmstamp);
00081
00082 prefix = WvString("%s: %s<%s>: ", timestr, last_source,
00083 loglevels[last_level]);
00084 prelen = prefix.len();
00085 }
00086
00087
00088
00089 WvLogFile::WvLogFile(WvStringParm _filename, WvLog::LogLevel _max_level,
00090 int _keep_for, bool _force_new_line, bool _allow_append)
00091 : WvLogFileBase(_max_level), keep_for(_keep_for), filename(_filename),
00092 allow_append(_allow_append)
00093 {
00094 WvLogRcv::force_new_line = _force_new_line;
00095 start_log();
00096 }
00097
00098 void WvLogFile::_make_prefix()
00099 {
00100 time_t timenow = wvtime().tv_sec;
00101
00102 struct stat statbuf;
00103
00104
00105 if (fstat(getfd(), &statbuf) == -1)
00106 statbuf.st_size = 0;
00107
00108
00109 if (last_day != ((timenow + gmtoffset())/86400)
00110 || statbuf.st_size > MAX_LOGFILE_SZ)
00111 start_log();
00112
00113 WvLogFileBase::_make_prefix();
00114 }
00115
00116 static void trim_old_logs(WvStringParm filename, WvStringParm base,
00117 int keep_for)
00118 {
00119 if (!keep_for) return;
00120 WvDirIter i(getdirname(filename), false);
00121 for (i.rewind(); i.next(); )
00122 {
00123
00124 if (!strncmp(i.ptr()->name, base, strlen(base)))
00125 {
00126
00127 if (i.ptr()->st_mtime < wvtime().tv_sec - keep_for*86400)
00128 ::unlink(i.ptr()->fullname);
00129 }
00130 }
00131 }
00132
00133
00134 void WvLogFile::start_log()
00135 {
00136 WvFile::close();
00137
00138 int num = 0;
00139 struct stat statbuf;
00140 time_t timenow = wvtime().tv_sec;
00141 last_day = (timenow + gmtoffset()) / 86400;
00142 struct tm* tmstamp = localtime(&timenow);
00143 char buf[20];
00144 WvString fullname;
00145 strftime(buf, 20, "%Y-%m-%d", tmstamp);
00146
00147
00148 do
00149 fullname = WvString("%s.%s.%s", filename, buf, num++);
00150 while (stat(fullname, &statbuf) != -1
00151 && (statbuf.st_size >= MAX_LOGFILE_SZ || !allow_append));
00152
00153 WvString curname("%s.current", filename);
00154 WvString base = getfilename(filename);
00155
00156 WvFile::open(fullname, O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0644);
00157
00158 #ifndef _WIN32 // no symlinks in win32
00159
00160 int sym = readlink(curname, buf, 20);
00161 if (sym > 0 || errno == ENOENT)
00162 {
00163 unlink(curname);
00164 symlink(getfilename(fullname), curname);
00165 }
00166 #endif
00167
00168 #ifndef _WIN32
00169
00170
00171 pid_t forky = wvfork();
00172 if (!forky)
00173 {
00174
00175 if (!wvfork())
00176 {
00177
00178 trim_old_logs(filename, base, keep_for);
00179 _exit(0);
00180 }
00181 _exit(0);
00182 }
00183
00184 pid_t rv;
00185 while ((rv = waitpid(forky, NULL, 0)) != forky)
00186 if (rv == -1 && errno != EINTR)
00187 break;
00188 #else
00189
00190 trim_old_logs(filename, base, keep_for);
00191 #endif
00192 }