Skip to content

Commit

Permalink
Replace C++ logging with tracing logging
Browse files Browse the repository at this point in the history
Does not yet handle SIGHUP log-reopening.
  • Loading branch information
str4d committed Aug 7, 2020
1 parent e04da74 commit d5de95f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 133 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ endif
BITCOIN_CONFIG_INCLUDES=-I$(builddir)/config
BITCOIN_INCLUDES=-I$(builddir) -I$(builddir)/obj $(BDB_CPPFLAGS) $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS) $(CRYPTO_CFLAGS) $(SSL_CFLAGS)

BITCOIN_CONFIG_INCLUDES += -I$(srcdir)/rust/include
BITCOIN_INCLUDES += -I$(srcdir)/rust/include
BITCOIN_INCLUDES += -I$(srcdir)/secp256k1/include
BITCOIN_INCLUDES += -I$(srcdir)/univalue/include
Expand Down
24 changes: 18 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@
#endif

#include "librustzcash.h"
#include <tracing.h>

using namespace std;

extern void ThreadSendAlert();

TracingHandle* pTracingHandle = nullptr;

bool fFeeEstimatesInitialized = false;
static const bool DEFAULT_PROXYRANDOMIZE = true;
static const bool DEFAULT_REST_ENABLE = false;
Expand Down Expand Up @@ -259,6 +262,9 @@ void Shutdown()
globalVerifyHandle.reset();
ECC_Stop();
LogPrintf("%s: done\n", __func__);
if (pTracingHandle) {
tracing_free(pTracingHandle);
}
}

/**
Expand Down Expand Up @@ -800,6 +806,18 @@ void InitLogging()
fLogTimestamps = GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
fLogIPs = GetBoolArg("-logips", DEFAULT_LOGIPS);

if (fPrintToConsole) {
pTracingHandle = tracing_init(nullptr, 0, fLogTimestamps);
} else {
boost::filesystem::path pathDebug = GetDebugLogPath();
auto pathDebugStr = pathDebug.native();
pTracingHandle = tracing_init(
reinterpret_cast<const codeunit*>(pathDebugStr.c_str()),
pathDebugStr.length(),
fLogTimestamps
);
}

LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
LogPrintf("Zcash version %s (%s)\n", FormatFullVersion(), CLIENT_DATE);
}
Expand Down Expand Up @@ -1144,12 +1162,6 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// if (GetBoolArg("-shrinkdebugfile", !fDebug))
// ShrinkDebugFile();

if (fPrintToDebugLog) {
if (!OpenDebugLog()) {
return InitError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
}
}

LogPrintf("Using OpenSSL version %s\n", SSLeay_version(SSLEAY_VERSION));
#ifdef ENABLE_WALLET
LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0));
Expand Down
90 changes: 0 additions & 90 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,32 +221,6 @@ boost::filesystem::path GetDebugLogPath()
}
}

bool OpenDebugLog()
{
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);

assert(fileout == NULL);
assert(vMsgsBeforeOpenLog);

boost::filesystem::path pathDebug = GetDebugLogPath();
fileout = fopen(pathDebug.string().c_str(), "a");
if (!fileout) {
return false;
}

setbuf(fileout, nullptr); // unbuffered
// dump buffered messages from before we opened the log
while (!vMsgsBeforeOpenLog->empty()) {
FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
vMsgsBeforeOpenLog->pop_front();
}

delete vMsgsBeforeOpenLog;
vMsgsBeforeOpenLog = NULL;
return true;
}

bool LogAcceptCategory(const char* category)
{
if (category != NULL)
Expand Down Expand Up @@ -276,70 +250,6 @@ bool LogAcceptCategory(const char* category)
return true;
}

/**
* fStartedNewLine is a state variable held by the calling context that will
* suppress printing of the timestamp when multiple calls are made that don't
* end in a newline. Initialize it to true, and hold it, in the calling context.
*/
static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
{
string strStamped;

if (!fLogTimestamps)
return str;

if (*fStartedNewLine)
strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()) + ' ' + str;
else
strStamped = str;

if (!str.empty() && str[str.size()-1] == '\n')
*fStartedNewLine = true;
else
*fStartedNewLine = false;

return strStamped;
}

int LogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
static bool fStartedNewLine = true;
if (fPrintToConsole)
{
// print to console
ret = fwrite(str.data(), 1, str.size(), stdout);
fflush(stdout);
}
else if (fPrintToDebugLog)
{
boost::call_once(&DebugPrintInit, debugPrintInitFlag);
boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);

string strTimestamped = LogTimestampStr(str, &fStartedNewLine);

// buffer if we haven't opened the log yet
if (fileout == NULL) {
assert(vMsgsBeforeOpenLog);
ret = strTimestamped.length();
vMsgsBeforeOpenLog->push_back(strTimestamped);
}
else
{
// reopen the log file, if requested
if (fReopenDebugLog) {
fReopenDebugLog = false;
boost::filesystem::path pathDebug = GetDebugLogPath();
if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
setbuf(fileout, NULL); // unbuffered
}

ret = FileWriteStr(strTimestamped, fileout);
}
}
return ret;
}

/** Interpret string as boolean, for argument parsing */
static bool InterpretBool(const std::string& strValue)
{
Expand Down
61 changes: 24 additions & 37 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <boost/signals2/signal.hpp>
#include <boost/thread/exceptions.hpp>

#include <tracing.h>

static const bool DEFAULT_LOGTIMEMICROS = false;
static const bool DEFAULT_LOGIPS = false;
static const bool DEFAULT_LOGTIMESTAMPS = true;
Expand Down Expand Up @@ -74,46 +76,32 @@ bool SetupNetworking();

/** Return true if log accepts specified category */
bool LogAcceptCategory(const char* category);
/** Send a string to the log output */
int LogPrintStr(const std::string &str);

#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)

/**
* When we switch to C++11, this can be switched to variadic templates instead
* of this macro-based construction (see tinyformat.h).
*/
#define MAKE_ERROR_AND_LOG_FUNC(n) \
/** Print to debug.log if -debug=category switch is given OR category is NULL. */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
{ \
if(!LogAcceptCategory(category)) return 0; \
return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
} \
/** Log error and return false */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
return false; \
}

TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC)

/**
* Zero-arg versions of logging and error, these are not covered by
* TINYFORMAT_FOREACH_ARGNUM
*/
static inline int LogPrint(const char* category, const char* format)
{
if(!LogAcceptCategory(category)) return 0;
return LogPrintStr(format);
}
static inline bool error(const char* format)
/** Print to debug.log if -debug=category switch is given OR category is NULL. */
#define LogPrint(category, ...) do { \
if (LogAcceptCategory(category)) { \
std::string T_MSG = tfm::format(__VA_ARGS__); \
if (!T_MSG.empty() && T_MSG[T_MSG.size()-1] == '\n') { \
T_MSG.erase(T_MSG.size()-1); \
} \
TracingInfo( \
category == NULL ? "main" : category, \
T_MSG.c_str()); \
} \
} while(0)

#define LogError(category, ...) ([&]() { \
std::string T_MSG = tfm::format(__VA_ARGS__); \
TracingError(category, T_MSG.c_str()); \
return false; \
}())

template<typename... Args>
static inline bool error(const char* format, const Args&... args)
{
LogPrintStr(std::string("ERROR: ") + format + "\n");
return false;
return LogError("main", format, args...);
}

const boost::filesystem::path &ZC_GetParamsDir();
Expand Down Expand Up @@ -144,7 +132,6 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
boost::filesystem::path GetTempPath();
boost::filesystem::path GetDebugLogPath();
bool OpenDebugLog();
void ShrinkDebugFile();
void runCommand(const std::string& strCommand);
const boost::filesystem::path GetExportDir();
Expand Down

0 comments on commit d5de95f

Please sign in to comment.