From 0c67d4c8447b999f456d64addc6106bb318e87ee Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Thu, 11 Apr 2019 22:14:26 +0300 Subject: [PATCH] logging: strip full paths from __FILE__ at compile-time --- src/laminar.cpp | 2 +- src/log.h | 47 ++++++++++++++++++++++++++++++++++++++++------- src/run.cpp | 4 ++-- src/server.cpp | 2 +- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/laminar.cpp b/src/laminar.cpp index 6e712d9..166fc0d 100644 --- a/src/laminar.cpp +++ b/src/laminar.cpp @@ -75,7 +75,7 @@ Laminar::Laminar(const char *home) : homePath(kj::Path::parse(&home[1])), fsHome(kj::newDiskFilesystem()->getRoot().openSubdir(homePath, kj::WriteMode::MODIFY)) { - KJ_ASSERT(home[0] == '/'); + LASSERT(home[0] == '/'); archiveUrl = ARCHIVE_URL_DEFAULT; if(char* envArchive = getenv("LAMINAR_ARCHIVE_URL")) { diff --git a/src/log.h b/src/log.h index 8cc3a2c..b35b812 100644 --- a/src/log.h +++ b/src/log.h @@ -1,5 +1,5 @@ /// -/// Copyright 2015 Oliver Giles +/// Copyright 2015-2019 Oliver Giles /// /// This file is part of Laminar /// @@ -20,18 +20,51 @@ #define LAMINAR_LOG_H_ #include +#include -// Simple override to prevent full paths to source files from -// appearing in log messages. Assumes / is the path separator. -// @see kj/debug.h +// Prevent full file paths from appearing in log messages. With compiler +// optimization, this compile-time method should completely prevent the +// paths from being encoded into the binary at all. Assumes / is the +// path separator. +namespace _ { + constexpr const char* static_basename_impl(const char* b, const char* t) { + return *t == '\0' ? b : static_basename_impl(*t == '/' ? t+1 : b, t+1); + } + constexpr const char* static_basename(const char* p) { + return static_basename_impl(p, p); + } + constexpr int static_strlen(const char* s) { + return *s == '\0' ? 0 : static_strlen(s + 1) + 1; + } + template + static constexpr decltype(auto) static_alloc_str_impl(const char* str, std::integer_sequence) { + return (struct {char buf[N];}) {str[I]...}; + } + template + static constexpr decltype(auto) static_alloc_str(const char* str) { + return static_alloc_str_impl(str, std::make_integer_sequence()); + } +} +#define __FILE_BASE__ (_::static_alloc_str<_::static_strlen(_::static_basename(__FILE__))>(_::static_basename(__FILE__)).buf) -#define _LBASENAME(path) strrchr(path, '/') ? strrchr(path, '/') + 1 : path +// Provide alternative implementations to those from kj/debug.h which +// use __FILE__ directly and thus cause the full path to be encoded in +// the final binary #define LLOG(severity, ...) \ if (!::kj::_::Debug::shouldLog(::kj::_::Debug::Severity::severity)) {} else \ - ::kj::_::Debug::log(_LBASENAME(__FILE__), __LINE__, ::kj::_::Debug::Severity::severity, \ - #__VA_ARGS__, __VA_ARGS__) + ::kj::_::Debug::log(__FILE_BASE__, __LINE__, \ + ::kj::_::Debug::Severity::severity, #__VA_ARGS__, __VA_ARGS__) +#define LASSERT(cond, ...) \ + if (KJ_LIKELY(cond)) {} else \ + for (::kj::_::Debug::Fault f(__FILE_BASE__, __LINE__, \ + ::kj::Exception::Type::FAILED, #cond, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) + +#define LSYSCALL(call, ...) \ + if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, false)) {} else \ + for (::kj::_::Debug::Fault f(__FILE_BASE__, __LINE__, \ + _kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #endif // LAMINAR_LOG_H_ diff --git a/src/run.cpp b/src/run.cpp index f9d8eb6..4862652 100644 --- a/src/run.cpp +++ b/src/run.cpp @@ -82,7 +82,7 @@ bool Run::configure(uint buildNum, std::shared_ptr nd, const kj::Directory kj::Path rd{"run",name,std::to_string(buildNum)}; bool createWorkdir = true; KJ_IF_MAYBE(ls, fsHome.tryLstat(rd)) { - KJ_ASSERT(ls->type == kj::FsNode::Type::DIRECTORY); + LASSERT(ls->type == kj::FsNode::Type::DIRECTORY); LLOG(WARNING, "Working directory already exists, removing", rd.toString()); if(fsHome.tryRemove(rd) == false) { LLOG(WARNING, "Failed to remove working directory"); @@ -194,7 +194,7 @@ bool Run::step() { PATH.append(p); } - KJ_SYSCALL(chdir((rootPath/currentScript.cwd).toString(true).cStr())); + LSYSCALL(chdir((rootPath/currentScript.cwd).toString(true).cStr())); // conf file env vars for(kj::Path& file : env) { diff --git a/src/server.cpp b/src/server.cpp index b26507f..4b92bc8 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -286,7 +286,7 @@ private: }, [](kj::Exception&& e){ // server logs suggest early catching here avoids fatal exception later // TODO: reproduce in unit test - KJ_LOG(WARNING, e.getDescription()); + LLOG(WARNING, e.getDescription()); return kj::READY_NOW; }); }