1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
#include "meta.h"
#include <string.h>
#include <errno.h>
#include "debug.h"
#include "aclogger.h"
#include "acfg.h"
#include "lockable.h"
#include "filereader.h"
#include "fileio.h"
#include <unistd.h>
#include <vector>
#include <deque>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace MYSTD;
namespace aclog
{
ofstream fErr, fStat;
static pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
bool open()
{
// only called in the beginning or when reopening, already locked...
// lockguard g(&mx);
if(acfg::logdir.empty())
return true;
string apath(acfg::logdir+"/apt-cacher.log"), epath(acfg::logdir+"/apt-cacher.err");
if(fErr.is_open())
fErr.close();
if(fStat.is_open())
fStat.close();
fErr.open(epath.c_str(), ios::out | ios::app);
fStat.open(apath.c_str(), ios::out | ios::app);
return fStat.is_open() && fErr.is_open();
}
void transfer(char cLogType, uint64_t nCount, const char *szClient, const char *szPath)
{
lockguard g(&mx);
if(!fStat.is_open())
return;
fStat << time(0) << '|' << cLogType << '|' << nCount;
if(acfg::verboselog)
fStat << '|' << szClient << '|' << szPath;
fStat << '\n';
if(acfg::debug&LOG_FLUSH) fStat.flush();
}
void misc(const string & sLine, const char cLogType)
{
lockguard g(&mx);
if(!fStat.is_open())
return;
fStat << time(0) << '|' << cLogType << '|' << sLine << '\n';
if(acfg::debug&LOG_FLUSH)
fStat.flush();
}
void err(const char *msg, const char *client)
{
lockguard g(&mx);
if(!fErr.is_open())
{
/*#ifdef DEBUG
cerr << msg <<endl;
#endif
*/
return;
}
static char buf[32];
const time_t tm=time(NULL);
ctime_r(&tm, buf);
buf[24]=0;
fErr << buf << '|';
if(client)
fErr << client << ": ";
fErr << msg << '\n';
#ifdef DEBUG
if(acfg::debug & LOG_DEBUG)
cerr << buf << msg <<endl;
#endif
if(acfg::debug & LOG_DEBUG)
fErr.flush();
}
void flush()
{
lockguard g(&mx);
if(fErr.is_open()) fErr.flush();
if(fStat.is_open()) fStat.flush();
}
void close(bool bReopen)
{
lockguard g(&mx);
if(acfg::debug>=LOG_MORE) cerr << (bReopen ? "Reopening logs...\n" : "Closing logs...\n");
fErr.close();
fStat.close();
if(bReopen)
aclog::open();
}
#define DAYSECONDS (3600*24)
#define WEEKSECONDS (DAYSECONDS * 7)
#ifndef MINIBUILD
void GetStats(deque<tRowData> & out)
{
string sDataFile=acfg::cachedir+SZPATHSEP"_stats_dat";
out.clear();
time_t now=time(NULL);
for(int i=0; i<7; i++)
{
tRowData d;
d.to=now - i*DAYSECONDS;
d.from=d.to - DAYSECONDS;
out.push_back(d);
}
//#warning stats cache disabled, restore it later?
#if 0
struct stat statbuf;
if ( 0!= stat(sDataFile.c_str(), &statbuf)
|| statbuf.st_mtime < time(NULL)-86000
)
{ // needs to be created/updated
#endif
tStrDeq logs=ExpandFilePattern(acfg::logdir +
SZPATHSEP "apt-cacher*.log", false);
for (tStrDeq::const_iterator it=logs.begin();it!=logs.end();it++)
{
if(acfg::debug>=LOG_MORE)
cerr << "Reading log file: " << *it <<endl;
filereader reader;
if (!reader.OpenFile(*it))
{
aclog::err("Error opening a log file");
continue;
}
string sLine;
tStrVec tokens;
while(reader.GetOneLine(sLine))
{
// cout << "got line: " << sLine <<endl;
tokens.clear();
if(Tokenize(sLine, "|", tokens)<3)
continue;
// cout << "having: " << tokens[0] << ", " << tokens[1] << ", " << tokens[2]<<endl;
time_t when=strtoul(tokens[0].c_str(),0,10);
if(when > out.front().to || when < out.back().from)
continue;
for(deque<tRowData>::reverse_iterator it=out.rbegin();
it!=out.rend();
it++)
{
if(when < it->from || when > it->to)
continue;
unsigned long dcount=strtoul(tokens[2].c_str(),0,10);
switch(* tokens[1].c_str()) {
case('I'):
it->byteIn+=dcount;
it->reqIn++;
break;
case('O'):
it->byteOut+=dcount;
it->reqOut++;
break;
default:
continue;
}
}
}
}
#if 0
// Normalize data and repack for user
map<time_t,tempCounts>::iterator it;
for(it=mapPeriod2InOut.begin(); it!=mapPeriod2InOut.end(); it++)
{
tRowData data;
data.count=it->second.first+it->second.second;
data.ratioSent=double(it->second.second)/double(data.count);
if(it->first < RANGEMARK)
data.from=data.to= now - it->first * 24 * 3600;
else
{
data.to = now - (it->first-RANGEMARK)*7*24*3600;
data.from=data.to-7*24*3600;
}
out.push_back(data);
}
}
else
{ // deserialize
filereader reader;
string sLine;
tStrVec tokens;
if(!reader.OpenFile(sDataFile))
{
aclog::err("Error opening stats file");
return;
}
while(reader.GetOneLine(sLine) && Tokenize(sLine, SPACECHARS, tokens)==4)
{
tRowData data;
data.from=strtoul(tokens[0].c_str(),0,10);
data.to=strtoul(tokens[1].c_str(),0,10);
data.count=atof(tokens[2].c_str());
data.ratioSent=atof(tokens[3].c_str());
out.push_back(data);
tokens.clear();
}
}
#endif
}
string GetStatReport()
{
string ret;
vector<char> buf(1024);
deque<aclog::tRowData> data;
aclog::GetStats(data);
for (deque<aclog::tRowData>::iterator it = data.begin(); it != data.end(); it++)
{
unsigned long reqMax = max(it->reqIn, it->reqOut);
uint64_t dataMax = max(it->byteIn, it->byteOut);
if (0 == dataMax || 0 == reqMax)
continue;
char tbuf[50];
size_t zlen(0);
ctime_r(&it->from, tbuf);
struct tm *tmp = localtime(&it->from);
if (!tmp)
goto time_error;
zlen = strftime(tbuf, sizeof(tbuf), TIMEFORMAT, tmp);
if (!zlen)
goto time_error;
if (it->from != it->to)
{
tmp = localtime(&it->to);
if (!tmp)
goto time_error;
if (0 == strftime(tbuf + zlen, sizeof(tbuf) - zlen,
" - " TIMEFORMAT, tmp))
goto time_error;
}
snprintf(&buf[0], buf.size(), "<tr bgcolor=\"white\">"
"<td class=\"colcont\">%s</td>"
"<td class=\"coltitle\"><span> </span></td>"
"<td class=\"colcont\">%lu (%2.2f%%)</td>"
"<td class=\"colcont\">%lu (%2.2f%%)</td>"
"<td class=\"colcont\">%lu</td>"
"<td class=\"coltitle\"><span> </span></td>"
"<td class=\"colcont\">%2.2f MiB (%2.2f%%)</td>"
"<td class=\"colcont\">%2.2f MiB (%2.2f%%)</td>"
"<td class=\"colcont\">%2.2f MiB</td>"
"</tr>", tbuf, reqMax - it->reqIn, double(reqMax - it->reqIn)
/ reqMax * 100, // hitcount
it->reqIn, double(it->reqIn) / reqMax * 100, // misscount
reqMax,
double(dataMax - it->byteIn) / 1048576, double(dataMax
- it->byteIn) / dataMax * 100, // hitdata
double(it->byteIn) / 1048576, double(it->byteIn) / dataMax
* 100, // missdata
double(dataMax) / 1048576
); //, int(it->ratioSent*nRatWid), int((1.0-it->ratioSent)*nRatWid));
ret += &buf[0];
continue;
time_error: ret
+= " Invalid time value detected, check the stats database. ";
}
return ret;
}
#endif
}
errnoFmter::errnoFmter(const char *prefix)
{
char buf[32];
buf[0]=buf[31]=0x0;
if(prefix)
assign(prefix);
#ifdef _GNU_SOURCE
//#warning COMPILER BUG DETECTED -- _GNU_SOURCE was preset in C++ mode
append(strerror_r(errno, buf, sizeof(buf)-1));
#else
int err=errno;
if(strerror_r(err, buf, sizeof(buf)-1))
append(tSS() << "UNKNOWN ERROR: " << err);
else
append(buf);
#endif
}
#ifdef DEBUG
static lockable mx_dbgStackMark;
MYMAP<pthread_t, int> stackDepths;
t_logger::t_logger(const char *szFuncName, const void * ptr)
{
m_id = pthread_self();
m_szName = szFuncName;
callobj = uintptr_t(ptr);
{
lockguard __lockguard(&mx_dbgStackMark);
m_nLevel = stackDepths[m_id]++;
}
// writing to the level of parent since it's being "created there"
GetFmter() << ">> " << szFuncName << " ["<<m_id<<" | "<<callobj <<"]";
Write();
m_nLevel++;
}
t_logger::~t_logger()
{
m_nLevel--;
GetFmter() << "<< " << m_szName << " ["<<m_id<<" | "<<callobj <<"]";
Write();
lockguard __lockguard(&mx_dbgStackMark);
stackDepths[m_id]--;
if(0 == stackDepths[m_id])
stackDepths.erase(m_id);
}
tSS & t_logger::GetFmter()
{
m_strm.clear();
for(UINT i=0;i<m_nLevel;i++)
m_strm << "\t";
m_strm<< " - ";
return m_strm;
}
void t_logger::Write(const char *pFile, unsigned int nLine)
{
if(pFile)
{
const char *p=strrchr(pFile, CPATHSEP);
pFile=p?(p+1):pFile;
m_strm << " [" << m_id << "@" << pFile << ":" << nLine <<"|"<<callobj <<"]";
}
aclog::err(m_strm.c_str());
}
#endif
|