Jelajahi Sumber

Replace fclose+fopen with freopen where possible

z3APA3A 7 tahun lalu
induk
melakukan
50277692f4
4 mengubah file dengan 14 tambahan dan 25 penghapusan
  1. 2 10
      src/3proxy.c
  2. 1 2
      src/common.c
  3. 9 13
      src/conf.c
  4. 2 0
      src/proxy.h

+ 2 - 10
src/3proxy.c

@@ -271,16 +271,8 @@ void cyclestep(void){
 	}
 	if(conf.logname) {
 		if(timechanged(conf.logtime, conf.time, conf.logtype)) {
-			FILE *fp;
-			fp = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.time), "a");
-			if (fp) {
-				pthread_mutex_lock(&log_mutex);
-				fclose(conf.stdlog);
-				conf.stdlog = fp;
-				pthread_mutex_unlock(&log_mutex);
-			}
-			fseek(stdout, 0L, SEEK_END);
-			usleep(SLEEPTIME);
+			if(conf.stdlog) conf.stdlog = freopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.time), "a", conf.stdlog);
+			else conf.stdlog = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.time), "a");
 			conf.logtime = conf.time;
 			if(conf.logtype != NONE && conf.rotate) {
 				int t;

+ 1 - 2
src/common.c

@@ -614,8 +614,7 @@ int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *
 	time(&t);
 	if(!param) return 0;
 	if(param->trafcountfunc)(*param->trafcountfunc)(param);
-	format = (char *)param->srv->logformat;
-	if(!format) format = "G%y%m%d%H%M%S.%. %p %E %U %C:%c %R:%r %O %I %h %T";
+	format = param->srv->logformat?(char *)param->srv->logformat : DEFLOGFORMAT;
 	tm = (*format == 'G' || *format == 'g')?
 		gmtime(&t) : localtime(&t);
 	i = dobuf2(param, buf, s, doublec, tm, format + 1);

+ 9 - 13
src/conf.c

@@ -301,7 +301,6 @@ static int h_log(int argc, unsigned char ** argv){
 
 
 	havelog = 1;
-	conf.logfunc = logstdout;
 	if(argc > 1 && conf.logtarget && *argv[1]!= '&' && *argv[1]!= '@' && !strcmp((char *)conf.logtarget, (char *)argv[1])) {
 		return 0;
 	}
@@ -336,28 +335,23 @@ static int h_log(int argc, unsigned char ** argv){
 		}
 #endif
 		else {
-			FILE *fp;
 			if(argc > 2) {
 				conf.logtype = getrotate(*argv[2]);
 			}
 			conf.logtime = time(0);
 			if(conf.logname)myfree(conf.logname);
 			conf.logname = (unsigned char *)mystrdup((char *)argv[1]);
-			fp = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.logtime), "a");
-			if(!fp){
+			if(conf.stdlog) conf.stdlog = freopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.logtime), "a", conf.stdlog);
+			else conf.stdlog = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.logtime), "a");
+			if(!conf.stdlog){
 				perror((char *)tmpbuf);
 				return 1;
 			}
-			else {
-				if(conf.stdlog)fclose(conf.stdlog);
-				conf.stdlog = fp;
-#ifdef _WINCE
-				freopen(tmpbuf, "w", stdout);
-				freopen(tmpbuf, "w", stderr);
-#endif
-			}
+			conf.logfunc = logstdout;
+
 		}
 	}
+	else conf.logfunc = logstdout;
 	return 0;
 }
 
@@ -464,8 +458,9 @@ static int h_rotate(int argc, unsigned char **argv){
 }
 
 static int h_logformat(int argc, unsigned char **argv){
-	if(conf.logformat) myfree(conf.logformat);
+	unsigned char * old = conf.logformat;
 	conf.logformat = (unsigned char *)mystrdup((char *)argv[1]);
+	if(old) myfree(old);
 	return 0;
 }
 
@@ -1807,6 +1802,7 @@ void freeconf(struct extparam *confp){
  acl = confp->acl;
  confp->acl = NULL;
  confp->logtime = confp->time = 0;
+ confp->logfunc = lognone;
 
  usleep(SLEEPTIME);
 

+ 2 - 0
src/proxy.h

@@ -136,6 +136,8 @@ void daemonize(void);
 
 #define MAXRADIUS 5
 
+#define DEFLOGFORMAT "G%y%m%d%H%M%S.%. %p %E %U %C:%c %R:%r %O %I %h %T"
+
 extern RESOLVFUNC resolvfunc;
 
 extern int wday;