Unify logging (make it always blocking in exchange for memory)

This commit is contained in:
z3APA3A 2015-12-27 19:27:17 +03:00
parent 0487084161
commit 7df2461a26
5 changed files with 28 additions and 36 deletions

View File

@ -19,7 +19,6 @@
#endif #endif
FILE * confopen(); FILE * confopen();
extern unsigned char tmpbuf[1024];
extern unsigned char *strings[]; extern unsigned char *strings[];
extern FILE *writable; extern FILE *writable;
extern struct counter_header cheader; extern struct counter_header cheader;
@ -65,9 +64,9 @@ void __stdcall CommandHandler( DWORD dwCommand )
Sleep(2000); Sleep(2000);
SetStatus( SERVICE_STOPPED, 0, 0 ); SetStatus( SERVICE_STOPPED, 0, 0 );
#ifndef NOODBC #ifndef NOODBC
pthread_mutex_lock(&odbc_mutex); pthread_mutex_lock(&log_mutex);
close_sql(); close_sql();
pthread_mutex_unlock(&odbc_mutex); pthread_mutex_unlock(&log_mutex);
#endif #endif
break; break;
case SERVICE_CONTROL_PAUSE: case SERVICE_CONTROL_PAUSE:
@ -120,9 +119,9 @@ void mysigterm (int sig){
usleep(999*SLEEPTIME); usleep(999*SLEEPTIME);
usleep(999*SLEEPTIME); usleep(999*SLEEPTIME);
#ifndef NOODBC #ifndef NOODBC
pthread_mutex_lock(&odbc_mutex); pthread_mutex_lock(&log_mutex);
close_sql(); close_sql();
pthread_mutex_unlock(&odbc_mutex); pthread_mutex_unlock(&log_mutex);
#endif #endif
conf.timetoexit = 1; conf.timetoexit = 1;
} }
@ -519,9 +518,6 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int
pthread_mutex_init(&hash_mutex, NULL); pthread_mutex_init(&hash_mutex, NULL);
pthread_mutex_init(&tc_mutex, NULL); pthread_mutex_init(&tc_mutex, NULL);
pthread_mutex_init(&pwl_mutex, NULL); pthread_mutex_init(&pwl_mutex, NULL);
#ifndef NOODBC
pthread_mutex_init(&odbc_mutex, NULL);
#endif
freeconf(&conf); freeconf(&conf);
res = readconfig(fp); res = readconfig(fp);

View File

@ -1296,46 +1296,45 @@ void sqlerr (char *buf){
fprintf(conf.stdlog, "%s\n", buf); fprintf(conf.stdlog, "%s\n", buf);
fflush(conf.stdlog); fflush(conf.stdlog);
} }
pthread_mutex_unlock(&odbc_mutex); pthread_mutex_unlock(&log_mutex);
} }
void logsql(struct clientparam * param, const unsigned char *s) { void logsql(struct clientparam * param, const unsigned char *s) {
unsigned char buf[4096];
SQLRETURN ret; SQLRETURN ret;
int len; int len;
len = dobuf(param, buf, s, "\'");
if(param->nolog) return; if(param->nolog) return;
pthread_mutex_lock(&odbc_mutex); pthread_mutex_lock(&log_mutex);
len = dobuf(param, tmpbuf, s, "\'");
if(attempt > 5){ if(attempt > 5){
time_t t; time_t t;
t = time(0); t = time(0);
if (t - attempt_time < 180){ if (t - attempt_time < 180){
sqlerr(buf); sqlerr(tmpbuf);
return; return;
} }
} }
if(!hstmt){ if(!hstmt){
if(!init_sql(sqlstring)) { if(!init_sql(sqlstring)) {
sqlerr(buf); sqlerr(tmpbuf);
return; return;
} }
} }
if(hstmt){ if(hstmt){
ret = SQLExecDirect(hstmt, (SQLCHAR *)buf, (SQLINTEGER)len); ret = SQLExecDirect(hstmt, (SQLCHAR *)tmpbuf, (SQLINTEGER)len);
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){ if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
close_sql(); close_sql();
if(!init_sql(sqlstring)){ if(!init_sql(sqlstring)){
sqlerr(buf); sqlerr(tmpbuf);
return; return;
} }
if(hstmt) { if(hstmt) {
ret = SQLExecDirect(hstmt, (SQLCHAR *)buf, (SQLINTEGER)len); ret = SQLExecDirect(hstmt, (SQLCHAR *)tmpbuf, (SQLINTEGER)len);
if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){ if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
sqlerr(buf); sqlerr(tmpbuf);
return; return;
} }
attempt = 0; attempt = 0;
@ -1343,7 +1342,7 @@ void logsql(struct clientparam * param, const unsigned char *s) {
} }
attempt = 0; attempt = 0;
} }
pthread_mutex_unlock(&odbc_mutex); pthread_mutex_unlock(&log_mutex);
} }
#endif #endif

View File

@ -603,9 +603,9 @@ void lognone(struct clientparam * param, const unsigned char *s) {
} }
pthread_mutex_t log_mutex; pthread_mutex_t log_mutex;
int logmutexinit = 0; int logmutexinit = 0;
unsigned char tmpbuf[4096];
void logstdout(struct clientparam * param, const unsigned char *s) { void logstdout(struct clientparam * param, const unsigned char *s) {
unsigned char buf[4096];
FILE *log; FILE *log;
if(!logmutexinit){ if(!logmutexinit){
@ -614,8 +614,8 @@ void logstdout(struct clientparam * param, const unsigned char *s) {
} }
pthread_mutex_lock(&log_mutex); pthread_mutex_lock(&log_mutex);
log = param->srv->stdlog?param->srv->stdlog:conf.stdlog?conf.stdlog:stdout; log = param->srv->stdlog?param->srv->stdlog:conf.stdlog?conf.stdlog:stdout;
dobuf(param, buf, s, NULL); dobuf(param, tmpbuf, s, NULL);
if(!param->nolog)if(fprintf(log, "%s\n", buf) < 0) { if(!param->nolog)if(fprintf(log, "%s\n", tmpbuf) < 0) {
perror("printf()"); perror("printf()");
}; };
if(log != conf.stdlog)fflush(log); if(log != conf.stdlog)fflush(log);
@ -623,10 +623,15 @@ void logstdout(struct clientparam * param, const unsigned char *s) {
} }
#ifndef _WIN32 #ifndef _WIN32
void logsyslog(struct clientparam * param, const unsigned char *s) { void logsyslog(struct clientparam * param, const unsigned char *s) {
unsigned char buf[4096];
dobuf(param, buf, s, NULL); if(!logmutexinit){
if(!param->nolog)syslog(LOG_INFO, "%s", buf); pthread_mutex_init(&log_mutex, NULL);
logmutexinit = 1;
}
pthread_mutex_lock(&log_mutex);
dobuf(param, tmpbuf, s, NULL)
if(!param->nolog)syslog(LOG_INFO, "%s", tmpbuf);
pthread_mutex_unlock(&log_mutex);
} }
#endif #endif

View File

@ -16,14 +16,9 @@ pthread_mutex_t pwl_mutex;
pthread_mutex_t hash_mutex; pthread_mutex_t hash_mutex;
pthread_mutex_t config_mutex; pthread_mutex_t config_mutex;
#ifndef NOODBC
pthread_mutex_t odbc_mutex;
#endif
int haveerror = 0; int haveerror = 0;
int linenum = 0; int linenum = 0;
unsigned char tmpbuf[1024];
FILE *writable; FILE *writable;
struct counter_header cheader = {"3CF", (time_t)0}; struct counter_header cheader = {"3CF", (time_t)0};
struct counter_record crecord; struct counter_record crecord;
@ -302,10 +297,10 @@ static int h_log(int argc, unsigned char ** argv){
} }
#ifndef NOODBC #ifndef NOODBC
else if(*argv[1]=='&'){ else if(*argv[1]=='&'){
pthread_mutex_lock(&odbc_mutex); pthread_mutex_lock(&log_mutex);
close_sql(); close_sql();
init_sql((char *)argv[1]+1); init_sql((char *)argv[1]+1);
pthread_mutex_unlock(&odbc_mutex); pthread_mutex_unlock(&log_mutex);
conf.logfunc = logsql; conf.logfunc = logsql;
} }
#endif #endif

View File

@ -309,7 +309,7 @@ struct datatype;
struct dictionary; struct dictionary;
struct node; struct node;
struct property; struct property;
extern unsigned char tmpbuf[4096];
extern pthread_mutex_t config_mutex; extern pthread_mutex_t config_mutex;
extern pthread_mutex_t bandlim_mutex; extern pthread_mutex_t bandlim_mutex;
extern pthread_mutex_t hash_mutex; extern pthread_mutex_t hash_mutex;
@ -317,9 +317,6 @@ extern pthread_mutex_t tc_mutex;
extern pthread_mutex_t pwl_mutex; extern pthread_mutex_t pwl_mutex;
extern pthread_mutex_t log_mutex; extern pthread_mutex_t log_mutex;
extern int logmutexinit; extern int logmutexinit;
#ifndef NOODBC
extern pthread_mutex_t odbc_mutex;
#endif
extern struct datatype datatypes[64]; extern struct datatype datatypes[64];