Logs: a logger for each log string, shared and closed once unused

A logger (struct logger) is identified by the string log or -l gives it:
"" stdout, "/dev/null" none, "@IDENT" syslog, "&DSN" ODBC, "radius",
anything else a file. Each type (struct logtype) has init, formatting of
a connection into a buffer, writing, close, and a periodic tick for
rotation and flushing; ODBC and RADIUS register their types from 3proxy,
which alone has them. Services and configurations hold the logger they
use; the last to let go closes it, except the standard ones (none,
stdout, stderr, syslog), which are opened at startup and kept.

-l takes every log type now, and a file named by several services or by
both log and -l is opened once. rotate and archiver are bound to the
logger of the last log command when its configuration comes into force,
and every file logger is rotated by the main loop.

Errors with no connection go to a new stderr logger; after daemon, or as
a Windows service, stdout logging writes nothing and errors go to the
last log configured. A log whose writes keep failing is closed and
reopened; while it cannot be, its lines go to stderr and it is retried
every minute. ODBC's own reconnection throttle gives way to this.

Formatting (logbuf) is separate from counting traffic and clearing the
statistics, done once by logdo(), the services' log function, so a later
step can format or copy a connection's line and write it from a thread
of its own. dologname() and timechanged() move to log.c, which the
standalone services link as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-23 11:44:55 +03:00
1 parent 57818ad497
commit d613750b94
14 files changed
+996 -415

No files matched your search

+16 -94
View File
@@ -101,11 +101,7 @@ void __stdcall ServiceMain(int argc, unsigned char* argv[] )
SetStatus( SERVICE_START_PENDING, 0, 1 );
SetStatus( SERVICE_RUNNING, 0, 0 );
cyclestep();
#ifdef WITH_ODBC
_3proxy_mutex_lock(&log_mutex);
close_sql();
_3proxy_mutex_unlock(&log_mutex);
#endif
logfinish();
/* the last thing: the process may be ended once this is reported */
SetStatus( SERVICE_STOPPED, 0, 0 );
}
@@ -145,42 +141,6 @@ struct schedule *schedule;
int wday = 0;
int timechanged (time_t oldtime, time_t newtime, ROTATION lt){
struct tm tmold;
struct tm *tm;
tm = localtime(&oldtime);
if(!tm) return 0;
tmold = *tm;
tm = localtime(&newtime);
if(!tm) return 0;
switch(lt){
case MINUTELY:
if(tm->tm_min != tmold.tm_min)return 1;
break;
case HOURLY:
if(tm->tm_hour != tmold.tm_hour)return 1;
break;
case DAILY:
if(tm->tm_yday != tmold.tm_yday)return 1;
break;
case MONTHLY:
if(tm->tm_mon != tmold.tm_mon)return 1;
break;
case ANNUALLY:
if(tm->tm_year != tmold.tm_year)return 1;
break;
case WEEKLY:
if(((newtime - oldtime) > (60*60*24*7))
|| tm->tm_wday < tmold.tm_wday
|| (tm->tm_wday == tmold.tm_wday && (newtime - oldtime) > (60*60*24*6))
)return 1;
break;
default:
break;
}
return 0;
}
void doschedule(void){
struct schedule *sched, *prevsched = NULL, *nextsched;
int res;
@@ -280,7 +240,6 @@ static void waitservices(void){
void cyclestep(void){
struct tm *tm;
time_t minutecounter;
unsigned char tmpbuf[8192];
minutecounter = time(0);
for(;;){
@@ -294,7 +253,6 @@ void cyclestep(void){
}
doschedule();
sysdnscheck();
if(conf->stdlog)fflush(conf->stdlog);
if(timechanged(minutecounter, curtime, MINUTELY)) {
struct filemon *fm;
struct stat sb;
@@ -317,55 +275,7 @@ void cyclestep(void){
basetime = mktime(tm);
}
}
if(conf->logname) {
if(timechanged(conf->logtime, curtime, conf->logtype)) {
if(conf->stdlog) conf->stdlog = freopen((char *)dologname (tmpbuf, conf->logname, NULL, conf->logtype, curtime), "a", conf->stdlog);
else conf->stdlog = fopen((char *)dologname (tmpbuf, conf->logname, NULL, conf->logtype, curtime), "a");
conf->logtime = curtime;
if(conf->logtype != NONE && conf->rotate) {
int t;
t = 1;
switch(conf->logtype){
case ANNUALLY:
t = t * 12;
case MONTHLY:
t = t * 4;
case WEEKLY:
t = t * 7;
case DAILY:
t = t * 24;
case HOURLY:
t = t * 60;
case MINUTELY:
t = t * 60;
default:
break;
}
dologname (tmpbuf, conf->logname, (conf->archiver)?conf->archiver[1]:NULL, conf->logtype, (conf->logtime - (time_t)t * conf->rotate));
remove ((char *) tmpbuf);
if(conf->archiver) {
int i;
*tmpbuf = 0;
for(i = 2; i < conf->archiverc && strlen((char *)tmpbuf) < 512; i++){
strcat((char *)tmpbuf, " ");
if(!strcmp((char *)conf->archiver[i], "%A")){
strcat((char *)tmpbuf, "\"");
dologname (tmpbuf + strlen((char *)tmpbuf), conf->logname, conf->archiver[1], conf->logtype, (conf->logtime - t));
strcat((char *)tmpbuf, "\"");
}
else if(!strcmp((char *)conf->archiver[i], "%F")){
strcat((char *)tmpbuf, "\"");
dologname (tmpbuf+strlen((char *)tmpbuf), conf->logname, NULL, conf->logtype, (conf->logtime-t));
strcat((char *)tmpbuf, "\"");
}
else
strcat((char *)tmpbuf, (char *)conf->archiver[i]);
}
if(system((char *)tmpbuf+1)){}
}
}
}
}
logtick(curtime);
if(conf->counterd >= 0 && conf->trafcounter) {
if(timechanged(cheader.updated, curtime, MINUTELY)){
dumpcounters(conf->trafcounter, conf->counterd);
@@ -555,8 +465,18 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int
#ifdef __linux__
nsopen(); /* before a chroot in the configuration takes /proc away */
#endif
loginit(); /* standard output and error, syslog, before a chroot too */
#if defined _WIN32 && !defined _WINCE
if(service) logstdclose();
#endif
sysdnsinit(); /* the system's name servers and hosts, before a chroot too */
#ifdef WITH_ODBC
logtypeadd(&logtype_odbc);
#endif
#ifndef NORADIUS
logtypeadd(&logtype_radius);
#endif
_3proxy_mutex_init(&config_mutex);
_3proxy_mutex_init(&confref_mutex);
_3proxy_mutex_init(&listener_mutex);
@@ -566,7 +486,6 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int
_3proxy_mutex_init(&bandlim_mutex);
_3proxy_mutex_init(&connlim_mutex);
_3proxy_mutex_init(&tc_mutex);
_3proxy_mutex_init(&log_mutex);
_3proxy_mutex_init(&tpool.mutex);
#ifndef NORADIUS
_3proxy_mutex_init(&rad_mutex);
@@ -594,7 +513,10 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int
/* numbered before it is read, as a reload numbers the new one */
conf->version++;
res = readconfig(fp);
if(!res) confdnscommit();
if(!res) {
confdnscommit();
logcommit(conf);
}
resolvmode = conf->resolvmode;
/* readconfig() names the failing line, which an exit status would cut