@@ -169,8 +169,32 @@ class CCoinsViewErrorCatcher : public CCoinsViewBacked
169169 // Writes do not need similar protection, as failure to write is handled by the caller.
170170};
171171
172+ class CLogRotationManager : public CStoppableServiceThread
173+ {
174+ public:
175+ CLogRotationManager () :
176+ CStoppableServiceThread (" logrt" )
177+ {}
178+ static constexpr auto LOG_ROTATION_INTERVAL = chrono::minutes(10 );
179+
180+ void execute () override
181+ {
182+ // check if debug log needs to be rotated every 10 minutes
183+ while (!shouldStop ())
184+ {
185+ unique_lock lock (m_mutex);
186+ if (m_condVar.wait_for (lock, LOG_ROTATION_INTERVAL) == cv_status::timeout)
187+ {
188+ if (gl_LogMgr)
189+ gl_LogMgr->ShrinkDebugLogFile ();
190+ }
191+ }
192+ }
193+ };
194+
172195static unique_ptr<CCoinsViewDB> gl_pCoinsDbView;
173196static unique_ptr<CCoinsViewErrorCatcher> pCoinsCatcher;
197+ static shared_ptr<CLogRotationManager> gl_LogRotationManager;
174198
175199void Interrupt (CServiceThreadGroup& threadGroup, CScheduler &scheduler)
176200{
@@ -180,6 +204,8 @@ void Interrupt(CServiceThreadGroup& threadGroup, CScheduler &scheduler)
180204 InterruptREST ();
181205 threadGroup.stop_all ();
182206 scheduler.stop (false );
207+ if (gl_LogRotationManager)
208+ gl_LogRotationManager->stop ();
183209}
184210
185211void Shutdown (CServiceThreadGroup& threadGroup, CScheduler &scheduler)
@@ -197,6 +223,9 @@ void Shutdown(CServiceThreadGroup& threadGroup, CScheduler &scheduler)
197223 RenameThread (" psl-shutoff" );
198224 mempool.AddTransactionsUpdated (1 );
199225
226+ if (gl_LogRotationManager)
227+ gl_LogRotationManager->waitForStop ();
228+
200229 StopHTTPRPC ();
201230 StopREST ();
202231 StopRPC ();
@@ -213,8 +242,12 @@ void Shutdown(CServiceThreadGroup& threadGroup, CScheduler &scheduler)
213242 #endif
214243#endif
215244 StopNode ();
245+ LogFnPrintf (" Waiting for Pastel threads to exit..." );
216246 threadGroup.join_all ();
247+ LogFnPrintf (" ...done" );
248+ LogFnPrintf (" Waiting for scheduler threads to exit..." );
217249 scheduler.join_all ();
250+ LogFnPrintf (" ...done" );
218251 UnregisterNodeSignals (GetNodeSignals ());
219252
220253 if (fFeeEstimatesInitialized )
@@ -290,7 +323,8 @@ void HandleSIGTERM(int)
290323
291324void HandleSIGHUP (int )
292325{
293- fReopenDebugLog = true ;
326+ if (gl_LogMgr)
327+ gl_LogMgr->ScheduleReopenDebugLog ();
294328}
295329
296330bool static InitError (const string &str)
@@ -796,6 +830,9 @@ bool AppInit2(CServiceThreadGroup& threadGroup, CScheduler& scheduler)
796830{
797831 string strError;
798832
833+ if (!gl_LogMgr)
834+ return InitError (" Error: Log Manager is not initialized" );
835+
799836 // ********************************************************* Step 1: setup
800837#ifdef _MSC_VER
801838 // Turn off Microsoft heap dump noise
@@ -866,7 +903,7 @@ bool AppInit2(CServiceThreadGroup& threadGroup, CScheduler& scheduler)
866903
867904 // Set this early so that parameter interactions go to console
868905 string error;
869- if (!SetPrintToConsoleMode (error))
906+ if (!gl_LogMgr-> SetPrintToConsoleMode (error))
870907 return InitError (error);
871908 fLogTimestamps = GetBoolArg (" -logtimestamps" , true );
872909 fLogIPs = GetBoolArg (" -logips" , false );
@@ -1194,10 +1231,15 @@ bool AppInit2(CServiceThreadGroup& threadGroup, CScheduler& scheduler)
11941231 CreatePidFile (GetPidFile (), getpid ());
11951232#endif
11961233 if (GetBoolArg (" -shrinkdebugfile" , !fDebug ))
1197- ShrinkDebugFile ( );
1234+ gl_LogMgr-> ShrinkDebugLogFile ( true );
11981235
1199- if (fPrintToDebugLog )
1200- OpenDebugLog ();
1236+ gl_LogMgr->OpenDebugLogFile ();
1237+ if (!gl_LogRotationManager)
1238+ {
1239+ gl_LogRotationManager = make_unique<CLogRotationManager>();
1240+ if (threadGroup.add_thread (error, gl_LogRotationManager, true ) == INVALID_THREAD_OBJECT_ID)
1241+ return InitError (translate (" Failed to create log rotation thread. " ) + error);
1242+ }
12011243
12021244 LogPrintf (" Using OpenSSL version %s\n " , OpenSSL_version (OPENSSL_VERSION_STRING));
12031245#ifdef ENABLE_WALLET
@@ -1226,7 +1268,7 @@ bool AppInit2(CServiceThreadGroup& threadGroup, CScheduler& scheduler)
12261268
12271269 if (!chainparams.IsRegTest () &&
12281270 GetBoolArg (" -showmetrics" , isatty (STDOUT_FILENO)) &&
1229- !IsPrintToConsole () && !GetBoolArg (" -daemon" , false ))
1271+ !gl_LogMgr-> IsPrintToConsole () && !GetBoolArg (" -daemon" , false ))
12301272 {
12311273 // Start the persistent metrics interface
12321274 ConnectMetricsScreen ();
@@ -1240,6 +1282,7 @@ bool AppInit2(CServiceThreadGroup& threadGroup, CScheduler& scheduler)
12401282 libsnark::inhibit_profiling_counters = true ;
12411283
12421284 // Initialize Zcash circuit parameters
1285+ uiInterface.InitMessage (translate (" Initializing chain parameters..." ));
12431286 ZC_LoadParams (chainparams);
12441287
12451288 /* Start the RPC server already. It will be started in "warmup" mode
0 commit comments