mirror of
				https://github.com/Zygo/bees.git
				synced 2025-11-03 19:50:34 +01:00 
			
		
		
		
	crucible: Allow setting a relative path option for name_fd()
This commit adds an option to store a relative path in prepartion for more human-friendly log output. Signed-off-by: Kai Krakow <kai@kaishome.de>
This commit is contained in:
		@@ -57,6 +57,10 @@ namespace crucible {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        typedef ResourceHandle<int, IOHandle> Fd;
 | 
					        typedef ResourceHandle<int, IOHandle> Fd;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						static string __relative_path;
 | 
				
			||||||
 | 
						void set_relative_path(string path);
 | 
				
			||||||
 | 
						string relative_path();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Functions named "foo_or_die" throw exceptions on failure.
 | 
						// Functions named "foo_or_die" throw exceptions on failure.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Attempt to open the file with the given mode
 | 
						// Attempt to open the file with the given mode
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								lib/fd.cc
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								lib/fd.cc
									
									
									
									
									
								
							@@ -527,6 +527,22 @@ namespace crucible {
 | 
				
			|||||||
		THROW_ERROR(runtime_error, "readlink: maximum buffer size exceeded");
 | 
							THROW_ERROR(runtime_error, "readlink: maximum buffer size exceeded");
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						string
 | 
				
			||||||
 | 
						relative_path()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return __relative_path;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void
 | 
				
			||||||
 | 
						set_relative_path(string path)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							path = path + "/";
 | 
				
			||||||
 | 
							for (string::size_type i = path.find("//"); i != string::npos; i = path.find("//")) {
 | 
				
			||||||
 | 
								path.erase(i, 1);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							__relative_path = path;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Turn a FD into a human-recognizable filename OR an error message.
 | 
						// Turn a FD into a human-recognizable filename OR an error message.
 | 
				
			||||||
	string
 | 
						string
 | 
				
			||||||
	name_fd(int fd)
 | 
						name_fd(int fd)
 | 
				
			||||||
@@ -534,7 +550,12 @@ namespace crucible {
 | 
				
			|||||||
		try {
 | 
							try {
 | 
				
			||||||
			ostringstream oss;
 | 
								ostringstream oss;
 | 
				
			||||||
			oss << "/proc/self/fd/" << fd;
 | 
								oss << "/proc/self/fd/" << fd;
 | 
				
			||||||
			return readlink_or_die(oss.str());
 | 
								string path = readlink_or_die(oss.str());
 | 
				
			||||||
 | 
								if (!__relative_path.empty() && 0 == path.find(__relative_path))
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									path.erase(0, __relative_path.length());
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return path;
 | 
				
			||||||
		} catch (exception &e) {
 | 
							} catch (exception &e) {
 | 
				
			||||||
			return string(e.what());
 | 
								return string(e.what());
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user