Commit 8e9f32a81b61096dec9feff914a07c34f4df6721

Con Kolivas 2013-10-18T23:30:05

Add a forcelog variant of applog which invalidates any console lock to force output.

diff --git a/cgminer.c b/cgminer.c
index fb739f2..26482f1 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -423,7 +423,7 @@ static void applog_and_exit(const char *fmt, ...)
 	va_start(ap, fmt);
 	vsnprintf(exit_buf, sizeof(exit_buf), fmt, ap);
 	va_end(ap);
-	_applog(LOG_ERR, exit_buf);
+	_applog(LOG_ERR, exit_buf, true);
 	exit(1);
 }
 
diff --git a/logging.c b/logging.c
index d00600f..52eb9ef 100644
--- a/logging.c
+++ b/logging.c
@@ -21,11 +21,17 @@ 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, const char *datetime, const char *str)
+static void my_log_curses(int prio, const char *datetime, const char *str, bool force)
 {
 	if (opt_quiet && prio != LOG_ERR)
 		return;
 
+	/* Mutex could be locked by dead thread on shutdown so forcelog will
+	 * invalidate any console lock status. */
+	if (force) {
+		mutex_trylock(&console_lock);
+		mutex_unlock(&console_lock);
+	}
 #ifdef HAVE_CURSES
 	extern bool use_curses;
 	if (use_curses && log_curses_only(prio, datetime, str))
@@ -44,7 +50,7 @@ static void my_log_curses(int prio, const char *datetime, const char *str)
 /*
  * log function
  */
-void _applog(int prio, const char *str)
+void _applog(int prio, const char *str, bool force)
 {
 #ifdef HAVE_SYSLOG_H
 	if (use_syslog) {
@@ -77,6 +83,6 @@ void _applog(int prio, const char *str)
 			fflush(stderr);
 		}
 
-		my_log_curses(prio, datetime, str);
+		my_log_curses(prio, datetime, str, force);
 	}
 }
diff --git a/logging.h b/logging.h
index 2956452..4d8a250 100644
--- a/logging.h
+++ b/logging.h
@@ -28,7 +28,7 @@ extern int opt_log_level;
 
 #define LOGBUFSIZ 256
 
-extern void _applog(int prio, const char *str);
+extern void _applog(int prio, const char *str, bool force);
 
 #define IN_FMT_FFL " in %s %s():%d"
 
@@ -37,7 +37,7 @@ extern void _applog(int prio, const char *str);
 		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
 			char tmp42[LOGBUFSIZ]; \
 			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
-			_applog(prio, tmp42); \
+			_applog(prio, tmp42, false); \
 		} \
 	} \
 } while (0)
@@ -47,7 +47,17 @@ extern void _applog(int prio, const char *str);
 		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
 			char tmp42[_SIZ]; \
 			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
-			_applog(prio, tmp42); \
+			_applog(prio, tmp42, false); \
+		} \
+	} \
+} while (0)
+
+#define forcelog(prio, fmt, ...) do { \
+	if (opt_debug || prio != LOG_DEBUG) { \
+		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
+			char tmp42[LOGBUFSIZ]; \
+			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
+			_applog(prio, tmp42, true); \
 		} \
 	} \
 } while (0)
@@ -56,7 +66,7 @@ extern void _applog(int prio, const char *str);
 	if (fmt) { \
 		char tmp42[LOGBUFSIZ]; \
 		snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
-		_applog(LOG_ERR, tmp42); \
+		_applog(LOG_ERR, tmp42, true); \
 	} \
 	_quit(status); \
 } while (0)
@@ -66,7 +76,7 @@ extern void _applog(int prio, const char *str);
 		char tmp42[LOGBUFSIZ]; \
 		snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
 				##__VA_ARGS__, __FILE__, __func__, __LINE__); \
-		_applog(LOG_ERR, tmp42); \
+		_applog(LOG_ERR, tmp42, true); \
 	} \
 	_quit(status); \
 } while (0)
@@ -76,7 +86,7 @@ extern void _applog(int prio, const char *str);
 		char tmp42[LOGBUFSIZ]; \
 		snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
 				##__VA_ARGS__, _file, _func, _line); \
-		_applog(LOG_ERR, tmp42); \
+		_applog(LOG_ERR, tmp42, true); \
 	} \
 	_quit(status); \
 } while (0)
diff --git a/usbutils.c b/usbutils.c
index 8f083d6..0a9c78f 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -933,7 +933,7 @@ void usb_all(int level)
 		for (i = 0; i < count; i++)
 			usb_full(&j, list[i], &buf, &off, &len, level);
 
-		_applog(LOG_WARNING, buf);
+		_applog(LOG_WARNING, buf, false);
 
 		free(buf);