Commit 7d5a40bb authored by Kenton Varda's avatar Kenton Varda

Make logging more readable by removing noisy prefixes on filenames.

parent 41773d2f
......@@ -182,6 +182,8 @@ public:
#endif
std::string fileLine(std::string file, int line) {
file = trimSourceFilename(file.c_str()).cStr();
file += ':';
char buffer[32];
sprintf(buffer, "%d", line);
......@@ -266,7 +268,7 @@ TEST(Debug, Exception) {
int line = __LINE__; Exception exception = KJ_EXCEPTION(DISCONNECTED, "foo", i);
EXPECT_EQ(Exception::Type::DISCONNECTED, exception.getType());
EXPECT_STREQ(__FILE__, exception.getFile());
EXPECT_TRUE(kj::StringPtr(__FILE__).endsWith(exception.getFile()));
EXPECT_EQ(line, exception.getLine());
EXPECT_EQ("foo; i = 123", exception.getDescription());
}
......
......@@ -268,7 +268,7 @@ static String makeDescriptionImpl(DescriptionStyle style, const char* code, int
void Debug::logInternal(const char* file, int line, LogSeverity severity, const char* macroArgs,
ArrayPtr<String> argValues) {
getExceptionCallback().logMessage(severity, file, line, 0,
getExceptionCallback().logMessage(severity, trimSourceFilename(file).cStr(), line, 0,
makeDescriptionImpl(LOG, nullptr, 0, macroArgs, argValues));
}
......
......@@ -27,6 +27,10 @@ namespace kj {
namespace _ { // private
namespace {
TEST(Exception, TrimSourceFilename) {
EXPECT_EQ(trimSourceFilename(__FILE__), "kj/exception-test.c++");
}
TEST(Exception, RunCatchingExceptions) {
bool recovered = false;
Maybe<Exception> e = kj::runCatchingExceptions([&]() {
......
......@@ -207,6 +207,28 @@ void printStackTraceOnCrash() {
#endif
}
kj::StringPtr trimSourceFilename(kj::StringPtr filename) {
// Removes noisy prefixes from source code file name.
static constexpr const char* PREFIXES[] = {
"../",
"/ekam-provider/canonical/",
"/ekam-provider/c++header/",
"src/",
"tmp/"
};
retry:
for (const char* prefix: PREFIXES) {
if (filename.startsWith(prefix)) {
filename = filename.slice(strlen(prefix));
goto retry;
}
}
return filename;
}
StringPtr KJ_STRINGIFY(Exception::Type type) {
static const char* TYPE_STRINGS[] = {
"failed",
......@@ -253,12 +275,12 @@ String KJ_STRINGIFY(const Exception& e) {
}
Exception::Exception(Type type, const char* file, int line, String description) noexcept
: file(file), line(line), type(type), description(mv(description)) {
: file(trimSourceFilename(file).cStr()), line(line), type(type), description(mv(description)) {
traceCount = kj::getStackTrace(trace).size();
}
Exception::Exception(Type type, String file, int line, String description) noexcept
: ownFile(kj::mv(file)), file(ownFile.cStr()), line(line), type(type),
: ownFile(kj::mv(file)), file(trimSourceFilename(ownFile).cStr()), line(line), type(type),
description(mv(description)) {
traceCount = kj::getStackTrace(trace).size();
}
......
......@@ -307,6 +307,10 @@ void printStackTraceOnCrash();
// a stack trace. You should call this as early as possible on program startup. Programs using
// KJ_MAIN get this automatically.
kj::StringPtr trimSourceFilename(kj::StringPtr filename);
// Given a source code file name, trim off noisy prefixes like "src/" or
// "/ekam-provider/canonical/".
} // namespace kj
#endif // KJ_EXCEPTION_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment