Skip to content

Commit 63175c9

Browse files
committed
Change the scraper logger to a singleton pattern
a la Bitcoin's newer approach.
1 parent de9732b commit 63175c9

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/scraper/scraper.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ class logger
332332
}
333333
};
334334

335+
logger& LogInstance()
336+
{
337+
// This is similar to Bitcoin's newer approach.
338+
static logger* scraperlogger{new logger()};
339+
return *scraperlogger;
340+
}
341+
342+
335343
boost::gregorian::date logger::PrevArchiveCheckDate = boost::posix_time::from_time_t(GetAdjustedTime()).date();
336344
CCriticalSection logger::cs_log;
337345

@@ -361,11 +369,13 @@ void _log(logattribute eType, const std::string& sCall, const std::string& sMess
361369
}
362370

363371
sOut = tfm::format("%s [%s] <%s> : %s", DateTimeStrFormat("%x %H:%M:%S", GetAdjustedTime()), sType, sCall, sMessage);
364-
logger log;
372+
373+
//logger log;
374+
logger& log = LogInstance();
365375

366376
log.output(sOut);
367377

368-
log.closelogfile();
378+
//log.closelogfile();
369379

370380
// Send to UI for log window.
371381
uiInterface.NotifyScraperEvent(scrapereventtypes::Log, CT_NEW, sOut);
@@ -681,6 +691,9 @@ void ScraperApplyAppCacheEntries()
681691
// It can also be called in "single shot" mode.
682692
void Scraper(bool bSingleShot)
683693
{
694+
// Initialize log singleton.
695+
LogInstance();
696+
684697
// Initialize these while still single-threaded. They cannot be initialized during declaration because GetDataDir()
685698
// gives the wrong value that early. If they are already initialized then leave them alone (because this function
686699
// can be called in singleshot mode.
@@ -776,14 +789,14 @@ void Scraper(bool bSingleShot)
776789
}
777790

778791
// Need the log archive check here, because we don't run housekeeping in this while loop.
779-
logger log;
792+
logger& log = LogInstance();
780793

781794
fs::path plogfile_out;
782795

783796
if (log.archive(false, plogfile_out))
784797
_log(logattribute::INFO, "Scraper", "Archived scraper.log to " + plogfile_out.filename().string());
785798

786-
log.closelogfile();
799+
//log.closelogfile();
787800

788801
sbage = SuperblockAge();
789802
_log(logattribute::INFO, "Scraper", "Superblock not needed. age=" + std::to_string(sbage));
@@ -1040,14 +1053,14 @@ bool ScraperHousekeeping()
10401053
+ ", Popularity: " + std::to_string(network_hash.second));
10411054
}
10421055

1043-
logger log;
1056+
logger& log = LogInstance();
10441057

10451058
fs::path plogfile_out;
10461059

10471060
if (log.archive(false, plogfile_out))
10481061
_log(logattribute::INFO, "ScraperHousekeeping", "Archived scraper.log to " + plogfile_out.filename().string());
10491062

1050-
log.closelogfile();
1063+
//log.closelogfile();
10511064

10521065
return true;
10531066
}
@@ -4245,12 +4258,12 @@ UniValue archivescraperlog(const UniValue& params, bool fHelp)
42454258
"archivescraperlog takes no arguments and results in immediate archiving of the scraper log\n"
42464259
);
42474260

4248-
logger log;
4261+
logger& log = LogInstance();
42494262

42504263
fs::path pfile_out;
42514264
bool ret = log.archive(true, pfile_out);
42524265

4253-
log.closelogfile();
4266+
//log.closelogfile();
42544267

42554268
return UniValue(ret);
42564269
}

0 commit comments

Comments
 (0)