File: aclogger.cc

package info (click to toggle)
apt-cacher-ng 3.3.1-2~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 2,040 kB
  • sloc: cpp: 17,564; sh: 553; ansic: 401; perl: 377; makefile: 126
file content (491 lines) | stat: -rw-r--r-- 10,685 bytes parent folder | download | duplicates (2)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491

#include "meta.h"

#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <dirent.h>

#include "debug.h"
#include "aclogger.h"
#include "acfg.h"
#include "lockable.h"
#include "filereader.h"
#include "fileio.h"

#include <vector>
#include <deque>
#include <iostream>
#include <fstream>
#include <atomic>

using namespace std;

namespace acng
{
namespace log
{

ofstream fErr, fStat;
static acmutex mx;

#ifndef DEBUG
bool logIsEnabled = false;
#else
bool logIsEnabled = true;
#endif

std::atomic<off_t> totalIn(0), totalOut(0);

std::pair<off_t,off_t> GetCurrentCountersInOut()
		{
	return std::make_pair(totalIn.load(), totalOut.load());
		}

std::pair<off_t,off_t> oldCounters(0,0);

void ResetOldCounters()
{
	char lbuf[600];
	// safe some cycles an snprintf
	auto xl = (CACHE_BASE.size() + cfg::privStoreRelQstatsSfx.size());
	if (xl > 550)
		return; // heh?
	memcpy(lbuf, CACHE_BASE.data(), CACHE_BASE.size());
	memcpy(lbuf + CACHE_BASE.size(), cfg::privStoreRelQstatsSfx.data(),
			cfg::privStoreRelQstatsSfx.size());

	for (char foldNam :
	{ 'i', 'o' })
	{
		lbuf[xl] = 0;
		auto xoff = sprintf(lbuf + xl, "/%c/", foldNam);
		auto lptr = lbuf + xl + xoff;
		auto dirp = opendir(lbuf);
		if (!dirp)
			continue;
		while (true)
		{
			auto ent = readdir(dirp);
			if (!ent)
				break;
			auto llen = strlen(ent->d_name);
			if (llen > 25 || llen < 4)
				continue;
			memcpy(lptr, ent->d_name, llen + 1);
			unlink(lbuf);
		}
		closedir(dirp);
	}
	oldCounters = decltype(oldCounters)();
}

decltype(oldCounters) GetOldCountersInOut(bool calcIncomming, bool calcOutgoing)
{
#ifndef MINIBUILD
	// needs to do first reading of old stats?
	char lbuf[600];
	// safe some cycles an snprintf
	auto xl=(CACHE_BASE.size() + cfg::privStoreRelQstatsSfx.size());
	if(xl > 550)
		return oldCounters; // heh?
	memcpy(lbuf, CACHE_BASE.data(), CACHE_BASE.size());
	memcpy(lbuf+CACHE_BASE.size(), cfg::privStoreRelQstatsSfx.data(), cfg::privStoreRelQstatsSfx.size());
	if (!oldCounters.first && !oldCounters.second)
	{
		auto rfunc = [&lbuf, xl](off_t& pRet, char foldNam)
		{
			lbuf[xl]=0;
			auto xoff=sprintf(lbuf+xl, "/%c/", foldNam);
			auto lptr = lbuf+xl+xoff;
			auto dirp = opendir(lbuf);
			if(!dirp)
				return;
			char buf[30];
			while(true)
			{
				auto ent = readdir(dirp);
				if(!ent) break;
				auto llen=strlen(ent->d_name);
				if(llen > 25 || llen<4) continue;
				memcpy(lptr, ent->d_name, llen+1);
				auto tpos=readlink(lbuf, buf, _countof(buf)-1);
				if(tpos < 1) continue;
				buf[tpos]=0;
				pRet += acng::strsizeToOfft(buf);
			}
			closedir(dirp);
		};
		if(calcIncomming)
			rfunc(oldCounters.first, 'i');
		if(calcOutgoing)
			rfunc(oldCounters.second, 'o');
	}
#endif
	return oldCounters;
}

bool open()
{
	// only called in the beginning or when reopening, already locked...
	// lockguard g(&mx);

	if(cfg::logdir.empty())
		return true;
	
	logIsEnabled = true;

	string apath(cfg::logdir+"/apt-cacher.log"), epath(cfg::logdir+"/apt-cacher.err");
	
	mkbasedir(apath);

	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(uint64_t bytesIn,
		uint64_t bytesOut,
		cmstring& sClient,
		cmstring& sPath,
		bool bAsError)
{
	totalIn.fetch_add(bytesIn);
	totalOut.fetch_add(bytesOut);

	if(!logIsEnabled)
		return;

	lockguard g(&mx);

	if(!fStat.is_open())
		return;
	auto tNow=GetTime();
	if (bytesIn)
	{
		fStat << tNow << "|I|" << bytesIn;
		if (cfg::verboselog)
			fStat << '|' << sClient << '|' << sPath;
		fStat << '\n'; // not endl, it might flush
	}
	if (bytesOut)
	{
		fStat << tNow << (bAsError ? "|E|" : "|O|") << bytesOut;
		if (cfg::verboselog)
			fStat << '|' << sClient << '|' << sPath;
		fStat << '\n'; // not endl, it might flush
	}

	if(cfg::debug & LOG_FLUSH) fStat.flush();
}

void misc(const string & sLine, const char cLogType)
{
	if(!logIsEnabled)
		return;

	lockguard g(&mx);
	if(!fStat.is_open())
		return;

	fStat << time(0) << '|' << cLogType << '|' << sLine << '\n';
	
	if(cfg::debug & LOG_FLUSH)
		fStat.flush();
}

void ACNG_API err(const char *msg, const char *client)
{
	if(!logIsEnabled)
		return;

	lockguard g(&mx);

	if(!fErr.is_open())
	{
#ifdef DEBUG // basic debugging of acngtool
		cerr << msg <<endl;
#endif
		return;
	}
	
	static char buf[32];
	const time_t tm=time(nullptr);
	ctime_r(&tm, buf);
	buf[24]=0;
	fErr << buf << '|';
	if(client)
		fErr << client << ": ";
	fErr << msg << '\n';

#ifdef DEBUG
	if(cfg::debug & log::LOG_DEBUG)
		cerr << buf << msg <<endl;
#endif

	if(cfg::debug & log::LOG_DEBUG)
		fErr.flush();
}

void ACNG_API flush()
{
	if(!logIsEnabled)
		return;

	lockguard g(mx);
	if(fErr.is_open()) fErr.flush();
	if(fStat.is_open()) fStat.flush();
}

void close(bool bReopen)
{
	// let's try to store a snapshot of the current stats
	auto snapIn = offttos(totalIn.exchange(0));
	auto snapOut = offttos(totalOut.exchange(0));
	timeval tp;
	gettimeofday(&tp, 0);
	auto inLinkPath = CACHE_BASE + cfg::privStoreRelQstatsSfx + "/i/"
			+ acng::offttos(tp.tv_sec) + "." + acng::ltos(tp.tv_usec);
	auto outLinkPath = CACHE_BASE + cfg::privStoreRelQstatsSfx + "/o/"
			+ acng::offttos(tp.tv_sec) + "." + acng::ltos(tp.tv_usec);
	ignore_value(symlink(snapIn.c_str(), inLinkPath.c_str()));
	ignore_value(symlink(snapOut.c_str(), outLinkPath.c_str()));


	if(!logIsEnabled)
		return;

	lockguard g(mx);
	if(cfg::debug >= LOG_MORE) cerr << (bReopen ? "Reopening logs...\n" : "Closing logs...\n");
	fErr.close();
	fStat.close();
	if(bReopen)
		log::open();
}


#define DAYSECONDS (3600*24)
#define WEEKSECONDS (DAYSECONDS * 7)

#ifndef MINIBUILD
inline deque<tRowData> GetStats()
{
	string sDataFile=cfg::cachedir+SZPATHSEP"_stats_dat";
	deque<tRowData> out;
	
	time_t now = time(nullptr);

	for (int i = 0; i < 7; i++)
	{
		tRowData d;
		d.to = now - i * DAYSECONDS;
		d.from = d.to - DAYSECONDS;
		out.emplace_back(d);
	}

	for (auto& log : ExpandFilePattern(cfg::logdir + SZPATHSEP "apt-cacher*.log", false))
	{
		if (cfg::debug >= LOG_MORE)
			cerr << "Reading log file: " << log << endl;
		filereader reader;
		if (!reader.OpenFile(log))
		{
			log::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 (auto 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;
				}
			}
		}
	}
	return out;
}


string GetStatReport()
{
	string ret;
	vector<char> buf(1024);
	for (auto& entry : log::GetStats())
	{
		auto reqMax = std::max(entry.reqIn, entry.reqOut);
		auto dataMax = std::max(entry.byteIn, entry.byteOut);

		if (0 == dataMax || 0 == reqMax)
			continue;

		char tbuf[50];
		size_t zlen(0);
		ctime_r(&entry.from, tbuf);
		struct tm *tmp = localtime(&entry.from);
		if (!tmp)
			goto time_error;
		zlen = strftime(tbuf, sizeof(tbuf), TIMEFORMAT, tmp);
		if (!zlen)
			goto time_error;

		if (entry.from != entry.to)
		{
			tmp = localtime(&entry.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>&nbsp;</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>&nbsp;</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 - entry.reqIn, double(reqMax - entry.reqIn)
				/ reqMax * 100, // hitcount
				entry.reqIn, double(entry.reqIn) / reqMax * 100, // misscount
				reqMax,

				double(dataMax - entry.byteIn) / 1048576, double(dataMax
						- entry.byteIn) / dataMax * 100, // hitdata
				double(entry.byteIn) / 1048576, double(entry.byteIn) / dataMax
						* 100, // missdata
				double(dataMax) / 1048576

		); //, int(entry.ratioSent*nRatWid), int((1.0-entry.ratioSent)*nRatWid));
		ret += &buf[0];
		continue;
		time_error: ret
				+= " Invalid time value detected, check the stats database. ";
	}
	return ret;
}
#endif
}

// let the compiler decide between GNU and XSI version
inline void add_msg(int r, int err, const char* buf, mstring *p)
{
	if(r)
		p->append(tSS() << "UNKNOWN ERROR: " << err);
	else
		p->append(buf);
}

inline void add_msg(const char *msg, int , const char* , mstring *p)
{
	p->append(msg);
}

tErrnoFmter::tErrnoFmter(const char *prefix)
{
	int err=errno;
	char buf[64];
	buf[0]=buf[sizeof(buf)-1]=0x0;
	if(prefix)
		assign(prefix);
	add_msg(strerror_r(err, buf, sizeof(buf)-1), err, buf, this);
}

#ifdef DEBUG

static struct : public base_with_mutex, public std::map<pthread_t, int>
{} indentPerThread;

t_logger::t_logger(const char *szFuncName,  const void * ptr)
{
	m_id = pthread_self();
	m_szName = szFuncName;
	callobj = uintptr_t(ptr);
	{
		lockguard __lockguard(indentPerThread);
		m_nLevel = indentPerThread[m_id]++;
	}
	// writing to the level of parent since it's being "created there"
	GetFmter() << ">> " << szFuncName << " [T:"<<m_id<<" P:0x"<< tSS::hex<< callobj << tSS::dec <<"]";
	Write();
	m_nLevel++;
}

t_logger::~t_logger()
{
	m_nLevel--;
	GetFmter() << "<< " << m_szName << " [T:"<<m_id<<" P:0x"<< tSS::hex<< callobj << tSS::dec <<"]";
	Write();
	lockguard __lockguard(indentPerThread);
	indentPerThread[m_id]--;
	if(0 == indentPerThread[m_id])
		indentPerThread.erase(m_id);
}

tSS & t_logger::GetFmter()
{
	m_strm.clear();
	for(unsigned 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 << " [T:" << m_id << " S:" << pFile << ":" << tSS::dec << nLine
				<<" P:0x"<< tSS::hex<< callobj << tSS::dec <<"]";
	}
	log::err(m_strm.c_str());
}

#endif

}