Commit 500ed85bb3217c47eb86c3617abf037c4c4a5eec

Con Kolivas 2012-07-29T03:46:49

Merge pull request #271 from luke-jr/cg_logfixes Logging bugfixes

diff --git a/adl.c b/adl.c
index 8573e16..85ec0aa 100644
--- a/adl.c
+++ b/adl.c
@@ -33,6 +33,10 @@
 #endif
 #include "adl_functions.h"
 
+#ifndef HAVE_CURSES
+#define wlogprint(...)  applog(LOG_WARNING, __VA_ARGS__)
+#endif
+
 bool adl_active;
 bool opt_reorder = false;
 
@@ -764,6 +768,7 @@ bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vdd
 	return true;
 }
 
+#ifdef HAVE_CURSES
 static void get_enginerange(int gpu, int *imin, int *imax)
 {
 	struct gpu_adl *ga;
@@ -776,6 +781,7 @@ static void get_enginerange(int gpu, int *imin, int *imax)
 	*imin = ga->lpOdParameters.sEngineClock.iMin / 100;
 	*imax = ga->lpOdParameters.sEngineClock.iMax / 100;
 }
+#endif
 
 int set_engineclock(int gpu, int iEngineClock)
 {
@@ -824,6 +830,7 @@ out:
 	return ret;
 }
 
+#ifdef HAVE_CURSES
 static void get_memoryrange(int gpu, int *imin, int *imax)
 {
 	struct gpu_adl *ga;
@@ -836,6 +843,7 @@ static void get_memoryrange(int gpu, int *imin, int *imax)
 	*imin = ga->lpOdParameters.sMemoryClock.iMin / 100;
 	*imax = ga->lpOdParameters.sMemoryClock.iMax / 100;
 }
+#endif
 
 int set_memoryclock(int gpu, int iMemoryClock)
 {
@@ -876,6 +884,7 @@ out:
 	return ret;
 }
 
+#ifdef HAVE_CURSES
 static void get_vddcrange(int gpu, float *imin, float *imax)
 {
 	struct gpu_adl *ga;
@@ -889,7 +898,6 @@ static void get_vddcrange(int gpu, float *imin, float *imax)
 	*imax = (float)ga->lpOdParameters.sVddc.iMax / 1000;
 }
 
-#ifdef HAVE_CURSES
 static float curses_float(const char *query)
 {
 	float ret;
@@ -997,6 +1005,7 @@ int set_fanspeed(int gpu, int iFanSpeed)
 	return ret;
 }
 
+#ifdef HAVE_CURSES
 static int set_powertune(int gpu, int iPercentage)
 {
 	struct gpu_adl *ga;
@@ -1018,6 +1027,7 @@ static int set_powertune(int gpu, int iPercentage)
 	unlock_adl();
 	return ret;
 }
+#endif
 
 /* Returns whether the fanspeed is optimal already or not. The fan_window bool
  * tells us whether the current fanspeed is in the target range for fanspeeds.
diff --git a/cgminer.c b/cgminer.c
index 36bdf22..edfe4e3 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -87,7 +87,7 @@ static bool opt_benchmark;
 static bool have_longpoll;
 static bool want_per_device_stats;
 bool use_syslog;
-static bool opt_quiet;
+bool opt_quiet;
 static bool opt_realquiet;
 bool opt_loginput;
 const int opt_cutofftemp = 95;
@@ -167,9 +167,7 @@ static int total_threads;
 static pthread_mutex_t hash_lock;
 static pthread_mutex_t qd_lock;
 static pthread_mutex_t *stgd_lock;
-#ifdef HAVE_CURSES
-static pthread_mutex_t curses_lock;
-#endif
+pthread_mutex_t console_lock;
 static pthread_mutex_t ch_lock;
 static pthread_rwlock_t blk_lock;
 
@@ -205,10 +203,11 @@ enum pool_strategy pool_strategy = POOL_FAILOVER;
 int opt_rotate_period;
 static int total_urls, total_users, total_passes, total_userpasses;
 
+static
 #ifndef HAVE_CURSES
 const
 #endif
-static bool curses_active;
+bool curses_active;
 
 static char current_block[37];
 static char *current_hash;
@@ -1339,8 +1338,10 @@ double total_secs = 0.1;
 static char statusline[256];
 /* logstart is where the log window should start */
 static int devcursor, logstart, logcursor;
+#ifdef HAVE_CURSES
 /* statusy is where the status window goes up to in cases where it won't fit at startup */
 static int statusy;
+#endif
 #ifdef HAVE_OPENCL
 struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
 #endif
@@ -1349,12 +1350,12 @@ struct cgpu_info *cpus;
 #ifdef HAVE_CURSES
 static inline void unlock_curses(void)
 {
-	mutex_unlock(&curses_lock);
+	mutex_unlock(&console_lock);
 }
 
 static inline void lock_curses(void)
 {
-	mutex_lock(&curses_lock);
+	mutex_lock(&console_lock);
 }
 
 static bool curses_active_locked(void)
@@ -1588,13 +1589,10 @@ void wlogprint(const char *f, ...)
 #endif
 
 #ifdef HAVE_CURSES
-void log_curses(int prio, const char *f, va_list ap)
+bool log_curses_only(int prio, const char *f, va_list ap)
 {
 	bool high_prio;
 
-	if (opt_quiet && prio != LOG_ERR)
-		return;
-
 	high_prio = (prio == LOG_WARNING || prio == LOG_ERR);
 
 	if (curses_active_locked()) {
@@ -1606,8 +1604,9 @@ void log_curses(int prio, const char *f, va_list ap)
 			}
 		}
 		unlock_curses();
-	} else
-		vprintf(f, ap);
+		return true;
+	}
+	return false;
 }
 
 void clear_logwin(void)
@@ -5126,9 +5125,7 @@ int main(int argc, char *argv[])
 
 	mutex_init(&hash_lock);
 	mutex_init(&qd_lock);
-#ifdef HAVE_CURSES
-	mutex_init(&curses_lock);
-#endif
+	mutex_init(&console_lock);
 	mutex_init(&control_lock);
 	mutex_init(&sharelog_lock);
 	mutex_init(&ch_lock);
diff --git a/driver-opencl.c b/driver-opencl.c
index 8df0b10..0d1d0ea 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -1322,16 +1322,17 @@ static bool opencl_thread_prepare(struct thr_info *thr)
 	applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
 	clStates[i] = initCl(virtual_gpu, name, sizeof(name));
 	if (!clStates[i]) {
+#ifdef HAVE_CURSES
 		if (use_curses)
 			enable_curses();
+#endif
 		applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
 		if (!failmessage) {
-			char *buf;
-
 			applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
 			applog(LOG_ERR, "Try restarting cgminer.");
 			failmessage = true;
 #ifdef HAVE_CURSES
+			char *buf;
 			if (use_curses) {
 				buf = curses_input("Press enter to continue");
 				if (buf)
diff --git a/logging.c b/logging.c
index 3195663..47d1970 100644
--- a/logging.c
+++ b/logging.c
@@ -18,12 +18,15 @@ bool opt_log_output = false;
 /* per default priorities higher than LOG_NOTICE are logged */
 int opt_log_level = LOG_NOTICE;
 
-static void my_log_curses(int prio, char *f, va_list ap)
+static void my_log_curses(__maybe_unused int prio, char *f, va_list ap)
 {
+	if (opt_quiet && prio != LOG_ERR)
+		return;
+
 #ifdef HAVE_CURSES
 	extern bool use_curses;
-	if (use_curses)
-		log_curses(prio, f, ap);
+	if (use_curses && log_curses_only(prio, f, ap))
+		;
 	else
 #endif
 	{
@@ -31,57 +34,20 @@ static void my_log_curses(int prio, char *f, va_list ap)
 
 		strcpy(f + len - 1, "                    \n");
 
-#ifdef HAVE_CURSES
-		log_curses(prio, f, ap);
-#else
+		mutex_lock(&console_lock);
 		vprintf(f, ap);
-#endif
+		mutex_unlock(&console_lock);
 	}
 }
 
+static void log_generic(int prio, const char *fmt, va_list ap);
+
 void vapplog(int prio, const char *fmt, va_list ap)
 {
 	if (!opt_debug && prio == LOG_DEBUG)
 		return;
-
-#ifdef HAVE_SYSLOG_H
-	if (use_syslog) {
-		vsyslog(prio, fmt, ap);
-	}
-#else
-	if (0) {}
-#endif
-	else if (opt_log_output || prio <= LOG_NOTICE) {
-		char *f;
-		int len;
-		struct timeval tv = {0, 0};
-		struct tm *tm;
-
-		gettimeofday(&tv, NULL);
-
-		tm = localtime(&tv.tv_sec);
-
-		len = 40 + strlen(fmt) + 22;
-		f = alloca(len);
-		sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
-			tm->tm_year + 1900,
-			tm->tm_mon + 1,
-			tm->tm_mday,
-			tm->tm_hour,
-			tm->tm_min,
-			tm->tm_sec,
-			fmt);
-		/* Only output to stderr if it's not going to the screen as well */
-		if (!isatty(fileno((FILE *)stderr))) {
-			va_list apc;
-
-			va_copy(apc, ap);
-			vfprintf(stderr, f, apc);	/* atomic write to stderr */
-			fflush(stderr);
-		}
-
-		my_log_curses(prio, f, ap);
-	}
+	if (use_syslog || opt_log_output || prio <= LOG_NOTICE)
+		log_generic(prio, fmt, ap);
 }
 
 void applog(int prio, const char *fmt, ...)
@@ -100,7 +66,7 @@ void applog(int prio, const char *fmt, ...)
  * generic log function used by priority specific ones
  * equals vapplog() without additional priority checks
  */
-static void __maybe_unused log_generic(int prio, const char *fmt, va_list ap)
+static void log_generic(int prio, const char *fmt, va_list ap)
 {
 #ifdef HAVE_SYSLOG_H
 	if (use_syslog) {
@@ -121,7 +87,7 @@ static void __maybe_unused log_generic(int prio, const char *fmt, va_list ap)
 
 		len = 40 + strlen(fmt) + 22;
 		f = alloca(len);
-		sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n",
+		sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
 			tm->tm_year + 1900,
 			tm->tm_mon + 1,
 			tm->tm_mday,
diff --git a/miner.h b/miner.h
index 11dcb5f..4f553cd 100644
--- a/miner.h
+++ b/miner.h
@@ -584,6 +584,8 @@ extern bool fulltest(const unsigned char *hash, const unsigned char *target);
 
 extern int opt_scantime;
 
+extern pthread_mutex_t console_lock;
+
 extern pthread_mutex_t restart_lock;
 extern pthread_cond_t restart_cond;
 
@@ -627,6 +629,7 @@ extern int opt_n_threads;
 extern int num_processors;
 extern int hw_errors;
 extern bool use_syslog;
+extern bool opt_quiet;
 extern struct thr_info *thr_info;
 extern struct cgpu_info gpus[MAX_GPUDEVICES];
 extern int gpu_threads;
@@ -817,7 +820,7 @@ extern void switch_pools(struct pool *selected);
 extern void remove_pool(struct pool *pool);
 extern void write_config(FILE *fcfg);
 extern void default_save_file(char *filename);
-extern void log_curses(int prio, const char *f, va_list ap);
+extern bool log_curses_only(int prio, const char *f, va_list ap);
 extern void clear_logwin(void);
 extern bool pool_tclear(struct pool *pool, bool *var);
 extern struct thread_q *tq_new(void);