mirror of
				https://github.com/ohwgiles/laminar.git
				synced 2025-06-13 12:54:29 +00:00 
			
		
		
		
	logging: strip full paths from __FILE__ at compile-time
This commit is contained in:
		
							parent
							
								
									bcb8b438f3
								
							
						
					
					
						commit
						0c67d4c844
					
				| @ -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")) { | ||||
|  | ||||
							
								
								
									
										47
									
								
								src/log.h
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								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 <kj/debug.h> | ||||
| #include <utility> | ||||
| 
 | ||||
| // 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<int N, int...I> | ||||
| 	static constexpr decltype(auto) static_alloc_str_impl(const char* str, std::integer_sequence<int, I...>) {  | ||||
| 		return (struct {char buf[N];}) {str[I]...}; | ||||
| 	} | ||||
| 	template<int N> | ||||
| 	static constexpr decltype(auto) static_alloc_str(const char* str) { | ||||
| 		return static_alloc_str_impl<N>(str, std::make_integer_sequence<int, N>()); | ||||
| 	} | ||||
| } | ||||
| #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_
 | ||||
| 
 | ||||
|  | ||||
| @ -82,7 +82,7 @@ bool Run::configure(uint buildNum, std::shared_ptr<Node> 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) { | ||||
|  | ||||
| @ -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; | ||||
|         }); | ||||
|     } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user