Skip to content

Commit 90dd6ed

Browse files
committed
[Utils] Introduce CBatchedLogger
>>> inspired by dash@9d25bb1d8f0d3e5acc10549d9db8fa52f52be5b2
1 parent 13b5c1b commit 90dd6ed

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/logging.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const CLogCategoryDesc LogCategories[] = {
124124
{BCLog::VALIDATION, "validation"},
125125
{BCLog::LLMQ, "llmq"},
126126
{BCLog::NET_MN, "net_mn"},
127+
{BCLog::DKG, "dkg"},
127128
{BCLog::ALL, "1"},
128129
{BCLog::ALL, "all"},
129130
};
@@ -260,3 +261,27 @@ void BCLog::Logger::ShrinkDebugFile()
260261
} else if (file != NULL)
261262
fclose(file);
262263
}
264+
265+
/// PIVX
266+
267+
CBatchedLogger::CBatchedLogger(BCLog::Logger* _logger, BCLog::LogFlags _category, const std::string& _header) :
268+
logger(_logger),
269+
accept(LogAcceptCategory(_category)),
270+
header(_header)
271+
{}
272+
273+
CBatchedLogger::~CBatchedLogger()
274+
{
275+
Flush();
276+
}
277+
278+
void CBatchedLogger::Flush()
279+
{
280+
if (!accept || msg.empty()) {
281+
return;
282+
}
283+
if (logger && logger->Enabled()) {
284+
logger->LogPrintStr(strprintf("%s:\n%s", header, msg));
285+
msg.clear();
286+
}
287+
}

src/logging.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
22
// Copyright (c) 2009-2018 The Bitcoin developers
3-
// Copyright (c) 2015-2020 The PIVX developers
3+
// Copyright (c) 2018-2021 The Dash Core developers
4+
// Copyright (c) 2015-2022 The PIVX developers
45
// Distributed under the MIT software license, see the accompanying
56
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
67

78
/**
89
* Server/client environment: argument handling, config file parsing,
910
* logging, thread wrappers
1011
*/
11-
#ifndef BITCOIN_LOGGING_H
12-
#define BITCOIN_LOGGING_H
12+
#ifndef PIVX_LOGGING_H
13+
#define PIVX_LOGGING_H
1314

1415
#include "fs.h"
1516
#include "tinyformat.h"
@@ -64,6 +65,7 @@ namespace BCLog {
6465
VALIDATION = (1 << 24),
6566
LLMQ = (1 << 25),
6667
NET_MN = (1 << 26),
68+
DKG = (1 << 27),
6769
ALL = ~(uint32_t)0,
6870
};
6971

@@ -163,4 +165,28 @@ static inline void LogPrintf(const char* fmt, const Args&... args)
163165
} \
164166
} while(0)
165167

166-
#endif // BITCOIN_LOGGING_H
168+
/// PIVX
169+
170+
class CBatchedLogger
171+
{
172+
private:
173+
BCLog::Logger* logger;
174+
bool accept;
175+
std::string header;
176+
std::string msg;
177+
public:
178+
CBatchedLogger(BCLog::Logger* _logger, BCLog::LogFlags _category, const std::string& _header);
179+
virtual ~CBatchedLogger();
180+
void Flush();
181+
182+
template<typename... Args>
183+
void Batch(const std::string& fmt, const Args&... args)
184+
{
185+
if (!accept) {
186+
return;
187+
}
188+
msg += " " + strprintf(fmt, args...) + "\n";
189+
}
190+
};
191+
192+
#endif // PIVX_LOGGING_H

0 commit comments

Comments
 (0)