From f057a3e9116539cd96916cfd8fb790cf6a87de79 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 18 Oct 2015 15:34:14 +0200 Subject: Implement support for sending logs to an arbitrary file The `CHANGE_LOG_TARGET` environment variable enables passing the path of an arbitrary target file to the preloaded library. This may be used to e.g. print the log to a separate `cat` instance and is necessary for logging change events without altering the output of the wrapped process. --- change_log.cc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/change_log.cc b/change_log.cc index 03c4343..58ba0db 100644 --- a/change_log.cc +++ b/change_log.cc @@ -7,14 +7,51 @@ #include #include +#include + #include #include #include #include #include +#include + +struct FileDescriptorGuard { + FileDescriptorGuard(const std::string& path) { + this->fd = open(path.c_str(), O_WRONLY | O_APPEND); + + if ( !this->fd ) { + this->fd = STDERR_FILENO; + } + } + + ~FileDescriptorGuard() { + close(this->fd); + } + + int fd; +}; + +class LogGuard { + public: + LogGuard(const int target_fd): + buffer(target_fd, std::ios::out), + stream(&this->buffer) { } + + void append(const std::string& msg) { + this->stream << msg << std::endl; + } + + private: + __gnu_cxx::stdio_filebuf buffer; + std::ostream stream; +}; static std::unordered_set tracked_files; +static std::unique_ptr fd_guard; +static std::unique_ptr log; + template std::function get_real_function( const std::string& symbol_name) { @@ -27,6 +64,16 @@ std::function get_real_function( return std::function(real_function); } +void init() __attribute__ ((constructor)); +void init() { + if ( getenv("CHANGE_LOG_TARGET") != NULL ) { + fd_guard = std::make_unique(getenv("CHANGE_LOG_TARGET")); + log = std::make_unique(fd_guard->fd); + } else { + log = std::make_unique(STDERR_FILENO); + } +} + void exit(int status) { get_real_function("exit")(status); } @@ -71,7 +118,7 @@ ssize_t write(int fd, const void* buffer, size_t count) { if ( !is_tracked_file(file_name) ) { track_file(file_name); - std::cerr << "wrote to '" << file_name << "'" << std::endl; + log->append("wrote to '" + file_name + "'"); } } @@ -81,7 +128,7 @@ ssize_t write(int fd, const void* buffer, size_t count) { int rename(const char* old_path, const char* new_path) { static auto real_rename = get_real_function("rename"); - std::cerr << "renamed '" << old_path << "' to '" << new_path << "'" << std::endl; + log->append("renamed '" + std::string(old_path) + "' to '" + std::string(new_path) + "'"); return real_rename(old_path, new_path); } @@ -89,7 +136,7 @@ int rename(const char* old_path, const char* new_path) { int rmdir(const char* path) { static auto real_rmdir = get_real_function("rmdir"); - std::cerr << "removed directory '" << path << "'" << std::endl; + log->append("removed directory '" + std::string(path) + "'"); return real_rmdir(path); } @@ -97,7 +144,7 @@ int rmdir(const char* path) { int unlink(const char* path) { static auto real_unlink = get_real_function("unlink"); - std::cerr << "removed '" << path << "'" << std::endl; + log->append("removed '" + std::string(path) + "'"); return real_unlink(path); } @@ -106,9 +153,9 @@ int unlinkat(int dirfd, const char* path, int flags) { static auto real_unlinkat = get_real_function("unlinkat"); if ( dirfd == AT_FDCWD ) { - std::cerr << "removed '" << path << "'" << std::endl; + log->append("removed '" + std::string(path) + "'"); } else { - std::cerr << "removed '" << get_file_name(dirfd) << path << "'" << std::endl; + log->append("removed '" + get_file_name(dirfd) + path + "'"); } return real_unlinkat(dirfd, path, flags); -- cgit v1.2.3