Page MenuHomePhabricator

D10951.id32120.diff
No OneTemporary

D10951.id32120.diff

diff --git a/src/init.cpp b/src/init.cpp
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1021,6 +1021,13 @@
#else
hidden_args.emplace_back("-logthreadnames");
#endif
+ argsman.AddArg(
+ "-logsourcelocations",
+ strprintf(
+ "Prepend debug output with name of the originating source location "
+ "(source file, line number and function name) (default: %u)",
+ DEFAULT_LOGSOURCELOCATIONS),
+ ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
argsman.AddArg(
"-logtimemicros",
strprintf("Add microsecond precision to debug timestamps (default: %d)",
@@ -1689,6 +1696,8 @@
LogInstance().m_log_threadnames =
args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
#endif
+ LogInstance().m_log_sourcelocations =
+ args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
diff --git a/src/logging.h b/src/logging.h
--- a/src/logging.h
+++ b/src/logging.h
@@ -22,6 +22,7 @@
static const bool DEFAULT_LOGIPS = false;
static const bool DEFAULT_LOGTIMESTAMPS = true;
static const bool DEFAULT_LOGTHREADNAMES = false;
+static const bool DEFAULT_LOGSOURCELOCATIONS = false;
extern bool fLogIPs;
extern const char *const DEFAULT_DEBUGLOGFILE;
@@ -96,6 +97,7 @@
bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
+ bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
fs::path m_file_path;
std::atomic<bool> m_reopen_file{false};
@@ -103,7 +105,9 @@
~Logger();
/** Send a string to the log output */
- void LogPrintStr(const std::string &str);
+ void LogPrintStr(const std::string &str,
+ const std::string &logging_function,
+ const std::string &source_file, const int source_line);
/** Returns whether logs will be written to any output */
bool Enabled() const {
@@ -172,7 +176,9 @@
// unconditionally log to debug.log! It should not be the case that an inbound
// peer can fill up a user's disk with debug.log entries.
template <typename... Args>
-static inline void LogPrintf(const char *fmt, const Args &... args) {
+static inline void
+LogPrintf_(const std::string &logging_function, const std::string &source_file,
+ const int source_line, const char *fmt, const Args &... args) {
if (LogInstance().Enabled()) {
std::string log_msg;
try {
@@ -184,10 +190,13 @@
log_msg = "Error \"" + std::string(fmterr.what()) +
"\" while formatting log message: " + fmt;
}
- LogInstance().LogPrintStr(log_msg);
+ LogInstance().LogPrintStr(log_msg, logging_function, source_file,
+ source_line);
}
}
+#define LogPrintf(...) LogPrintf_(__func__, __FILE__, __LINE__, __VA_ARGS__)
+
// Use a macro instead of a function for conditional logging to prevent
// evaluating arguments when logging for the category is not enabled.
#define LogPrint(category, ...) \
diff --git a/src/logging.cpp b/src/logging.cpp
--- a/src/logging.cpp
+++ b/src/logging.cpp
@@ -6,6 +6,7 @@
#include <logging.h>
+#include <util/string.h>
#include <util/threadnames.h>
#include <util/time.h>
@@ -210,10 +211,19 @@
}
} // namespace BCLog
-void BCLog::Logger::LogPrintStr(const std::string &str) {
+void BCLog::Logger::LogPrintStr(const std::string &str,
+ const std::string &logging_function,
+ const std::string &source_file,
+ const int source_line) {
StdLockGuard scoped_lock(m_cs);
std::string str_prefixed = LogEscapeMessage(str);
+ if (m_log_sourcelocations && m_started_new_line) {
+ str_prefixed.insert(0, "[" + RemovePrefix(source_file, "./") + ":" +
+ ToString(source_line) + "] [" +
+ logging_function + "] ");
+ }
+
if (m_log_threadnames && m_started_new_line) {
str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] ");
}
diff --git a/src/test/fuzz/string.cpp b/src/test/fuzz/string.cpp
--- a/src/test/fuzz/string.cpp
+++ b/src/test/fuzz/string.cpp
@@ -74,6 +74,7 @@
}
OutputType output_type;
(void)ParseOutputType(random_string_1, output_type);
+ (void)RemovePrefix(random_string_1, random_string_2);
(void)ResolveErrMsg(random_string_1, random_string_2);
try {
(void)RPCConvertNamedValues(random_string_1, random_string_vector);
diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -97,6 +97,7 @@
{
"dummy",
"-printtoconsole=0",
+ "-logsourcelocations",
"-logtimemicros",
"-debug",
"-debugexclude=libevent",
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -2548,4 +2548,16 @@
BOOST_CHECK_NE(message_hash1, signature_hash);
}
+BOOST_AUTO_TEST_CASE(remove_prefix) {
+ BOOST_CHECK_EQUAL(RemovePrefix("./util/system.h", "./"), "util/system.h");
+ BOOST_CHECK_EQUAL(RemovePrefix("foo", "foo"), "");
+ BOOST_CHECK_EQUAL(RemovePrefix("foo", "fo"), "o");
+ BOOST_CHECK_EQUAL(RemovePrefix("foo", "f"), "oo");
+ BOOST_CHECK_EQUAL(RemovePrefix("foo", ""), "foo");
+ BOOST_CHECK_EQUAL(RemovePrefix("fo", "foo"), "fo");
+ BOOST_CHECK_EQUAL(RemovePrefix("f", "foo"), "f");
+ BOOST_CHECK_EQUAL(RemovePrefix("", "foo"), "");
+ BOOST_CHECK_EQUAL(RemovePrefix("", ""), "");
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/string.h b/src/util/string.h
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -25,6 +25,14 @@
return str.substr(front, end - front + 1);
}
+[[nodiscard]] inline std::string RemovePrefix(const std::string &str,
+ const std::string &prefix) {
+ if (str.substr(0, prefix.size()) == prefix) {
+ return str.substr(prefix.size());
+ }
+ return str;
+}
+
/**
* Join a list of items
*
diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py
--- a/test/functional/test_framework/test_node.py
+++ b/test/functional/test_framework/test_node.py
@@ -107,6 +107,7 @@
"-datadir=" + self.datadir,
"-logtimemicros",
"-logthreadnames",
+ "-logsourcelocations",
"-debug",
"-debugexclude=libevent",
"-debugexclude=leveldb",

File Metadata

Mime Type
text/plain
Expires
Sat, Apr 26, 12:10 (2 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5571692
Default Alt Text
D10951.id32120.diff (6 KB)

Event Timeline