Description: add option: flush stdout
 Added the option to forcefully flush STDOUT, which may be useful when running the miner through a pipe instead than a shell.
 If STDOUT is a shell, it's unbuffered by default.
 If STDOUT is a pipe, it's buffered by default.
 .
 Applied by hand on 1.5.0 CPU only version.
 .
 xmr-stak-cpu (1.3.0+1.5.0-4) unstable; urgency=medium
 .
   * Merge upstream patch to make stdout output unbuffered
     (remove delayed logging in syslog).
Author: Adam Cecile <acecile@le-vert.net>

---
Origin: upstream, https://github.com/fireice-uk/xmr-stak/pull/80
Bug: https://github.com/fireice-uk/xmr-stak/pull/80
Forwarded: not-needed
Reviewed-By: fireice-uk / psychocrypt / darugnaa
Last-Update: 2018-01-24

Index: xmr-stak-cpu-1.3.0+1.5.0/config.txt
===================================================================
--- xmr-stak-cpu-1.3.0+1.5.0.orig/config.txt
+++ xmr-stak-cpu-1.3.0+1.5.0/config.txt
@@ -160,6 +160,14 @@ null,
 "daemon_mode" : false,
 
 /*
+ * Buffered output control.
+ * When running the miner through a pipe, standard output is buffered. This means that the pipe won't read
+ * each output line immediately. This can cause delays when running in background.
+ * Set this option to true to flush stdout after each line, so it can be read immediately.
+ */
+"flush_stdout" : false,
+
+/*
  * Output file
  *
  * output_file  - This option will log all output to a file.
Index: xmr-stak-cpu-1.3.0+1.5.0/console.cpp
===================================================================
--- xmr-stak-cpu-1.3.0+1.5.0.orig/console.cpp
+++ xmr-stak-cpu-1.3.0+1.5.0/console.cpp
@@ -156,6 +156,7 @@ printer::printer()
 {
 	verbose_level = LINF;
 	logfile = nullptr;
+	b_flush_stdout = false;
 }
 
 bool printer::open_logfile(const char* file)
@@ -193,6 +194,11 @@ void printer::print_msg(verbosity verbos
 	std::unique_lock<std::mutex> lck(print_mutex);
 	fputs(buf, stdout);
 
+	if (b_flush_stdout)
+	{
+		fflush(stdout);
+	}
+
 	if(logfile != nullptr)
 	{
 		fputs(buf, logfile);
Index: xmr-stak-cpu-1.3.0+1.5.0/console.h
===================================================================
--- xmr-stak-cpu-1.3.0+1.5.0.orig/console.h
+++ xmr-stak-cpu-1.3.0+1.5.0/console.h
@@ -29,6 +29,7 @@ public:
 	};
 
 	inline void set_verbose_level(size_t level) { verbose_level = (verbosity)level; }
+	inline void set_flush_stdout(bool status) { b_flush_stdout = status; }
 	void print_msg(verbosity verbose, const char* fmt, ...);
 	void print_str(const char* str);
 	bool open_logfile(const char* file);
@@ -39,5 +40,6 @@ private:
 
 	std::mutex print_mutex;
 	verbosity verbose_level;
+	bool b_flush_stdout;
 	FILE* logfile;
 };
Index: xmr-stak-cpu-1.3.0+1.5.0/jconf.cpp
===================================================================
--- xmr-stak-cpu-1.3.0+1.5.0.orig/jconf.cpp
+++ xmr-stak-cpu-1.3.0+1.5.0/jconf.cpp
@@ -48,7 +48,7 @@ using namespace rapidjson;
 enum configEnum { aCpuThreadsConf, sUseSlowMem, bNiceHashMode, bAesOverride,
 	bTlsMode, bTlsSecureAlgo, sTlsFingerprint, sPoolAddr, sWalletAddr, sPoolPwd,
 	iCallTimeout, iNetRetry, iGiveUpLimit, iVerboseLevel, iAutohashTime,
-	bDaemonMode, sOutputFile, iHttpdPort, bPreferIpv4 };
+	bDaemonMode, bFlushStdout, sOutputFile, iHttpdPort, bPreferIpv4 };
 
 struct configVal {
 	configEnum iName;
@@ -75,6 +75,7 @@ configVal oConfigValues[] = {
 	{ iVerboseLevel, "verbose_level", kNumberType },
 	{ iAutohashTime, "h_print_time", kNumberType },
 	{ bDaemonMode, "daemon_mode", kTrueType },
+	{ bFlushStdout, "flush_stdout", kTrueType},
 	{ sOutputFile, "output_file", kStringType },
 	{ iHttpdPort, "httpd_port", kNumberType },
 	{ bPreferIpv4, "prefer_ipv4", kTrueType }
@@ -463,5 +464,15 @@ bool jconf::parse_config(const char* sFi
 	if(!bHaveAes)
 		printer::inst()->print_msg(L0, "Your CPU doesn't support hardware AES. Don't expect high hashrates.");
 
+	if (prv->configValues[bFlushStdout]->IsBool())
+	{
+		bool bflush = prv->configValues[bFlushStdout]->GetBool();
+		printer::inst()->set_flush_stdout(bflush);
+		if (bflush)
+		{
+			printer::inst()->print_msg(L0, "Flush stdout forced.");
+		}
+	}
+
 	return true;
 }
