Skip to content

Commit 85274ea

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

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/scraper/scraper.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ class logger
259259
return false;
260260
}
261261

262+
// Re-open log file.
263+
fs::path plogfile = pathDataDir / "scraper.log";
264+
logfile.open(plogfile.c_str(), std::ios_base::out | std::ios_base::app);
265+
266+
if (!logfile.is_open())
267+
LogPrintf("ERROR: Scraper: Logger: Failed to open logging file\n");
268+
262269
PrevArchiveCheckDate = ArchiveCheckDate;
263270
}
264271

@@ -328,10 +335,20 @@ class logger
328335
return true;
329336
}
330337
else
338+
{
331339
return false;
340+
}
332341
}
333342
};
334343

344+
logger& LogInstance()
345+
{
346+
// This is similar to Bitcoin's newer approach.
347+
static logger* scraperlogger{new logger()};
348+
return *scraperlogger;
349+
}
350+
351+
335352
boost::gregorian::date logger::PrevArchiveCheckDate = boost::posix_time::from_time_t(GetAdjustedTime()).date();
336353
CCriticalSection logger::cs_log;
337354

@@ -361,11 +378,13 @@ void _log(logattribute eType, const std::string& sCall, const std::string& sMess
361378
}
362379

363380
sOut = tfm::format("%s [%s] <%s> : %s", DateTimeStrFormat("%x %H:%M:%S", GetAdjustedTime()), sType, sCall, sMessage);
364-
logger log;
381+
382+
//logger log;
383+
logger& log = LogInstance();
365384

366385
log.output(sOut);
367386

368-
log.closelogfile();
387+
//log.closelogfile();
369388

370389
// Send to UI for log window.
371390
uiInterface.NotifyScraperEvent(scrapereventtypes::Log, CT_NEW, sOut);
@@ -681,6 +700,7 @@ void ScraperApplyAppCacheEntries()
681700
// It can also be called in "single shot" mode.
682701
void Scraper(bool bSingleShot)
683702
{
703+
684704
// Initialize these while still single-threaded. They cannot be initialized during declaration because GetDataDir()
685705
// gives the wrong value that early. If they are already initialized then leave them alone (because this function
686706
// can be called in singleshot mode.
@@ -693,6 +713,9 @@ void Scraper(bool bSingleShot)
693713
pathScraper = pathDataDir / "Scraper";
694714
}
695715

716+
// Initialize log singleton. Must be after the imbue.
717+
LogInstance();
718+
696719
if (!bSingleShot)
697720
_log(logattribute::INFO, "Scraper", "Starting Scraper thread.");
698721
else
@@ -776,14 +799,14 @@ void Scraper(bool bSingleShot)
776799
}
777800

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

781804
fs::path plogfile_out;
782805

783806
if (log.archive(false, plogfile_out))
784807
_log(logattribute::INFO, "Scraper", "Archived scraper.log to " + plogfile_out.filename().string());
785808

786-
log.closelogfile();
809+
//log.closelogfile();
787810

788811
sbage = SuperblockAge();
789812
_log(logattribute::INFO, "Scraper", "Superblock not needed. age=" + std::to_string(sbage));
@@ -938,6 +961,9 @@ void NeuralNetwork()
938961
pathDataDir.imbue(std::locale(std::locale(), new std::codecvt_utf8_utf16<wchar_t>()));
939962

940963
pathScraper = pathDataDir / "Scraper";
964+
965+
// Initialize log singleton. Must be after the imbue.
966+
LogInstance();
941967
}
942968

943969

@@ -1040,14 +1066,14 @@ bool ScraperHousekeeping()
10401066
+ ", Popularity: " + std::to_string(network_hash.second));
10411067
}
10421068

1043-
logger log;
1069+
logger& log = LogInstance();
10441070

10451071
fs::path plogfile_out;
10461072

10471073
if (log.archive(false, plogfile_out))
10481074
_log(logattribute::INFO, "ScraperHousekeeping", "Archived scraper.log to " + plogfile_out.filename().string());
10491075

1050-
log.closelogfile();
1076+
//log.closelogfile();
10511077

10521078
return true;
10531079
}
@@ -4245,12 +4271,12 @@ UniValue archivescraperlog(const UniValue& params, bool fHelp)
42454271
"archivescraperlog takes no arguments and results in immediate archiving of the scraper log\n"
42464272
);
42474273

4248-
logger log;
4274+
logger& log = LogInstance();
42494275

42504276
fs::path pfile_out;
42514277
bool ret = log.archive(true, pfile_out);
42524278

4253-
log.closelogfile();
4279+
//log.closelogfile();
42544280

42554281
return UniValue(ret);
42564282
}

0 commit comments

Comments
 (0)