From e9f968582664c28d409a87f7f9d3740dd889e20f Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 11:04:47 +0200 Subject: [PATCH 01/26] Began porting SERVER logger to new infrastructure --- capio/common/env.hpp | 2 +- capio/common/logger.hpp | 233 ++++-------------- capio/posix/libcapio_posix.cpp | 2 + capio/posix/utils/PosixLogger.hpp | 128 ++++++++++ capio/posix/utils/cache.hpp | 188 -------------- capio/server/capio_server.cpp | 3 +- .../include/client-manager/client_manager.hpp | 2 + capio/server/include/remote/backend.hpp | 3 +- capio/server/include/remote/handlers/read.hpp | 1 + capio/server/include/remote/handlers/stat.hpp | 1 + capio/server/include/storage/capio_file.hpp | 2 + capio/server/include/utils/ServerLogger.hpp | 74 ++++++ capio/server/include/utils/cli_parser.hpp | 1 + capio/server/include/utils/common.hpp | 1 + capio/server/include/utils/env.hpp | 1 + capio/server/include/utils/location.hpp | 6 +- capio/server/include/utils/signals.hpp | 1 + capio/server/src/backend.cpp | 1 + capio/server/src/cli_parser.cpp | 33 +-- capio/server/src/client_manager.cpp | 4 +- capio/server/src/mpi_backend.cpp | 2 + capio/server/src/none_backend.cpp | 2 + capio/server/src/remote_request.cpp | 2 + capio/server/src/storage_manager.cpp | 5 +- 24 files changed, 281 insertions(+), 417 deletions(-) create mode 100644 capio/posix/utils/PosixLogger.hpp delete mode 100644 capio/posix/utils/cache.hpp create mode 100644 capio/server/include/utils/ServerLogger.hpp diff --git a/capio/common/env.hpp b/capio/common/env.hpp index cbfdc05aa..0e7313772 100644 --- a/capio/common/env.hpp +++ b/capio/common/env.hpp @@ -10,7 +10,7 @@ #include #include "common/syscall.hpp" -#include "logger.hpp" + // TODO: remove forward declaration of function by splitting into header and impl. capio/common inline bool is_forbidden_path(const std::string_view &path); diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index e507e5959..4616c44cb 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -6,10 +6,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include "constants.hpp" @@ -37,18 +39,6 @@ inline void raise_termination(const bool raise_exception, const std::string &mes #include "syscallnames.h" #endif -#ifndef __CAPIO_POSIX -#include -// FIXME: Remove the inline specifier by splitting into header and source code -inline thread_local std::ofstream logfile; // if building for server, self contained logfile -inline std::string log_master_dir_name = CAPIO_DEFAULT_LOG_FOLDER; -inline std::string logfile_prefix = CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX; -#else -inline thread_local bool logfileOpen = false; -inline thread_local int logfileFD = -1; -inline thread_local char logfile_path[PATH_MAX]{'\0'}; -#endif - // FIXME: Remove the inline specifier by splitting into header and source code inline thread_local int current_log_level = 0; @@ -63,97 +53,6 @@ inline thread_local bool logging_syscall = false; // FIXME: Remove the inline specifier by splitting into header and source code inline int CAPIO_LOG_LEVEL = CAPIO_MAX_LOG_LEVEL; -#ifndef __CAPIO_POSIX -inline auto open_server_logfile() { - auto hostname = new char[HOST_NAME_MAX]; - gethostname(hostname, HOST_NAME_MAX); - - if (log_master_dir_name.empty()) { - log_master_dir_name = CAPIO_DEFAULT_LOG_FOLDER; - } - - const std::filesystem::path output_folder = - std::string{log_master_dir_name + "/server/" + hostname}; - - std::filesystem::create_directories(output_folder); - - const std::filesystem::path logfile_name = output_folder.string() + "/" + logfile_prefix + - std::to_string(capio_syscall(SYS_gettid)) + ".log"; - - logfile.open(logfile_name, std::ofstream::out); - delete[] hostname; - - return logfile_name; -} -#else - -inline auto get_hostname() { - static char *hostname_prefix = nullptr; - if (hostname_prefix == nullptr) { - hostname_prefix = new char[HOST_NAME_MAX]; - gethostname(hostname_prefix, HOST_NAME_MAX); - } - return hostname_prefix; -} - -inline auto get_log_dir() { - static char *posix_log_master_dir_name = nullptr; - if (posix_log_master_dir_name == nullptr) { - posix_log_master_dir_name = std::getenv("CAPIO_LOG_DIR"); - if (posix_log_master_dir_name == nullptr) { - posix_log_master_dir_name = new char[strlen(CAPIO_DEFAULT_LOG_FOLDER)]; - strcpy(posix_log_master_dir_name, CAPIO_DEFAULT_LOG_FOLDER); - } - } - return posix_log_master_dir_name; -} - -inline auto get_log_prefix() { - static char *posix_logfile_prefix = nullptr; - if (posix_logfile_prefix == nullptr) { - posix_logfile_prefix = std::getenv("CAPIO_LOG_PREFIX"); - if (posix_logfile_prefix == nullptr) { - posix_logfile_prefix = new char[strlen(CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX)]; - strcpy(posix_logfile_prefix, CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX); - } - } - return posix_logfile_prefix; -} - -inline auto get_posix_log_dir() { - static char *posix_log_dir_path = nullptr; - if (posix_log_dir_path == nullptr) { - // allocate space for a path in the following structure (including 10 digits for thread id - // max - // log_master_dir_name/posix/hostname/logfile_prefix_.log - auto len = strlen(get_log_dir()) + 7; - posix_log_dir_path = new char[len]{0}; - sprintf(posix_log_dir_path, "%s/posix", get_log_dir()); - } - return posix_log_dir_path; -} - -inline auto get_host_log_dir() { - static char *host_log_dir_path = nullptr; - if (host_log_dir_path == nullptr) { - // allocate space for a path in the following structure (including 10 digits for thread id - // max - // log_master_dir_name/posix/hostname/logfile_prefix_.log - auto len = strlen(get_posix_log_dir()) + HOST_NAME_MAX; - host_log_dir_path = new char[len]{0}; - sprintf(host_log_dir_path, "%s/%s", get_posix_log_dir(), get_hostname()); - } - return host_log_dir_path; -} - -inline void setup_posix_log_filename() { - if (logfile_path[0] == '\0') { - sprintf(logfile_path, "%s/%s%ld.log", get_host_log_dir(), get_log_prefix(), - capio_syscall(SYS_gettid)); - } -} -#endif - inline long long current_time_in_millis() { timespec ts{}; static long long start_time = -1; @@ -166,25 +65,9 @@ inline long long current_time_in_millis() { return time_now - start_time; } -inline void log_write_to(char *buffer, size_t bufflen) { -#ifdef __CAPIO_POSIX - if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - capio_syscall(SYS_write, logfileFD, buffer, bufflen); - capio_syscall(SYS_write, logfileFD, "\n", 1); - } -#else - if (current_log_level < CAPIO_LOG_LEVEL || CAPIO_LOG_LEVEL < 0) { - logfile << buffer << std::endl; - logfile.flush(); - } - -#endif -} - /** * @brief Class used to suspend the logging capabilities of CAPIO, by setting the logging_syscall * flag to false at instantiation, and restarting the logging at destruction - * */ class SyscallLoggingSuspender { public: @@ -194,50 +77,31 @@ class SyscallLoggingSuspender { /** * @brief Class that provides logging capabilities to CAPIO. - * When compiled with the CAPIO intercepting library, it uses - * direct POSIX system calls. Otherwise, it relies on the STL. * + * Parameterised on a LogWriteAdapter so that the I/O strategy (POSIX + * syscalls vs. STL streams vs. any custom backend) is a compile-time + * choice with no virtual dispatch overhead. + * + * The default adapter (@ref DefaultLogWriteAdapter) mirrors the behaviour + * of the old monolithic Logger class for both the server and POSIX builds. */ -class Logger { - private: +template class TemplateLogger { char invoker[256]{0}; char file[256]{0}; char format[CAPIO_LOG_MAX_MSG_LEN]{0}; + unsigned int line{0}; + long int tid{0}; + + Adapter adapter; public: - inline Logger(const char invoker[], const char file[], int line, long int tid, - const char *message, ...) { -#ifndef __CAPIO_POSIX - if (!logfile.is_open()) { - // NOTE: should never get to this point as capio_server opens up the log file while - // parsing command line arguments. This is only for failsafe purpose - open_server_logfile(); - } -#else - if (!logfileOpen) { - setup_posix_log_filename(); - current_log_level = - 0; // reset log level after clone, avoiding propagation to child threads - - capio_syscall(SYS_mkdirat, AT_FDCWD, get_log_dir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, get_posix_log_dir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, get_host_log_dir(), 0755); - - logfileFD = capio_syscall(SYS_openat, AT_FDCWD, logfile_path, - O_CREAT | O_WRONLY | O_TRUNC, 0644); - - if (logfileFD == -1) { - capio_syscall(SYS_write, fileno(stdout), - "Err fopen file: ", strlen("Err fopen file: ")); - capio_syscall(SYS_write, fileno(stdout), logfile_path, strlen(logfile_path)); - capio_syscall(SYS_write, fileno(stdout), " ", 1); - capio_syscall(SYS_write, fileno(stdout), strerror(errno), strlen(strerror(errno))); - capio_syscall(SYS_write, fileno(stdout), "\n", 1); - exit(EXIT_FAILURE); - } - logfileOpen = true; - } -#endif + TemplateLogger(const char invoker[], const char file[], unsigned int line, long int tid, + const char *message, ...) { + + adapter.openLogFile(); + + this->tid = tid; + this->line = line; strncpy(this->invoker, invoker, sizeof(this->invoker)); strncpy(this->file, file, sizeof(this->file)); @@ -245,7 +109,6 @@ class Logger { sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); const size_t pre_msg_len = strlen(format); - strcpy(format + pre_msg_len, message); va_start(argp, message); @@ -266,17 +129,17 @@ class Logger { SYS_mmap, nullptr, 50, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); sprintf(buf1, CAPIO_LOG_POSIX_SYSCALL_START, sys_num_to_string(syscallNumber).c_str(), syscallNumber); - log_write_to(buf1, strlen(buf1)); + adapter.writeRaw(buf1, strlen(buf1)); capio_syscall(SYS_munmap, buf1, 50); } #endif - int size = vsnprintf(nullptr, 0U, format, argp); - auto buf = reinterpret_cast(capio_syscall(SYS_mmap, nullptr, size + 1, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + const int size = vsnprintf(nullptr, 0U, format, argp); + auto buf = reinterpret_cast(capio_syscall(SYS_mmap, nullptr, size + 1, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); vsnprintf(buf, size + 1, format, argpc); - log_write_to(buf, strlen(buf)); + adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); va_end(argp); va_end(argpc); @@ -284,41 +147,34 @@ class Logger { current_log_level++; } - Logger(const Logger &) = delete; - Logger &operator=(const Logger &) = delete; + TemplateLogger(const TemplateLogger &) = delete; + TemplateLogger &operator=(const TemplateLogger &) = delete; - inline ~Logger() { + ~TemplateLogger() { current_log_level--; sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); - size_t pre_msg_len = strlen(format); + const size_t pre_msg_len = strlen(format); strcpy(format + pre_msg_len, "returned"); - log_write_to(format, strlen(format)); -#ifdef __CAPIO_POSIX + adapter.writeRaw(format, strlen(format)); + if (current_log_level == 0 && logging_syscall) { - log_write_to(const_cast(CAPIO_LOG_POSIX_SYSCALL_END), - strlen(CAPIO_LOG_POSIX_SYSCALL_END)); + adapter.writeSyscallEnd(); } -#endif } - inline void log(const char *message, ...) { + void log(const char *message, ...) { va_list argp, argpc; sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); const size_t pre_msg_len = strlen(format); - strcpy(format + pre_msg_len, message); -#ifndef __CAPIO_POSIX - // if the log message to serve a new request or concludes, add new spaces - if (strcmp(invoker, "capio_server") == 0 && - (strcmp(CAPIO_LOG_SERVER_REQUEST_START, message) == 0 || - strcmp(CAPIO_LOG_SERVER_REQUEST_END, message) == 0)) { - logfile << message << std::endl; + // Delegate the server request start/end special case to the adapter + if (adapter.isServerInvoker(this->invoker, message)) { + adapter.writeRaw(format, strlen(format)); return; } -#endif va_start(argp, message); va_copy(argpc, argp); @@ -327,24 +183,27 @@ class Logger { capio_syscall(SYS_mmap, nullptr, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); vsnprintf(buf, size + 1, format, argpc); - log_write_to(buf, strlen(buf)); + adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); va_end(argp); va_end(argpc); capio_syscall(SYS_munmap, buf, size); } + + std::string getLogFileName() { return adapter.getLogFileName(); } }; +// --------------------------------------------------------------------------- +// Macros — identical surface to the old ones; Logger is now a template +// --------------------------------------------------------------------------- #ifdef CAPIO_LOG #define ERR_EXIT_EXCEPT_CHOICE(raise_exception, message, ...) \ log.log(message, ##__VA_ARGS__); \ if (!continue_on_error) { \ - if (!continue_on_error) { \ - char err_msg[CAPIO_LOG_MAX_MSG_LEN]; \ - snprintf(err_msg, CAPIO_LOG_MAX_MSG_LEN, message, ##__VA_ARGS__); \ - raise_termination(raise_exception, err_msg); \ - } \ + char err_msg[CAPIO_LOG_MAX_MSG_LEN]; \ + snprintf(err_msg, CAPIO_LOG_MAX_MSG_LEN, message, ##__VA_ARGS__); \ + raise_termination(raise_exception, err_msg); \ } #define ERR_EXIT(message, ...) ERR_EXIT_EXCEPT_CHOICE(true, message, ##__VA_ARGS__) diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 3aedd1a46..d6bf64396 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -7,6 +7,8 @@ #include #include +#include "utils/PosixLogger.hpp" + #include "common/syscall.hpp" #include "utils/clone.hpp" diff --git a/capio/posix/utils/PosixLogger.hpp b/capio/posix/utils/PosixLogger.hpp new file mode 100644 index 000000000..d3ea59f03 --- /dev/null +++ b/capio/posix/utils/PosixLogger.hpp @@ -0,0 +1,128 @@ +#ifndef CAPIO_POSIXLOGGER_HPP +#define CAPIO_POSIXLOGGER_HPP + +#include "common/logger.hpp" + + +struct PosixLogWriteAdapter { + private: + bool fileOpen{false}; + int fileFD{-1}; + char filePath[PATH_MAX]{'\0'}; + + static const char *getHostname() { + static char hostname[HOST_NAME_MAX]{'\0'}; + if (hostname[0] == '\0') { + gethostname(hostname, HOST_NAME_MAX); + } + return hostname; + } + + static const char *getLogDir() { + static char *dir = nullptr; + if (dir == nullptr) { + dir = std::getenv("CAPIO_LOG_DIR"); + if (dir == nullptr) { + dir = new char[strlen(CAPIO_DEFAULT_LOG_FOLDER) + 1]; + strcpy(dir, CAPIO_DEFAULT_LOG_FOLDER); + } + } + return dir; + } + + static const char *getLogPrefix() { + static char *prefix = nullptr; + if (prefix == nullptr) { + prefix = std::getenv("CAPIO_LOG_PREFIX"); + if (prefix == nullptr) { + prefix = new char[strlen(CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX) + 1]; + strcpy(prefix, CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX); + } + } + return prefix; + } + + static const char *getPosixLogDir() { + static char *dir = nullptr; + if (dir == nullptr) { + const char *base = getLogDir(); + dir = new char[strlen(base) + 7]{0}; // "/posix\0" + sprintf(dir, "%s/posix", base); + } + return dir; + } + + static const char *getHostLogDir() { + static char *dir = nullptr; + if (dir == nullptr) { + const char *posixDir = getPosixLogDir(); + dir = new char[strlen(posixDir) + HOST_NAME_MAX]{0}; + sprintf(dir, "%s/%s", posixDir, getHostname()); + } + return dir; + } + + void setupLogFilename() { + if (filePath[0] == '\0') { + sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), + capio_syscall(SYS_gettid)); + } + } + + // ---- write helper ------------------------------------------------------ + + void writeToFD(const char *buf, size_t len) const { + capio_syscall(SYS_write, fileFD, buf, len); + capio_syscall(SYS_write, fileFD, "\n", 1); + } + + public: + [[nodiscard]] bool isFileOpened() const { return fileOpen; } + + void openLogFile() { + setupLogFilename(); + current_log_level = 0; // reset log level after clone, avoiding propagation to child threads + + capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); + capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); + capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); + + fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, O_CREAT | O_WRONLY | O_TRUNC, 0644); + + if (fileFD == -1) { + capio_syscall(SYS_write, fileno(stdout), + "Err fopen file: ", strlen("Err fopen file: ")); + capio_syscall(SYS_write, fileno(stdout), filePath, strlen(filePath)); + capio_syscall(SYS_write, fileno(stdout), " ", 1); + capio_syscall(SYS_write, fileno(stdout), strerror(errno), strlen(strerror(errno))); + capio_syscall(SYS_write, fileno(stdout), "\n", 1); + exit(EXIT_FAILURE); + } + fileOpen = true; + } + + void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, + long int /*tid*/, const char *buf, size_t len) const { + if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { + writeToFD(buf, len); + } + } + + void writeRaw(const char *buf, size_t len) const { + if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { + writeToFD(buf, len); + } + } + + void writeSyscallEnd() const { + writeRaw(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); + } + + static bool isServerInvoker(const char * /*invoker*/, const char * /*message*/) { + return false; + } +}; + +using Logger = TemplateLogger; + +#endif // CAPIO_POSIXLOGGER_HPP diff --git a/capio/posix/utils/cache.hpp b/capio/posix/utils/cache.hpp deleted file mode 100644 index 597d644a5..000000000 --- a/capio/posix/utils/cache.hpp +++ /dev/null @@ -1,188 +0,0 @@ -#ifndef CAPIO_SERVER_UTILS_CACHE -#define CAPIO_SERVER_UTILS_CACHE - -#include "common/dirent.hpp" -#include "common/queue.hpp" - -#include "requests.hpp" - -class ReadCache { - private: - char *_cache; - long _tid; - int _last_fd; - off64_t _max_line_size, _actual_size, _cache_offset; - SPSCQueue _queue; - - inline void _read(void *buffer, off64_t count) { - START_LOG(capio_syscall(SYS_gettid), "call(count=%ld)", count); - - if (count > 0) { - memcpy(buffer, _cache + _cache_offset, count); - LOG("Read %ld. adding it to _cache_offset of value %ld", count, _cache_offset); - _cache_offset += count; - } - } - - public: - ReadCache(long tid, off64_t lines, off64_t line_size, const std::string &workflow_name) - : _cache(nullptr), _tid(tid), _last_fd(-1), _max_line_size(line_size), _actual_size(0), - _cache_offset(0), - _queue(SHM_SPSC_PREFIX_READ + std::to_string(tid), lines, line_size, workflow_name) {} - - inline void flush() { - START_LOG(capio_syscall(SYS_gettid), "call()"); - - if (_cache_offset != _actual_size) { - _actual_size = _cache_offset = 0; - seek_request(_last_fd, get_capio_fd_offset(_last_fd), _tid); - } - } - - inline off64_t read(int fd, void *buffer, off64_t count, bool is_getdents, bool is64bit) { - START_LOG(capio_syscall(SYS_gettid), "call(fd=%d, count=%ld, is_getdents=%s, is64bit=%s)", - fd, count, is_getdents ? "true" : "false", is64bit ? "true" : "false"); - - if (_last_fd != fd) { - LOG("changed fd from %d to %d: flushing", _last_fd, fd); - flush(); - _last_fd = fd; - } - - off64_t remaining_bytes = _actual_size - _cache_offset; - off64_t file_offset = get_capio_fd_offset(fd); - off64_t bytes_read; - - if (is_getdents) { - auto dirent_size = static_cast(sizeof(linux_dirent64)); - count = (count / dirent_size) * dirent_size; - } - - auto read_size = count - remaining_bytes; - LOG("Read() will need to read %ld bytes", read_size); - - if (count <= remaining_bytes) { - LOG("count %ld <= remaining_bytes %ld", count, remaining_bytes); - _read(buffer, count); - bytes_read = count; - } else { - LOG("count %ld > remaining_bytes %ld", count, remaining_bytes); - _read(buffer, remaining_bytes); - buffer = reinterpret_cast(buffer) + remaining_bytes; - - // NOTE: if getdents send a request for exactly the correct amount of data. - if (read_size > _max_line_size || is_getdents) { - LOG("count - remaining_bytes %ld > _max_line_size %ld", read_size, _max_line_size); - LOG("Reading exactly requested size"); - off64_t end_of_read = is_getdents ? getdents_request(fd, read_size, is64bit, _tid) - : read_request(fd, read_size, _tid); - bytes_read = end_of_read - file_offset; - _queue.read(reinterpret_cast(buffer), bytes_read); - } else { - LOG("count - remaining_bytes %ld <= _max_line_size %ld", read_size, _max_line_size); - LOG("Reading more to use pre fetching and caching"); - off64_t end_of_read = is_getdents - ? getdents_request(fd, _max_line_size, is64bit, _tid) - : read_request(fd, _max_line_size, _tid); - LOG("request return value is %ld", end_of_read); - _actual_size = end_of_read - file_offset - remaining_bytes; - LOG("ReaderCache actual size, after requested read is: %ld bytes", _actual_size); - _cache_offset = 0; - if (_actual_size > 0) { - LOG("Fetching data from shm _queue"); - _cache = _queue.fetch(); - } - if (read_size < _actual_size) { - LOG("count - remaining_bytes %ld < _actual_size %ld", read_size, _actual_size); - _read(buffer, read_size); - bytes_read = count; - } else { - LOG("count - remaining_bytes %ld >= _actual_size %ld", read_size, _actual_size); - _read(buffer, _actual_size); - bytes_read = remaining_bytes + _actual_size; - } - } - } - LOG("%ld bytes have been read. setting fd offset to %ld", bytes_read, - file_offset + bytes_read); - set_capio_fd_offset(fd, file_offset + bytes_read); - return bytes_read; - } -}; - -class WriteCache { - private: - char *_cache; - long _tid; - int _fd; - off64_t _max_line_size, _actual_size; - SPSCQueue _queue; - - inline void _write(off64_t count, const void *buffer) { - START_LOG(capio_syscall(SYS_gettid), "call(count=%ld)", count); - - if (count > 0) { - if (_cache == nullptr) { - _cache = _queue.reserve(); - } - memcpy(_cache + _actual_size, buffer, count); - _actual_size += count; - if (_actual_size == _max_line_size) { - flush(); - } - } - } - - public: - WriteCache(long tid, off64_t lines, off64_t line_size, const std::string &workflow_name) - : _cache(nullptr), _tid(tid), _fd(-1), _max_line_size(line_size), _actual_size(0), - _queue(SHM_SPSC_PREFIX_WRITE + std::to_string(tid), lines, line_size, workflow_name) {} - - inline void flush() { - START_LOG(capio_syscall(SYS_gettid), "call()"); - - if (_actual_size != 0) { - write_request(_fd, _actual_size, _tid); - _cache = nullptr; - _actual_size = 0; - } - } - - inline void write(int fd, const void *buffer, off64_t count) { - START_LOG(capio_syscall(SYS_gettid), "call(fd=%d, buffer=0x%08x, count=%ld)", fd, buffer, - count); - - if (_fd != fd) { - LOG("changed fd from %d to %d: flushing", _fd, fd); - flush(); - _fd = fd; - } - - if (count <= _max_line_size - _actual_size) { - LOG("count %ld <= _max_line_size - _actual_size %ld", count, - _max_line_size - _actual_size); - _write(count, buffer); - } else { - LOG("count %ld > _max_line_size - _actual_size %ld", count, - _max_line_size - _actual_size); - flush(); - if (count - _actual_size > _max_line_size) { - LOG("count - _actual_size %ld > _max_line_size %ld", count - _actual_size, - _max_line_size); - write_request(_fd, count, _tid); - _queue.write(reinterpret_cast(buffer), count); - } else { - LOG("count - _actual_size %ld <= _max_line_size %ld", count - _actual_size, - _max_line_size); - _write(count, buffer); - } - } - - set_capio_fd_offset(fd, get_capio_fd_offset(fd) + count); - } -}; - -inline thread_local WriteCache *write_cache; -inline thread_local ReadCache *read_cache; - -#endif // CAPIO_SERVER_UTILS_CACHE diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index 403ec8f49..971ae6c5c 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -17,6 +17,8 @@ #include #include +#include "utils/ServerLogger.hpp" + #include "capiocl.hpp" #include "capiocl/engine.h" #include "capiocl/parser.h" @@ -24,7 +26,6 @@ #include "client-manager/client_manager.hpp" #include "common/env.hpp" -#include "common/logger.hpp" #include "common/requests.hpp" #include "common/semaphore.hpp" #include "remote/backend.hpp" diff --git a/capio/server/include/client-manager/client_manager.hpp b/capio/server/include/client-manager/client_manager.hpp index 3c91eb705..53af1c9cc 100644 --- a/capio/server/include/client-manager/client_manager.hpp +++ b/capio/server/include/client-manager/client_manager.hpp @@ -5,6 +5,8 @@ #include #include +#include "utils/ServerLogger.hpp" + #include "common/queue.hpp" /** * @brief Class to handle libcapio_posix clients applications diff --git a/capio/server/include/remote/backend.hpp b/capio/server/include/remote/backend.hpp index f1c827821..02622f0b5 100644 --- a/capio/server/include/remote/backend.hpp +++ b/capio/server/include/remote/backend.hpp @@ -2,8 +2,7 @@ #define CAPIO_SERVER_REMOTE_BACKEND_HPP #include #include - -#include "common/logger.hpp" +#include class RemoteRequest { char *_buf_recv; diff --git a/capio/server/include/remote/handlers/read.hpp b/capio/server/include/remote/handlers/read.hpp index d4a4f6cd2..19483f756 100644 --- a/capio/server/include/remote/handlers/read.hpp +++ b/capio/server/include/remote/handlers/read.hpp @@ -4,6 +4,7 @@ #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" +#include "utils/ServerLogger.hpp" extern StorageManager *storage_manager; diff --git a/capio/server/include/remote/handlers/stat.hpp b/capio/server/include/remote/handlers/stat.hpp index 80cd4cc47..9ea22e336 100644 --- a/capio/server/include/remote/handlers/stat.hpp +++ b/capio/server/include/remote/handlers/stat.hpp @@ -5,6 +5,7 @@ #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" +#include "utils/ServerLogger.hpp" extern StorageManager *storage_manager; extern ClientManager *client_manager; diff --git a/capio/server/include/storage/capio_file.hpp b/capio/server/include/storage/capio_file.hpp index aca4ff842..5605fbe97 100644 --- a/capio/server/include/storage/capio_file.hpp +++ b/capio/server/include/storage/capio_file.hpp @@ -10,6 +10,8 @@ #include #include +#include "utils/ServerLogger.hpp" + #include "common/queue.hpp" /** diff --git a/capio/server/include/utils/ServerLogger.hpp b/capio/server/include/utils/ServerLogger.hpp new file mode 100644 index 000000000..f4c9d8322 --- /dev/null +++ b/capio/server/include/utils/ServerLogger.hpp @@ -0,0 +1,74 @@ +#ifndef CAPIO_SERVERLOGGER_HPP +#define CAPIO_SERVERLOGGER_HPP + +#include + +struct ServerLogWriteAdapter { + private: + std::ofstream logfile; + std::string logMasterDirName; + std::string logfilePrefix; + std::string logFileName; + + void writeToStream(const char *buf) { + if (current_log_level < CAPIO_LOG_LEVEL || CAPIO_LOG_LEVEL < 0) { + logfile << buf << std::endl; + logfile.flush(); + } + } + + public: + explicit ServerLogWriteAdapter() { + if (const char *tmp = std::getenv("CAPIO_LOGGER_MASTER_DIR_NAME"); tmp == nullptr) { + logMasterDirName = CAPIO_DEFAULT_LOG_FOLDER; + } else { + logMasterDirName = tmp; + } + + if (const char *tmp = std::getenv("CAPIO_LOGGER_FILE_PREFIX"); tmp == nullptr) { + logfilePrefix = CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX; + } else { + logfilePrefix = tmp; + } + } + + void openLogFile() { + if (this->logfile.is_open()) { + return; + } + + char hostname[HOST_NAME_MAX]; + gethostname(hostname, HOST_NAME_MAX); + + const std::filesystem::path outputFolder{logMasterDirName + "/server/" + hostname}; + std::filesystem::create_directories(outputFolder); + + const std::filesystem::path logfileName = outputFolder.string() + "/" + logfilePrefix + + std::to_string(capio_syscall(SYS_gettid)) + + ".log"; + + logfile.open(logfileName, std::ofstream::out); + this->logFileName = logfileName; + } + + void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, + long int /*tid*/, const char *buf, size_t /*len*/) { + writeToStream(buf); + } + + void writeRaw(const char *buf, size_t /*len*/) { writeToStream(buf); } + + static void writeSyscallEnd() {} + + static bool isServerInvoker(const char *invoker, const char *message) { + return strcmp(invoker, "capio_server") == 0 && + (strcmp(CAPIO_LOG_SERVER_REQUEST_START, message) == 0 || + strcmp(CAPIO_LOG_SERVER_REQUEST_END, message) == 0); + } + + const std::string &getLogFileName() { return logFileName; } +}; + +using Logger = TemplateLogger; + +#endif // CAPIO_SERVERLOGGER_HPP diff --git a/capio/server/include/utils/cli_parser.hpp b/capio/server/include/utils/cli_parser.hpp index be588b994..73022d203 100644 --- a/capio/server/include/utils/cli_parser.hpp +++ b/capio/server/include/utils/cli_parser.hpp @@ -1,6 +1,7 @@ #ifndef CAPIO_CLI_PARSER_HPP #define CAPIO_CLI_PARSER_HPP #include +#include "utils/ServerLogger.hpp" struct CapioParsedConfig { std::string backend_name; diff --git a/capio/server/include/utils/common.hpp b/capio/server/include/utils/common.hpp index a0f90cdbb..95a9ca467 100644 --- a/capio/server/include/utils/common.hpp +++ b/capio/server/include/utils/common.hpp @@ -10,6 +10,7 @@ #include "common/dirent.hpp" #include "storage/capio_file.hpp" #include "storage/manager.hpp" +#include "utils/ServerLogger.hpp" extern ClientManager *client_manager; extern StorageManager *storage_manager; diff --git a/capio/server/include/utils/env.hpp b/capio/server/include/utils/env.hpp index ca41cb36d..1b0fa7af7 100644 --- a/capio/server/include/utils/env.hpp +++ b/capio/server/include/utils/env.hpp @@ -4,6 +4,7 @@ #include #include "common/constants.hpp" +#include "utils/ServerLogger.hpp" off64_t get_file_initial_size() { START_LOG(gettid(), "call()"); diff --git a/capio/server/include/utils/location.hpp b/capio/server/include/utils/location.hpp index 3a14af685..522321b77 100644 --- a/capio/server/include/utils/location.hpp +++ b/capio/server/include/utils/location.hpp @@ -1,11 +1,11 @@ #ifndef CAPIO_SERVER_UTILS_LOCATIONS_HPP #define CAPIO_SERVER_UTILS_LOCATIONS_HPP - -#include "remote/backend.hpp" - #include #include +#include "remote/backend.hpp" + +#include "utils/ServerLogger.hpp" #include "utils/types.hpp" extern Backend *backend; diff --git a/capio/server/include/utils/signals.hpp b/capio/server/include/utils/signals.hpp index 94660c0a1..1027ac4f2 100644 --- a/capio/server/include/utils/signals.hpp +++ b/capio/server/include/utils/signals.hpp @@ -5,6 +5,7 @@ #include "remote/backend.hpp" #include "server_println.hpp" +#include "utils/ServerLogger.hpp" #ifdef CAPIO_COVERAGE extern "C" void __gcov_dump(void); diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index 269893eae..f6da19b0a 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,5 +1,6 @@ #include "remote/backend.hpp" +#include "utils/ServerLogger.hpp" #include "utils/common.hpp" #include "utils/server_println.hpp" diff --git a/capio/server/src/cli_parser.cpp b/capio/server/src/cli_parser.cpp index a4f55cfa8..50f2b9693 100644 --- a/capio/server/src/cli_parser.cpp +++ b/capio/server/src/cli_parser.cpp @@ -9,7 +9,6 @@ CapioParsedConfig parseCLI(int argc, char **argv) { CapioParsedConfig capio_config; - Logger *log; args::ArgumentParser parser(CAPIO_SERVER_ARG_PARSER_PRE, CAPIO_SERVER_ARG_PARSER_EPILOGUE); parser.LongSeparator(" "); @@ -18,10 +17,6 @@ CapioParsedConfig parseCLI(int argc, char **argv) { args::Group arguments(parser, "Arguments"); args::HelpFlag help(arguments, "help", "Display this help menu", {'h', "help"}); - args::ValueFlag logfile_src(arguments, "filename", - CAPIO_SERVER_ARG_PARSER_LOGILE_OPT_HELP, {'l', "log"}); - args::ValueFlag logfile_folder( - arguments, "filename", CAPIO_SERVER_ARG_PARSER_LOGILE_DIR_OPT_HELP, {'d', "log-dir"}); args::ValueFlag resolve_prefix(arguments, "resolve-prefix", CAPIO_SERVER_ARG_PARSER_RESOLVE_PREFIX_OPT_HELP, {'r', "resolve-prefix"}); @@ -69,33 +64,9 @@ CapioParsedConfig parseCLI(int argc, char **argv) { #endif } - if (logfile_folder) { #ifdef CAPIO_LOG - log_master_dir_name = args::get(logfile_folder); -#else - server_println("Capio logfile folder, but logging capabilities not compiled into capio!", - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, "parseCLI"); -#endif - } - - if (logfile_src) { -#ifdef CAPIO_LOG - // log file was given - std::string token = args::get(logfile_src); - if (token.find(".log") != std::string::npos) { - token.erase(token.length() - 4); // delete .log if for some reason - // is given as parameter - } - logfile_prefix = token; -#else - server_println("Capio logfile provided, but logging capabilities not compiled into capio!", - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, "parseCLI"); -#endif - } -#ifdef CAPIO_LOG - auto logname = open_server_logfile(); - log = new Logger(__func__, __FILE__, __LINE__, gettid(), "Created new log file"); - server_println("started logging to logfile " + logname.string(), get_capio_workflow_name(), + auto log = new Logger(__func__, __FILE__, __LINE__, gettid(), "Created new log file"); + server_println("started logging to logfile " + log->getLogFileName(), get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "parseCLI"); #endif diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index f8000c9a7..296180d6c 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -1,10 +1,10 @@ #include -#include "utils/capiocl_adapter.hpp" - #include "client-manager/client_manager.hpp" + #include "common/constants.hpp" #include "common/queue.hpp" +#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" #include "utils/server_println.hpp" diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index ead84a42c..9c73994f5 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,4 +1,6 @@ #include "remote/backend/mpi.hpp" + +#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/server_println.hpp" diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index e0e8e3208..a5c90bf55 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -1,6 +1,8 @@ #include #include "remote/backend/none.hpp" + +#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/server_println.hpp" diff --git a/capio/server/src/remote_request.cpp b/capio/server/src/remote_request.cpp index a15bab108..dce981e91 100644 --- a/capio/server/src/remote_request.cpp +++ b/capio/server/src/remote_request.cpp @@ -1,5 +1,7 @@ #include "remote/backend.hpp" +#include "utils/ServerLogger.hpp" + RemoteRequest::RemoteRequest(char *buf_recv, const std::string &source) : _source(source) { START_LOG(gettid(), "call(buf_recv=%s, source=%s)", buf_recv, source.c_str()); int code; diff --git a/capio/server/src/storage_manager.cpp b/capio/server/src/storage_manager.cpp index 4b866bd27..b5f5c01d5 100644 --- a/capio/server/src/storage_manager.cpp +++ b/capio/server/src/storage_manager.cpp @@ -3,11 +3,12 @@ #include #include +#include "storage/manager.hpp" + #include "common/dirent.hpp" #include "common/filesystem.hpp" -#include "common/logger.hpp" #include "storage/capio_file.hpp" -#include "storage/manager.hpp" +#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" #include "utils/location.hpp" From e60358af811adb43076be9650a67d7cc92dfdbbb Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 11:14:10 +0200 Subject: [PATCH 02/26] Moved POSIX to new logger infrastructure --- capio/posix/handlers/exit_group.hpp | 2 - capio/posix/utils/cache.hpp | 188 ++++++++++++++++++++++++ capio/posix/utils/clone.hpp | 1 + capio/posix/utils/common.hpp | 5 +- capio/posix/utils/env.hpp | 3 - capio/posix/utils/filesystem.hpp | 2 +- capio/posix/utils/requests.hpp | 1 + capio/posix/utils/snapshot.hpp | 3 +- capio/tests/unit/posix/src/realpath.cpp | 2 + 9 files changed, 197 insertions(+), 10 deletions(-) create mode 100644 capio/posix/utils/cache.hpp diff --git a/capio/posix/handlers/exit_group.hpp b/capio/posix/handlers/exit_group.hpp index 6c4acb454..498622dc0 100644 --- a/capio/posix/handlers/exit_group.hpp +++ b/capio/posix/handlers/exit_group.hpp @@ -3,8 +3,6 @@ #if defined(SYS_exit) || defined(SYS_exit_group) -#include "common/logger.hpp" - #include "utils/requests.hpp" /* diff --git a/capio/posix/utils/cache.hpp b/capio/posix/utils/cache.hpp new file mode 100644 index 000000000..7a4f4d0f8 --- /dev/null +++ b/capio/posix/utils/cache.hpp @@ -0,0 +1,188 @@ +#ifndef CAPIO_SERVER_UTILS_CACHE +#define CAPIO_SERVER_UTILS_CACHE + +#include "common/dirent.hpp" +#include "common/queue.hpp" + +#include "requests.hpp" + +class ReadCache { + private: + char *_cache; + long _tid; + int _last_fd; + off64_t _max_line_size, _actual_size, _cache_offset; + SPSCQueue _queue; + + inline void _read(void *buffer, off64_t count) { + START_LOG(capio_syscall(SYS_gettid), "call(count=%ld)", count); + + if (count > 0) { + memcpy(buffer, _cache + _cache_offset, count); + LOG("Read %ld. adding it to _cache_offset of value %ld", count, _cache_offset); + _cache_offset += count; + } + } + + public: + ReadCache(long tid, off64_t lines, off64_t line_size, const std::string &workflow_name) + : _cache(nullptr), _tid(tid), _last_fd(-1), _max_line_size(line_size), _actual_size(0), + _cache_offset(0), + _queue(SHM_SPSC_PREFIX_READ + std::to_string(tid), lines, line_size, workflow_name) {} + + inline void flush() { + START_LOG(capio_syscall(SYS_gettid), "call()"); + + if (_cache_offset != _actual_size) { + _actual_size = _cache_offset = 0; + seek_request(_last_fd, get_capio_fd_offset(_last_fd), _tid); + } + } + + inline off64_t read(int fd, void *buffer, off64_t count, bool is_getdents, bool is64bit) { + START_LOG(capio_syscall(SYS_gettid), "call(fd=%d, count=%ld, is_getdents=%s, is64bit=%s)", + fd, count, is_getdents ? "true" : "false", is64bit ? "true" : "false"); + + if (_last_fd != fd) { + LOG("changed fd from %d to %d: flushing", _last_fd, fd); + flush(); + _last_fd = fd; + } + + off64_t remaining_bytes = _actual_size - _cache_offset; + off64_t file_offset = get_capio_fd_offset(fd); + off64_t bytes_read; + + if (is_getdents) { + auto dirent_size = static_cast(sizeof(linux_dirent64)); + count = (count / dirent_size) * dirent_size; + } + + auto read_size = count - remaining_bytes; + LOG("Read() will need to read %ld bytes", read_size); + + if (count <= remaining_bytes) { + LOG("count %ld <= remaining_bytes %ld", count, remaining_bytes); + _read(buffer, count); + bytes_read = count; + } else { + LOG("count %ld > remaining_bytes %ld", count, remaining_bytes); + _read(buffer, remaining_bytes); + buffer = reinterpret_cast(buffer) + remaining_bytes; + + // NOTE: if getdents send a request for exactly the correct amount of data. + if (read_size > _max_line_size || is_getdents) { + LOG("count - remaining_bytes %ld > _max_line_size %ld", read_size, _max_line_size); + LOG("Reading exactly requested size"); + off64_t end_of_read = is_getdents ? getdents_request(fd, read_size, is64bit, _tid) + : read_request(fd, read_size, _tid); + bytes_read = end_of_read - file_offset; + _queue.read(reinterpret_cast(buffer), bytes_read); + } else { + LOG("count - remaining_bytes %ld <= _max_line_size %ld", read_size, _max_line_size); + LOG("Reading more to use pre fetching and caching"); + off64_t end_of_read = is_getdents + ? getdents_request(fd, _max_line_size, is64bit, _tid) + : read_request(fd, _max_line_size, _tid); + LOG("request return value is %ld", end_of_read); + _actual_size = end_of_read - file_offset - remaining_bytes; + LOG("ReaderCache actual size, after requested read is: %ld bytes", _actual_size); + _cache_offset = 0; + if (_actual_size > 0) { + LOG("Fetching data from shm _queue"); + _cache = _queue.fetch(); + } + if (read_size < _actual_size) { + LOG("count - remaining_bytes %ld < _actual_size %ld", read_size, _actual_size); + _read(buffer, read_size); + bytes_read = count; + } else { + LOG("count - remaining_bytes %ld >= _actual_size %ld", read_size, _actual_size); + _read(buffer, _actual_size); + bytes_read = remaining_bytes + _actual_size; + } + } + } + LOG("%ld bytes have been read. setting fd offset to %ld", bytes_read, + file_offset + bytes_read); + set_capio_fd_offset(fd, file_offset + bytes_read); + return bytes_read; + } +}; + +class WriteCache { + private: + char *_cache; + long _tid; + int _fd; + off64_t _max_line_size, _actual_size; + SPSCQueue _queue; + + inline void _write(off64_t count, const void *buffer) { + START_LOG(capio_syscall(SYS_gettid), "call(count=%ld)", count); + + if (count > 0) { + if (_cache == nullptr) { + _cache = _queue.reserve(); + } + memcpy(_cache + _actual_size, buffer, count); + _actual_size += count; + if (_actual_size == _max_line_size) { + flush(); + } + } + } + + public: + WriteCache(long tid, off64_t lines, off64_t line_size, const std::string &workflow_name) + : _cache(nullptr), _tid(tid), _fd(-1), _max_line_size(line_size), _actual_size(0), + _queue(SHM_SPSC_PREFIX_WRITE + std::to_string(tid), lines, line_size, workflow_name) {} + + inline void flush() { + START_LOG(capio_syscall(SYS_gettid), "call()"); + + if (_actual_size != 0) { + write_request(_fd, _actual_size, _tid); + _cache = nullptr; + _actual_size = 0; + } + } + + inline void write(int fd, const void *buffer, off64_t count) { + START_LOG(capio_syscall(SYS_gettid), "call(fd=%d, buffer=0x%08x, count=%ld)", fd, buffer, + count); + + if (_fd != fd) { + LOG("changed fd from %d to %d: flushing", _fd, fd); + flush(); + _fd = fd; + } + + if (count <= _max_line_size - _actual_size) { + LOG("count %ld <= _max_line_size - _actual_size %ld", count, + _max_line_size - _actual_size); + _write(count, buffer); + } else { + LOG("count %ld > _max_line_size - _actual_size %ld", count, + _max_line_size - _actual_size); + flush(); + if (count - _actual_size > _max_line_size) { + LOG("count - _actual_size %ld > _max_line_size %ld", count - _actual_size, + _max_line_size); + write_request(_fd, count, _tid); + _queue.write(reinterpret_cast(buffer), count); + } else { + LOG("count - _actual_size %ld <= _max_line_size %ld", count - _actual_size, + _max_line_size); + _write(count, buffer); + } + } + + set_capio_fd_offset(fd, get_capio_fd_offset(fd) + count); + } +}; + +inline thread_local WriteCache *write_cache; +inline thread_local ReadCache *read_cache; + +#endif // CAPIO_SERVER_UTILS_CACHE \ No newline at end of file diff --git a/capio/posix/utils/clone.hpp b/capio/posix/utils/clone.hpp index 54cd24b72..6673a1c24 100644 --- a/capio/posix/utils/clone.hpp +++ b/capio/posix/utils/clone.hpp @@ -4,6 +4,7 @@ #include "common/syscall.hpp" #include "data.hpp" #include "requests.hpp" +#include "utils/PosixLogger.hpp" /** * Initialize the required data structures for the new child thread, and then proceed to execute a diff --git a/capio/posix/utils/common.hpp b/capio/posix/utils/common.hpp index 9727b334f..83c2c27e2 100644 --- a/capio/posix/utils/common.hpp +++ b/capio/posix/utils/common.hpp @@ -1,7 +1,8 @@ -#include - #ifndef CAPIO_FUNCTIONS_H #define CAPIO_FUNCTIONS_H +#include + +#include "utils/PosixLogger.hpp" int posix_return_value(long res, long *result) { START_LOG(capio_syscall(SYS_gettid), "cal(res=%ld)", res); diff --git a/capio/posix/utils/env.hpp b/capio/posix/utils/env.hpp index c43f56fcb..0893ef500 100644 --- a/capio/posix/utils/env.hpp +++ b/capio/posix/utils/env.hpp @@ -2,9 +2,6 @@ #define CAPIO_POSIX_UTILS_ENV_HPP #include -#include - -#include "common/logger.hpp" inline const char *get_capio_app_name() { static char *capio_app_name = std::getenv("CAPIO_APP_NAME"); diff --git a/capio/posix/utils/filesystem.hpp b/capio/posix/utils/filesystem.hpp index 6ba6921e8..fdbde2e4a 100644 --- a/capio/posix/utils/filesystem.hpp +++ b/capio/posix/utils/filesystem.hpp @@ -12,8 +12,8 @@ #include "common/env.hpp" #include "common/filesystem.hpp" -#include "common/logger.hpp" #include "common/syscall.hpp" +#include "utils/PosixLogger.hpp" #include "types.hpp" diff --git a/capio/posix/utils/requests.hpp b/capio/posix/utils/requests.hpp index 4867e6117..c41dabce7 100644 --- a/capio/posix/utils/requests.hpp +++ b/capio/posix/utils/requests.hpp @@ -6,6 +6,7 @@ #include "env.hpp" #include "filesystem.hpp" #include "types.hpp" +#include "utils/PosixLogger.hpp" inline thread_local CircularBuffer *buf_requests; inline thread_local CircularBuffer *buff_response; diff --git a/capio/posix/utils/snapshot.hpp b/capio/posix/utils/snapshot.hpp index 55a85553b..016481032 100644 --- a/capio/posix/utils/snapshot.hpp +++ b/capio/posix/utils/snapshot.hpp @@ -5,9 +5,8 @@ #include #include -#include "common/logger.hpp" - #include "types.hpp" +#include "utils/PosixLogger.hpp" inline int *get_fd_snapshot(long tid) { return static_cast(get_shm_if_exist("capio_snapshot_" + std::to_string(tid))); diff --git a/capio/tests/unit/posix/src/realpath.cpp b/capio/tests/unit/posix/src/realpath.cpp index cfa38e8ae..11a17b55b 100644 --- a/capio/tests/unit/posix/src/realpath.cpp +++ b/capio/tests/unit/posix/src/realpath.cpp @@ -4,6 +4,8 @@ #include +#include "posix/utils/PosixLogger.hpp" + #include "utils/filesystem.hpp" class RealpathPosixTest : public testing::Test { From e240a7f0d1a0c9c931beb665df11c6001e08b253 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 14:02:37 +0200 Subject: [PATCH 03/26] Fixed segfaults --- capio/common/logger.hpp | 53 ++++++++----------- capio/posix/libcapio_posix.cpp | 2 +- capio/posix/utils/PosixLogger.hpp | 56 ++++++++++++++------- capio/server/capio_server.cpp | 1 + capio/server/include/utils/ServerLogger.hpp | 32 ++++++------ 5 files changed, 78 insertions(+), 66 deletions(-) diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index 4616c44cb..dcd6c0083 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -45,7 +45,7 @@ inline thread_local int current_log_level = 0; // this variable tells the logger that syscall logging // has started, and we are not in setup phase // FIXME: Remove the inline specifier by splitting into header and source code -inline thread_local bool logging_syscall = false; +inline thread_local bool enable_logger = false; #ifndef CAPIO_MAX_LOG_LEVEL // capio max log level. defaults to -1, where everything is logged #define CAPIO_MAX_LOG_LEVEL -1 @@ -71,8 +71,8 @@ inline long long current_time_in_millis() { */ class SyscallLoggingSuspender { public: - SyscallLoggingSuspender() { logging_syscall = false; } - ~SyscallLoggingSuspender() { logging_syscall = true; } + SyscallLoggingSuspender() { enable_logger = false; } + ~SyscallLoggingSuspender() { enable_logger = true; } }; /** @@ -98,42 +98,27 @@ template class TemplateLogger { TemplateLogger(const char invoker[], const char file[], unsigned int line, long int tid, const char *message, ...) { - adapter.openLogFile(); - this->tid = tid; this->line = line; strncpy(this->invoker, invoker, sizeof(this->invoker)); strncpy(this->file, file, sizeof(this->file)); va_list argp, argpc; - - sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); - const size_t pre_msg_len = strlen(format); - strcpy(format + pre_msg_len, message); - va_start(argp, message); va_copy(argpc, argp); -#if defined(CAPIO_LOG) && defined(__CAPIO_POSIX) - if (current_log_level == 0 && logging_syscall) { - int syscallNumber; - if (strcmp(invoker, "hook_clone_child") == 0) { - // Explicitly propagate SYS_clone to child thread after clone - // to avoid spurious unknown syscall logs - syscallNumber = SYS_clone; - } else { - syscallNumber = va_arg(argp, int); - } - - auto buf1 = reinterpret_cast(capio_syscall( - SYS_mmap, nullptr, 50, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); - sprintf(buf1, CAPIO_LOG_POSIX_SYSCALL_START, sys_num_to_string(syscallNumber).c_str(), - syscallNumber); - adapter.writeRaw(buf1, strlen(buf1)); - capio_syscall(SYS_munmap, buf1, 50); + adapter.openLogFile(); + +#ifdef __CAPIO_POSIX + if (!enable_logger) { + return; } #endif + sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); + const size_t pre_msg_len = strlen(format); + strcpy(format + pre_msg_len, message); + const int size = vsnprintf(nullptr, 0U, format, argp); auto buf = reinterpret_cast(capio_syscall(SYS_mmap, nullptr, size + 1, PROT_READ | PROT_WRITE, @@ -158,20 +143,24 @@ template class TemplateLogger { adapter.writeRaw(format, strlen(format)); - if (current_log_level == 0 && logging_syscall) { + if (current_log_level == 0 && enable_logger) { adapter.writeSyscallEnd(); } } void log(const char *message, ...) { +#ifdef __CAPIO_POSIX + if (!enable_logger) { + return; + } +#endif va_list argp, argpc; sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); const size_t pre_msg_len = strlen(format); strcpy(format + pre_msg_len, message); - // Delegate the server request start/end special case to the adapter - if (adapter.isServerInvoker(this->invoker, message)) { + if (adapter.isSTLSafe()) { adapter.writeRaw(format, strlen(format)); return; } @@ -210,7 +199,7 @@ template class TemplateLogger { #define LOG(message, ...) log.log(message, ##__VA_ARGS__) #define START_LOG(tid, message, ...) \ Logger log(__func__, __FILE__, __LINE__, tid, message, ##__VA_ARGS__) -#define START_SYSCALL_LOGGING() logging_syscall = true +#define START_SYSCALL_LOGGING() enable_logger = true #define SUSPEND_SYSCALL_LOGGING() SyscallLoggingSuspender sls{}; /** @@ -257,6 +246,8 @@ template class TemplateLogger { __SHM_CHECK_CLI_MSG; \ } #define DBG(tid, lambda) +#define START_SYSCALL_LOGGING() +#define SUSPEND_SYSCALL_LOGGING() #endif diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index d6bf64396..0a63fc1c8 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -411,4 +411,4 @@ static __attribute__((constructor)) void init() { intercept_hook_point_clone_parent = hook_clone_parent; intercept_hook_point = hook; START_SYSCALL_LOGGING(); -} +} \ No newline at end of file diff --git a/capio/posix/utils/PosixLogger.hpp b/capio/posix/utils/PosixLogger.hpp index d3ea59f03..cc4b49788 100644 --- a/capio/posix/utils/PosixLogger.hpp +++ b/capio/posix/utils/PosixLogger.hpp @@ -3,12 +3,24 @@ #include "common/logger.hpp" - struct PosixLogWriteAdapter { private: - bool fileOpen{false}; - int fileFD{-1}; - char filePath[PATH_MAX]{'\0'}; + // These three fields MUST be thread_local static rather than instance + // members. The adapter is constructed inside Logger, which is a stack + // variable instantiated at the first intercepted syscall — potentially + // before the C++ runtime has finished setting up TLS for the new thread + // (dl_init fires before __cxa_thread_atexit is live). The linker + // pre-allocates thread_local storage in the TLS block for every thread + // before any user code runs, so reading/writing these is always safe, + // even at the earliest possible call site. + static thread_local bool fileOpen; + static thread_local int fileFD; + static thread_local char filePath[PATH_MAX]; + + // ---- path helpers ------------------------------------------------------ + // Kept as static methods so they have no dependency on 'this' and their + // internal statics are initialised lazily on first call from a stable + // context, not during struct construction. static const char *getHostname() { static char hostname[HOST_NAME_MAX]{'\0'}; @@ -62,7 +74,7 @@ struct PosixLogWriteAdapter { return dir; } - void setupLogFilename() { + static void setupLogFilename() { if (filePath[0] == '\0') { sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), capio_syscall(SYS_gettid)); @@ -71,15 +83,18 @@ struct PosixLogWriteAdapter { // ---- write helper ------------------------------------------------------ - void writeToFD(const char *buf, size_t len) const { + static void writeToFD(const char *buf, const size_t len) { + if (!fileOpen) { + return; + } capio_syscall(SYS_write, fileFD, buf, len); capio_syscall(SYS_write, fileFD, "\n", 1); } public: - [[nodiscard]] bool isFileOpened() const { return fileOpen; } + [[nodiscard]] static bool isFileOpened() { return fileOpen; } - void openLogFile() { + static void openLogFile() { setupLogFilename(); current_log_level = 0; // reset log level after clone, avoiding propagation to child threads @@ -87,7 +102,8 @@ struct PosixLogWriteAdapter { capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); - fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, O_CREAT | O_WRONLY | O_TRUNC, 0644); + fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, + O_CREAT | O_WRONLY | O_APPEND, 0644); if (fileFD == -1) { capio_syscall(SYS_write, fileno(stdout), @@ -101,28 +117,34 @@ struct PosixLogWriteAdapter { fileOpen = true; } - void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, - long int /*tid*/, const char *buf, size_t len) const { + static void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, + long int /*tid*/, const char *buf, size_t len) { if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { writeToFD(buf, len); } } - void writeRaw(const char *buf, size_t len) const { + static void writeRaw(const char *buf, size_t len) { if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { writeToFD(buf, len); } } - void writeSyscallEnd() const { + static void writeSyscallEnd() { writeRaw(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); } - static bool isServerInvoker(const char * /*invoker*/, const char * /*message*/) { - return false; - } + static bool isSTLSafe() { return false; } }; +// Definitions of the thread_local static members. +// 'inline' (C++17) ensures a single definition across all translation units +// that include this header, while thread_local gives each thread its own copy +// pre-allocated in the TLS block before any user code runs. +inline thread_local bool PosixLogWriteAdapter::fileOpen{false}; +inline thread_local int PosixLogWriteAdapter::fileFD{-1}; +inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX]{'\0'}; + using Logger = TemplateLogger; -#endif // CAPIO_POSIXLOGGER_HPP +#endif // CAPIO_POSIXLOGGER_HPP \ No newline at end of file diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index 971ae6c5c..739214004 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -114,6 +114,7 @@ static constexpr std::array build_request_handle } int main(int argc, char **argv) { + START_SYSCALL_LOGGING(); Semaphore internal_server_sem(0); diff --git a/capio/server/include/utils/ServerLogger.hpp b/capio/server/include/utils/ServerLogger.hpp index f4c9d8322..05a4a877c 100644 --- a/capio/server/include/utils/ServerLogger.hpp +++ b/capio/server/include/utils/ServerLogger.hpp @@ -6,11 +6,13 @@ struct ServerLogWriteAdapter { private: std::ofstream logfile; - std::string logMasterDirName; - std::string logfilePrefix; std::string logFileName; void writeToStream(const char *buf) { + if (!logfile.is_open()) { + return; + } + if (current_log_level < CAPIO_LOG_LEVEL || CAPIO_LOG_LEVEL < 0) { logfile << buf << std::endl; logfile.flush(); @@ -18,24 +20,24 @@ struct ServerLogWriteAdapter { } public: - explicit ServerLogWriteAdapter() { - if (const char *tmp = std::getenv("CAPIO_LOGGER_MASTER_DIR_NAME"); tmp == nullptr) { + void openLogFile() { + if (this->logfile.is_open()) { + return; + } + std::string logMasterDirName; + std::string logfilePrefix; + + if (const char *tmp = std::getenv("CAPIO_LOG_DIR"); tmp == nullptr) { logMasterDirName = CAPIO_DEFAULT_LOG_FOLDER; } else { logMasterDirName = tmp; } - if (const char *tmp = std::getenv("CAPIO_LOGGER_FILE_PREFIX"); tmp == nullptr) { + if (const char *tmp = std::getenv("CAPIO_LOG_PREFIX"); tmp == nullptr) { logfilePrefix = CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX; } else { logfilePrefix = tmp; } - } - - void openLogFile() { - if (this->logfile.is_open()) { - return; - } char hostname[HOST_NAME_MAX]; gethostname(hostname, HOST_NAME_MAX); @@ -47,7 +49,7 @@ struct ServerLogWriteAdapter { std::to_string(capio_syscall(SYS_gettid)) + ".log"; - logfile.open(logfileName, std::ofstream::out); + logfile.open(logfileName, std::ofstream::app); this->logFileName = logfileName; } @@ -60,11 +62,7 @@ struct ServerLogWriteAdapter { static void writeSyscallEnd() {} - static bool isServerInvoker(const char *invoker, const char *message) { - return strcmp(invoker, "capio_server") == 0 && - (strcmp(CAPIO_LOG_SERVER_REQUEST_START, message) == 0 || - strcmp(CAPIO_LOG_SERVER_REQUEST_END, message) == 0); - } + static bool isSTLSafe() { return true; } const std::string &getLogFileName() { return logFileName; } }; From a8555096b77a4739311b23648c547a65df3dbd00 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 15:05:19 +0200 Subject: [PATCH 04/26] Fixed logger --- capio/common/constants.hpp | 2 +- capio/common/logger.hpp | 40 +++++++++------ capio/posix/libcapio_posix.cpp | 3 +- capio/posix/utils/PosixLogger.hpp | 57 +++++++-------------- capio/server/include/utils/ServerLogger.hpp | 15 +++--- 5 files changed, 52 insertions(+), 65 deletions(-) diff --git a/capio/common/constants.hpp b/capio/common/constants.hpp index 3a230f4bc..e775dfd81 100644 --- a/capio/common/constants.hpp +++ b/capio/common/constants.hpp @@ -68,7 +68,7 @@ constexpr char CAPIO_SHM_OPEN_ERROR[] = // CAPIO logger - POSIX constexpr char CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX[] = "posix_thread_\0"; -constexpr char CAPIO_LOG_POSIX_SYSCALL_START[] = "\n+++++++++ SYSCALL %s (%d) +++++++++"; +constexpr char CAPIO_LOG_POSIX_SYSCALL_START[] = "\n+++++++++ START SYSCALL +++++++++"; constexpr char CAPIO_LOG_POSIX_SYSCALL_END[] = "~~~~~~~~~ END SYSCALL ~~~~~~~~~\n"; // CAPIO logger - server diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index dcd6c0083..b0bbd745a 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -39,9 +39,6 @@ inline void raise_termination(const bool raise_exception, const std::string &mes #include "syscallnames.h" #endif -// FIXME: Remove the inline specifier by splitting into header and source code -inline thread_local int current_log_level = 0; - // this variable tells the logger that syscall logging // has started, and we are not in setup phase // FIXME: Remove the inline specifier by splitting into header and source code @@ -86,6 +83,7 @@ class SyscallLoggingSuspender { * of the old monolithic Logger class for both the server and POSIX builds. */ template class TemplateLogger { + static thread_local int current_log_level; char invoker[256]{0}; char file[256]{0}; char format[CAPIO_LOG_MAX_MSG_LEN]{0}; @@ -97,7 +95,7 @@ template class TemplateLogger { public: TemplateLogger(const char invoker[], const char file[], unsigned int line, long int tid, const char *message, ...) { - + current_log_level++; this->tid = tid; this->line = line; strncpy(this->invoker, invoker, sizeof(this->invoker)); @@ -107,8 +105,6 @@ template class TemplateLogger { va_start(argp, message); va_copy(argpc, argp); - adapter.openLogFile(); - #ifdef __CAPIO_POSIX if (!enable_logger) { return; @@ -124,28 +120,34 @@ template class TemplateLogger { PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); vsnprintf(buf, size + 1, format, argpc); - adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + + if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { + if (current_log_level == 1) { + adapter.writeOpening(); + } + adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + } va_end(argp); va_end(argpc); capio_syscall(SYS_munmap, buf, size); - current_log_level++; } TemplateLogger(const TemplateLogger &) = delete; TemplateLogger &operator=(const TemplateLogger &) = delete; ~TemplateLogger() { - current_log_level--; sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); const size_t pre_msg_len = strlen(format); strcpy(format + pre_msg_len, "returned"); adapter.writeRaw(format, strlen(format)); - if (current_log_level == 0 && enable_logger) { - adapter.writeSyscallEnd(); + if (current_log_level == 1 && enable_logger && + (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0)) { + adapter.writeEpilogue(); } + current_log_level--; } void log(const char *message, ...) { @@ -160,11 +162,6 @@ template class TemplateLogger { const size_t pre_msg_len = strlen(format); strcpy(format + pre_msg_len, message); - if (adapter.isSTLSafe()) { - adapter.writeRaw(format, strlen(format)); - return; - } - va_start(argp, message); va_copy(argpc, argp); const int size = vsnprintf(nullptr, 0U, format, argp); @@ -172,7 +169,14 @@ template class TemplateLogger { capio_syscall(SYS_mmap, nullptr, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); vsnprintf(buf, size + 1, format, argpc); - adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + + if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { + if (adapter.isSTLSafe()) { + adapter.writeRaw(format, strlen(format)); + } else { + adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + } + } va_end(argp); va_end(argpc); @@ -182,6 +186,8 @@ template class TemplateLogger { std::string getLogFileName() { return adapter.getLogFileName(); } }; +template inline thread_local int TemplateLogger::current_log_level = 0; + // --------------------------------------------------------------------------- // Macros — identical surface to the old ones; Logger is now a template // --------------------------------------------------------------------------- diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 0a63fc1c8..1114155f6 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -353,7 +353,8 @@ static int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, CAPIO_LOG_LEVEL = get_capio_log_level(); #endif - START_LOG(syscall_no_intercept(SYS_gettid), "call(syscall_number=%ld)", syscall_number); + START_LOG(syscall_no_intercept(SYS_gettid), "call(syscall_number=%ld, syscall_name=%s)", + syscall_number, sys_num_to_string(syscall_number).c_str()); // If the syscall_number is higher than the maximum // syscall captured by CAPIO, simply return diff --git a/capio/posix/utils/PosixLogger.hpp b/capio/posix/utils/PosixLogger.hpp index cc4b49788..a5634f538 100644 --- a/capio/posix/utils/PosixLogger.hpp +++ b/capio/posix/utils/PosixLogger.hpp @@ -5,23 +5,10 @@ struct PosixLogWriteAdapter { private: - // These three fields MUST be thread_local static rather than instance - // members. The adapter is constructed inside Logger, which is a stack - // variable instantiated at the first intercepted syscall — potentially - // before the C++ runtime has finished setting up TLS for the new thread - // (dl_init fires before __cxa_thread_atexit is live). The linker - // pre-allocates thread_local storage in the TLS block for every thread - // before any user code runs, so reading/writing these is always safe, - // even at the earliest possible call site. static thread_local bool fileOpen; - static thread_local int fileFD; + static thread_local int fileFD; static thread_local char filePath[PATH_MAX]; - // ---- path helpers ------------------------------------------------------ - // Kept as static methods so they have no dependency on 'this' and their - // internal statics are initialised lazily on first call from a stable - // context, not during struct construction. - static const char *getHostname() { static char hostname[HOST_NAME_MAX]{'\0'}; if (hostname[0] == '\0') { @@ -74,15 +61,6 @@ struct PosixLogWriteAdapter { return dir; } - static void setupLogFilename() { - if (filePath[0] == '\0') { - sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), - capio_syscall(SYS_gettid)); - } - } - - // ---- write helper ------------------------------------------------------ - static void writeToFD(const char *buf, const size_t len) { if (!fileOpen) { return; @@ -94,16 +72,18 @@ struct PosixLogWriteAdapter { public: [[nodiscard]] static bool isFileOpened() { return fileOpen; } - static void openLogFile() { - setupLogFilename(); - current_log_level = 0; // reset log level after clone, avoiding propagation to child threads + explicit PosixLogWriteAdapter() { + + if (filePath[0] == '\0') { + sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), + capio_syscall(SYS_gettid)); + } capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); - fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, - O_CREAT | O_WRONLY | O_APPEND, 0644); + fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, O_CREAT | O_WRONLY | O_APPEND, 0644); if (fileFD == -1) { capio_syscall(SYS_write, fileno(stdout), @@ -119,22 +99,21 @@ struct PosixLogWriteAdapter { static void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, long int /*tid*/, const char *buf, size_t len) { - if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - writeToFD(buf, len); - } + writeToFD(buf, len); } - static void writeRaw(const char *buf, size_t len) { - if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - writeToFD(buf, len); - } + static void writeRaw(const char *buf, size_t len) { writeToFD(buf, len); } + + static bool isSTLSafe() { return false; } + + static void writeOpening() { + + writeRaw(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); } - static void writeSyscallEnd() { + static void writeEpilogue() { writeRaw(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); } - - static bool isSTLSafe() { return false; } }; // Definitions of the thread_local static members. @@ -142,7 +121,7 @@ struct PosixLogWriteAdapter { // that include this header, while thread_local gives each thread its own copy // pre-allocated in the TLS block before any user code runs. inline thread_local bool PosixLogWriteAdapter::fileOpen{false}; -inline thread_local int PosixLogWriteAdapter::fileFD{-1}; +inline thread_local int PosixLogWriteAdapter::fileFD{-1}; inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX]{'\0'}; using Logger = TemplateLogger; diff --git a/capio/server/include/utils/ServerLogger.hpp b/capio/server/include/utils/ServerLogger.hpp index 05a4a877c..e6f5363ff 100644 --- a/capio/server/include/utils/ServerLogger.hpp +++ b/capio/server/include/utils/ServerLogger.hpp @@ -13,14 +13,13 @@ struct ServerLogWriteAdapter { return; } - if (current_log_level < CAPIO_LOG_LEVEL || CAPIO_LOG_LEVEL < 0) { - logfile << buf << std::endl; - logfile.flush(); - } + logfile << buf << std::endl; + logfile.flush(); } public: - void openLogFile() { + explicit ServerLogWriteAdapter() { + if (this->logfile.is_open()) { return; } @@ -60,11 +59,13 @@ struct ServerLogWriteAdapter { void writeRaw(const char *buf, size_t /*len*/) { writeToStream(buf); } - static void writeSyscallEnd() {} - static bool isSTLSafe() { return true; } const std::string &getLogFileName() { return logFileName; } + + static void writeOpening() {} + + static void writeEpilogue() {} }; using Logger = TemplateLogger; From 81baa2735678d14a56f190b47a4db72fce358d91 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 15:23:50 +0200 Subject: [PATCH 05/26] Fixed bug in server unit tests --- capio/tests/unit/server/src/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/capio/tests/unit/server/src/main.cpp b/capio/tests/unit/server/src/main.cpp index 28cb277e4..dc4c24dc1 100644 --- a/capio/tests/unit/server/src/main.cpp +++ b/capio/tests/unit/server/src/main.cpp @@ -3,6 +3,7 @@ #include "capiocl.hpp" #include "capiocl/engine.h" #include "client-manager/client_manager.hpp" +#include "remote/backend/none.hpp" #include "storage/manager.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/location.hpp" @@ -22,12 +23,15 @@ class ServerUnitTestEnvironment : public testing::Environment { capio_cl_engine = new capiocl::engine::Engine(false); client_manager = new ClientManager(); storage_manager = new StorageManager(); + backend = new NoneBackend(0, nullptr); + open_files_location(); } void TearDown() override { delete storage_manager; delete client_manager; delete capio_cl_engine; + delete backend; } }; From 4bb5111ec97a8066dbf0882792042ed937230e58 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 15:33:12 +0200 Subject: [PATCH 06/26] Fixed unit tests --- capio/server/capio_server.cpp | 1 - capio/tests/unit/server/src/capio_file.cpp | 31 ++-------------------- capio/tests/unit/server/src/main.cpp | 19 ++++++++++++- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index 739214004..971ae6c5c 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -114,7 +114,6 @@ static constexpr std::array build_request_handle } int main(int argc, char **argv) { - START_SYSCALL_LOGGING(); Semaphore internal_server_sem(0); diff --git a/capio/tests/unit/server/src/capio_file.cpp b/capio/tests/unit/server/src/capio_file.cpp index db7ead849..3dfc8cb77 100644 --- a/capio/tests/unit/server/src/capio_file.cpp +++ b/capio/tests/unit/server/src/capio_file.cpp @@ -323,34 +323,7 @@ TEST(ServerTest, TestFileSetCommitToFalse) { EXPECT_FALSE(file.isCommitted()); } -class MockBackend : public Backend { - public: - MockBackend() : Backend(HOST_NAME_MAX) {} - - void recv_file(char *shm, const std::string &source, const long int bytes_expected) override { - for (long int i = 0; i < bytes_expected; ++i) { - shm[i] = 33 + (i % 93); - } - } - - const std::set get_nodes() override { return {node_name}; } - void handshake_servers() override {} - RemoteRequest read_next_request() override { return {nullptr, ""}; } - void send_file(char *shm, long int nbytes, const std::string &target) override {} - void send_request(const char *message, int message_len, const std::string &target) override {} -}; - -class MockBackendTestFixture : public ::testing::Test { - protected: - void SetUp() override { - backend = new MockBackend(); - open_files_location(); - } - - void TearDown() override { delete backend; } -}; - -TEST_F(MockBackendTestFixture, TestReadFromNodeMockBackend) { +TEST(ServerTest, TestReadFromNodeMockBackend) { CapioFile file1; file1.createBuffer("testDir", true); @@ -377,7 +350,7 @@ TEST(ServerTest, TestGetSectorEnd) { EXPECT_EQ(file.getSectorEnd(12000), -1); } -TEST_F(MockBackendTestFixture, TestSimulateDirectoryStreaming) { +TEST(ServerTest, TestSimulateDirectoryStreaming) { constexpr int NUM_FILES_EXPECTED = 10; diff --git a/capio/tests/unit/server/src/main.cpp b/capio/tests/unit/server/src/main.cpp index dc4c24dc1..ec4620798 100644 --- a/capio/tests/unit/server/src/main.cpp +++ b/capio/tests/unit/server/src/main.cpp @@ -15,6 +15,23 @@ Backend *backend = nullptr; const capiocl::engine::Engine &CapioCLEngine::get() { return *capio_cl_engine; } +class MockBackend : public Backend { +public: + MockBackend() : Backend(HOST_NAME_MAX) {} + + void recv_file(char *shm, const std::string &source, const long int bytes_expected) override { + for (long int i = 0; i < bytes_expected; ++i) { + shm[i] = 33 + (i % 93); + } + } + + const std::set get_nodes() override { return {node_name}; } + void handshake_servers() override {} + RemoteRequest read_next_request() override { return {nullptr, ""}; } + void send_file(char *shm, long int nbytes, const std::string &target) override {} + void send_request(const char *message, int message_len, const std::string &target) override {} +}; + class ServerUnitTestEnvironment : public testing::Environment { public: explicit ServerUnitTestEnvironment() = default; @@ -23,7 +40,7 @@ class ServerUnitTestEnvironment : public testing::Environment { capio_cl_engine = new capiocl::engine::Engine(false); client_manager = new ClientManager(); storage_manager = new StorageManager(); - backend = new NoneBackend(0, nullptr); + backend = new MockBackend(); open_files_location(); } From 01b74460b6d9cec4329d3c431923dda7cda4be22 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 16:57:17 +0200 Subject: [PATCH 07/26] Boh --- capio/posix/utils/PosixLogger.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/capio/posix/utils/PosixLogger.hpp b/capio/posix/utils/PosixLogger.hpp index a5634f538..e8332c724 100644 --- a/capio/posix/utils/PosixLogger.hpp +++ b/capio/posix/utils/PosixLogger.hpp @@ -2,13 +2,12 @@ #define CAPIO_POSIXLOGGER_HPP #include "common/logger.hpp" +static thread_local bool fileOpen = false; +static thread_local int fileFD = -1; +static thread_local char filePath[PATH_MAX]; struct PosixLogWriteAdapter { private: - static thread_local bool fileOpen; - static thread_local int fileFD; - static thread_local char filePath[PATH_MAX]; - static const char *getHostname() { static char hostname[HOST_NAME_MAX]{'\0'}; if (hostname[0] == '\0') { @@ -120,9 +119,9 @@ struct PosixLogWriteAdapter { // 'inline' (C++17) ensures a single definition across all translation units // that include this header, while thread_local gives each thread its own copy // pre-allocated in the TLS block before any user code runs. -inline thread_local bool PosixLogWriteAdapter::fileOpen{false}; -inline thread_local int PosixLogWriteAdapter::fileFD{-1}; -inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX]{'\0'}; +// inline thread_local bool PosixLogWriteAdapter::fileOpen{false}; +// inline thread_local int PosixLogWriteAdapter::fileFD{-1}; +// inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX]{'\0'}; using Logger = TemplateLogger; From de6757a47aa01afbbf727585c26affa12faafc20 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 17:03:06 +0200 Subject: [PATCH 08/26] Boh --- capio/posix/utils/PosixLogger.hpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/capio/posix/utils/PosixLogger.hpp b/capio/posix/utils/PosixLogger.hpp index e8332c724..20d6a6c54 100644 --- a/capio/posix/utils/PosixLogger.hpp +++ b/capio/posix/utils/PosixLogger.hpp @@ -2,9 +2,9 @@ #define CAPIO_POSIXLOGGER_HPP #include "common/logger.hpp" -static thread_local bool fileOpen = false; -static thread_local int fileFD = -1; -static thread_local char filePath[PATH_MAX]; +inline thread_local bool fileOpen = false; +inline thread_local int fileFD = -1; +inline thread_local char filePath[PATH_MAX]; struct PosixLogWriteAdapter { private: @@ -69,15 +69,15 @@ struct PosixLogWriteAdapter { } public: - [[nodiscard]] static bool isFileOpened() { return fileOpen; } - explicit PosixLogWriteAdapter() { - if (filePath[0] == '\0') { - sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), - capio_syscall(SYS_gettid)); + if (fileOpen) { + return; } + sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), + capio_syscall(SYS_gettid)); + capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); @@ -115,14 +115,6 @@ struct PosixLogWriteAdapter { } }; -// Definitions of the thread_local static members. -// 'inline' (C++17) ensures a single definition across all translation units -// that include this header, while thread_local gives each thread its own copy -// pre-allocated in the TLS block before any user code runs. -// inline thread_local bool PosixLogWriteAdapter::fileOpen{false}; -// inline thread_local int PosixLogWriteAdapter::fileFD{-1}; -// inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX]{'\0'}; - using Logger = TemplateLogger; #endif // CAPIO_POSIXLOGGER_HPP \ No newline at end of file From afdb77daf639f888c1c1bf9e2d09eed5c56edb6c Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 17:18:55 +0200 Subject: [PATCH 09/26] refactor --- capio/common/logger.hpp | 4 ++-- capio/posix/libcapio_posix.cpp | 2 +- capio/posix/utils/clone.hpp | 2 +- capio/posix/utils/common.hpp | 2 +- capio/posix/utils/filesystem.hpp | 2 +- .../utils/{PosixLogger.hpp => posix_logger.hpp} | 12 ++++-------- capio/posix/utils/requests.hpp | 2 +- capio/posix/utils/snapshot.hpp | 2 +- capio/tests/unit/posix/src/realpath.cpp | 2 +- 9 files changed, 13 insertions(+), 17 deletions(-) rename capio/posix/utils/{PosixLogger.hpp => posix_logger.hpp} (90%) diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index b0bbd745a..34c16e29a 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -141,7 +141,7 @@ template class TemplateLogger { const size_t pre_msg_len = strlen(format); strcpy(format + pre_msg_len, "returned"); - adapter.writeRaw(format, strlen(format)); + adapter.write(format, strlen(format)); if (current_log_level == 1 && enable_logger && (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0)) { @@ -172,7 +172,7 @@ template class TemplateLogger { if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { if (adapter.isSTLSafe()) { - adapter.writeRaw(format, strlen(format)); + adapter.write(format, strlen(format)); } else { adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); } diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 1114155f6..67121f8c8 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -7,7 +7,7 @@ #include #include -#include "utils/PosixLogger.hpp" +#include "utils/posix_logger.hpp" #include "common/syscall.hpp" diff --git a/capio/posix/utils/clone.hpp b/capio/posix/utils/clone.hpp index 6673a1c24..97bc869bf 100644 --- a/capio/posix/utils/clone.hpp +++ b/capio/posix/utils/clone.hpp @@ -4,7 +4,7 @@ #include "common/syscall.hpp" #include "data.hpp" #include "requests.hpp" -#include "utils/PosixLogger.hpp" +#include "utils/posix_logger.hpp" /** * Initialize the required data structures for the new child thread, and then proceed to execute a diff --git a/capio/posix/utils/common.hpp b/capio/posix/utils/common.hpp index 83c2c27e2..d8abba1e7 100644 --- a/capio/posix/utils/common.hpp +++ b/capio/posix/utils/common.hpp @@ -2,7 +2,7 @@ #define CAPIO_FUNCTIONS_H #include -#include "utils/PosixLogger.hpp" +#include "utils/posix_logger.hpp" int posix_return_value(long res, long *result) { START_LOG(capio_syscall(SYS_gettid), "cal(res=%ld)", res); diff --git a/capio/posix/utils/filesystem.hpp b/capio/posix/utils/filesystem.hpp index fdbde2e4a..99c7ba257 100644 --- a/capio/posix/utils/filesystem.hpp +++ b/capio/posix/utils/filesystem.hpp @@ -13,7 +13,7 @@ #include "common/env.hpp" #include "common/filesystem.hpp" #include "common/syscall.hpp" -#include "utils/PosixLogger.hpp" +#include "utils/posix_logger.hpp" #include "types.hpp" diff --git a/capio/posix/utils/PosixLogger.hpp b/capio/posix/utils/posix_logger.hpp similarity index 90% rename from capio/posix/utils/PosixLogger.hpp rename to capio/posix/utils/posix_logger.hpp index 20d6a6c54..ca4266784 100644 --- a/capio/posix/utils/PosixLogger.hpp +++ b/capio/posix/utils/posix_logger.hpp @@ -4,7 +4,7 @@ #include "common/logger.hpp" inline thread_local bool fileOpen = false; inline thread_local int fileFD = -1; -inline thread_local char filePath[PATH_MAX]; +inline thread_local char filePath[PATH_MAX]{'\0'}; struct PosixLogWriteAdapter { private: @@ -61,9 +61,6 @@ struct PosixLogWriteAdapter { } static void writeToFD(const char *buf, const size_t len) { - if (!fileOpen) { - return; - } capio_syscall(SYS_write, fileFD, buf, len); capio_syscall(SYS_write, fileFD, "\n", 1); } @@ -101,17 +98,16 @@ struct PosixLogWriteAdapter { writeToFD(buf, len); } - static void writeRaw(const char *buf, size_t len) { writeToFD(buf, len); } + static void write(const char *buf, size_t len) { writeToFD(buf, len); } static bool isSTLSafe() { return false; } static void writeOpening() { - - writeRaw(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); + writeToFD(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); } static void writeEpilogue() { - writeRaw(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); + writeToFD(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); } }; diff --git a/capio/posix/utils/requests.hpp b/capio/posix/utils/requests.hpp index c41dabce7..da04be22c 100644 --- a/capio/posix/utils/requests.hpp +++ b/capio/posix/utils/requests.hpp @@ -6,7 +6,7 @@ #include "env.hpp" #include "filesystem.hpp" #include "types.hpp" -#include "utils/PosixLogger.hpp" +#include "utils/posix_logger.hpp" inline thread_local CircularBuffer *buf_requests; inline thread_local CircularBuffer *buff_response; diff --git a/capio/posix/utils/snapshot.hpp b/capio/posix/utils/snapshot.hpp index 016481032..1f06142ba 100644 --- a/capio/posix/utils/snapshot.hpp +++ b/capio/posix/utils/snapshot.hpp @@ -6,7 +6,7 @@ #include #include "types.hpp" -#include "utils/PosixLogger.hpp" +#include "utils/posix_logger.hpp" inline int *get_fd_snapshot(long tid) { return static_cast(get_shm_if_exist("capio_snapshot_" + std::to_string(tid))); diff --git a/capio/tests/unit/posix/src/realpath.cpp b/capio/tests/unit/posix/src/realpath.cpp index 11a17b55b..004f9e62c 100644 --- a/capio/tests/unit/posix/src/realpath.cpp +++ b/capio/tests/unit/posix/src/realpath.cpp @@ -4,7 +4,7 @@ #include -#include "posix/utils/PosixLogger.hpp" +#include "posix/utils/posix_logger.hpp" #include "utils/filesystem.hpp" From 3b6309b99914da8d265d45b7b4778375b4c9ba0f Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 17:21:37 +0200 Subject: [PATCH 10/26] boh --- capio/common/logger.hpp | 4 ++-- capio/posix/utils/posix_logger.hpp | 13 +++++++++---- capio/server/include/utils/ServerLogger.hpp | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index 34c16e29a..2f250d9a0 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -125,7 +125,7 @@ template class TemplateLogger { if (current_log_level == 1) { adapter.writeOpening(); } - adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + adapter.writeFormatted(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); } va_end(argp); @@ -174,7 +174,7 @@ template class TemplateLogger { if (adapter.isSTLSafe()) { adapter.write(format, strlen(format)); } else { - adapter.write(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + adapter.writeFormatted(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); } } diff --git a/capio/posix/utils/posix_logger.hpp b/capio/posix/utils/posix_logger.hpp index ca4266784..657132165 100644 --- a/capio/posix/utils/posix_logger.hpp +++ b/capio/posix/utils/posix_logger.hpp @@ -2,11 +2,12 @@ #define CAPIO_POSIXLOGGER_HPP #include "common/logger.hpp" -inline thread_local bool fileOpen = false; -inline thread_local int fileFD = -1; -inline thread_local char filePath[PATH_MAX]{'\0'}; struct PosixLogWriteAdapter { + static thread_local bool fileOpen; + static thread_local int fileFD; + static thread_local char filePath[PATH_MAX]; + private: static const char *getHostname() { static char hostname[HOST_NAME_MAX]{'\0'}; @@ -93,7 +94,7 @@ struct PosixLogWriteAdapter { fileOpen = true; } - static void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, + static void writeFormatted(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, long int /*tid*/, const char *buf, size_t len) { writeToFD(buf, len); } @@ -111,6 +112,10 @@ struct PosixLogWriteAdapter { } }; +thread_local bool PosixLogWriteAdapter::fileOpen = false; +thread_local int PosixLogWriteAdapter::fileFD = -1; +thread_local char PosixLogWriteAdapter::filePath[PATH_MAX] = {'\0'}; + using Logger = TemplateLogger; #endif // CAPIO_POSIXLOGGER_HPP \ No newline at end of file diff --git a/capio/server/include/utils/ServerLogger.hpp b/capio/server/include/utils/ServerLogger.hpp index e6f5363ff..a6f9b761d 100644 --- a/capio/server/include/utils/ServerLogger.hpp +++ b/capio/server/include/utils/ServerLogger.hpp @@ -52,12 +52,12 @@ struct ServerLogWriteAdapter { this->logFileName = logfileName; } - void write(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, + void writeFormatted(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, long int /*tid*/, const char *buf, size_t /*len*/) { writeToStream(buf); } - void writeRaw(const char *buf, size_t /*len*/) { writeToStream(buf); } + void write(const char *buf, size_t /*len*/) { writeToStream(buf); } static bool isSTLSafe() { return true; } From 3e91607e6bc81732e8de7de8964140e0f0889ce7 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 17:32:09 +0200 Subject: [PATCH 11/26] refactor --- capio/server/capio_server.cpp | 2 +- capio/server/include/client-manager/client_manager.hpp | 2 +- capio/server/include/remote/handlers/read.hpp | 2 +- capio/server/include/remote/handlers/stat.hpp | 2 +- capio/server/include/storage/capio_file.hpp | 2 +- capio/server/include/utils/cli_parser.hpp | 2 +- capio/server/include/utils/common.hpp | 2 +- capio/server/include/utils/env.hpp | 2 +- capio/server/include/utils/location.hpp | 2 +- .../include/utils/{ServerLogger.hpp => server_logger.hpp} | 0 capio/server/include/utils/signals.hpp | 2 +- capio/server/src/backend.cpp | 2 +- capio/server/src/client_manager.cpp | 2 +- capio/server/src/mpi_backend.cpp | 2 +- capio/server/src/none_backend.cpp | 2 +- capio/server/src/remote_request.cpp | 2 +- capio/server/src/storage_manager.cpp | 2 +- 17 files changed, 16 insertions(+), 16 deletions(-) rename capio/server/include/utils/{ServerLogger.hpp => server_logger.hpp} (100%) diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index 971ae6c5c..ef70188d6 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -17,7 +17,7 @@ #include #include -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" #include "capiocl.hpp" #include "capiocl/engine.h" diff --git a/capio/server/include/client-manager/client_manager.hpp b/capio/server/include/client-manager/client_manager.hpp index 53af1c9cc..4ea53b70d 100644 --- a/capio/server/include/client-manager/client_manager.hpp +++ b/capio/server/include/client-manager/client_manager.hpp @@ -5,7 +5,7 @@ #include #include -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" #include "common/queue.hpp" /** diff --git a/capio/server/include/remote/handlers/read.hpp b/capio/server/include/remote/handlers/read.hpp index 19483f756..8c56321b9 100644 --- a/capio/server/include/remote/handlers/read.hpp +++ b/capio/server/include/remote/handlers/read.hpp @@ -4,7 +4,7 @@ #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" extern StorageManager *storage_manager; diff --git a/capio/server/include/remote/handlers/stat.hpp b/capio/server/include/remote/handlers/stat.hpp index 9ea22e336..28219760c 100644 --- a/capio/server/include/remote/handlers/stat.hpp +++ b/capio/server/include/remote/handlers/stat.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" extern StorageManager *storage_manager; extern ClientManager *client_manager; diff --git a/capio/server/include/storage/capio_file.hpp b/capio/server/include/storage/capio_file.hpp index 5605fbe97..58f3a8afa 100644 --- a/capio/server/include/storage/capio_file.hpp +++ b/capio/server/include/storage/capio_file.hpp @@ -10,7 +10,7 @@ #include #include -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" #include "common/queue.hpp" diff --git a/capio/server/include/utils/cli_parser.hpp b/capio/server/include/utils/cli_parser.hpp index 73022d203..aec16d35f 100644 --- a/capio/server/include/utils/cli_parser.hpp +++ b/capio/server/include/utils/cli_parser.hpp @@ -1,7 +1,7 @@ #ifndef CAPIO_CLI_PARSER_HPP #define CAPIO_CLI_PARSER_HPP +#include "utils/server_logger.hpp" #include -#include "utils/ServerLogger.hpp" struct CapioParsedConfig { std::string backend_name; diff --git a/capio/server/include/utils/common.hpp b/capio/server/include/utils/common.hpp index 95a9ca467..067507617 100644 --- a/capio/server/include/utils/common.hpp +++ b/capio/server/include/utils/common.hpp @@ -10,7 +10,7 @@ #include "common/dirent.hpp" #include "storage/capio_file.hpp" #include "storage/manager.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" extern ClientManager *client_manager; extern StorageManager *storage_manager; diff --git a/capio/server/include/utils/env.hpp b/capio/server/include/utils/env.hpp index 1b0fa7af7..98a9b3a61 100644 --- a/capio/server/include/utils/env.hpp +++ b/capio/server/include/utils/env.hpp @@ -4,7 +4,7 @@ #include #include "common/constants.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" off64_t get_file_initial_size() { START_LOG(gettid(), "call()"); diff --git a/capio/server/include/utils/location.hpp b/capio/server/include/utils/location.hpp index 522321b77..9bb4bf8df 100644 --- a/capio/server/include/utils/location.hpp +++ b/capio/server/include/utils/location.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" #include "utils/types.hpp" extern Backend *backend; diff --git a/capio/server/include/utils/ServerLogger.hpp b/capio/server/include/utils/server_logger.hpp similarity index 100% rename from capio/server/include/utils/ServerLogger.hpp rename to capio/server/include/utils/server_logger.hpp diff --git a/capio/server/include/utils/signals.hpp b/capio/server/include/utils/signals.hpp index 1027ac4f2..030924d23 100644 --- a/capio/server/include/utils/signals.hpp +++ b/capio/server/include/utils/signals.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" #include "server_println.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" #ifdef CAPIO_COVERAGE extern "C" void __gcov_dump(void); diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index f6da19b0a..96f69a0cf 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend.hpp" -#include "utils/ServerLogger.hpp" #include "utils/common.hpp" +#include "utils/server_logger.hpp" #include "utils/server_println.hpp" #include diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index 296180d6c..3548f1a21 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -4,9 +4,9 @@ #include "common/constants.hpp" #include "common/queue.hpp" -#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" +#include "utils/server_logger.hpp" #include "utils/server_println.hpp" ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToServerName, diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index 9c73994f5..57093f1ce 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend/mpi.hpp" -#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" +#include "utils/server_logger.hpp" #include "utils/server_println.hpp" MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) { diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index a5c90bf55..5f6327584 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -2,8 +2,8 @@ #include "remote/backend/none.hpp" -#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" +#include "utils/server_logger.hpp" #include "utils/server_println.hpp" NoneBackend::NoneBackend(int argc, char **argv) : Backend(HOST_NAME_MAX) { diff --git a/capio/server/src/remote_request.cpp b/capio/server/src/remote_request.cpp index dce981e91..60bab8db5 100644 --- a/capio/server/src/remote_request.cpp +++ b/capio/server/src/remote_request.cpp @@ -1,6 +1,6 @@ #include "remote/backend.hpp" -#include "utils/ServerLogger.hpp" +#include "utils/server_logger.hpp" RemoteRequest::RemoteRequest(char *buf_recv, const std::string &source) : _source(source) { START_LOG(gettid(), "call(buf_recv=%s, source=%s)", buf_recv, source.c_str()); diff --git a/capio/server/src/storage_manager.cpp b/capio/server/src/storage_manager.cpp index b5f5c01d5..13f7517eb 100644 --- a/capio/server/src/storage_manager.cpp +++ b/capio/server/src/storage_manager.cpp @@ -8,10 +8,10 @@ #include "common/dirent.hpp" #include "common/filesystem.hpp" #include "storage/capio_file.hpp" -#include "utils/ServerLogger.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" #include "utils/location.hpp" +#include "utils/server_logger.hpp" #include "utils/shared_mutex.hpp" #include "utils/types.hpp" From c60b8beeac75db44d3357ef1cd52e0b281fee746 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 17:48:27 +0200 Subject: [PATCH 12/26] cleanup --- capio/common/logger.hpp | 11 ++--------- capio/posix/utils/posix_logger.hpp | 9 +-------- capio/server/include/utils/server_logger.hpp | 7 ------- 3 files changed, 3 insertions(+), 24 deletions(-) diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index 2f250d9a0..1255feccc 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -125,7 +125,7 @@ template class TemplateLogger { if (current_log_level == 1) { adapter.writeOpening(); } - adapter.writeFormatted(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); + adapter.write(buf, strlen(buf)); } va_end(argp); @@ -171,11 +171,7 @@ template class TemplateLogger { vsnprintf(buf, size + 1, format, argpc); if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - if (adapter.isSTLSafe()) { - adapter.write(format, strlen(format)); - } else { - adapter.writeFormatted(this->invoker, this->file, this->line, this->tid, buf, strlen(buf)); - } + adapter.write(buf, strlen(buf)); } va_end(argp); @@ -188,9 +184,6 @@ template class TemplateLogger { template inline thread_local int TemplateLogger::current_log_level = 0; -// --------------------------------------------------------------------------- -// Macros — identical surface to the old ones; Logger is now a template -// --------------------------------------------------------------------------- #ifdef CAPIO_LOG #define ERR_EXIT_EXCEPT_CHOICE(raise_exception, message, ...) \ diff --git a/capio/posix/utils/posix_logger.hpp b/capio/posix/utils/posix_logger.hpp index 657132165..19eb16635 100644 --- a/capio/posix/utils/posix_logger.hpp +++ b/capio/posix/utils/posix_logger.hpp @@ -94,14 +94,7 @@ struct PosixLogWriteAdapter { fileOpen = true; } - static void writeFormatted(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, - long int /*tid*/, const char *buf, size_t len) { - writeToFD(buf, len); - } - - static void write(const char *buf, size_t len) { writeToFD(buf, len); } - - static bool isSTLSafe() { return false; } + static void write(const char *buf, const size_t len) { writeToFD(buf, len); } static void writeOpening() { writeToFD(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); diff --git a/capio/server/include/utils/server_logger.hpp b/capio/server/include/utils/server_logger.hpp index a6f9b761d..a31ecb462 100644 --- a/capio/server/include/utils/server_logger.hpp +++ b/capio/server/include/utils/server_logger.hpp @@ -52,15 +52,8 @@ struct ServerLogWriteAdapter { this->logFileName = logfileName; } - void writeFormatted(const char * /*invoker*/, const char * /*file*/, unsigned int /*line*/, - long int /*tid*/, const char *buf, size_t /*len*/) { - writeToStream(buf); - } - void write(const char *buf, size_t /*len*/) { writeToStream(buf); } - static bool isSTLSafe() { return true; } - const std::string &getLogFileName() { return logFileName; } static void writeOpening() {} From 8918e8c22a900bca1c78f5f55a03b4b83b7f4dea Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 18:16:51 +0200 Subject: [PATCH 13/26] format --- capio/common/env.hpp | 1 - capio/tests/unit/server/src/main.cpp | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/capio/common/env.hpp b/capio/common/env.hpp index 0e7313772..2b76a538c 100644 --- a/capio/common/env.hpp +++ b/capio/common/env.hpp @@ -11,7 +11,6 @@ #include "common/syscall.hpp" - // TODO: remove forward declaration of function by splitting into header and impl. capio/common inline bool is_forbidden_path(const std::string_view &path); diff --git a/capio/tests/unit/server/src/main.cpp b/capio/tests/unit/server/src/main.cpp index ec4620798..b57f22e41 100644 --- a/capio/tests/unit/server/src/main.cpp +++ b/capio/tests/unit/server/src/main.cpp @@ -3,7 +3,6 @@ #include "capiocl.hpp" #include "capiocl/engine.h" #include "client-manager/client_manager.hpp" -#include "remote/backend/none.hpp" #include "storage/manager.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/location.hpp" @@ -16,7 +15,7 @@ Backend *backend = nullptr; const capiocl::engine::Engine &CapioCLEngine::get() { return *capio_cl_engine; } class MockBackend : public Backend { -public: + public: MockBackend() : Backend(HOST_NAME_MAX) {} void recv_file(char *shm, const std::string &source, const long int bytes_expected) override { From 56ca3547c1183f19f3eee17962c9653d911a85de Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 19 May 2026 19:00:37 +0200 Subject: [PATCH 14/26] refactor --- capio/posix/utils/posix_logger.hpp | 75 +++++++++++++++--------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/capio/posix/utils/posix_logger.hpp b/capio/posix/utils/posix_logger.hpp index 19eb16635..2de8e11a7 100644 --- a/capio/posix/utils/posix_logger.hpp +++ b/capio/posix/utils/posix_logger.hpp @@ -8,6 +8,43 @@ struct PosixLogWriteAdapter { static thread_local int fileFD; static thread_local char filePath[PATH_MAX]; + explicit PosixLogWriteAdapter() { + + if (fileOpen) { + return; + } + + sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), + capio_syscall(SYS_gettid)); + + capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); + capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); + capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); + + fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, O_CREAT | O_WRONLY | O_APPEND, 0644); + + if (fileFD == -1) { + capio_syscall(SYS_write, fileno(stdout), + "Err fopen file: ", strlen("Err fopen file: ")); + capio_syscall(SYS_write, fileno(stdout), filePath, strlen(filePath)); + capio_syscall(SYS_write, fileno(stdout), " ", 1); + capio_syscall(SYS_write, fileno(stdout), strerror(errno), strlen(strerror(errno))); + capio_syscall(SYS_write, fileno(stdout), "\n", 1); + exit(EXIT_FAILURE); + } + fileOpen = true; + } + + static void write(const char *buf, const size_t len) { writeToFD(buf, len); } + + static void writeOpening() { + writeToFD(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); + } + + static void writeEpilogue() { + writeToFD(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); + } + private: static const char *getHostname() { static char hostname[HOST_NAME_MAX]{'\0'}; @@ -65,44 +102,6 @@ struct PosixLogWriteAdapter { capio_syscall(SYS_write, fileFD, buf, len); capio_syscall(SYS_write, fileFD, "\n", 1); } - - public: - explicit PosixLogWriteAdapter() { - - if (fileOpen) { - return; - } - - sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), - capio_syscall(SYS_gettid)); - - capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); - - fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, O_CREAT | O_WRONLY | O_APPEND, 0644); - - if (fileFD == -1) { - capio_syscall(SYS_write, fileno(stdout), - "Err fopen file: ", strlen("Err fopen file: ")); - capio_syscall(SYS_write, fileno(stdout), filePath, strlen(filePath)); - capio_syscall(SYS_write, fileno(stdout), " ", 1); - capio_syscall(SYS_write, fileno(stdout), strerror(errno), strlen(strerror(errno))); - capio_syscall(SYS_write, fileno(stdout), "\n", 1); - exit(EXIT_FAILURE); - } - fileOpen = true; - } - - static void write(const char *buf, const size_t len) { writeToFD(buf, len); } - - static void writeOpening() { - writeToFD(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); - } - - static void writeEpilogue() { - writeToFD(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); - } }; thread_local bool PosixLogWriteAdapter::fileOpen = false; From 2986cd1e31b4c054eb10d448175a7b6339a6f8cd Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 09:14:08 +0200 Subject: [PATCH 15/26] Refactor logger macro --- capio/common/logger.hpp | 17 +++++++---------- capio/posix/libcapio_posix.cpp | 2 +- capio/server/capio_server.cpp | 1 + 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index 1255feccc..d37daef2f 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -105,11 +105,10 @@ template class TemplateLogger { va_start(argp, message); va_copy(argpc, argp); -#ifdef __CAPIO_POSIX if (!enable_logger) { return; } -#endif + sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); const size_t pre_msg_len = strlen(format); @@ -151,11 +150,11 @@ template class TemplateLogger { } void log(const char *message, ...) { -#ifdef __CAPIO_POSIX + if (!enable_logger) { return; } -#endif + va_list argp, argpc; sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); @@ -198,8 +197,8 @@ template inline thread_local int TemplateLogger::current_log_lev #define LOG(message, ...) log.log(message, ##__VA_ARGS__) #define START_LOG(tid, message, ...) \ Logger log(__func__, __FILE__, __LINE__, tid, message, ##__VA_ARGS__) -#define START_SYSCALL_LOGGING() enable_logger = true -#define SUSPEND_SYSCALL_LOGGING() SyscallLoggingSuspender sls{}; +#define ENABLE_LOGGER() enable_logger = true +#define DISABLE_LOGGER() SyscallLoggingSuspender sls{}; /** * This macro is used to inject code into debug mode. It needs a self calling lambda function, @@ -238,15 +237,13 @@ template inline thread_local int TemplateLogger::current_log_lev #define ERR_EXIT(message, ...) ERR_EXIT_EXCEPT_CHOICE(true, message, ##__VA_ARGS__) #define LOG(message, ...) #define START_LOG(tid, message, ...) -#define START_SYSCALL_LOGGING() -#define SUSPEND_SYSCALL_LOGGING() #define SEM_CREATE_CHECK(sem, source) \ if (sem == SEM_FAILED) { \ __SHM_CHECK_CLI_MSG; \ } #define DBG(tid, lambda) -#define START_SYSCALL_LOGGING() -#define SUSPEND_SYSCALL_LOGGING() +#define ENABLE_LOGGER() +#define DISABLE_LOGGER() #endif diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 67121f8c8..45b3d9fa7 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -411,5 +411,5 @@ static __attribute__((constructor)) void init() { intercept_hook_point_clone_child = hook_clone_child; intercept_hook_point_clone_parent = hook_clone_parent; intercept_hook_point = hook; - START_SYSCALL_LOGGING(); + ENABLE_LOGGER(); } \ No newline at end of file diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index ef70188d6..e98ef9504 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -114,6 +114,7 @@ static constexpr std::array build_request_handle } int main(int argc, char **argv) { + ENABLE_LOGGER(); Semaphore internal_server_sem(0); From c3357c963fcccc0b3c796230ea5789de054e62e5 Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 10:38:45 +0200 Subject: [PATCH 16/26] Ported log to use JSON based logfiles --- capio/common/json_base_logger.hpp | 276 +++++++++++++++++++ capio/common/logger.hpp | 180 ++++++------ capio/posix/utils/posix_logger.hpp | 83 +++--- capio/server/capio_server.cpp | 1 - capio/server/include/utils/server_logger.hpp | 82 +++--- 5 files changed, 454 insertions(+), 168 deletions(-) create mode 100644 capio/common/json_base_logger.hpp diff --git a/capio/common/json_base_logger.hpp b/capio/common/json_base_logger.hpp new file mode 100644 index 000000000..bd1fd5066 --- /dev/null +++ b/capio/common/json_base_logger.hpp @@ -0,0 +1,276 @@ +#ifndef CAPIO_LOGGER_JSON_BASE_HPP +#define CAPIO_LOGGER_JSON_BASE_HPP + +// Pull in everything this header needs directly so it is self-contained +// regardless of include order. +#include // va_list, va_copy, va_end +#include // snprintf, vsnprintf +#include // strlen +#include // std::string (used in expandAndEscape) + +#include "constants.hpp" // CAPIO_LOG_MAX_MSG_LEN, CAPIO_DEFAULT_LOG_FOLDER, … +#include "syscall.hpp" // current_time_in_millis (declared in logger.hpp which + // includes syscall.hpp; guard against double-include + // by relying on header guards in logger.hpp) + +/** + * @file logger_json_base.hpp + * + * CRTP mixin that implements the full structured-JSON adapter interface + * required by TemplateLogger. Derived adapters only need to supply two + * I/O primitives as *static* methods: + * + * static void rawWriteBytes(const char *buf, int len); + * static void rawWriteStr (const char *buf); // NUL-terminated + * + * All JSON structure, indentation, pending-comma handling, and string + * escaping live here, shared between PosixLogWriteAdapter and + * ServerLogWriteAdapter. + */ +template +struct JsonLogBase { + + // ------------------------------------------------------------------ // + // Thread-local JSON state + // ------------------------------------------------------------------ // + + static thread_local int nestingDepth; + static thread_local bool rootArrayOpen; + + // Pending-line buffer: holds the last event/closing-brace WITHOUT its + // trailing newline. Flushed with ",\n" when a sibling follows, plain + // "\n" when the enclosing array closes. Avoids trailing commas on an + // append-only stream without any seek-back. + static thread_local int pendingLen; + static thread_local char pendingBuf[CAPIO_LOG_MAX_MSG_LEN * 6 + 256]; + + // ------------------------------------------------------------------ // + // TemplateLogger adapter interface + // ------------------------------------------------------------------ // + + static void write(const char * /*legacy*/, const size_t /*legacy*/) { + // ts_exit is written by writeEpilogue; nothing to do here. + } + + /** + * Called for every LOG(...) inside a syscall scope. + * Flushes the previous pending entry with a comma, then buffers this + * event object WITHOUT a trailing newline. + */ + static void printFormatted(unsigned long int timestamp, + const char *invoker, const char *file, int line, + const char * /*output_template*/, + const char *message_format, va_list args) { + + char escaped_args[CAPIO_LOG_MAX_MSG_LEN * 6]; + expandAndEscape(message_format, args, escaped_args, + static_cast(sizeof(escaped_args))); + + char escaped_invoker[512]; + jsonEscape(invoker, static_cast(::strlen(invoker)), + escaped_invoker, static_cast(sizeof(escaped_invoker))); + + char escaped_file[512]; + jsonEscape(file, static_cast(::strlen(file)), + escaped_file, static_cast(sizeof(escaped_file))); + + flushPending(true); // previous sibling gets a comma + + const int indent = indentSize(); + char *p = JsonLogBase::pendingBuf; + for (int i = 0; i < indent; ++i) { *p++ = ' '; } + p += ::snprintf(p, + sizeof(JsonLogBase::pendingBuf) - static_cast(indent) - 1, + "{ \"ts\": %lu, \"invoker\": \"%s\"," + " \"file\": \"%s\", \"line\": %d, \"args\": \"%s\" }", + timestamp, escaped_invoker, escaped_file, line, escaped_args); + JsonLogBase::pendingLen = static_cast( + p - JsonLogBase::pendingBuf); + } + + /** + * Called by TemplateLogger constructor (current_log_level == 1). + * + * Emits the opening of one syscall object: + * { + * "invoker": "...", + * "file": "...", + * "line": N, + * "ts_enter": T, + * "args": "...", + * "events": [ + */ + static void writeOpening(unsigned long int timestamp, + const char *invoker, const char *file, int line, + const char *message_format, va_list args) { + + if (!JsonLogBase::rootArrayOpen) { + Derived::rawWriteStr("[\n"); + JsonLogBase::rootArrayOpen = true; + JsonLogBase::nestingDepth = 1; + } + + char escaped_args[CAPIO_LOG_MAX_MSG_LEN * 6]; + expandAndEscape(message_format, args, escaped_args, + static_cast(sizeof(escaped_args))); + + char escaped_invoker[512]; + jsonEscape(invoker, static_cast(::strlen(invoker)), + escaped_invoker, static_cast(sizeof(escaped_invoker))); + + char escaped_file[512]; + jsonEscape(file, static_cast(::strlen(file)), + escaped_file, static_cast(sizeof(escaped_file))); + + flushPending(true); // flush previous top-level "}" with comma if present + + writeImmediate("{"); + JsonLogBase::nestingDepth++; + + writeField ("\"invoker\"", "\"%s\",", escaped_invoker); + writeField ("\"file\"", "\"%s\",", escaped_file); + writeFieldInt("\"line\"", "%d,", line); + writeFieldUL ("\"ts_enter\"", "%lu,", timestamp); + writeField ("\"args\"", "\"%s\",", escaped_args); + + writeImmediate("\"events\": ["); + JsonLogBase::nestingDepth++; + + JsonLogBase::pendingLen = 0; + } + + /** + * Called by TemplateLogger destructor (current_log_level == 1). + * Closes the events array, emits ts_exit, and stores the closing "}" + * as a new pending line so the next sibling gets a leading comma. + * Receives the timestamp captured at destructor time by TemplateLogger. + */ + static void writeEpilogue(unsigned long int timestamp) { + if (JsonLogBase::nestingDepth < 2) { return; } + + flushPending(false); // last event — no trailing comma + + JsonLogBase::nestingDepth--; + writeImmediate("],"); // close "events" array with trailing comma for ts_exit + + { + char buf[64]; + ::snprintf(buf, sizeof(buf), "\"ts_exit\": %lu", timestamp); + writeImmediate(buf); + } + + JsonLogBase::nestingDepth--; + char *p = JsonLogBase::pendingBuf; + const int indent = indentSize(); + for (int i = 0; i < indent; ++i) { *p++ = ' '; } + *p++ = '}'; + JsonLogBase::pendingLen = static_cast( + p - JsonLogBase::pendingBuf); + } + + protected: + // ------------------------------------------------------------------ // + // Pending-line management + // ------------------------------------------------------------------ // + + static void flushPending(bool withComma) { + if (JsonLogBase::pendingLen <= 0) { return; } + Derived::rawWriteBytes(JsonLogBase::pendingBuf, + JsonLogBase::pendingLen); + Derived::rawWriteStr(withComma ? ",\n" : "\n"); + JsonLogBase::pendingLen = 0; + } + + static void writeImmediate(const char *buf, int len = -1) { + if (len < 0) { len = static_cast(::strlen(buf)); } + const int indent = indentSize(); + char spaces[65] = {0}; + for (int i = 0; i < indent; ++i) { spaces[i] = ' '; } + Derived::rawWriteBytes(spaces, indent); + Derived::rawWriteBytes(buf, len); + Derived::rawWriteStr("\n"); + } + + static void writeField(const char *key, const char *fmt, const char *val) { + char tmp[768]; ::snprintf(tmp, sizeof(tmp), fmt, val); + char buf[1024]; ::snprintf(buf, sizeof(buf), "%s: %s", key, tmp); + writeImmediate(buf); + } + static void writeFieldInt(const char *key, const char *fmt, int val) { + char tmp[64]; ::snprintf(tmp, sizeof(tmp), fmt, val); + char buf[128]; ::snprintf(buf, sizeof(buf), "%s: %s", key, tmp); + writeImmediate(buf); + } + static void writeFieldUL(const char *key, const char *fmt, unsigned long val) { + char tmp[64]; ::snprintf(tmp, sizeof(tmp), fmt, val); + char buf[128]; ::snprintf(buf, sizeof(buf), "%s: %s", key, tmp); + writeImmediate(buf); + } + + static int indentSize() { + const int n = JsonLogBase::nestingDepth * 2; + return n < 64 ? n : 64; + } + + // ------------------------------------------------------------------ // + // String helpers + // ------------------------------------------------------------------ // + + static void expandAndEscape(const char *fmt, va_list args, + char *dst, int dst_size) { + va_list copy; + va_copy(copy, args); + const int raw_len = ::vsnprintf(nullptr, 0, fmt, copy); + va_end(copy); + + std::string raw(static_cast(raw_len) + 1, '\0'); + va_copy(copy, args); + ::vsnprintf(&raw[0], static_cast(raw_len) + 1, fmt, copy); + va_end(copy); + + jsonEscape(raw.c_str(), raw_len, dst, dst_size); + } + + static void jsonEscape(const char *src, int src_len, + char *dst, int dst_size) { + int di = 0; + for (int si = 0; si < src_len && di + 7 < dst_size; ++si) { + const unsigned char c = static_cast(src[si]); + if (c == '"' || c == '\\') { + dst[di++] = '\\'; dst[di++] = static_cast(c); + } else if (c == '\n') { dst[di++] = '\\'; dst[di++] = 'n'; + } else if (c == '\r') { dst[di++] = '\\'; dst[di++] = 'r'; + } else if (c == '\t') { dst[di++] = '\\'; dst[di++] = 't'; + } else if (c < 0x20 || c == 0x7f) { + dst[di++] = '\\'; dst[di++] = 'u'; + dst[di++] = '0'; dst[di++] = '0'; + dst[di++] = hexChar(c >> 4); + dst[di++] = hexChar(c & 0x0fu); + } else { + dst[di++] = static_cast(c); + } + } + dst[di] = '\0'; + } + + static char hexChar(unsigned int n) { + return static_cast(n < 10u ? '0' + n : 'a' + n - 10u); + } +}; + +// Out-of-class definitions for thread_local statics. +// The template parameter ensures PosixLogWriteAdapter and +// ServerLogWriteAdapter each get independent storage. +template +thread_local int JsonLogBase::nestingDepth = 0; + +template +thread_local bool JsonLogBase::rootArrayOpen = false; + +template +thread_local int JsonLogBase::pendingLen = 0; + +template +thread_local char JsonLogBase::pendingBuf[CAPIO_LOG_MAX_MSG_LEN * 6 + 256] = {'\0'}; + +#endif // CAPIO_LOGGER_JSON_BASE_HPP \ No newline at end of file diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp index d37daef2f..43a1b78bd 100644 --- a/capio/common/logger.hpp +++ b/capio/common/logger.hpp @@ -25,7 +25,7 @@ template std::string demangled_name(const T &obj) { return status == 0 ? demangled.get() : mangled; } -inline bool continue_on_error = false; // if set to true, CAPIO does not terminate on ERR_EXIT +inline bool continue_on_error = false; inline void raise_termination(const bool raise_exception, const std::string &message) { if (raise_exception) { @@ -39,15 +39,18 @@ inline void raise_termination(const bool raise_exception, const std::string &mes #include "syscallnames.h" #endif -// this variable tells the logger that syscall logging -// has started, and we are not in setup phase -// FIXME: Remove the inline specifier by splitting into header and source code +#ifdef __CAPIO_POSIX +// POSIX interceptor: logging starts disabled and is enabled explicitly +// after setup to prevent re-entrancy into the interceptor itself. inline thread_local bool enable_logger = false; +#else +// Server / non-POSIX: logging is always active from the first call. +inline thread_local bool enable_logger = true; +#endif -#ifndef CAPIO_MAX_LOG_LEVEL // capio max log level. defaults to -1, where everything is logged +#ifndef CAPIO_MAX_LOG_LEVEL #define CAPIO_MAX_LOG_LEVEL -1 #endif -// FIXME: Remove the inline specifier by splitting into header and source code inline int CAPIO_LOG_LEVEL = CAPIO_MAX_LOG_LEVEL; inline long long current_time_in_millis() { @@ -58,14 +61,9 @@ inline long long current_time_in_millis() { start_time = static_cast(ts.tv_sec) * 1000 + (ts.tv_nsec) / 1000000; } capio_syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts); - const auto time_now = static_cast(ts.tv_sec) * 1000 + (ts.tv_nsec) / 1000000; - return time_now - start_time; + return static_cast(ts.tv_sec) * 1000 + (ts.tv_nsec) / 1000000 - start_time; } -/** - * @brief Class used to suspend the logging capabilities of CAPIO, by setting the logging_syscall - * flag to false at instantiation, and restarting the logging at destruction - */ class SyscallLoggingSuspender { public: SyscallLoggingSuspender() { enable_logger = false; } @@ -73,20 +71,33 @@ class SyscallLoggingSuspender { }; /** - * @brief Class that provides logging capabilities to CAPIO. + * @brief Logging front-end, parameterised on an Adapter for the I/O backend. + * + * Adapter contract + * ---------------- + * // Called once on entry (current_log_level == 1 after increment). + * // Receives all metadata so the adapter can open a structured record. + * static void writeOpening(unsigned long ts, const char *invoker, + * const char *file, int line, + * const char *message_format, va_list args); * - * Parameterised on a LogWriteAdapter so that the I/O strategy (POSIX - * syscalls vs. STL streams vs. any custom backend) is a compile-time - * choice with no virtual dispatch overhead. + * // Called for every LOG() inside a scope. + * static void printFormatted(unsigned long ts, const char *invoker, + * const char *file, int line, + * const char *output_template, + * const char *message_format, va_list args); * - * The default adapter (@ref DefaultLogWriteAdapter) mirrors the behaviour - * of the old monolithic Logger class for both the server and POSIX builds. + * // Called on scope exit to close the structured record. + * static void writeEpilogue(); + * + * // Legacy write — kept for ERR_EXIT paths; adapters may ignore it. + * static void write(const char *buf, size_t len); */ template class TemplateLogger { static thread_local int current_log_level; + char invoker[256]{0}; char file[256]{0}; - char format[CAPIO_LOG_MAX_MSG_LEN]{0}; unsigned int line{0}; long int tid{0}; @@ -95,90 +106,83 @@ template class TemplateLogger { public: TemplateLogger(const char invoker[], const char file[], unsigned int line, long int tid, const char *message, ...) { - current_log_level++; this->tid = tid; this->line = line; - strncpy(this->invoker, invoker, sizeof(this->invoker)); - strncpy(this->file, file, sizeof(this->file)); - - va_list argp, argpc; - va_start(argp, message); - va_copy(argpc, argp); - + strncpy(this->invoker, invoker, sizeof(this->invoker) - 1); + strncpy(this->file, file, sizeof(this->file) - 1); + + // Only track nesting when logging is actually active on this thread. + // If we incremented unconditionally, a START_LOG during the setup + // phase (before ENABLE_LOGGER) would leave current_log_level at 1 + // for the rest of the thread's lifetime, making every subsequent + // top-level call look like a nested one. if (!enable_logger) { return; } - - sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); - const size_t pre_msg_len = strlen(format); - strcpy(format + pre_msg_len, message); - - const int size = vsnprintf(nullptr, 0U, format, argp); - auto buf = reinterpret_cast(capio_syscall(SYS_mmap, nullptr, size + 1, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); - vsnprintf(buf, size + 1, format, argpc); + current_log_level++; if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { + va_list argp; + va_start(argp, message); + if (current_log_level == 1) { - adapter.writeOpening(); + adapter.writeOpening(current_time_in_millis(), this->invoker, this->file, + this->line, message, argp); + } else { + adapter.printFormatted(current_time_in_millis(), this->invoker, this->file, + this->line, CAPIO_LOG_PRE_MSG, message, argp); } - adapter.write(buf, strlen(buf)); - } - va_end(argp); - va_end(argpc); - capio_syscall(SYS_munmap, buf, size); + va_end(argp); + } } TemplateLogger(const TemplateLogger &) = delete; TemplateLogger &operator=(const TemplateLogger &) = delete; ~TemplateLogger() { - sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); - const size_t pre_msg_len = strlen(format); - strcpy(format + pre_msg_len, "returned"); - - adapter.write(format, strlen(format)); + if (!enable_logger) { + return; // level was never incremented for this instance + } - if (current_log_level == 1 && enable_logger && + if (current_log_level == 1 && (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0)) { - adapter.writeEpilogue(); + // Capture the exit timestamp here, as close to the actual return + // as possible, and pass it into the adapter so it doesn't need + // to call current_time_in_millis() itself. + adapter.writeEpilogue(static_cast(current_time_in_millis())); } current_log_level--; } void log(const char *message, ...) { - if (!enable_logger) { return; } - va_list argp, argpc; - - sprintf(format, CAPIO_LOG_PRE_MSG, current_time_in_millis(), this->invoker); - const size_t pre_msg_len = strlen(format); - strcpy(format + pre_msg_len, message); - - va_start(argp, message); - va_copy(argpc, argp); - const int size = vsnprintf(nullptr, 0U, format, argp); - const auto buf = reinterpret_cast( - capio_syscall(SYS_mmap, nullptr, size + 1, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); - vsnprintf(buf, size + 1, format, argpc); - + // current_log_level is only incremented when enable_logger is true, + // so this check is always consistent with the constructor. if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - adapter.write(buf, strlen(buf)); + va_list argp; + va_start(argp, message); + adapter.printFormatted(current_time_in_millis(), this->invoker, this->file, this->line, + CAPIO_LOG_PRE_MSG, message, argp); + va_end(argp); } - - va_end(argp); - va_end(argpc); - capio_syscall(SYS_munmap, buf, size); } std::string getLogFileName() { return adapter.getLogFileName(); } + + /** + * Resets the per-thread nesting counter to zero. + * Called by the server-side START_LOG macro before constructing a new + * Logger, so that every top-level server call always opens a fresh + * JSON object even when invoked from inside an existing Logger scope + * (e.g. the server main loop calling helper functions that also use + * START_LOG). Not used in the POSIX build where nesting is meaningful. + */ + static void reset_log_level() { current_log_level = 0; } }; template inline thread_local int TemplateLogger::current_log_level = 0; @@ -195,30 +199,26 @@ template inline thread_local int TemplateLogger::current_log_lev #define ERR_EXIT(message, ...) ERR_EXIT_EXCEPT_CHOICE(true, message, ##__VA_ARGS__) #define LOG(message, ...) log.log(message, ##__VA_ARGS__) + +// On the POSIX build, enable_logger starts false and is flipped explicitly; +// nesting is meaningful so current_log_level is never reset. +// On the server build, enable_logger starts true (see declaration above) and +// current_log_level is reset to 0 on every START_LOG so that each top-level +// request handler always opens its own JSON object, even when called from +// inside an outer Logger scope. +#ifdef __CAPIO_POSIX +#define START_LOG(tid, message, ...) \ + Logger log(__func__, __FILE__, __LINE__, tid, message, ##__VA_ARGS__) +#else #define START_LOG(tid, message, ...) \ + Logger::reset_log_level(); \ Logger log(__func__, __FILE__, __LINE__, tid, message, ##__VA_ARGS__) +#endif + #define ENABLE_LOGGER() enable_logger = true -#define DISABLE_LOGGER() SyscallLoggingSuspender sls{}; +#define DISABLE_LOGGER() \ + SyscallLoggingSuspender sls {} -/** - * This macro is used to inject code into debug mode. It needs a self calling lambda function, - * that is a lambda in the following form: - * - * [](){}() - * - * For example, a debug code to print a value might be injected in this way: - * - * int value = 10; - * DBG(tid, [](int i){printf("%d", i);}(value) ); - * - * This is useful to print or run debug actions with variables defined outside - * of the scope of the given lambda function Be careful that the code defined inside the DBG - * macro is not compiled when building in Release. - * - * Be even MORE CAREFUL to not use any STL code inside the DBG lambda function as it could be - * captured by syscall_intercept (ie do not use std::cout unless you are 100% sure of what you are - * doing) - */ #define DBG(tid, lambda) \ { \ START_LOG(tid, "[ DBG ]~~~~~~~~~~~~ START ~~~~~~~~~~~~~~[ DBG ]"); \ diff --git a/capio/posix/utils/posix_logger.hpp b/capio/posix/utils/posix_logger.hpp index 2de8e11a7..224bab459 100644 --- a/capio/posix/utils/posix_logger.hpp +++ b/capio/posix/utils/posix_logger.hpp @@ -2,55 +2,68 @@ #define CAPIO_POSIXLOGGER_HPP #include "common/logger.hpp" +#include "common/json_base_logger.hpp" -struct PosixLogWriteAdapter { - static thread_local bool fileOpen; - static thread_local int fileFD; +struct PosixLogWriteAdapter : JsonLogBase { + + static thread_local int fileFD; static thread_local char filePath[PATH_MAX]; - explicit PosixLogWriteAdapter() { + // The constructor is called every time a Logger is constructed (i.e. + // on every START_LOG). It opens the per-thread log file on first call + // and is a no-op on subsequent calls. + explicit PosixLogWriteAdapter() { ensureFileOpen(); } + + // ------------------------------------------------------------------ // + // I/O primitives required by JsonLogBase + // + // These are static because JsonLogBase calls them as Derived::xxx(). + // They call ensureFileOpen() so that even if a new thread reaches a + // static write path before its first constructor call, the file is + // always valid. + // ------------------------------------------------------------------ // + + static void rawWriteBytes(const char *buf, int len) { + ensureFileOpen(); + capio_syscall(SYS_write, fileFD, buf, len); + } - if (fileOpen) { - return; - } + static void rawWriteStr(const char *buf) { + ensureFileOpen(); + capio_syscall(SYS_write, fileFD, buf, strlen(buf)); + } - sprintf(filePath, "%s/%s%ld.log", getHostLogDir(), getLogPrefix(), + private: + // Opens the per-thread log file exactly once per thread. + // Safe to call from both instance constructor and static write paths. + static void ensureFileOpen() { + if (fileFD != -1) { return; } + + sprintf(filePath, "%s/%s%ld.log", + getHostLogDir(), getLogPrefix(), capio_syscall(SYS_gettid)); - capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); + capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); + capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); - fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, O_CREAT | O_WRONLY | O_APPEND, 0644); + fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, + O_CREAT | O_WRONLY | O_APPEND, 0644); if (fileFD == -1) { capio_syscall(SYS_write, fileno(stdout), "Err fopen file: ", strlen("Err fopen file: ")); capio_syscall(SYS_write, fileno(stdout), filePath, strlen(filePath)); - capio_syscall(SYS_write, fileno(stdout), " ", 1); + capio_syscall(SYS_write, fileno(stdout), " ", 1); capio_syscall(SYS_write, fileno(stdout), strerror(errno), strlen(strerror(errno))); - capio_syscall(SYS_write, fileno(stdout), "\n", 1); + capio_syscall(SYS_write, fileno(stdout), "\n", 1); exit(EXIT_FAILURE); } - fileOpen = true; } - static void write(const char *buf, const size_t len) { writeToFD(buf, len); } - - static void writeOpening() { - writeToFD(CAPIO_LOG_POSIX_SYSCALL_START, strlen(CAPIO_LOG_POSIX_SYSCALL_START)); - } - - static void writeEpilogue() { - writeToFD(CAPIO_LOG_POSIX_SYSCALL_END, strlen(CAPIO_LOG_POSIX_SYSCALL_END)); - } - - private: static const char *getHostname() { static char hostname[HOST_NAME_MAX]{'\0'}; - if (hostname[0] == '\0') { - gethostname(hostname, HOST_NAME_MAX); - } + if (hostname[0] == '\0') { gethostname(hostname, HOST_NAME_MAX); } return hostname; } @@ -82,7 +95,7 @@ struct PosixLogWriteAdapter { static char *dir = nullptr; if (dir == nullptr) { const char *base = getLogDir(); - dir = new char[strlen(base) + 7]{0}; // "/posix\0" + dir = new char[strlen(base) + 7]{0}; sprintf(dir, "%s/posix", base); } return dir; @@ -92,21 +105,15 @@ struct PosixLogWriteAdapter { static char *dir = nullptr; if (dir == nullptr) { const char *posixDir = getPosixLogDir(); - dir = new char[strlen(posixDir) + HOST_NAME_MAX]{0}; + dir = new char[strlen(posixDir) + HOST_NAME_MAX]{0}; sprintf(dir, "%s/%s", posixDir, getHostname()); } return dir; } - - static void writeToFD(const char *buf, const size_t len) { - capio_syscall(SYS_write, fileFD, buf, len); - capio_syscall(SYS_write, fileFD, "\n", 1); - } }; -thread_local bool PosixLogWriteAdapter::fileOpen = false; -thread_local int PosixLogWriteAdapter::fileFD = -1; -thread_local char PosixLogWriteAdapter::filePath[PATH_MAX] = {'\0'}; +inline thread_local int PosixLogWriteAdapter::fileFD = -1; +inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX] = {'\0'}; using Logger = TemplateLogger; diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index e98ef9504..ef70188d6 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -114,7 +114,6 @@ static constexpr std::array build_request_handle } int main(int argc, char **argv) { - ENABLE_LOGGER(); Semaphore internal_server_sem(0); diff --git a/capio/server/include/utils/server_logger.hpp b/capio/server/include/utils/server_logger.hpp index a31ecb462..a8ed5db65 100644 --- a/capio/server/include/utils/server_logger.hpp +++ b/capio/server/include/utils/server_logger.hpp @@ -1,66 +1,70 @@ #ifndef CAPIO_SERVERLOGGER_HPP #define CAPIO_SERVERLOGGER_HPP -#include +#include "common/logger.hpp" +#include "common/json_base_logger.hpp" -struct ServerLogWriteAdapter { - private: - std::ofstream logfile; - std::string logFileName; +struct ServerLogWriteAdapter : JsonLogBase { - void writeToStream(const char *buf) { - if (!logfile.is_open()) { - return; - } + static thread_local std::ofstream *logfile; + static thread_local std::string *logFileName; - logfile << buf << std::endl; - logfile.flush(); + explicit ServerLogWriteAdapter() { ensureFileOpen(); } + + std::string getLogFileName() const { + return logFileName ? *logFileName : std::string{}; } - public: - explicit ServerLogWriteAdapter() { + // ------------------------------------------------------------------ // + // I/O primitives required by JsonLogBase + // ------------------------------------------------------------------ // - if (this->logfile.is_open()) { - return; - } - std::string logMasterDirName; - std::string logfilePrefix; + static void rawWriteBytes(const char *buf, int len) { + ensureFileOpen(); + logfile->write(buf, len); + logfile->flush(); + } + + static void rawWriteStr(const char *buf) { + rawWriteBytes(buf, static_cast(strlen(buf))); + } - if (const char *tmp = std::getenv("CAPIO_LOG_DIR"); tmp == nullptr) { - logMasterDirName = CAPIO_DEFAULT_LOG_FOLDER; + private: + static void ensureFileOpen() { + if (logfile != nullptr && logfile->is_open()) { return; } + + std::string logDir; + std::string prefix; + + if (const char *tmp = std::getenv("CAPIO_LOG_DIR"); tmp != nullptr) { + logDir = tmp; } else { - logMasterDirName = tmp; + logDir = CAPIO_DEFAULT_LOG_FOLDER; } - if (const char *tmp = std::getenv("CAPIO_LOG_PREFIX"); tmp == nullptr) { - logfilePrefix = CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX; + if (const char *tmp = std::getenv("CAPIO_LOG_PREFIX"); tmp != nullptr) { + prefix = tmp; } else { - logfilePrefix = tmp; + prefix = CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX; } char hostname[HOST_NAME_MAX]; gethostname(hostname, HOST_NAME_MAX); - const std::filesystem::path outputFolder{logMasterDirName + "/server/" + hostname}; + const std::filesystem::path outputFolder{logDir + "/server/" + hostname}; std::filesystem::create_directories(outputFolder); - const std::filesystem::path logfileName = outputFolder.string() + "/" + logfilePrefix + - std::to_string(capio_syscall(SYS_gettid)) + - ".log"; + const std::filesystem::path path = + outputFolder / (prefix + std::to_string(capio_syscall(SYS_gettid)) + ".log"); - logfile.open(logfileName, std::ofstream::app); - this->logFileName = logfileName; + logfile = new std::ofstream(path, std::ofstream::app); + logFileName = new std::string(path.string()); } - - void write(const char *buf, size_t /*len*/) { writeToStream(buf); } - - const std::string &getLogFileName() { return logFileName; } - - static void writeOpening() {} - - static void writeEpilogue() {} }; +inline thread_local std::ofstream *ServerLogWriteAdapter::logfile = nullptr; +inline thread_local std::string *ServerLogWriteAdapter::logFileName = nullptr; + using Logger = TemplateLogger; -#endif // CAPIO_SERVERLOGGER_HPP +#endif // CAPIO_SERVERLOGGER_HPP \ No newline at end of file From 22ebb81f044e0208a2bf52905c128ff4bea5016e Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 14:00:52 +0200 Subject: [PATCH 17/26] Moved logger to external project --- CMakeLists.txt | 12 + capio/common/constants.hpp | 14 +- capio/common/env.hpp | 8 +- capio/common/filesystem.hpp | 9 +- capio/common/json_base_logger.hpp | 276 ------------------ capio/common/logger.hpp | 250 ---------------- capio/common/queue.hpp | 6 +- capio/common/semaphore.hpp | 6 +- capio/common/shm.hpp | 6 +- capio/posix/CMakeLists.txt | 2 +- capio/posix/libcapio_posix.cpp | 12 +- capio/posix/utils/clone.hpp | 2 +- capio/posix/utils/common.hpp | 2 +- capio/posix/utils/filesystem.hpp | 2 +- capio/posix/utils/posix_logger.hpp | 120 -------- capio/posix/utils/requests.hpp | 2 +- capio/posix/utils/snapshot.hpp | 2 +- capio/server/CMakeLists.txt | 2 +- capio/server/capio_server.cpp | 2 +- .../include/client-manager/client_manager.hpp | 2 +- capio/server/include/remote/handlers/read.hpp | 2 +- capio/server/include/remote/handlers/stat.hpp | 2 +- capio/server/include/remote/listener.hpp | 2 +- capio/server/include/storage/capio_file.hpp | 2 +- capio/server/include/utils/cli_parser.hpp | 1 - capio/server/include/utils/common.hpp | 2 +- capio/server/include/utils/env.hpp | 2 +- capio/server/include/utils/location.hpp | 2 +- capio/server/include/utils/server_logger.hpp | 70 ----- capio/server/include/utils/signals.hpp | 2 +- capio/server/src/backend.cpp | 2 +- capio/server/src/capio_file.cpp | 2 +- capio/server/src/cli_parser.cpp | 20 +- capio/server/src/client_manager.cpp | 2 +- capio/server/src/mpi_backend.cpp | 2 +- capio/server/src/none_backend.cpp | 2 +- capio/server/src/remote_request.cpp | 3 +- capio/server/src/storage_manager.cpp | 2 +- capio/tests/unit/posix/CMakeLists.txt | 2 +- capio/tests/unit/posix/src/realpath.cpp | 2 +- capio/tests/unit/server/CMakeLists.txt | 2 +- capio/tests/unit/syscall/CMakeLists.txt | 2 +- 42 files changed, 78 insertions(+), 789 deletions(-) delete mode 100644 capio/common/json_base_logger.hpp delete mode 100644 capio/common/logger.hpp delete mode 100644 capio/posix/utils/posix_logger.hpp delete mode 100644 capio/server/include/utils/server_logger.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a930e879..bd87b94e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,18 @@ FetchContent_Declare( GIT_TAG v1.4.0 ) +FetchContent_Declare( + captura + GIT_REPOSITORY https://github.com/High-Performance-IO/captura.git + GIT_TAG main +) + +set(CAPTURA_LOG ${CAPIO_LOG} CACHE BOOL "" FORCE) +set(CAPTURA_BUILD_STL ON CACHE BOOL "" FORCE) +set(CAPTURA_BUILD_SYSCALL ON CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(captura) + ##################################### # Targets ##################################### diff --git a/capio/common/constants.hpp b/capio/common/constants.hpp index e775dfd81..7c250610b 100644 --- a/capio/common/constants.hpp +++ b/capio/common/constants.hpp @@ -32,7 +32,6 @@ constexpr char LOG_CAPIO_START_REQUEST[] = "\n+++++++++++ SYSCALL %s constexpr char LOG_CAPIO_END_REQUEST[] = "----------- END SYSCALL ----------\n"; constexpr char CAPIO_SERVER_LOG_START_REQUEST_MSG[] = "+++++++++++++++++REQUEST+++++++++++++++++"; constexpr char CAPIO_SERVER_LOG_END_REQUEST_MSG[] = "~~~~~~~~~~~~~~~END REQUEST~~~~~~~~~~~~~~~"; -constexpr int CAPIO_LOG_MAX_MSG_LEN = 2048; constexpr int CAPIO_SEM_RETRIES = 100; constexpr int THEORETICAL_SIZE_DIRENT64 = sizeof(ino64_t) + sizeof(off64_t) + sizeof(unsigned short) + sizeof(unsigned char) + @@ -44,10 +43,6 @@ constexpr int CAPIO_POSIX_SYSCALL_REQUEST_SKIP = -2; constexpr int CAPIO_POSIX_SYSCALL_SKIP = 1; constexpr int CAPIO_POSIX_SYSCALL_SUCCESS = 0; -// CAPIO logger - common -constexpr char CAPIO_LOG_PRE_MSG[] = "at[%.15llu][%.40s]: "; -constexpr char CAPIO_DEFAULT_LOG_FOLDER[] = "capio_logs\0"; - // CAPIO common - shared memory constant names constexpr char SHM_FIRST_ELEM[] = "_first_elem_"; constexpr char SHM_LAST_ELEM[] = "_last_elem_"; @@ -66,15 +61,8 @@ constexpr char CAPIO_SHM_OPEN_ERROR[] = "Unable to open shared memory segment. Could it be that another instance of capio server is " "running with the same WORKFLOW_NAME?"; -// CAPIO logger - POSIX -constexpr char CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX[] = "posix_thread_\0"; -constexpr char CAPIO_LOG_POSIX_SYSCALL_START[] = "\n+++++++++ START SYSCALL +++++++++"; -constexpr char CAPIO_LOG_POSIX_SYSCALL_END[] = "~~~~~~~~~ END SYSCALL ~~~~~~~~~\n"; - -// CAPIO logger - server -constexpr char CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX[] = "server_thread_\0"; // Note: Ensure CAPIO_VERSION is defined as a string literal, e.g., #define CAPIO_VERSION "1.0.0" -constexpr char CAPIO_LOG_SERVER_BANNER[14][80] = { +constexpr char CAPIO_LOG_SERVER_BANNER[14][80] = { "", "\033[1;34m /$$$$$$ /$$$$$$ /$$$$$$$ \033[0;96m /$$$$$$ /$$$$$$ ", "\033[1;34m /$$__ $$ /$$__ $$| $$__ $$ \033[0;96m|_ $$_/ /$$__ $$", diff --git a/capio/common/env.hpp b/capio/common/env.hpp index 2b76a538c..fb41f3123 100644 --- a/capio/common/env.hpp +++ b/capio/common/env.hpp @@ -6,11 +6,17 @@ #include #include #include - #include +#include "common/constants.hpp" #include "common/syscall.hpp" +#ifdef __CAPIO_POSIX +#include +#else +#include +#endif + // TODO: remove forward declaration of function by splitting into header and impl. capio/common inline bool is_forbidden_path(const std::string_view &path); diff --git a/capio/common/filesystem.hpp b/capio/common/filesystem.hpp index 8098bafe0..89b9f93e9 100644 --- a/capio/common/filesystem.hpp +++ b/capio/common/filesystem.hpp @@ -10,7 +10,12 @@ #include -#include "common/logger.hpp" +#ifdef __CAPIO_POSIX +#include +#else +#include +#endif + #include "common/syscall.hpp" #include "env.hpp" @@ -37,7 +42,7 @@ inline bool in_dir(const std::string &path, const std::string &glob) { inline bool is_directory(int dirfd) { START_LOG(capio_syscall(SYS_gettid), "call(dirfd=%d)", dirfd); - struct stat path_stat {}; + struct stat path_stat{}; int tmp = fstat(dirfd, &path_stat); if (tmp != 0) { LOG("Error at is_directory(dirfd=%d) -> %d: %d (%s)", dirfd, tmp, errno, diff --git a/capio/common/json_base_logger.hpp b/capio/common/json_base_logger.hpp deleted file mode 100644 index bd1fd5066..000000000 --- a/capio/common/json_base_logger.hpp +++ /dev/null @@ -1,276 +0,0 @@ -#ifndef CAPIO_LOGGER_JSON_BASE_HPP -#define CAPIO_LOGGER_JSON_BASE_HPP - -// Pull in everything this header needs directly so it is self-contained -// regardless of include order. -#include // va_list, va_copy, va_end -#include // snprintf, vsnprintf -#include // strlen -#include // std::string (used in expandAndEscape) - -#include "constants.hpp" // CAPIO_LOG_MAX_MSG_LEN, CAPIO_DEFAULT_LOG_FOLDER, … -#include "syscall.hpp" // current_time_in_millis (declared in logger.hpp which - // includes syscall.hpp; guard against double-include - // by relying on header guards in logger.hpp) - -/** - * @file logger_json_base.hpp - * - * CRTP mixin that implements the full structured-JSON adapter interface - * required by TemplateLogger. Derived adapters only need to supply two - * I/O primitives as *static* methods: - * - * static void rawWriteBytes(const char *buf, int len); - * static void rawWriteStr (const char *buf); // NUL-terminated - * - * All JSON structure, indentation, pending-comma handling, and string - * escaping live here, shared between PosixLogWriteAdapter and - * ServerLogWriteAdapter. - */ -template -struct JsonLogBase { - - // ------------------------------------------------------------------ // - // Thread-local JSON state - // ------------------------------------------------------------------ // - - static thread_local int nestingDepth; - static thread_local bool rootArrayOpen; - - // Pending-line buffer: holds the last event/closing-brace WITHOUT its - // trailing newline. Flushed with ",\n" when a sibling follows, plain - // "\n" when the enclosing array closes. Avoids trailing commas on an - // append-only stream without any seek-back. - static thread_local int pendingLen; - static thread_local char pendingBuf[CAPIO_LOG_MAX_MSG_LEN * 6 + 256]; - - // ------------------------------------------------------------------ // - // TemplateLogger adapter interface - // ------------------------------------------------------------------ // - - static void write(const char * /*legacy*/, const size_t /*legacy*/) { - // ts_exit is written by writeEpilogue; nothing to do here. - } - - /** - * Called for every LOG(...) inside a syscall scope. - * Flushes the previous pending entry with a comma, then buffers this - * event object WITHOUT a trailing newline. - */ - static void printFormatted(unsigned long int timestamp, - const char *invoker, const char *file, int line, - const char * /*output_template*/, - const char *message_format, va_list args) { - - char escaped_args[CAPIO_LOG_MAX_MSG_LEN * 6]; - expandAndEscape(message_format, args, escaped_args, - static_cast(sizeof(escaped_args))); - - char escaped_invoker[512]; - jsonEscape(invoker, static_cast(::strlen(invoker)), - escaped_invoker, static_cast(sizeof(escaped_invoker))); - - char escaped_file[512]; - jsonEscape(file, static_cast(::strlen(file)), - escaped_file, static_cast(sizeof(escaped_file))); - - flushPending(true); // previous sibling gets a comma - - const int indent = indentSize(); - char *p = JsonLogBase::pendingBuf; - for (int i = 0; i < indent; ++i) { *p++ = ' '; } - p += ::snprintf(p, - sizeof(JsonLogBase::pendingBuf) - static_cast(indent) - 1, - "{ \"ts\": %lu, \"invoker\": \"%s\"," - " \"file\": \"%s\", \"line\": %d, \"args\": \"%s\" }", - timestamp, escaped_invoker, escaped_file, line, escaped_args); - JsonLogBase::pendingLen = static_cast( - p - JsonLogBase::pendingBuf); - } - - /** - * Called by TemplateLogger constructor (current_log_level == 1). - * - * Emits the opening of one syscall object: - * { - * "invoker": "...", - * "file": "...", - * "line": N, - * "ts_enter": T, - * "args": "...", - * "events": [ - */ - static void writeOpening(unsigned long int timestamp, - const char *invoker, const char *file, int line, - const char *message_format, va_list args) { - - if (!JsonLogBase::rootArrayOpen) { - Derived::rawWriteStr("[\n"); - JsonLogBase::rootArrayOpen = true; - JsonLogBase::nestingDepth = 1; - } - - char escaped_args[CAPIO_LOG_MAX_MSG_LEN * 6]; - expandAndEscape(message_format, args, escaped_args, - static_cast(sizeof(escaped_args))); - - char escaped_invoker[512]; - jsonEscape(invoker, static_cast(::strlen(invoker)), - escaped_invoker, static_cast(sizeof(escaped_invoker))); - - char escaped_file[512]; - jsonEscape(file, static_cast(::strlen(file)), - escaped_file, static_cast(sizeof(escaped_file))); - - flushPending(true); // flush previous top-level "}" with comma if present - - writeImmediate("{"); - JsonLogBase::nestingDepth++; - - writeField ("\"invoker\"", "\"%s\",", escaped_invoker); - writeField ("\"file\"", "\"%s\",", escaped_file); - writeFieldInt("\"line\"", "%d,", line); - writeFieldUL ("\"ts_enter\"", "%lu,", timestamp); - writeField ("\"args\"", "\"%s\",", escaped_args); - - writeImmediate("\"events\": ["); - JsonLogBase::nestingDepth++; - - JsonLogBase::pendingLen = 0; - } - - /** - * Called by TemplateLogger destructor (current_log_level == 1). - * Closes the events array, emits ts_exit, and stores the closing "}" - * as a new pending line so the next sibling gets a leading comma. - * Receives the timestamp captured at destructor time by TemplateLogger. - */ - static void writeEpilogue(unsigned long int timestamp) { - if (JsonLogBase::nestingDepth < 2) { return; } - - flushPending(false); // last event — no trailing comma - - JsonLogBase::nestingDepth--; - writeImmediate("],"); // close "events" array with trailing comma for ts_exit - - { - char buf[64]; - ::snprintf(buf, sizeof(buf), "\"ts_exit\": %lu", timestamp); - writeImmediate(buf); - } - - JsonLogBase::nestingDepth--; - char *p = JsonLogBase::pendingBuf; - const int indent = indentSize(); - for (int i = 0; i < indent; ++i) { *p++ = ' '; } - *p++ = '}'; - JsonLogBase::pendingLen = static_cast( - p - JsonLogBase::pendingBuf); - } - - protected: - // ------------------------------------------------------------------ // - // Pending-line management - // ------------------------------------------------------------------ // - - static void flushPending(bool withComma) { - if (JsonLogBase::pendingLen <= 0) { return; } - Derived::rawWriteBytes(JsonLogBase::pendingBuf, - JsonLogBase::pendingLen); - Derived::rawWriteStr(withComma ? ",\n" : "\n"); - JsonLogBase::pendingLen = 0; - } - - static void writeImmediate(const char *buf, int len = -1) { - if (len < 0) { len = static_cast(::strlen(buf)); } - const int indent = indentSize(); - char spaces[65] = {0}; - for (int i = 0; i < indent; ++i) { spaces[i] = ' '; } - Derived::rawWriteBytes(spaces, indent); - Derived::rawWriteBytes(buf, len); - Derived::rawWriteStr("\n"); - } - - static void writeField(const char *key, const char *fmt, const char *val) { - char tmp[768]; ::snprintf(tmp, sizeof(tmp), fmt, val); - char buf[1024]; ::snprintf(buf, sizeof(buf), "%s: %s", key, tmp); - writeImmediate(buf); - } - static void writeFieldInt(const char *key, const char *fmt, int val) { - char tmp[64]; ::snprintf(tmp, sizeof(tmp), fmt, val); - char buf[128]; ::snprintf(buf, sizeof(buf), "%s: %s", key, tmp); - writeImmediate(buf); - } - static void writeFieldUL(const char *key, const char *fmt, unsigned long val) { - char tmp[64]; ::snprintf(tmp, sizeof(tmp), fmt, val); - char buf[128]; ::snprintf(buf, sizeof(buf), "%s: %s", key, tmp); - writeImmediate(buf); - } - - static int indentSize() { - const int n = JsonLogBase::nestingDepth * 2; - return n < 64 ? n : 64; - } - - // ------------------------------------------------------------------ // - // String helpers - // ------------------------------------------------------------------ // - - static void expandAndEscape(const char *fmt, va_list args, - char *dst, int dst_size) { - va_list copy; - va_copy(copy, args); - const int raw_len = ::vsnprintf(nullptr, 0, fmt, copy); - va_end(copy); - - std::string raw(static_cast(raw_len) + 1, '\0'); - va_copy(copy, args); - ::vsnprintf(&raw[0], static_cast(raw_len) + 1, fmt, copy); - va_end(copy); - - jsonEscape(raw.c_str(), raw_len, dst, dst_size); - } - - static void jsonEscape(const char *src, int src_len, - char *dst, int dst_size) { - int di = 0; - for (int si = 0; si < src_len && di + 7 < dst_size; ++si) { - const unsigned char c = static_cast(src[si]); - if (c == '"' || c == '\\') { - dst[di++] = '\\'; dst[di++] = static_cast(c); - } else if (c == '\n') { dst[di++] = '\\'; dst[di++] = 'n'; - } else if (c == '\r') { dst[di++] = '\\'; dst[di++] = 'r'; - } else if (c == '\t') { dst[di++] = '\\'; dst[di++] = 't'; - } else if (c < 0x20 || c == 0x7f) { - dst[di++] = '\\'; dst[di++] = 'u'; - dst[di++] = '0'; dst[di++] = '0'; - dst[di++] = hexChar(c >> 4); - dst[di++] = hexChar(c & 0x0fu); - } else { - dst[di++] = static_cast(c); - } - } - dst[di] = '\0'; - } - - static char hexChar(unsigned int n) { - return static_cast(n < 10u ? '0' + n : 'a' + n - 10u); - } -}; - -// Out-of-class definitions for thread_local statics. -// The template parameter ensures PosixLogWriteAdapter and -// ServerLogWriteAdapter each get independent storage. -template -thread_local int JsonLogBase::nestingDepth = 0; - -template -thread_local bool JsonLogBase::rootArrayOpen = false; - -template -thread_local int JsonLogBase::pendingLen = 0; - -template -thread_local char JsonLogBase::pendingBuf[CAPIO_LOG_MAX_MSG_LEN * 6 + 256] = {'\0'}; - -#endif // CAPIO_LOGGER_JSON_BASE_HPP \ No newline at end of file diff --git a/capio/common/logger.hpp b/capio/common/logger.hpp deleted file mode 100644 index 43a1b78bd..000000000 --- a/capio/common/logger.hpp +++ /dev/null @@ -1,250 +0,0 @@ -#ifndef CAPIO_COMMON_LOGGER_HPP -#define CAPIO_COMMON_LOGGER_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "constants.hpp" -#include "syscall.hpp" - -template std::string demangled_name(const T &obj) { - int status; - const char *mangled = typeid(obj).name(); - std::unique_ptr demangled( - abi::__cxa_demangle(mangled, nullptr, nullptr, &status), std::free); - return status == 0 ? demangled.get() : mangled; -} - -inline bool continue_on_error = false; - -inline void raise_termination(const bool raise_exception, const std::string &message) { - if (raise_exception) { - throw std::runtime_error(message); - } - capio_syscall(SYS_write, stderr, message.c_str(), message.size()); - exit(EXIT_FAILURE); -} - -#if defined(CAPIO_LOG) && defined(__CAPIO_POSIX) -#include "syscallnames.h" -#endif - -#ifdef __CAPIO_POSIX -// POSIX interceptor: logging starts disabled and is enabled explicitly -// after setup to prevent re-entrancy into the interceptor itself. -inline thread_local bool enable_logger = false; -#else -// Server / non-POSIX: logging is always active from the first call. -inline thread_local bool enable_logger = true; -#endif - -#ifndef CAPIO_MAX_LOG_LEVEL -#define CAPIO_MAX_LOG_LEVEL -1 -#endif -inline int CAPIO_LOG_LEVEL = CAPIO_MAX_LOG_LEVEL; - -inline long long current_time_in_millis() { - timespec ts{}; - static long long start_time = -1; - if (start_time == -1) { - capio_syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts); - start_time = static_cast(ts.tv_sec) * 1000 + (ts.tv_nsec) / 1000000; - } - capio_syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts); - return static_cast(ts.tv_sec) * 1000 + (ts.tv_nsec) / 1000000 - start_time; -} - -class SyscallLoggingSuspender { - public: - SyscallLoggingSuspender() { enable_logger = false; } - ~SyscallLoggingSuspender() { enable_logger = true; } -}; - -/** - * @brief Logging front-end, parameterised on an Adapter for the I/O backend. - * - * Adapter contract - * ---------------- - * // Called once on entry (current_log_level == 1 after increment). - * // Receives all metadata so the adapter can open a structured record. - * static void writeOpening(unsigned long ts, const char *invoker, - * const char *file, int line, - * const char *message_format, va_list args); - * - * // Called for every LOG() inside a scope. - * static void printFormatted(unsigned long ts, const char *invoker, - * const char *file, int line, - * const char *output_template, - * const char *message_format, va_list args); - * - * // Called on scope exit to close the structured record. - * static void writeEpilogue(); - * - * // Legacy write — kept for ERR_EXIT paths; adapters may ignore it. - * static void write(const char *buf, size_t len); - */ -template class TemplateLogger { - static thread_local int current_log_level; - - char invoker[256]{0}; - char file[256]{0}; - unsigned int line{0}; - long int tid{0}; - - Adapter adapter; - - public: - TemplateLogger(const char invoker[], const char file[], unsigned int line, long int tid, - const char *message, ...) { - this->tid = tid; - this->line = line; - strncpy(this->invoker, invoker, sizeof(this->invoker) - 1); - strncpy(this->file, file, sizeof(this->file) - 1); - - // Only track nesting when logging is actually active on this thread. - // If we incremented unconditionally, a START_LOG during the setup - // phase (before ENABLE_LOGGER) would leave current_log_level at 1 - // for the rest of the thread's lifetime, making every subsequent - // top-level call look like a nested one. - if (!enable_logger) { - return; - } - - current_log_level++; - - if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - va_list argp; - va_start(argp, message); - - if (current_log_level == 1) { - adapter.writeOpening(current_time_in_millis(), this->invoker, this->file, - this->line, message, argp); - } else { - adapter.printFormatted(current_time_in_millis(), this->invoker, this->file, - this->line, CAPIO_LOG_PRE_MSG, message, argp); - } - - va_end(argp); - } - } - - TemplateLogger(const TemplateLogger &) = delete; - TemplateLogger &operator=(const TemplateLogger &) = delete; - - ~TemplateLogger() { - if (!enable_logger) { - return; // level was never incremented for this instance - } - - if (current_log_level == 1 && - (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0)) { - // Capture the exit timestamp here, as close to the actual return - // as possible, and pass it into the adapter so it doesn't need - // to call current_time_in_millis() itself. - adapter.writeEpilogue(static_cast(current_time_in_millis())); - } - current_log_level--; - } - - void log(const char *message, ...) { - if (!enable_logger) { - return; - } - - // current_log_level is only incremented when enable_logger is true, - // so this check is always consistent with the constructor. - if (current_log_level < CAPIO_MAX_LOG_LEVEL || CAPIO_MAX_LOG_LEVEL < 0) { - va_list argp; - va_start(argp, message); - adapter.printFormatted(current_time_in_millis(), this->invoker, this->file, this->line, - CAPIO_LOG_PRE_MSG, message, argp); - va_end(argp); - } - } - - std::string getLogFileName() { return adapter.getLogFileName(); } - - /** - * Resets the per-thread nesting counter to zero. - * Called by the server-side START_LOG macro before constructing a new - * Logger, so that every top-level server call always opens a fresh - * JSON object even when invoked from inside an existing Logger scope - * (e.g. the server main loop calling helper functions that also use - * START_LOG). Not used in the POSIX build where nesting is meaningful. - */ - static void reset_log_level() { current_log_level = 0; } -}; - -template inline thread_local int TemplateLogger::current_log_level = 0; - -#ifdef CAPIO_LOG - -#define ERR_EXIT_EXCEPT_CHOICE(raise_exception, message, ...) \ - log.log(message, ##__VA_ARGS__); \ - if (!continue_on_error) { \ - char err_msg[CAPIO_LOG_MAX_MSG_LEN]; \ - snprintf(err_msg, CAPIO_LOG_MAX_MSG_LEN, message, ##__VA_ARGS__); \ - raise_termination(raise_exception, err_msg); \ - } -#define ERR_EXIT(message, ...) ERR_EXIT_EXCEPT_CHOICE(true, message, ##__VA_ARGS__) - -#define LOG(message, ...) log.log(message, ##__VA_ARGS__) - -// On the POSIX build, enable_logger starts false and is flipped explicitly; -// nesting is meaningful so current_log_level is never reset. -// On the server build, enable_logger starts true (see declaration above) and -// current_log_level is reset to 0 on every START_LOG so that each top-level -// request handler always opens its own JSON object, even when called from -// inside an outer Logger scope. -#ifdef __CAPIO_POSIX -#define START_LOG(tid, message, ...) \ - Logger log(__func__, __FILE__, __LINE__, tid, message, ##__VA_ARGS__) -#else -#define START_LOG(tid, message, ...) \ - Logger::reset_log_level(); \ - Logger log(__func__, __FILE__, __LINE__, tid, message, ##__VA_ARGS__) -#endif - -#define ENABLE_LOGGER() enable_logger = true -#define DISABLE_LOGGER() \ - SyscallLoggingSuspender sls {} - -#define DBG(tid, lambda) \ - { \ - START_LOG(tid, "[ DBG ]~~~~~~~~~~~~ START ~~~~~~~~~~~~~~[ DBG ]"); \ - lambda; \ - LOG("[ DBG ]~~~~~~~~~~~~ END ~~~~~~~~~~~~~~[ DBG ]"); \ - } - -#else - -#define ERR_EXIT_EXCEPT_CHOICE(raise_exception, message, ...) \ - if (!continue_on_error) { \ - char err_msg[CAPIO_LOG_MAX_MSG_LEN]; \ - snprintf(err_msg, sizeof(err_msg), message, ##__VA_ARGS__); \ - raise_termination(raise_exception, err_msg); \ - } -#define ERR_EXIT(message, ...) ERR_EXIT_EXCEPT_CHOICE(true, message, ##__VA_ARGS__) -#define LOG(message, ...) -#define START_LOG(tid, message, ...) -#define SEM_CREATE_CHECK(sem, source) \ - if (sem == SEM_FAILED) { \ - __SHM_CHECK_CLI_MSG; \ - } -#define DBG(tid, lambda) -#define ENABLE_LOGGER() -#define DISABLE_LOGGER() - -#endif - -#endif // CAPIO_COMMON_LOGGER_HPP \ No newline at end of file diff --git a/capio/common/queue.hpp b/capio/common/queue.hpp index bd519d464..c11d634f2 100644 --- a/capio/common/queue.hpp +++ b/capio/common/queue.hpp @@ -7,7 +7,11 @@ #include #include "common/env.hpp" -#include "common/logger.hpp" +#ifdef __CAPIO_POSIX +#include +#else +#include +#endif #include "common/semaphore.hpp" #include "common/shm.hpp" #include "filesystem.hpp" diff --git a/capio/common/semaphore.hpp b/capio/common/semaphore.hpp index 73c899e05..72aaf0f85 100644 --- a/capio/common/semaphore.hpp +++ b/capio/common/semaphore.hpp @@ -5,7 +5,11 @@ #include -#include "common/logger.hpp" +#ifdef __CAPIO_POSIX +#include +#else +#include +#endif class NoLock { public: diff --git a/capio/common/shm.hpp b/capio/common/shm.hpp index c425e080b..f1e49b564 100644 --- a/capio/common/shm.hpp +++ b/capio/common/shm.hpp @@ -9,7 +9,11 @@ #include #include -#include "common/logger.hpp" +#ifdef __CAPIO_POSIX +#include +#else +#include +#endif #ifdef __CAPIO_POSIX diff --git a/capio/posix/CMakeLists.txt b/capio/posix/CMakeLists.txt index 7345ca559..651d3027e 100644 --- a/capio/posix/CMakeLists.txt +++ b/capio/posix/CMakeLists.txt @@ -81,7 +81,7 @@ set_target_properties(${TARGET_NAME} PROPERTIES # Link libraries ##################################### target_link_directories(${TARGET_NAME} PRIVATE ${SYSCALL_INTERCEPT_LIB_FOLDER}) -target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept) +target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept captura::syscall) ##################################### # Install rules diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 45b3d9fa7..3a1c688a4 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -7,8 +7,6 @@ #include #include -#include "utils/posix_logger.hpp" - #include "common/syscall.hpp" #include "utils/clone.hpp" @@ -17,6 +15,10 @@ #include "handlers.hpp" +#ifdef CAPIO_LOG +#include "syscallnames.h" +#endif + /** * Handler for syscall not handled and interrupt syscall_intercept */ @@ -349,10 +351,6 @@ static int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, return 1; } -#ifdef CAPIO_LOG - CAPIO_LOG_LEVEL = get_capio_log_level(); -#endif - START_LOG(syscall_no_intercept(SYS_gettid), "call(syscall_number=%ld, syscall_name=%s)", syscall_number, sys_num_to_string(syscall_number).c_str()); @@ -411,5 +409,7 @@ static __attribute__((constructor)) void init() { intercept_hook_point_clone_child = hook_clone_child; intercept_hook_point_clone_parent = hook_clone_parent; intercept_hook_point = hook; + + SyscallLogger::setSyscallFn(syscall_no_intercept); ENABLE_LOGGER(); } \ No newline at end of file diff --git a/capio/posix/utils/clone.hpp b/capio/posix/utils/clone.hpp index 97bc869bf..1f89fb841 100644 --- a/capio/posix/utils/clone.hpp +++ b/capio/posix/utils/clone.hpp @@ -4,7 +4,7 @@ #include "common/syscall.hpp" #include "data.hpp" #include "requests.hpp" -#include "utils/posix_logger.hpp" +#include "SyscallLogger.h" /** * Initialize the required data structures for the new child thread, and then proceed to execute a diff --git a/capio/posix/utils/common.hpp b/capio/posix/utils/common.hpp index d8abba1e7..15195d05e 100644 --- a/capio/posix/utils/common.hpp +++ b/capio/posix/utils/common.hpp @@ -2,7 +2,7 @@ #define CAPIO_FUNCTIONS_H #include -#include "utils/posix_logger.hpp" +#include "SyscallLogger.h" int posix_return_value(long res, long *result) { START_LOG(capio_syscall(SYS_gettid), "cal(res=%ld)", res); diff --git a/capio/posix/utils/filesystem.hpp b/capio/posix/utils/filesystem.hpp index 99c7ba257..4660cba1c 100644 --- a/capio/posix/utils/filesystem.hpp +++ b/capio/posix/utils/filesystem.hpp @@ -13,7 +13,7 @@ #include "common/env.hpp" #include "common/filesystem.hpp" #include "common/syscall.hpp" -#include "utils/posix_logger.hpp" +#include "SyscallLogger.h" #include "types.hpp" diff --git a/capio/posix/utils/posix_logger.hpp b/capio/posix/utils/posix_logger.hpp deleted file mode 100644 index 224bab459..000000000 --- a/capio/posix/utils/posix_logger.hpp +++ /dev/null @@ -1,120 +0,0 @@ -#ifndef CAPIO_POSIXLOGGER_HPP -#define CAPIO_POSIXLOGGER_HPP - -#include "common/logger.hpp" -#include "common/json_base_logger.hpp" - -struct PosixLogWriteAdapter : JsonLogBase { - - static thread_local int fileFD; - static thread_local char filePath[PATH_MAX]; - - // The constructor is called every time a Logger is constructed (i.e. - // on every START_LOG). It opens the per-thread log file on first call - // and is a no-op on subsequent calls. - explicit PosixLogWriteAdapter() { ensureFileOpen(); } - - // ------------------------------------------------------------------ // - // I/O primitives required by JsonLogBase - // - // These are static because JsonLogBase calls them as Derived::xxx(). - // They call ensureFileOpen() so that even if a new thread reaches a - // static write path before its first constructor call, the file is - // always valid. - // ------------------------------------------------------------------ // - - static void rawWriteBytes(const char *buf, int len) { - ensureFileOpen(); - capio_syscall(SYS_write, fileFD, buf, len); - } - - static void rawWriteStr(const char *buf) { - ensureFileOpen(); - capio_syscall(SYS_write, fileFD, buf, strlen(buf)); - } - - private: - // Opens the per-thread log file exactly once per thread. - // Safe to call from both instance constructor and static write paths. - static void ensureFileOpen() { - if (fileFD != -1) { return; } - - sprintf(filePath, "%s/%s%ld.log", - getHostLogDir(), getLogPrefix(), - capio_syscall(SYS_gettid)); - - capio_syscall(SYS_mkdirat, AT_FDCWD, getLogDir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, getPosixLogDir(), 0755); - capio_syscall(SYS_mkdirat, AT_FDCWD, getHostLogDir(), 0755); - - fileFD = capio_syscall(SYS_openat, AT_FDCWD, filePath, - O_CREAT | O_WRONLY | O_APPEND, 0644); - - if (fileFD == -1) { - capio_syscall(SYS_write, fileno(stdout), - "Err fopen file: ", strlen("Err fopen file: ")); - capio_syscall(SYS_write, fileno(stdout), filePath, strlen(filePath)); - capio_syscall(SYS_write, fileno(stdout), " ", 1); - capio_syscall(SYS_write, fileno(stdout), strerror(errno), strlen(strerror(errno))); - capio_syscall(SYS_write, fileno(stdout), "\n", 1); - exit(EXIT_FAILURE); - } - } - - static const char *getHostname() { - static char hostname[HOST_NAME_MAX]{'\0'}; - if (hostname[0] == '\0') { gethostname(hostname, HOST_NAME_MAX); } - return hostname; - } - - static const char *getLogDir() { - static char *dir = nullptr; - if (dir == nullptr) { - dir = std::getenv("CAPIO_LOG_DIR"); - if (dir == nullptr) { - dir = new char[strlen(CAPIO_DEFAULT_LOG_FOLDER) + 1]; - strcpy(dir, CAPIO_DEFAULT_LOG_FOLDER); - } - } - return dir; - } - - static const char *getLogPrefix() { - static char *prefix = nullptr; - if (prefix == nullptr) { - prefix = std::getenv("CAPIO_LOG_PREFIX"); - if (prefix == nullptr) { - prefix = new char[strlen(CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX) + 1]; - strcpy(prefix, CAPIO_LOG_POSIX_DEFAULT_LOG_FILE_PREFIX); - } - } - return prefix; - } - - static const char *getPosixLogDir() { - static char *dir = nullptr; - if (dir == nullptr) { - const char *base = getLogDir(); - dir = new char[strlen(base) + 7]{0}; - sprintf(dir, "%s/posix", base); - } - return dir; - } - - static const char *getHostLogDir() { - static char *dir = nullptr; - if (dir == nullptr) { - const char *posixDir = getPosixLogDir(); - dir = new char[strlen(posixDir) + HOST_NAME_MAX]{0}; - sprintf(dir, "%s/%s", posixDir, getHostname()); - } - return dir; - } -}; - -inline thread_local int PosixLogWriteAdapter::fileFD = -1; -inline thread_local char PosixLogWriteAdapter::filePath[PATH_MAX] = {'\0'}; - -using Logger = TemplateLogger; - -#endif // CAPIO_POSIXLOGGER_HPP \ No newline at end of file diff --git a/capio/posix/utils/requests.hpp b/capio/posix/utils/requests.hpp index da04be22c..93c47ba37 100644 --- a/capio/posix/utils/requests.hpp +++ b/capio/posix/utils/requests.hpp @@ -6,7 +6,7 @@ #include "env.hpp" #include "filesystem.hpp" #include "types.hpp" -#include "utils/posix_logger.hpp" +#include "SyscallLogger.h" inline thread_local CircularBuffer *buf_requests; inline thread_local CircularBuffer *buff_response; diff --git a/capio/posix/utils/snapshot.hpp b/capio/posix/utils/snapshot.hpp index 1f06142ba..838ee74eb 100644 --- a/capio/posix/utils/snapshot.hpp +++ b/capio/posix/utils/snapshot.hpp @@ -6,7 +6,7 @@ #include #include "types.hpp" -#include "utils/posix_logger.hpp" +#include "SyscallLogger.h" inline int *get_fd_snapshot(long tid) { return static_cast(get_shm_if_exist("capio_snapshot_" + std::to_string(tid))); diff --git a/capio/server/CMakeLists.txt b/capio/server/CMakeLists.txt index 701789550..696db0c4b 100644 --- a/capio/server/CMakeLists.txt +++ b/capio/server/CMakeLists.txt @@ -54,7 +54,7 @@ ENDIF (MPI_LINK_FLAGS) ##################################### # Link libraries ##################################### -target_link_libraries(${TARGET_NAME} PRIVATE ${MPI_LIBRARIES} pthread rt stdc++fs libcapio_cl args) +target_link_libraries(${TARGET_NAME} PRIVATE ${MPI_LIBRARIES} pthread rt stdc++fs libcapio_cl args captura::stl) ##################################### # Install rules diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index ef70188d6..010a38594 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -17,7 +17,7 @@ #include #include -#include "utils/server_logger.hpp" +#include #include "capiocl.hpp" #include "capiocl/engine.h" diff --git a/capio/server/include/client-manager/client_manager.hpp b/capio/server/include/client-manager/client_manager.hpp index 4ea53b70d..adf61b364 100644 --- a/capio/server/include/client-manager/client_manager.hpp +++ b/capio/server/include/client-manager/client_manager.hpp @@ -5,7 +5,7 @@ #include #include -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "common/queue.hpp" /** diff --git a/capio/server/include/remote/handlers/read.hpp b/capio/server/include/remote/handlers/read.hpp index 8c56321b9..7f5904de1 100644 --- a/capio/server/include/remote/handlers/read.hpp +++ b/capio/server/include/remote/handlers/read.hpp @@ -4,7 +4,7 @@ #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" extern StorageManager *storage_manager; diff --git a/capio/server/include/remote/handlers/stat.hpp b/capio/server/include/remote/handlers/stat.hpp index 28219760c..0115ae07a 100644 --- a/capio/server/include/remote/handlers/stat.hpp +++ b/capio/server/include/remote/handlers/stat.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" extern StorageManager *storage_manager; extern ClientManager *client_manager; diff --git a/capio/server/include/remote/listener.hpp b/capio/server/include/remote/listener.hpp index 747c20efc..00cb321f7 100644 --- a/capio/server/include/remote/listener.hpp +++ b/capio/server/include/remote/listener.hpp @@ -1,7 +1,7 @@ #ifndef CAPIO_SERVER_REMOTE_LISTENER_HPP #define CAPIO_SERVER_REMOTE_LISTENER_HPP -#include "common/logger.hpp" +#include "StlLogger.h" #include "remote/backend.hpp" #include "remote/backend/include.hpp" diff --git a/capio/server/include/storage/capio_file.hpp b/capio/server/include/storage/capio_file.hpp index 58f3a8afa..fa59cb9e5 100644 --- a/capio/server/include/storage/capio_file.hpp +++ b/capio/server/include/storage/capio_file.hpp @@ -10,7 +10,7 @@ #include #include -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "common/queue.hpp" diff --git a/capio/server/include/utils/cli_parser.hpp b/capio/server/include/utils/cli_parser.hpp index aec16d35f..be588b994 100644 --- a/capio/server/include/utils/cli_parser.hpp +++ b/capio/server/include/utils/cli_parser.hpp @@ -1,6 +1,5 @@ #ifndef CAPIO_CLI_PARSER_HPP #define CAPIO_CLI_PARSER_HPP -#include "utils/server_logger.hpp" #include struct CapioParsedConfig { diff --git a/capio/server/include/utils/common.hpp b/capio/server/include/utils/common.hpp index 067507617..c72ac654b 100644 --- a/capio/server/include/utils/common.hpp +++ b/capio/server/include/utils/common.hpp @@ -10,7 +10,7 @@ #include "common/dirent.hpp" #include "storage/capio_file.hpp" #include "storage/manager.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" extern ClientManager *client_manager; extern StorageManager *storage_manager; diff --git a/capio/server/include/utils/env.hpp b/capio/server/include/utils/env.hpp index 98a9b3a61..95e6aed07 100644 --- a/capio/server/include/utils/env.hpp +++ b/capio/server/include/utils/env.hpp @@ -4,7 +4,7 @@ #include #include "common/constants.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" off64_t get_file_initial_size() { START_LOG(gettid(), "call()"); diff --git a/capio/server/include/utils/location.hpp b/capio/server/include/utils/location.hpp index 9bb4bf8df..dc1cf20c5 100644 --- a/capio/server/include/utils/location.hpp +++ b/capio/server/include/utils/location.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "utils/types.hpp" extern Backend *backend; diff --git a/capio/server/include/utils/server_logger.hpp b/capio/server/include/utils/server_logger.hpp deleted file mode 100644 index a8ed5db65..000000000 --- a/capio/server/include/utils/server_logger.hpp +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef CAPIO_SERVERLOGGER_HPP -#define CAPIO_SERVERLOGGER_HPP - -#include "common/logger.hpp" -#include "common/json_base_logger.hpp" - -struct ServerLogWriteAdapter : JsonLogBase { - - static thread_local std::ofstream *logfile; - static thread_local std::string *logFileName; - - explicit ServerLogWriteAdapter() { ensureFileOpen(); } - - std::string getLogFileName() const { - return logFileName ? *logFileName : std::string{}; - } - - // ------------------------------------------------------------------ // - // I/O primitives required by JsonLogBase - // ------------------------------------------------------------------ // - - static void rawWriteBytes(const char *buf, int len) { - ensureFileOpen(); - logfile->write(buf, len); - logfile->flush(); - } - - static void rawWriteStr(const char *buf) { - rawWriteBytes(buf, static_cast(strlen(buf))); - } - - private: - static void ensureFileOpen() { - if (logfile != nullptr && logfile->is_open()) { return; } - - std::string logDir; - std::string prefix; - - if (const char *tmp = std::getenv("CAPIO_LOG_DIR"); tmp != nullptr) { - logDir = tmp; - } else { - logDir = CAPIO_DEFAULT_LOG_FOLDER; - } - - if (const char *tmp = std::getenv("CAPIO_LOG_PREFIX"); tmp != nullptr) { - prefix = tmp; - } else { - prefix = CAPIO_SERVER_DEFAULT_LOG_FILE_PREFIX; - } - - char hostname[HOST_NAME_MAX]; - gethostname(hostname, HOST_NAME_MAX); - - const std::filesystem::path outputFolder{logDir + "/server/" + hostname}; - std::filesystem::create_directories(outputFolder); - - const std::filesystem::path path = - outputFolder / (prefix + std::to_string(capio_syscall(SYS_gettid)) + ".log"); - - logfile = new std::ofstream(path, std::ofstream::app); - logFileName = new std::string(path.string()); - } -}; - -inline thread_local std::ofstream *ServerLogWriteAdapter::logfile = nullptr; -inline thread_local std::string *ServerLogWriteAdapter::logFileName = nullptr; - -using Logger = TemplateLogger; - -#endif // CAPIO_SERVERLOGGER_HPP \ No newline at end of file diff --git a/capio/server/include/utils/signals.hpp b/capio/server/include/utils/signals.hpp index 030924d23..12afe6a39 100644 --- a/capio/server/include/utils/signals.hpp +++ b/capio/server/include/utils/signals.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" #include "server_println.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #ifdef CAPIO_COVERAGE extern "C" void __gcov_dump(void); diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index 96f69a0cf..451b38cd2 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend.hpp" #include "utils/common.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "utils/server_println.hpp" #include diff --git a/capio/server/src/capio_file.cpp b/capio/server/src/capio_file.cpp index 0aa478863..01dcfd688 100644 --- a/capio/server/src/capio_file.cpp +++ b/capio/server/src/capio_file.cpp @@ -1,6 +1,6 @@ #include "server/include/storage/capio_file.hpp" -#include "common/logger.hpp" +#include #include "remote/backend.hpp" #include "server/include/utils/common.hpp" #include "server/include/utils/server_println.hpp" diff --git a/capio/server/src/cli_parser.cpp b/capio/server/src/cli_parser.cpp index 50f2b9693..0ac0f6ef9 100644 --- a/capio/server/src/cli_parser.cpp +++ b/capio/server/src/cli_parser.cpp @@ -1,7 +1,7 @@ #include "utils/cli_parser.hpp" +#include "StlLogger.h" #include "common/constants.hpp" -#include "common/logger.hpp" #include "utils/common.hpp" #include "utils/server_println.hpp" @@ -111,24 +111,6 @@ CapioParsedConfig parseCLI(int argc, char **argv) { server_println("CAPIO_DIR=" + get_capio_dir().string(), get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, __func__); -#ifdef CAPIO_LOG - CAPIO_LOG_LEVEL = get_capio_log_level(); - server_println("LOG_LEVEL set to: " + std::to_string(CAPIO_LOG_LEVEL), - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, __func__); - for (const auto &msg : CAPIO_LOG_SERVER_CLI_LOGGING_ENABLED_WARNING) { - server_println(msg, get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, - __func__); - } - - log->log("LOG_LEVEL set to: %d", CAPIO_LOG_LEVEL); - delete log; -#else - if (std::getenv("CAPIO_LOG_LEVEL") != nullptr) { - server_println(CAPIO_LOG_SERVER_CLI_LOGGING_NOT_AVAILABLE, get_capio_workflow_name(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, __func__); - } -#endif - // Backend selection phase if (backend_flag) { capio_config.backend_name = args::get(backend_flag); diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index 3548f1a21..c22149114 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -6,7 +6,7 @@ #include "common/queue.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "utils/server_println.hpp" ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToServerName, diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index 57093f1ce..93e17afc7 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend/mpi.hpp" #include "utils/capiocl_adapter.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "utils/server_println.hpp" MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) { diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index 5f6327584..bb6c96782 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -3,7 +3,7 @@ #include "remote/backend/none.hpp" #include "utils/capiocl_adapter.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "utils/server_println.hpp" NoneBackend::NoneBackend(int argc, char **argv) : Backend(HOST_NAME_MAX) { diff --git a/capio/server/src/remote_request.cpp b/capio/server/src/remote_request.cpp index 60bab8db5..fe1279b3c 100644 --- a/capio/server/src/remote_request.cpp +++ b/capio/server/src/remote_request.cpp @@ -1,6 +1,7 @@ +#include "common/constants.hpp" #include "remote/backend.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" RemoteRequest::RemoteRequest(char *buf_recv, const std::string &source) : _source(source) { START_LOG(gettid(), "call(buf_recv=%s, source=%s)", buf_recv, source.c_str()); diff --git a/capio/server/src/storage_manager.cpp b/capio/server/src/storage_manager.cpp index 13f7517eb..d64506a28 100644 --- a/capio/server/src/storage_manager.cpp +++ b/capio/server/src/storage_manager.cpp @@ -11,7 +11,7 @@ #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" #include "utils/location.hpp" -#include "utils/server_logger.hpp" +#include "StlLogger.h" #include "utils/shared_mutex.hpp" #include "utils/types.hpp" diff --git a/capio/tests/unit/posix/CMakeLists.txt b/capio/tests/unit/posix/CMakeLists.txt index 092bd62c9..cbfbe68c9 100644 --- a/capio/tests/unit/posix/CMakeLists.txt +++ b/capio/tests/unit/posix/CMakeLists.txt @@ -46,7 +46,7 @@ target_include_directories(${TARGET_NAME} PRIVATE # Link libraries ##################################### target_link_directories(${TARGET_NAME} PRIVATE ${SYSCALL_INTERCEPT_LIB_FOLDER}) -target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept GTest::gtest_main) +target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept GTest::gtest_main captura::syscall) ##################################### # Configure tests diff --git a/capio/tests/unit/posix/src/realpath.cpp b/capio/tests/unit/posix/src/realpath.cpp index 004f9e62c..33f1f6f77 100644 --- a/capio/tests/unit/posix/src/realpath.cpp +++ b/capio/tests/unit/posix/src/realpath.cpp @@ -4,7 +4,7 @@ #include -#include "posix/utils/posix_logger.hpp" +#include "SyscallLogger.h" #include "utils/filesystem.hpp" diff --git a/capio/tests/unit/server/CMakeLists.txt b/capio/tests/unit/server/CMakeLists.txt index ca41e31bc..494fa5ff2 100644 --- a/capio/tests/unit/server/CMakeLists.txt +++ b/capio/tests/unit/server/CMakeLists.txt @@ -37,7 +37,7 @@ target_include_directories(${TARGET_NAME} PRIVATE ##################################### # Link libraries ##################################### -target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest_main rt libcapio_cl MPI::MPI_CXX args) +target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest_main rt libcapio_cl MPI::MPI_CXX args captura::stl) ##################################### # Configure tests diff --git a/capio/tests/unit/syscall/CMakeLists.txt b/capio/tests/unit/syscall/CMakeLists.txt index abfbff475..89517a0e1 100644 --- a/capio/tests/unit/syscall/CMakeLists.txt +++ b/capio/tests/unit/syscall/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable(${TARGET_NAME} ${TARGET_SOURCES}) ##################################### # Link libraries ##################################### -target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest) +target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest captura::syscall) ##################################### # Configure tests From f8dd8a8d9c332f7ed7994de2e42201698acfddf1 Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 14:20:43 +0200 Subject: [PATCH 18/26] Refactor namespace --- capio/common/env.hpp | 4 ++-- capio/common/filesystem.hpp | 4 ++-- capio/common/queue.hpp | 4 ++-- capio/common/semaphore.hpp | 4 ++-- capio/common/shm.hpp | 4 ++-- capio/posix/utils/clone.hpp | 2 +- capio/posix/utils/common.hpp | 2 +- capio/posix/utils/filesystem.hpp | 2 +- capio/posix/utils/requests.hpp | 2 +- capio/posix/utils/snapshot.hpp | 2 +- capio/server/capio_server.cpp | 2 +- capio/server/include/client-manager/client_manager.hpp | 2 +- capio/server/include/remote/handlers/read.hpp | 2 +- capio/server/include/remote/handlers/stat.hpp | 2 +- capio/server/include/remote/listener.hpp | 3 +-- capio/server/include/storage/capio_file.hpp | 2 +- capio/server/include/utils/common.hpp | 2 +- capio/server/include/utils/env.hpp | 2 +- capio/server/include/utils/location.hpp | 2 +- capio/server/include/utils/signals.hpp | 2 +- capio/server/src/backend.cpp | 2 +- capio/server/src/capio_file.cpp | 2 +- capio/server/src/cli_parser.cpp | 2 +- capio/server/src/client_manager.cpp | 2 +- capio/server/src/mpi_backend.cpp | 2 +- capio/server/src/none_backend.cpp | 2 +- capio/server/src/remote_request.cpp | 2 +- capio/server/src/storage_manager.cpp | 2 +- capio/tests/unit/posix/src/realpath.cpp | 2 +- 29 files changed, 34 insertions(+), 35 deletions(-) diff --git a/capio/common/env.hpp b/capio/common/env.hpp index fb41f3123..d5bc9fb36 100644 --- a/capio/common/env.hpp +++ b/capio/common/env.hpp @@ -12,9 +12,9 @@ #include "common/syscall.hpp" #ifdef __CAPIO_POSIX -#include +#include "captura/SyscallLogger.h" #else -#include +#include "captura/StlLogger.h" #endif // TODO: remove forward declaration of function by splitting into header and impl. capio/common diff --git a/capio/common/filesystem.hpp b/capio/common/filesystem.hpp index 89b9f93e9..250720796 100644 --- a/capio/common/filesystem.hpp +++ b/capio/common/filesystem.hpp @@ -11,9 +11,9 @@ #include #ifdef __CAPIO_POSIX -#include +#include "captura/SyscallLogger.h" #else -#include +#include "captura/StlLogger.h" #endif #include "common/syscall.hpp" diff --git a/capio/common/queue.hpp b/capio/common/queue.hpp index c11d634f2..d141a6377 100644 --- a/capio/common/queue.hpp +++ b/capio/common/queue.hpp @@ -8,9 +8,9 @@ #include "common/env.hpp" #ifdef __CAPIO_POSIX -#include +#include "captura/SyscallLogger.h" #else -#include +#include "captura/StlLogger.h" #endif #include "common/semaphore.hpp" #include "common/shm.hpp" diff --git a/capio/common/semaphore.hpp b/capio/common/semaphore.hpp index 72aaf0f85..5b730d3fe 100644 --- a/capio/common/semaphore.hpp +++ b/capio/common/semaphore.hpp @@ -6,9 +6,9 @@ #include #ifdef __CAPIO_POSIX -#include +#include "captura/SyscallLogger.h" #else -#include +#include "captura/StlLogger.h" #endif class NoLock { diff --git a/capio/common/shm.hpp b/capio/common/shm.hpp index f1e49b564..081299044 100644 --- a/capio/common/shm.hpp +++ b/capio/common/shm.hpp @@ -10,9 +10,9 @@ #include #ifdef __CAPIO_POSIX -#include +#include "captura/SyscallLogger.h" #else -#include +#include "captura/StlLogger.h" #endif #ifdef __CAPIO_POSIX diff --git a/capio/posix/utils/clone.hpp b/capio/posix/utils/clone.hpp index 1f89fb841..ae27658fc 100644 --- a/capio/posix/utils/clone.hpp +++ b/capio/posix/utils/clone.hpp @@ -4,7 +4,7 @@ #include "common/syscall.hpp" #include "data.hpp" #include "requests.hpp" -#include "SyscallLogger.h" +#include "captura/SyscallLogger.h" /** * Initialize the required data structures for the new child thread, and then proceed to execute a diff --git a/capio/posix/utils/common.hpp b/capio/posix/utils/common.hpp index 15195d05e..f5038675f 100644 --- a/capio/posix/utils/common.hpp +++ b/capio/posix/utils/common.hpp @@ -2,7 +2,7 @@ #define CAPIO_FUNCTIONS_H #include -#include "SyscallLogger.h" +#include "captura/SyscallLogger.h" int posix_return_value(long res, long *result) { START_LOG(capio_syscall(SYS_gettid), "cal(res=%ld)", res); diff --git a/capio/posix/utils/filesystem.hpp b/capio/posix/utils/filesystem.hpp index 4660cba1c..a92385511 100644 --- a/capio/posix/utils/filesystem.hpp +++ b/capio/posix/utils/filesystem.hpp @@ -13,7 +13,7 @@ #include "common/env.hpp" #include "common/filesystem.hpp" #include "common/syscall.hpp" -#include "SyscallLogger.h" +#include "captura/SyscallLogger.h" #include "types.hpp" diff --git a/capio/posix/utils/requests.hpp b/capio/posix/utils/requests.hpp index 93c47ba37..5fa0aa68e 100644 --- a/capio/posix/utils/requests.hpp +++ b/capio/posix/utils/requests.hpp @@ -6,7 +6,7 @@ #include "env.hpp" #include "filesystem.hpp" #include "types.hpp" -#include "SyscallLogger.h" +#include "captura/SyscallLogger.h" inline thread_local CircularBuffer *buf_requests; inline thread_local CircularBuffer *buff_response; diff --git a/capio/posix/utils/snapshot.hpp b/capio/posix/utils/snapshot.hpp index 838ee74eb..0e254a2af 100644 --- a/capio/posix/utils/snapshot.hpp +++ b/capio/posix/utils/snapshot.hpp @@ -6,7 +6,7 @@ #include #include "types.hpp" -#include "SyscallLogger.h" +#include "captura/SyscallLogger.h" inline int *get_fd_snapshot(long tid) { return static_cast(get_shm_if_exist("capio_snapshot_" + std::to_string(tid))); diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index 010a38594..093fcbb18 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include "captura/StlLogger.h" #include "capiocl.hpp" #include "capiocl/engine.h" diff --git a/capio/server/include/client-manager/client_manager.hpp b/capio/server/include/client-manager/client_manager.hpp index adf61b364..d2603017b 100644 --- a/capio/server/include/client-manager/client_manager.hpp +++ b/capio/server/include/client-manager/client_manager.hpp @@ -5,7 +5,7 @@ #include #include -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "common/queue.hpp" /** diff --git a/capio/server/include/remote/handlers/read.hpp b/capio/server/include/remote/handlers/read.hpp index 7f5904de1..3a2dbfeea 100644 --- a/capio/server/include/remote/handlers/read.hpp +++ b/capio/server/include/remote/handlers/read.hpp @@ -1,10 +1,10 @@ #ifndef CAPIO_SERVER_REMOTE_HANDLERS_READ_HPP #define CAPIO_SERVER_REMOTE_HANDLERS_READ_HPP +#include "captura/StlLogger.h" #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" -#include "StlLogger.h" extern StorageManager *storage_manager; diff --git a/capio/server/include/remote/handlers/stat.hpp b/capio/server/include/remote/handlers/stat.hpp index 0115ae07a..3ab3916da 100644 --- a/capio/server/include/remote/handlers/stat.hpp +++ b/capio/server/include/remote/handlers/stat.hpp @@ -1,11 +1,11 @@ #ifndef CAPIO_SERVER_REMOTE_HANDLERS_STAT_HPP #define CAPIO_SERVER_REMOTE_HANDLERS_STAT_HPP +#include "captura/StlLogger.h" #include "client-manager/client_manager.hpp" #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" -#include "StlLogger.h" extern StorageManager *storage_manager; extern ClientManager *client_manager; diff --git a/capio/server/include/remote/listener.hpp b/capio/server/include/remote/listener.hpp index 00cb321f7..df14763cc 100644 --- a/capio/server/include/remote/listener.hpp +++ b/capio/server/include/remote/listener.hpp @@ -1,8 +1,7 @@ #ifndef CAPIO_SERVER_REMOTE_LISTENER_HPP #define CAPIO_SERVER_REMOTE_LISTENER_HPP -#include "StlLogger.h" - +#include "captura/StlLogger.h" #include "remote/backend.hpp" #include "remote/backend/include.hpp" #include "remote/handlers/read.hpp" diff --git a/capio/server/include/storage/capio_file.hpp b/capio/server/include/storage/capio_file.hpp index fa59cb9e5..d9b3a36a8 100644 --- a/capio/server/include/storage/capio_file.hpp +++ b/capio/server/include/storage/capio_file.hpp @@ -10,7 +10,7 @@ #include #include -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "common/queue.hpp" diff --git a/capio/server/include/utils/common.hpp b/capio/server/include/utils/common.hpp index c72ac654b..c39e6f8cb 100644 --- a/capio/server/include/utils/common.hpp +++ b/capio/server/include/utils/common.hpp @@ -10,7 +10,7 @@ #include "common/dirent.hpp" #include "storage/capio_file.hpp" #include "storage/manager.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" extern ClientManager *client_manager; extern StorageManager *storage_manager; diff --git a/capio/server/include/utils/env.hpp b/capio/server/include/utils/env.hpp index 95e6aed07..14f45fde9 100644 --- a/capio/server/include/utils/env.hpp +++ b/capio/server/include/utils/env.hpp @@ -4,7 +4,7 @@ #include #include "common/constants.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" off64_t get_file_initial_size() { START_LOG(gettid(), "call()"); diff --git a/capio/server/include/utils/location.hpp b/capio/server/include/utils/location.hpp index dc1cf20c5..c7045b597 100644 --- a/capio/server/include/utils/location.hpp +++ b/capio/server/include/utils/location.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "utils/types.hpp" extern Backend *backend; diff --git a/capio/server/include/utils/signals.hpp b/capio/server/include/utils/signals.hpp index 12afe6a39..2fc4c134c 100644 --- a/capio/server/include/utils/signals.hpp +++ b/capio/server/include/utils/signals.hpp @@ -3,9 +3,9 @@ #include +#include "captura/StlLogger.h" #include "remote/backend.hpp" #include "server_println.hpp" -#include "StlLogger.h" #ifdef CAPIO_COVERAGE extern "C" void __gcov_dump(void); diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index 451b38cd2..b0263e98b 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend.hpp" #include "utils/common.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "utils/server_println.hpp" #include diff --git a/capio/server/src/capio_file.cpp b/capio/server/src/capio_file.cpp index 01dcfd688..db3cefa53 100644 --- a/capio/server/src/capio_file.cpp +++ b/capio/server/src/capio_file.cpp @@ -1,6 +1,6 @@ #include "server/include/storage/capio_file.hpp" -#include +#include "captura/StlLogger.h" #include "remote/backend.hpp" #include "server/include/utils/common.hpp" #include "server/include/utils/server_println.hpp" diff --git a/capio/server/src/cli_parser.cpp b/capio/server/src/cli_parser.cpp index 0ac0f6ef9..665e8263a 100644 --- a/capio/server/src/cli_parser.cpp +++ b/capio/server/src/cli_parser.cpp @@ -1,6 +1,6 @@ #include "utils/cli_parser.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "common/constants.hpp" #include "utils/common.hpp" #include "utils/server_println.hpp" diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index c22149114..d1e1db3c4 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -6,7 +6,7 @@ #include "common/queue.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "utils/server_println.hpp" ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToServerName, diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index 93e17afc7..768373787 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend/mpi.hpp" #include "utils/capiocl_adapter.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "utils/server_println.hpp" MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) { diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index bb6c96782..af1ab25d5 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -3,7 +3,7 @@ #include "remote/backend/none.hpp" #include "utils/capiocl_adapter.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" #include "utils/server_println.hpp" NoneBackend::NoneBackend(int argc, char **argv) : Backend(HOST_NAME_MAX) { diff --git a/capio/server/src/remote_request.cpp b/capio/server/src/remote_request.cpp index fe1279b3c..882d96927 100644 --- a/capio/server/src/remote_request.cpp +++ b/capio/server/src/remote_request.cpp @@ -1,7 +1,7 @@ #include "common/constants.hpp" #include "remote/backend.hpp" -#include "StlLogger.h" +#include "captura/StlLogger.h" RemoteRequest::RemoteRequest(char *buf_recv, const std::string &source) : _source(source) { START_LOG(gettid(), "call(buf_recv=%s, source=%s)", buf_recv, source.c_str()); diff --git a/capio/server/src/storage_manager.cpp b/capio/server/src/storage_manager.cpp index d64506a28..48786f3d7 100644 --- a/capio/server/src/storage_manager.cpp +++ b/capio/server/src/storage_manager.cpp @@ -5,13 +5,13 @@ #include "storage/manager.hpp" +#include "captura/StlLogger.h" #include "common/dirent.hpp" #include "common/filesystem.hpp" #include "storage/capio_file.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" #include "utils/location.hpp" -#include "StlLogger.h" #include "utils/shared_mutex.hpp" #include "utils/types.hpp" diff --git a/capio/tests/unit/posix/src/realpath.cpp b/capio/tests/unit/posix/src/realpath.cpp index 33f1f6f77..dd750385e 100644 --- a/capio/tests/unit/posix/src/realpath.cpp +++ b/capio/tests/unit/posix/src/realpath.cpp @@ -4,7 +4,7 @@ #include -#include "SyscallLogger.h" +#include "captura/SyscallLogger.h" #include "utils/filesystem.hpp" From 7deeed8af30c0bf53a7fea0b1672986c26ad99a9 Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 15:05:44 +0200 Subject: [PATCH 19/26] reformat --- capio/posix/libcapio_posix.cpp | 10 +++++++++- capio/server/include/utils/common.hpp | 2 +- capio/server/include/utils/env.hpp | 2 +- capio/server/src/backend.cpp | 2 +- capio/server/src/client_manager.cpp | 2 +- capio/server/src/mpi_backend.cpp | 2 +- capio/server/src/none_backend.cpp | 2 +- 7 files changed, 15 insertions(+), 7 deletions(-) diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 3a1c688a4..8f8ffc958 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -19,6 +19,14 @@ #include "syscallnames.h" #endif +template std::string demangled_name(const T &obj) { + int status; + const char *mangled = typeid(obj).name(); + std::unique_ptr demangled( + abi::__cxa_demangle(mangled, nullptr, nullptr, &status), std::free); + return status == 0 ? demangled.get() : mangled; +} + /** * Handler for syscall not handled and interrupt syscall_intercept */ @@ -410,6 +418,6 @@ static __attribute__((constructor)) void init() { intercept_hook_point_clone_parent = hook_clone_parent; intercept_hook_point = hook; - SyscallLogger::setSyscallFn(syscall_no_intercept); + SET_CAPTURA_SYSCALL_HANDLER(syscall_no_intercept); ENABLE_LOGGER(); } \ No newline at end of file diff --git a/capio/server/include/utils/common.hpp b/capio/server/include/utils/common.hpp index c39e6f8cb..d5e6c1ba4 100644 --- a/capio/server/include/utils/common.hpp +++ b/capio/server/include/utils/common.hpp @@ -6,11 +6,11 @@ #include #include "capiocl_adapter.hpp" +#include "captura/StlLogger.h" #include "client-manager/client_manager.hpp" #include "common/dirent.hpp" #include "storage/capio_file.hpp" #include "storage/manager.hpp" -#include "captura/StlLogger.h" extern ClientManager *client_manager; extern StorageManager *storage_manager; diff --git a/capio/server/include/utils/env.hpp b/capio/server/include/utils/env.hpp index 14f45fde9..cdcc50f82 100644 --- a/capio/server/include/utils/env.hpp +++ b/capio/server/include/utils/env.hpp @@ -3,8 +3,8 @@ #include -#include "common/constants.hpp" #include "captura/StlLogger.h" +#include "common/constants.hpp" off64_t get_file_initial_size() { START_LOG(gettid(), "call()"); diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index b0263e98b..125d9a58d 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend.hpp" -#include "utils/common.hpp" #include "captura/StlLogger.h" +#include "utils/common.hpp" #include "utils/server_println.hpp" #include diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index d1e1db3c4..a733e11fb 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -2,11 +2,11 @@ #include "client-manager/client_manager.hpp" +#include "captura/StlLogger.h" #include "common/constants.hpp" #include "common/queue.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" -#include "captura/StlLogger.h" #include "utils/server_println.hpp" ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToServerName, diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index 768373787..be9c5e2f0 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend/mpi.hpp" -#include "utils/capiocl_adapter.hpp" #include "captura/StlLogger.h" +#include "utils/capiocl_adapter.hpp" #include "utils/server_println.hpp" MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) { diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index af1ab25d5..cfece7082 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -2,8 +2,8 @@ #include "remote/backend/none.hpp" -#include "utils/capiocl_adapter.hpp" #include "captura/StlLogger.h" +#include "utils/capiocl_adapter.hpp" #include "utils/server_println.hpp" NoneBackend::NoneBackend(int argc, char **argv) : Backend(HOST_NAME_MAX) { From 393b8b527383b244904972a7510d37ab5e7d9479 Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 15:10:24 +0200 Subject: [PATCH 20/26] readme --- README.md | 1 + capio/common/filesystem.hpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d73031df9..5037daa8f 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ CAPIO depends on the following software that needs to be manually installed: The following dependencies are automatically fetched during cmake configuration phase, and compiled when required. - [CAPIO-CL](https://github.com/High-Performance-IO/CAPIO-CL) To handle the CAPIO-CL configuration and enforce streaming directives +- [CAPTURA](https://github.com/High-Performance-IO/CAPTURA) To manage logging and CLI printing - [alpha-unito/syscall_intercept](https://github.com/alpha-unito/syscall_intercept) to intercept syscalls (forked from ```pmem/syscall_interept```) - [Taywee/args](https://github.com/Taywee/args) to parse server command line inputs diff --git a/capio/common/filesystem.hpp b/capio/common/filesystem.hpp index 250720796..0b64a7998 100644 --- a/capio/common/filesystem.hpp +++ b/capio/common/filesystem.hpp @@ -42,7 +42,7 @@ inline bool in_dir(const std::string &path, const std::string &glob) { inline bool is_directory(int dirfd) { START_LOG(capio_syscall(SYS_gettid), "call(dirfd=%d)", dirfd); - struct stat path_stat{}; + struct stat path_stat {}; int tmp = fstat(dirfd, &path_stat); if (tmp != 0) { LOG("Error at is_directory(dirfd=%d) -> %d: %d (%s)", dirfd, tmp, errno, From 19ee15f07d1211a56bb3061f63f371e79e2f6417 Mon Sep 17 00:00:00 2001 From: marcoSanti Date: Wed, 20 May 2026 15:23:54 +0200 Subject: [PATCH 21/26] code-reformat --- capio/posix/utils/clone.hpp | 2 +- capio/posix/utils/filesystem.hpp | 2 +- capio/posix/utils/requests.hpp | 2 +- capio/posix/utils/snapshot.hpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/capio/posix/utils/clone.hpp b/capio/posix/utils/clone.hpp index ae27658fc..6674ab88f 100644 --- a/capio/posix/utils/clone.hpp +++ b/capio/posix/utils/clone.hpp @@ -1,10 +1,10 @@ #ifndef CAPIO_POSIX_UTILS_CLONE_HPP #define CAPIO_POSIX_UTILS_CLONE_HPP +#include "captura/SyscallLogger.h" #include "common/syscall.hpp" #include "data.hpp" #include "requests.hpp" -#include "captura/SyscallLogger.h" /** * Initialize the required data structures for the new child thread, and then proceed to execute a diff --git a/capio/posix/utils/filesystem.hpp b/capio/posix/utils/filesystem.hpp index a92385511..68c328cb3 100644 --- a/capio/posix/utils/filesystem.hpp +++ b/capio/posix/utils/filesystem.hpp @@ -10,10 +10,10 @@ #include +#include "captura/SyscallLogger.h" #include "common/env.hpp" #include "common/filesystem.hpp" #include "common/syscall.hpp" -#include "captura/SyscallLogger.h" #include "types.hpp" diff --git a/capio/posix/utils/requests.hpp b/capio/posix/utils/requests.hpp index 5fa0aa68e..ffa3c6e72 100644 --- a/capio/posix/utils/requests.hpp +++ b/capio/posix/utils/requests.hpp @@ -3,10 +3,10 @@ #include "common/requests.hpp" +#include "captura/SyscallLogger.h" #include "env.hpp" #include "filesystem.hpp" #include "types.hpp" -#include "captura/SyscallLogger.h" inline thread_local CircularBuffer *buf_requests; inline thread_local CircularBuffer *buff_response; diff --git a/capio/posix/utils/snapshot.hpp b/capio/posix/utils/snapshot.hpp index 0e254a2af..aa442341f 100644 --- a/capio/posix/utils/snapshot.hpp +++ b/capio/posix/utils/snapshot.hpp @@ -5,8 +5,8 @@ #include #include -#include "types.hpp" #include "captura/SyscallLogger.h" +#include "types.hpp" inline int *get_fd_snapshot(long tid) { return static_cast(get_shm_if_exist("capio_snapshot_" + std::to_string(tid))); From 7b05c77b587877cfbcc75adc9359b3037f2a6308 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Wed, 20 May 2026 17:33:00 +0200 Subject: [PATCH 22/26] fix --- capio/common/constants.hpp | 7 ---- capio/common/env.hpp | 19 +++++---- capio/common/shm.hpp | 19 ++++----- capio/server/capio_server.cpp | 16 +++++--- capio/server/include/remote/listener.hpp | 31 +++++++------- capio/server/include/utils/server_println.hpp | 38 ------------------ capio/server/include/utils/signals.hpp | 22 +++++----- capio/server/src/backend.cpp | 9 ++--- capio/server/src/capio_file.cpp | 8 ++-- capio/server/src/cli_parser.cpp | 40 +++++++++---------- capio/server/src/client_manager.cpp | 32 ++++++--------- capio/server/src/mpi_backend.cpp | 16 ++++---- capio/server/src/none_backend.cpp | 5 +-- capio/server/src/storage_manager.cpp | 8 ++-- 14 files changed, 103 insertions(+), 167 deletions(-) delete mode 100644 capio/server/include/utils/server_println.hpp diff --git a/capio/common/constants.hpp b/capio/common/constants.hpp index 7c250610b..07fd0ed61 100644 --- a/capio/common/constants.hpp +++ b/capio/common/constants.hpp @@ -134,11 +134,4 @@ constexpr char CAPIO_SERVER_ARG_PARSER_CONFIG_BACKEND_HELP[] = "Backend used in CAPIO. The value [backend] can be one of the following implemented backends: " "\n\t> mpi \n\t> mpisync \n\t> none (default)"; -// Cli pre messages -constexpr char CAPIO_LOG_SERVER_CLI_LEVEL_RESET[] = "\033[0m"; -constexpr char CAPIO_LOG_SERVER_CLI_LEVEL_STATUS[] = "\033[1;34m"; -constexpr char CAPIO_LOG_SERVER_CLI_LEVEL_INFO[] = "\033[1;32m"; -constexpr char CAPIO_LOG_SERVER_CLI_LEVEL_WARNING[] = "\033[1;33m"; -constexpr char CAPIO_LOG_SERVER_CLI_LEVEL_ERROR[] = "\033[1;31m"; - #endif // CAPIO_COMMON_CONSTANTS_HPP diff --git a/capio/common/env.hpp b/capio/common/env.hpp index d5bc9fb36..aaf48fa31 100644 --- a/capio/common/env.hpp +++ b/capio/common/env.hpp @@ -11,6 +11,8 @@ #include "common/constants.hpp" #include "common/syscall.hpp" +#include + #ifdef __CAPIO_POSIX #include "captura/SyscallLogger.h" #else @@ -31,19 +33,20 @@ inline const std::filesystem::path &get_capio_dir() { if (val == nullptr) { - std::cout << "\n" - << CAPIO_LOG_SERVER_CLI_LEVEL_ERROR << "Fatal: CAPIO_DIR not provided!" - << std::endl; +#ifndef __CAPIO_POSIX + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "CAPIO_DIR not provided"); +#endif ERR_EXIT("Fatal: CAPIO_DIR not provided!"); } else { const char *realpath_res = capio_realpath(val, buf.get()); if (realpath_res == nullptr) { - std::cout << "\n" - << CAPIO_LOG_SERVER_CLI_LEVEL_ERROR - << "Fatal: CAPIO_DIR set, but folder does not exists on filesystem!" - << std::endl; +#ifndef __CAPIO_POSIX + CAPTURA_PRINT_COLOR( + CAPTURA_CLI_LEVEL_ERROR, + "Fatal: CAPIO_DIR set, but folder does not exists on filesystem!"); +#endif ERR_EXIT("error CAPIO_DIR: directory %s does not " "exist. [buf=%s]", val, buf.get()); @@ -105,7 +108,7 @@ inline int get_capio_log_level() { } else { auto [ptr, ec] = std::from_chars(log_level, log_level + strlen(log_level), level); if (ec != std::errc()) { - std::cout << CAPIO_LOG_SERVER_CLI_LEVEL_WARNING << "invalid CAPIO_LOG_LEVEL value" + std::cout << CAPTURA_CLI_LEVEL_WARNING << "invalid CAPIO_LOG_LEVEL value" << std::endl; level = 0; } diff --git a/capio/common/shm.hpp b/capio/common/shm.hpp index 081299044..e34ec0ed4 100644 --- a/capio/common/shm.hpp +++ b/capio/common/shm.hpp @@ -29,19 +29,18 @@ #else -#include "utils/server_println.hpp" +#include "captura/StdOutLogger.h" #define SHM_DESTROY_CHECK(source_name) \ if (shm_unlink(source_name) == -1) { \ - std::cout << CAPIO_LOG_SERVER_CLI_LEVEL_WARNING << "Unable to destroy shared mem: '" \ - << source_name << "' (" << strerror(errno) << ")" << std::endl; \ + std::cout << CAPTURA_CLI_LEVEL_WARNING << "Unable to destroy shared mem: '" << source_name \ + << "' (" << strerror(errno) << ")" << std::endl; \ }; #define SHM_CREATE_CHECK(condition, source) \ if (condition) { \ LOG("error while creating %s", source); \ - std::cout << CAPIO_LOG_SERVER_CLI_LEVEL_ERROR << "Unable to create shm: " << source \ - << std::endl; \ + std::cout << CAPTURA_CLI_LEVEL_ERROR << "Unable to create shm: " << source << std::endl; \ ERR_EXIT("Unable to open shm: %s", source); \ }; @@ -63,23 +62,21 @@ class CapioShmCanary { #ifndef __CAPIO_POSIX const auto message = new char[strlen(CAPIO_SHM_CANARY_ERROR)]; sprintf(message, CAPIO_SHM_CANARY_ERROR, _canary_name.data()); - server_println(message, capio_workflow_name, CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, - "CapioShmCanary"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "%s", message); delete[] message; #endif ERR_EXIT("ERR: shm canary flag already exists"); } #ifndef __CAPIO_POSIX - server_println("Created Capio SHM canary: " + _canary_name, capio_workflow_name, - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "CapioShmCanary"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "Created Capio SHM canary: %s", + _canary_name.c_str()); #endif }; ~CapioShmCanary() { START_LOG(capio_syscall(SYS_gettid), "call()"); #ifndef __CAPIO_POSIX - server_println("Removing shared memory canary flag", get_capio_workflow_name(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, "CapioShmCanary"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "Removing shared memory canary flag"); #endif close(_shm_id); SHM_DESTROY_CHECK(_canary_name.c_str()); diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index 093fcbb18..b5dba67a2 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -45,6 +45,8 @@ Backend *backend; #include "remote/listener.hpp" +#include "captura/StdOutLogger.h" + /** * The capio_cl_engine is declared here to ensure that other components of the CAPIO server * can only access it through a const reference. This prevents any modifications to the engine @@ -102,9 +104,7 @@ static constexpr std::array build_request_handle LOG(CAPIO_LOG_SERVER_REQUEST_START); int code = client_manager->readNextRequest(str.get()); if (code < 0 || code > CAPIO_NR_REQUESTS) { - server_println("Received invalid code: " + std::to_string(code), - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, - __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Received invalid code: %d", code); ERR_EXIT("Error: received invalid request code"); } @@ -115,10 +115,13 @@ static constexpr std::array build_request_handle int main(int argc, char **argv) { + StdoutLogger::options.componentName = "SERVER"; + StdoutLogger::options.workflowName = ""; + Semaphore internal_server_sem(0); for (const auto line : CAPIO_LOG_SERVER_BANNER) { - server_println(line, "", "", ""); + CAPTURA_PRINT("%s", line); } const auto configuration = parseCLI(argc, argv); @@ -135,6 +138,8 @@ int main(int argc, char **argv) { capio_cl_engine->setWorkflowName(get_capio_workflow_name()); } + StdoutLogger::options.workflowName = capio_cl_engine->getWorkflowName(); + if (configuration.store_all_in_memory) { capio_cl_engine->setAllStoreInMemory(); } @@ -155,8 +160,7 @@ int main(int argc, char **argv) { LOG("capio_server thread started"); std::thread remote_listener_thread(capio_remote_listener, std::ref(internal_server_sem)); LOG("capio_remote_listener thread started."); - server_println("Server instance successfully started!", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "main"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "Server instance successfully started!"); server_thread.join(); remote_listener_thread.join(); diff --git a/capio/server/include/remote/listener.hpp b/capio/server/include/remote/listener.hpp index df14763cc..2382d5835 100644 --- a/capio/server/include/remote/listener.hpp +++ b/capio/server/include/remote/listener.hpp @@ -1,7 +1,9 @@ #ifndef CAPIO_SERVER_REMOTE_LISTENER_HPP #define CAPIO_SERVER_REMOTE_LISTENER_HPP +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" + #include "remote/backend.hpp" #include "remote/backend/include.hpp" #include "remote/handlers/read.hpp" @@ -26,34 +28,29 @@ inline Backend *select_backend(const std::string &backend_name, int argc, char * if (backend_name.empty() || backend_name == "none") { LOG("backend selected: none"); - server_println( - "Starting CAPIO with default backend (none) as no preferred backend was chosen", - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, - "select_backend"); + CAPTURA_PRINT_COLOR( + CAPTURA_CLI_LEVEL_INFO, + "Starting CAPIO with default backend (none) as no preferred backend was chosen"); return new NoneBackend(argc, argv); } if (backend_name == "mpi") { LOG("backend selected: mpi"); - server_println("Starting CAPIO with MPI backend", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "select_backend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Starting CAPIO with MPI backend"); return new MPIBackend(argc, argv); } if (backend_name == "mpisync") { LOG("backend selected: mpisync"); - server_println("Starting CAPIO with MPI (SYNC) backend", - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, - "select_backend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Starting CAPIO with MPI (SYNC) backend"); return new MPISYNCBackend(argc, argv); } LOG("Backend %s does not exist in CAPIO. Reverting back to the default backend (none)", backend_name.c_str()); - server_println("Backend " + backend_name + - " does not exist. Reverting to the default backend (none)", - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, - "select_backend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, + "Backend %s does not exist. Reverting to the default backend (none)", + backend_name.c_str()); return new NoneBackend(argc, argv); } @@ -64,10 +61,10 @@ inline void capio_remote_listener(Semaphore &internal_server_sem) { internal_server_sem.lock(); if (typeid(*backend) == typeid(NoneBackend)) { - server_println( - "backend is of type NoneBackend. Stopping capio_remote_listener() execution.", - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, - "capio_remote_listener"); + + CAPTURA_PRINT_COLOR( + CAPTURA_CLI_LEVEL_INFO, + "backend is of type NoneBackend. Stopping capio_remote_listener() execution."); return; } diff --git a/capio/server/include/utils/server_println.hpp b/capio/server/include/utils/server_println.hpp deleted file mode 100644 index bf378ede1..000000000 --- a/capio/server/include/utils/server_println.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef CAPIO_PRINT_TEXT_HPP -#define CAPIO_PRINT_TEXT_HPP -#include -#include -#include - -#include "common/constants.hpp" - -inline void server_println(const std::string_view message_line, - const std::string_view workflow_name = CAPIO_DEFAULT_WORKFLOW_NAME, - const std::string_view message_color = CAPIO_LOG_SERVER_CLI_LEVEL_RESET, - const std::string_view component_name = "CAPIO") { - - static char node_name[HOST_NAME_MAX]; - // static init once the nodename - [[maybe_unused]] static bool host_init = []() { - if (gethostname(node_name, HOST_NAME_MAX) != 0) { - snprintf(node_name, HOST_NAME_MAX, "unknown"); - } - return true; - }(); - - if (message_color.empty()) { - std::cout << message_line << std::endl; - return; - } - - // Get current time - const auto in_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - - std::cout << message_color << "[CAPIO-SERVER > " << workflow_name << "]" - << CAPIO_LOG_SERVER_CLI_LEVEL_RESET << " [" - << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S") << "] " - << " " << node_name << "@" << std::left << std::setw(20) - << component_name.substr(0, 20) << " | " << message_line << std::endl - << std::flush; -} -#endif // CAPIO_PRINT_TEXT_HPP diff --git a/capio/server/include/utils/signals.hpp b/capio/server/include/utils/signals.hpp index 2fc4c134c..fb217ae36 100644 --- a/capio/server/include/utils/signals.hpp +++ b/capio/server/include/utils/signals.hpp @@ -3,9 +3,10 @@ #include +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" + #include "remote/backend.hpp" -#include "server_println.hpp" #ifdef CAPIO_COVERAGE extern "C" void __gcov_dump(void); @@ -14,22 +15,17 @@ extern "C" void __gcov_dump(void); void sig_term_handler(int signum, siginfo_t *info, void *ptr) { START_LOG(gettid(), "call(signal=[%d] (%s) from process with pid=%ld)", signum, strsignal(signum), info != nullptr ? info->si_pid : -1); - - server_println("shutting down server", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "shutting down server"); if (signum == SIGSEGV) { - server_println("Segfault detected!", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Segfault detected!"); } // free all the memory used delete client_manager; delete storage_manager; - - server_println("data_buffers cleanup completed", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "data_buffers cleanup completed"); #ifdef CAPIO_COVERAGE __gcov_dump(); @@ -38,8 +34,8 @@ void sig_term_handler(int signum, siginfo_t *info, void *ptr) { delete backend; delete shm_canary; - server_println("shutdown completed", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "shutdown completed"); + exit(EXIT_SUCCESS); } @@ -60,8 +56,8 @@ void setup_signal_handlers() { if (res == -1) { ERR_EXIT("sigaction for SIGTERM"); } - server_println("Installed signal handlers", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, __func__); + + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "Installed signal handlers"); } #endif // CAPIO_SERVER_HANDLERS_SIGNALS_HPP diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index 125d9a58d..c9d815fa4 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,8 +1,8 @@ #include "remote/backend.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" #include "utils/common.hpp" -#include "utils/server_println.hpp" #include @@ -13,11 +13,8 @@ Backend::Backend(const unsigned int node_name_max_length) gethostname(node_name.data(), node_name_max_length); node_name.resize(strlen(node_name.data())); - server_println("Node name: " + node_name, CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "Backend"); - server_println("Node count: " + std::to_string(n_servers), - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, - "Backend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Node name: %s", node_name.c_str()); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Node count: %d", n_servers); } [[nodiscard]] const std::string &Backend::get_node_name() const { diff --git a/capio/server/src/capio_file.cpp b/capio/server/src/capio_file.cpp index db3cefa53..4d5a8eb5e 100644 --- a/capio/server/src/capio_file.cpp +++ b/capio/server/src/capio_file.cpp @@ -1,9 +1,10 @@ #include "server/include/storage/capio_file.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" + #include "remote/backend.hpp" #include "server/include/utils/common.hpp" -#include "server/include/utils/server_println.hpp" #include "utils/shared_mutex.hpp" extern Backend *backend; @@ -34,9 +35,8 @@ CapioFile::~CapioFile() { delete[] _buf; } else { if (munmap(_buf, _buf_size) == -1) { - server_println("WARN: unable to unmap CapioFile: " + std::string(strerror(errno)), - CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, "CapioFile"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, + "WARN: unable to unmap CapioFile: %s", strerror(errno)); } } } else { diff --git a/capio/server/src/cli_parser.cpp b/capio/server/src/cli_parser.cpp index 665e8263a..d95130657 100644 --- a/capio/server/src/cli_parser.cpp +++ b/capio/server/src/cli_parser.cpp @@ -1,9 +1,10 @@ #include "utils/cli_parser.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" + #include "common/constants.hpp" #include "utils/common.hpp" -#include "utils/server_println.hpp" #include @@ -53,21 +54,20 @@ CapioParsedConfig parseCLI(int argc, char **argv) { #ifdef CAPIO_LOG continue_on_error = true; for (const auto line : CAPIO_LOG_SERVER_CLI_CONT_ON_ERR_WARNING) { - server_println(line, get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, - __func__); + CAPTURA_PRINT("%s", line); } #else - server_println("--continue-on-error flag given, but logger is not compiled into CAPIO. " - "Flag is ignored.", - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, "parseCLI"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, + "--continue-on-error flag given, but logger is not compiled into " + "CAPIO. Flag is ignored."); #endif } #ifdef CAPIO_LOG auto log = new Logger(__func__, __FILE__, __LINE__, gettid(), "Created new log file"); - server_println("started logging to logfile " + log->getLogFileName(), get_capio_workflow_name(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "parseCLI"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "started logging to logfile %s", + log->getLogFileName().c_str()); #endif if (mem_only_flag) { @@ -79,8 +79,8 @@ CapioParsedConfig parseCLI(int argc, char **argv) { capio_config.capio_cl_config_path = args::get(config); if (std::string token = args::get(config); token == "dynamic") { - server_println("Starting CAPIO-CL engine with dynamic configuration", - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, + "Starting CAPIO-CL engine with dynamic configuration"); capio_config.capio_cl_dynamic_config = true; } else { @@ -92,25 +92,21 @@ CapioParsedConfig parseCLI(int argc, char **argv) { } } else if (noConfigFile) { - - server_println("skipping config file parsing.", get_capio_workflow_name(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, __func__); - server_println("Obtained from environment variable current workflow name: " + - get_capio_workflow_name(), - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, __func__); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "skipping config file parsing."); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, + "Obtained from environment variable current workflow name: %s", + get_capio_workflow_name().c_str()); } else { - server_println( - "Error: no config file provided. To skip config file use --no-config option!", - get_capio_workflow_name(), CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, __func__); + CAPTURA_PRINT_COLOR( + CAPTURA_CLI_LEVEL_ERROR, + "Error: no config file provided. To skip config file use --no-config option!"); #ifdef CAPIO_LOG log->log("no config file provided, and --no-config not provided"); #endif exit(EXIT_FAILURE); } - server_println("CAPIO_DIR=" + get_capio_dir().string(), get_capio_workflow_name(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, __func__); - + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "CAPIO_DIR=%s", get_capio_dir().c_str()); // Backend selection phase if (backend_flag) { capio_config.backend_name = args::get(backend_flag); diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index a733e11fb..380a0df18 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -2,12 +2,12 @@ #include "client-manager/client_manager.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" #include "common/constants.hpp" #include "common/queue.hpp" #include "utils/capiocl_adapter.hpp" #include "utils/common.hpp" -#include "utils/server_println.hpp" ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToServerName, const std::string &serverToClientName, @@ -18,13 +18,11 @@ ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToS ClientManager::ClientManager() : requests{SHM_COMM_CHAN_NAME, CAPIO_REQ_BUFF_CNT, CAPIO_REQ_MAX_SIZE, CapioCLEngine::get().getWorkflowName()} { - server_println("initialization completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "ClientManager"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); } ClientManager::~ClientManager() { - server_println("teardown completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, "ClientManager"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "teardown completed."); } void ClientManager::registerClient(pid_t tid, const std::string &app_name, const bool wait) { @@ -40,10 +38,9 @@ void ClientManager::registerClient(pid_t tid, const std::string &app_name, const responses.try_emplace(tid, SHM_COMM_CHAN_NAME_RESP + std::to_string(tid), CAPIO_REQ_BUFF_CNT, sizeof(off_t), CapioCLEngine::get().getWorkflowName()); - DBG(tid, [](const int tid_app, const std::string &app_name) { - server_println("Registered PID " + std::to_string(tid_app) + " with app name " + app_name, - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_INFO, - "ClientManager"); + DBG(tid, [](const int tid_app, const std::string &app_name_) { + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Registered PID %d with app name %s", tid_app, + app_name_.c_str()); }(tid, app_name)); if (wait) { @@ -83,10 +80,9 @@ void ClientManager::removeClient(const pid_t tid) { if (const auto response_buffer = responses.find(tid); response_buffer != responses.end()) { responses.erase(response_buffer); } - DBG(tid, [](const int tid_app, const std::string &app_name) { - server_println("Removed PID " + std::to_string(tid_app) + " with app name " + app_name, - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_WARNING, - "ClientManager"); + DBG(tid, [&](const int tid_app, const std::string &app_name) { + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "Removed PID %d with app name %s", tid_app, + app_name.c_str()); }(tid, app_name)); } @@ -189,12 +185,10 @@ int ClientManager::readNextRequest(char *str) { if (ec == std::errc()) { strcpy(str, ptr + 1); } else { - server_println("Received invalid request: " + std::string(str), - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, - __func__); - server_println("Code " + std::to_string(code) + " is not mapped to a valid request handler", - CapioCLEngine::get().getWorkflowName(), CAPIO_LOG_SERVER_CLI_LEVEL_ERROR, - __func__); + + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Received invalid request: %s", str); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, + "Code: %d is not mapped to a valid request handler", code); return -1; } diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index be9c5e2f0..13af5286c 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,8 +1,10 @@ #include "remote/backend/mpi.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" + +#include "common/constants.hpp" #include "utils/capiocl_adapter.hpp" -#include "utils/server_println.hpp" MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) { int node_name_len, provided; @@ -24,15 +26,13 @@ MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) nodes.emplace(node_name); rank_to_hostname[rank] = node_name; hostname_to_rank[node_name] = rank; - server_println("initialization completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "MPIBackend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); } MPIBackend::~MPIBackend() { START_LOG(gettid(), "Call()"); MPI_Finalize(); - server_println("teardown completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "MPIBackend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "teardown completed."); } const std::set MPIBackend::get_nodes() { return nodes; } @@ -125,15 +125,13 @@ void MPIBackend::recv_file(char *shm, const std::string &source, long int bytes_ MPISYNCBackend::MPISYNCBackend(int argc, char *argv[]) : MPIBackend(argc, argv) { START_LOG(gettid(), "call()"); LOG("Wrapped MPI backend with MPISYC backend"); - server_println("initialization completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "MPISYNCBackend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); } MPISYNCBackend::~MPISYNCBackend() { START_LOG(gettid(), "Call()"); MPI_Finalize(); - server_println("teardown completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "MPISYNCBackend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "teardown completed."); } RemoteRequest MPISYNCBackend::read_next_request() { diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index cfece7082..e08f74dc0 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -2,14 +2,13 @@ #include "remote/backend/none.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" #include "utils/capiocl_adapter.hpp" -#include "utils/server_println.hpp" NoneBackend::NoneBackend(int argc, char **argv) : Backend(HOST_NAME_MAX) { START_LOG(gettid(), "call()"); - server_println("initialization completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "NoneBackend"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); } RemoteRequest NoneBackend::read_next_request() { diff --git a/capio/server/src/storage_manager.cpp b/capio/server/src/storage_manager.cpp index 48786f3d7..98dab3eef 100644 --- a/capio/server/src/storage_manager.cpp +++ b/capio/server/src/storage_manager.cpp @@ -5,7 +5,9 @@ #include "storage/manager.hpp" +#include "captura/StdOutLogger.h" #include "captura/StlLogger.h" + #include "common/dirent.hpp" #include "common/filesystem.hpp" #include "storage/capio_file.hpp" @@ -68,8 +70,7 @@ void StorageManager::addDirectoryEntry(const pid_t tid, const std::filesystem::p } } StorageManager::StorageManager() { - server_println("initialization completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_STATUS, "StorageManager"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); } StorageManager::~StorageManager() { @@ -84,8 +85,7 @@ StorageManager::~StorageManager() { _removeFromTid(tid, fd); } } - server_println("teardown completed.", CapioCLEngine::get().getWorkflowName(), - CAPIO_LOG_SERVER_CLI_LEVEL_INFO, "StorageManager"); + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "teardown completed."); } std::optional> From 1b29bdfcf431c99b8b5e85e3ebd1c01f696ad3c9 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Wed, 20 May 2026 18:09:35 +0200 Subject: [PATCH 23/26] cleanup --- .gitignore | 2 +- capio/common/constants.hpp | 23 ++++++++--------------- capio/server/capio_server.cpp | 5 +---- capio/server/src/cli_parser.cpp | 4 ++++ 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index d29456e74..06aace328 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,7 @@ cmake-build-* #logfies and capio files *.log files_location*.txt -capio_logs +captura_logs # Other debug diff --git a/capio/common/constants.hpp b/capio/common/constants.hpp index 07fd0ed61..3c456717e 100644 --- a/capio/common/constants.hpp +++ b/capio/common/constants.hpp @@ -28,12 +28,8 @@ constexpr int CAPIO_CACHE_LINE_SIZE_DEFAULT = 256 * 1024; constexpr size_t CAPIO_SERVER_REQUEST_MAX_SIZE = sizeof(char) * (PATH_MAX + 81920); constexpr size_t CAPIO_REQ_MAX_SIZE = 256 * sizeof(char); -constexpr char LOG_CAPIO_START_REQUEST[] = "\n+++++++++++ SYSCALL %s (%d) +++++++++++"; -constexpr char LOG_CAPIO_END_REQUEST[] = "----------- END SYSCALL ----------\n"; -constexpr char CAPIO_SERVER_LOG_START_REQUEST_MSG[] = "+++++++++++++++++REQUEST+++++++++++++++++"; -constexpr char CAPIO_SERVER_LOG_END_REQUEST_MSG[] = "~~~~~~~~~~~~~~~END REQUEST~~~~~~~~~~~~~~~"; -constexpr int CAPIO_SEM_RETRIES = 100; -constexpr int THEORETICAL_SIZE_DIRENT64 = sizeof(ino64_t) + sizeof(off64_t) + +constexpr int CAPIO_SEM_RETRIES = 100; +constexpr int THEORETICAL_SIZE_DIRENT64 = sizeof(ino64_t) + sizeof(off64_t) + sizeof(unsigned short) + sizeof(unsigned char) + sizeof(char) * NAME_MAX; @@ -79,27 +75,24 @@ constexpr char CAPIO_LOG_SERVER_BANNER[14][80] = { "", }; -constexpr char CAPIO_LOG_SERVER_CLI_LOGGING_NOT_AVAILABLE[] = - "CAPIO_LOG set but log support was not compiled into CAPIO!"; -constexpr char CAPIO_LOG_SERVER_REQUEST_START[] = "\n+++++++++++ REQUEST +++++++++++"; -constexpr char CAPIO_LOG_SERVER_REQUEST_END[] = "~~~~~~~~~ END REQUEST ~~~~~~~~~\n"; - // Server - Warning banners -constexpr char CAPIO_LOG_SERVER_CLI_LOGGING_ENABLED_WARNING[5][80] = { +constexpr char CAPIO_LOG_SERVER_CLI_LOGGING_ENABLED_WARNING[6][80] = { "\033[1;33m|==================================================================|\033[0m", "\033[1;33m| you are running a build of CAPIO with logging enabled. |\033[0m", "\033[1;33m| this will have impact on performance. you should recompile CAPIO |\033[0m", "\033[1;33m| with -DCAPIO_LOG=FALSE |\033[0m", - "\033[1;33m|==================================================================|\033[0m"}; + "\033[1;33m|==================================================================|\033[0m", + ""}; -constexpr char CAPIO_LOG_SERVER_CLI_CONT_ON_ERR_WARNING[7][80] = { +constexpr char CAPIO_LOG_SERVER_CLI_CONT_ON_ERR_WARNING[8][80] = { "\033[1;31m|==================================================================|\033[0m", "\033[1;31m| you are running CAPIO with --continue-on-error |\033[0m", "\033[1;31m| This is extremely dangerous as CAPIO server will continue |\033[0m", "\033[1;31m| its execution even if it should terminate. |\033[0m", "\033[1;31m| |\033[0m", "\033[1;31m| USE IT AT YOUR OWN RISK |\033[0m", - "\033[1;31m|==================================================================|\033[0m"}; + "\033[1;31m|==================================================================|\033[0m", + ""}; // CAPIO server argument parser constexpr char CAPIO_SERVER_ARG_PARSER_PRE[] = diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index b5dba67a2..dcb6e7975 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -90,8 +90,6 @@ static constexpr std::array build_request_handle static const std::array request_handlers = build_request_handlers_table(); - START_LOG(gettid(), "call()"); - setup_signal_handlers(); backend->handshake_servers(); @@ -101,7 +99,7 @@ static constexpr std::array build_request_handle auto str = std::unique_ptr(new char[CAPIO_REQ_MAX_SIZE]); while (true) { - LOG(CAPIO_LOG_SERVER_REQUEST_START); + START_LOG(gettid(), "call()"); int code = client_manager->readNextRequest(str.get()); if (code < 0 || code > CAPIO_NR_REQUESTS) { CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Received invalid code: %d", code); @@ -109,7 +107,6 @@ static constexpr std::array build_request_handle ERR_EXIT("Error: received invalid request code"); } request_handlers[code](str.get()); - LOG(CAPIO_LOG_SERVER_REQUEST_END); } } diff --git a/capio/server/src/cli_parser.cpp b/capio/server/src/cli_parser.cpp index d95130657..4c1e68caa 100644 --- a/capio/server/src/cli_parser.cpp +++ b/capio/server/src/cli_parser.cpp @@ -65,6 +65,10 @@ CapioParsedConfig parseCLI(int argc, char **argv) { } #ifdef CAPIO_LOG + for (const auto line : CAPIO_LOG_SERVER_CLI_LOGGING_ENABLED_WARNING) { + CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "%s", line); + } + auto log = new Logger(__func__, __FILE__, __LINE__, gettid(), "Created new log file"); CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "started logging to logfile %s", log->getLogFileName().c_str()); From 8d5fe27f5d4c5f2f5d6a29861951798b44003bf1 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Thu, 21 May 2026 10:02:27 +0200 Subject: [PATCH 24/26] project rename --- .gitignore | 2 +- CMakeLists.txt | 12 +++---- README.md | 2 +- capio/common/env.hpp | 31 ++++------------ capio/common/filesystem.hpp | 4 +-- capio/common/queue.hpp | 4 +-- capio/common/semaphore.hpp | 4 +-- capio/common/shm.hpp | 18 +++++----- capio/posix/CMakeLists.txt | 2 +- capio/posix/libcapio_posix.cpp | 2 +- capio/posix/utils/clone.hpp | 2 +- capio/posix/utils/common.hpp | 2 +- capio/posix/utils/filesystem.hpp | 2 +- capio/posix/utils/requests.hpp | 2 +- capio/posix/utils/snapshot.hpp | 2 +- capio/server/CMakeLists.txt | 2 +- capio/server/capio_server.cpp | 10 +++--- .../include/client-manager/client_manager.hpp | 2 +- capio/server/include/remote/handlers/read.hpp | 2 +- capio/server/include/remote/handlers/stat.hpp | 2 +- capio/server/include/remote/listener.hpp | 22 ++++++------ capio/server/include/storage/capio_file.hpp | 2 +- capio/server/include/utils/common.hpp | 2 +- capio/server/include/utils/env.hpp | 2 +- capio/server/include/utils/location.hpp | 2 +- capio/server/include/utils/signals.hpp | 14 ++++---- capio/server/src/backend.cpp | 8 ++--- capio/server/src/capio_file.cpp | 8 ++--- capio/server/src/cli_parser.cpp | 36 +++++++++---------- capio/server/src/client_manager.cpp | 24 ++++++------- capio/server/src/mpi_backend.cpp | 12 +++---- capio/server/src/none_backend.cpp | 6 ++-- capio/server/src/remote_request.cpp | 2 +- capio/server/src/storage_manager.cpp | 8 ++--- capio/tests/unit/posix/CMakeLists.txt | 2 +- capio/tests/unit/posix/src/realpath.cpp | 2 +- capio/tests/unit/server/CMakeLists.txt | 2 +- capio/tests/unit/syscall/CMakeLists.txt | 2 +- 38 files changed, 122 insertions(+), 143 deletions(-) diff --git a/.gitignore b/.gitignore index 06aace328..e38da6603 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,7 @@ cmake-build-* #logfies and capio files *.log files_location*.txt -captura_logs +calf_logs # Other debug diff --git a/CMakeLists.txt b/CMakeLists.txt index bd87b94e5..6d4212ac5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,16 +82,16 @@ FetchContent_Declare( ) FetchContent_Declare( - captura - GIT_REPOSITORY https://github.com/High-Performance-IO/captura.git + calf + GIT_REPOSITORY https://github.com/High-Performance-IO/calf.git GIT_TAG main ) -set(CAPTURA_LOG ${CAPIO_LOG} CACHE BOOL "" FORCE) -set(CAPTURA_BUILD_STL ON CACHE BOOL "" FORCE) -set(CAPTURA_BUILD_SYSCALL ON CACHE BOOL "" FORCE) +set(CALF_LOG ${CAPIO_LOG} CACHE BOOL "" FORCE) +set(CALF_BUILD_STL ON CACHE BOOL "" FORCE) +set(CALF_BUILD_SYSCALL ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(captura) +FetchContent_MakeAvailable(calf) ##################################### # Targets diff --git a/README.md b/README.md index 5037daa8f..552ebf9f2 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ CAPIO depends on the following software that needs to be manually installed: The following dependencies are automatically fetched during cmake configuration phase, and compiled when required. - [CAPIO-CL](https://github.com/High-Performance-IO/CAPIO-CL) To handle the CAPIO-CL configuration and enforce streaming directives -- [CAPTURA](https://github.com/High-Performance-IO/CAPTURA) To manage logging and CLI printing +- [CALF](https://github.com/High-Performance-IO/CALF) To manage logging and CLI printing - [alpha-unito/syscall_intercept](https://github.com/alpha-unito/syscall_intercept) to intercept syscalls (forked from ```pmem/syscall_interept```) - [Taywee/args](https://github.com/Taywee/args) to parse server command line inputs diff --git a/capio/common/env.hpp b/capio/common/env.hpp index aaf48fa31..e344cbe61 100644 --- a/capio/common/env.hpp +++ b/capio/common/env.hpp @@ -11,12 +11,12 @@ #include "common/constants.hpp" #include "common/syscall.hpp" -#include +#include #ifdef __CAPIO_POSIX -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #else -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #endif // TODO: remove forward declaration of function by splitting into header and impl. capio/common @@ -34,7 +34,7 @@ inline const std::filesystem::path &get_capio_dir() { if (val == nullptr) { #ifndef __CAPIO_POSIX - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "CAPIO_DIR not provided"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, "CAPIO_DIR not provided"); #endif ERR_EXIT("Fatal: CAPIO_DIR not provided!"); @@ -43,9 +43,8 @@ inline const std::filesystem::path &get_capio_dir() { const char *realpath_res = capio_realpath(val, buf.get()); if (realpath_res == nullptr) { #ifndef __CAPIO_POSIX - CAPTURA_PRINT_COLOR( - CAPTURA_CLI_LEVEL_ERROR, - "Fatal: CAPIO_DIR set, but folder does not exists on filesystem!"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, + "Fatal: CAPIO_DIR set, but folder does not exists on filesystem!"); #endif ERR_EXIT("error CAPIO_DIR: directory %s does not " "exist. [buf=%s]", @@ -99,24 +98,6 @@ inline long get_cache_line_size() { return data_bufs_count; } -inline int get_capio_log_level() { - static int level = -2; - if (level == -2) { - char *log_level = std::getenv("CAPIO_LOG_LEVEL"); - if (log_level == nullptr) { - level = 0; - } else { - auto [ptr, ec] = std::from_chars(log_level, log_level + strlen(log_level), level); - if (ec != std::errc()) { - std::cout << CAPTURA_CLI_LEVEL_WARNING << "invalid CAPIO_LOG_LEVEL value" - << std::endl; - level = 0; - } - } - } - return level; -} - inline std::string get_capio_workflow_name() { static std::string name; if (name.empty()) { diff --git a/capio/common/filesystem.hpp b/capio/common/filesystem.hpp index 0b64a7998..b9a8c5bdc 100644 --- a/capio/common/filesystem.hpp +++ b/capio/common/filesystem.hpp @@ -11,9 +11,9 @@ #include #ifdef __CAPIO_POSIX -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #else -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #endif #include "common/syscall.hpp" diff --git a/capio/common/queue.hpp b/capio/common/queue.hpp index d141a6377..ef824f7e8 100644 --- a/capio/common/queue.hpp +++ b/capio/common/queue.hpp @@ -8,9 +8,9 @@ #include "common/env.hpp" #ifdef __CAPIO_POSIX -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #else -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #endif #include "common/semaphore.hpp" #include "common/shm.hpp" diff --git a/capio/common/semaphore.hpp b/capio/common/semaphore.hpp index 5b730d3fe..b061ca20a 100644 --- a/capio/common/semaphore.hpp +++ b/capio/common/semaphore.hpp @@ -6,9 +6,9 @@ #include #ifdef __CAPIO_POSIX -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #else -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #endif class NoLock { diff --git a/capio/common/shm.hpp b/capio/common/shm.hpp index e34ec0ed4..060e5cdb0 100644 --- a/capio/common/shm.hpp +++ b/capio/common/shm.hpp @@ -10,9 +10,9 @@ #include #ifdef __CAPIO_POSIX -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #else -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #endif #ifdef __CAPIO_POSIX @@ -29,18 +29,18 @@ #else -#include "captura/StdOutLogger.h" +#include "calf/StdOutLogger.h" #define SHM_DESTROY_CHECK(source_name) \ if (shm_unlink(source_name) == -1) { \ - std::cout << CAPTURA_CLI_LEVEL_WARNING << "Unable to destroy shared mem: '" << source_name \ + std::cout << CALF_CLI_LEVEL_WARNING << "Unable to destroy shared mem: '" << source_name \ << "' (" << strerror(errno) << ")" << std::endl; \ }; #define SHM_CREATE_CHECK(condition, source) \ if (condition) { \ LOG("error while creating %s", source); \ - std::cout << CAPTURA_CLI_LEVEL_ERROR << "Unable to create shm: " << source << std::endl; \ + std::cout << CALF_CLI_LEVEL_ERROR << "Unable to create shm: " << source << std::endl; \ ERR_EXIT("Unable to open shm: %s", source); \ }; @@ -62,21 +62,21 @@ class CapioShmCanary { #ifndef __CAPIO_POSIX const auto message = new char[strlen(CAPIO_SHM_CANARY_ERROR)]; sprintf(message, CAPIO_SHM_CANARY_ERROR, _canary_name.data()); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "%s", message); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, "%s", message); delete[] message; #endif ERR_EXIT("ERR: shm canary flag already exists"); } #ifndef __CAPIO_POSIX - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "Created Capio SHM canary: %s", - _canary_name.c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "Created Capio SHM canary: %s", + _canary_name.c_str()); #endif }; ~CapioShmCanary() { START_LOG(capio_syscall(SYS_gettid), "call()"); #ifndef __CAPIO_POSIX - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "Removing shared memory canary flag"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "Removing shared memory canary flag"); #endif close(_shm_id); SHM_DESTROY_CHECK(_canary_name.c_str()); diff --git a/capio/posix/CMakeLists.txt b/capio/posix/CMakeLists.txt index 651d3027e..a2665b88f 100644 --- a/capio/posix/CMakeLists.txt +++ b/capio/posix/CMakeLists.txt @@ -81,7 +81,7 @@ set_target_properties(${TARGET_NAME} PROPERTIES # Link libraries ##################################### target_link_directories(${TARGET_NAME} PRIVATE ${SYSCALL_INTERCEPT_LIB_FOLDER}) -target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept captura::syscall) +target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept calf::syscall) ##################################### # Install rules diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index 8f8ffc958..f326430f8 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -418,6 +418,6 @@ static __attribute__((constructor)) void init() { intercept_hook_point_clone_parent = hook_clone_parent; intercept_hook_point = hook; - SET_CAPTURA_SYSCALL_HANDLER(syscall_no_intercept); + SET_CALF_SYSCALL_HANDLER(syscall_no_intercept); ENABLE_LOGGER(); } \ No newline at end of file diff --git a/capio/posix/utils/clone.hpp b/capio/posix/utils/clone.hpp index 6674ab88f..eb1c5e3bb 100644 --- a/capio/posix/utils/clone.hpp +++ b/capio/posix/utils/clone.hpp @@ -1,7 +1,7 @@ #ifndef CAPIO_POSIX_UTILS_CLONE_HPP #define CAPIO_POSIX_UTILS_CLONE_HPP -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #include "common/syscall.hpp" #include "data.hpp" #include "requests.hpp" diff --git a/capio/posix/utils/common.hpp b/capio/posix/utils/common.hpp index f5038675f..af05e5d55 100644 --- a/capio/posix/utils/common.hpp +++ b/capio/posix/utils/common.hpp @@ -2,7 +2,7 @@ #define CAPIO_FUNCTIONS_H #include -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" int posix_return_value(long res, long *result) { START_LOG(capio_syscall(SYS_gettid), "cal(res=%ld)", res); diff --git a/capio/posix/utils/filesystem.hpp b/capio/posix/utils/filesystem.hpp index 68c328cb3..2ac11871c 100644 --- a/capio/posix/utils/filesystem.hpp +++ b/capio/posix/utils/filesystem.hpp @@ -10,7 +10,7 @@ #include -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #include "common/env.hpp" #include "common/filesystem.hpp" #include "common/syscall.hpp" diff --git a/capio/posix/utils/requests.hpp b/capio/posix/utils/requests.hpp index ffa3c6e72..46ba9e73f 100644 --- a/capio/posix/utils/requests.hpp +++ b/capio/posix/utils/requests.hpp @@ -3,7 +3,7 @@ #include "common/requests.hpp" -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #include "env.hpp" #include "filesystem.hpp" #include "types.hpp" diff --git a/capio/posix/utils/snapshot.hpp b/capio/posix/utils/snapshot.hpp index aa442341f..59b0f805f 100644 --- a/capio/posix/utils/snapshot.hpp +++ b/capio/posix/utils/snapshot.hpp @@ -5,7 +5,7 @@ #include #include -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #include "types.hpp" inline int *get_fd_snapshot(long tid) { diff --git a/capio/server/CMakeLists.txt b/capio/server/CMakeLists.txt index 696db0c4b..2e7d39433 100644 --- a/capio/server/CMakeLists.txt +++ b/capio/server/CMakeLists.txt @@ -54,7 +54,7 @@ ENDIF (MPI_LINK_FLAGS) ##################################### # Link libraries ##################################### -target_link_libraries(${TARGET_NAME} PRIVATE ${MPI_LIBRARIES} pthread rt stdc++fs libcapio_cl args captura::stl) +target_link_libraries(${TARGET_NAME} PRIVATE ${MPI_LIBRARIES} pthread rt stdc++fs libcapio_cl args calf::stl) ##################################### # Install rules diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index dcb6e7975..b0c75b237 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -17,7 +17,7 @@ #include #include -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "capiocl.hpp" #include "capiocl/engine.h" @@ -45,7 +45,7 @@ Backend *backend; #include "remote/listener.hpp" -#include "captura/StdOutLogger.h" +#include "calf/StdOutLogger.h" /** * The capio_cl_engine is declared here to ensure that other components of the CAPIO server @@ -102,7 +102,7 @@ static constexpr std::array build_request_handle START_LOG(gettid(), "call()"); int code = client_manager->readNextRequest(str.get()); if (code < 0 || code > CAPIO_NR_REQUESTS) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Received invalid code: %d", code); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, "Received invalid code: %d", code); ERR_EXIT("Error: received invalid request code"); } @@ -118,7 +118,7 @@ int main(int argc, char **argv) { Semaphore internal_server_sem(0); for (const auto line : CAPIO_LOG_SERVER_BANNER) { - CAPTURA_PRINT("%s", line); + CALF_PRINT("%s", line); } const auto configuration = parseCLI(argc, argv); @@ -157,7 +157,7 @@ int main(int argc, char **argv) { LOG("capio_server thread started"); std::thread remote_listener_thread(capio_remote_listener, std::ref(internal_server_sem)); LOG("capio_remote_listener thread started."); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "Server instance successfully started!"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "Server instance successfully started!"); server_thread.join(); remote_listener_thread.join(); diff --git a/capio/server/include/client-manager/client_manager.hpp b/capio/server/include/client-manager/client_manager.hpp index d2603017b..245d4dcb7 100644 --- a/capio/server/include/client-manager/client_manager.hpp +++ b/capio/server/include/client-manager/client_manager.hpp @@ -5,7 +5,7 @@ #include #include -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "common/queue.hpp" /** diff --git a/capio/server/include/remote/handlers/read.hpp b/capio/server/include/remote/handlers/read.hpp index 3a2dbfeea..b6ca85c0c 100644 --- a/capio/server/include/remote/handlers/read.hpp +++ b/capio/server/include/remote/handlers/read.hpp @@ -1,7 +1,7 @@ #ifndef CAPIO_SERVER_REMOTE_HANDLERS_READ_HPP #define CAPIO_SERVER_REMOTE_HANDLERS_READ_HPP -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "remote/backend.hpp" #include "remote/requests.hpp" #include "storage/manager.hpp" diff --git a/capio/server/include/remote/handlers/stat.hpp b/capio/server/include/remote/handlers/stat.hpp index 3ab3916da..cade1ca87 100644 --- a/capio/server/include/remote/handlers/stat.hpp +++ b/capio/server/include/remote/handlers/stat.hpp @@ -1,7 +1,7 @@ #ifndef CAPIO_SERVER_REMOTE_HANDLERS_STAT_HPP #define CAPIO_SERVER_REMOTE_HANDLERS_STAT_HPP -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "client-manager/client_manager.hpp" #include "remote/backend.hpp" #include "remote/requests.hpp" diff --git a/capio/server/include/remote/listener.hpp b/capio/server/include/remote/listener.hpp index 2382d5835..f8a5af525 100644 --- a/capio/server/include/remote/listener.hpp +++ b/capio/server/include/remote/listener.hpp @@ -1,8 +1,8 @@ #ifndef CAPIO_SERVER_REMOTE_LISTENER_HPP #define CAPIO_SERVER_REMOTE_LISTENER_HPP -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "remote/backend.hpp" #include "remote/backend/include.hpp" @@ -28,29 +28,29 @@ inline Backend *select_backend(const std::string &backend_name, int argc, char * if (backend_name.empty() || backend_name == "none") { LOG("backend selected: none"); - CAPTURA_PRINT_COLOR( - CAPTURA_CLI_LEVEL_INFO, + CALF_PRINT_COLOR( + CALF_CLI_LEVEL_INFO, "Starting CAPIO with default backend (none) as no preferred backend was chosen"); return new NoneBackend(argc, argv); } if (backend_name == "mpi") { LOG("backend selected: mpi"); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Starting CAPIO with MPI backend"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "Starting CAPIO with MPI backend"); return new MPIBackend(argc, argv); } if (backend_name == "mpisync") { LOG("backend selected: mpisync"); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Starting CAPIO with MPI (SYNC) backend"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "Starting CAPIO with MPI (SYNC) backend"); return new MPISYNCBackend(argc, argv); } LOG("Backend %s does not exist in CAPIO. Reverting back to the default backend (none)", backend_name.c_str()); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, - "Backend %s does not exist. Reverting to the default backend (none)", - backend_name.c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, + "Backend %s does not exist. Reverting to the default backend (none)", + backend_name.c_str()); return new NoneBackend(argc, argv); } @@ -62,8 +62,8 @@ inline void capio_remote_listener(Semaphore &internal_server_sem) { if (typeid(*backend) == typeid(NoneBackend)) { - CAPTURA_PRINT_COLOR( - CAPTURA_CLI_LEVEL_INFO, + CALF_PRINT_COLOR( + CALF_CLI_LEVEL_INFO, "backend is of type NoneBackend. Stopping capio_remote_listener() execution."); return; } diff --git a/capio/server/include/storage/capio_file.hpp b/capio/server/include/storage/capio_file.hpp index d9b3a36a8..4c9339601 100644 --- a/capio/server/include/storage/capio_file.hpp +++ b/capio/server/include/storage/capio_file.hpp @@ -10,7 +10,7 @@ #include #include -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "common/queue.hpp" diff --git a/capio/server/include/utils/common.hpp b/capio/server/include/utils/common.hpp index d5e6c1ba4..906f6f06e 100644 --- a/capio/server/include/utils/common.hpp +++ b/capio/server/include/utils/common.hpp @@ -5,8 +5,8 @@ #include #include +#include "calf/StlLogger.h" #include "capiocl_adapter.hpp" -#include "captura/StlLogger.h" #include "client-manager/client_manager.hpp" #include "common/dirent.hpp" #include "storage/capio_file.hpp" diff --git a/capio/server/include/utils/env.hpp b/capio/server/include/utils/env.hpp index cdcc50f82..18b9588d0 100644 --- a/capio/server/include/utils/env.hpp +++ b/capio/server/include/utils/env.hpp @@ -3,7 +3,7 @@ #include -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "common/constants.hpp" off64_t get_file_initial_size() { diff --git a/capio/server/include/utils/location.hpp b/capio/server/include/utils/location.hpp index c7045b597..f830dea05 100644 --- a/capio/server/include/utils/location.hpp +++ b/capio/server/include/utils/location.hpp @@ -5,7 +5,7 @@ #include "remote/backend.hpp" -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" #include "utils/types.hpp" extern Backend *backend; diff --git a/capio/server/include/utils/signals.hpp b/capio/server/include/utils/signals.hpp index fb217ae36..dae3612b1 100644 --- a/capio/server/include/utils/signals.hpp +++ b/capio/server/include/utils/signals.hpp @@ -3,8 +3,8 @@ #include -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "remote/backend.hpp" @@ -15,17 +15,17 @@ extern "C" void __gcov_dump(void); void sig_term_handler(int signum, siginfo_t *info, void *ptr) { START_LOG(gettid(), "call(signal=[%d] (%s) from process with pid=%ld)", signum, strsignal(signum), info != nullptr ? info->si_pid : -1); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "shutting down server"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "shutting down server"); if (signum == SIGSEGV) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Segfault detected!"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, "Segfault detected!"); } // free all the memory used delete client_manager; delete storage_manager; - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "data_buffers cleanup completed"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "data_buffers cleanup completed"); #ifdef CAPIO_COVERAGE __gcov_dump(); @@ -34,7 +34,7 @@ void sig_term_handler(int signum, siginfo_t *info, void *ptr) { delete backend; delete shm_canary; - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "shutdown completed"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "shutdown completed"); exit(EXIT_SUCCESS); } @@ -57,7 +57,7 @@ void setup_signal_handlers() { ERR_EXIT("sigaction for SIGTERM"); } - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "Installed signal handlers"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "Installed signal handlers"); } #endif // CAPIO_SERVER_HANDLERS_SIGNALS_HPP diff --git a/capio/server/src/backend.cpp b/capio/server/src/backend.cpp index c9d815fa4..bfa79ceda 100644 --- a/capio/server/src/backend.cpp +++ b/capio/server/src/backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "utils/common.hpp" #include @@ -13,8 +13,8 @@ Backend::Backend(const unsigned int node_name_max_length) gethostname(node_name.data(), node_name_max_length); node_name.resize(strlen(node_name.data())); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Node name: %s", node_name.c_str()); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Node count: %d", n_servers); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "Node name: %s", node_name.c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "Node count: %d", n_servers); } [[nodiscard]] const std::string &Backend::get_node_name() const { diff --git a/capio/server/src/capio_file.cpp b/capio/server/src/capio_file.cpp index 4d5a8eb5e..80b9500ed 100644 --- a/capio/server/src/capio_file.cpp +++ b/capio/server/src/capio_file.cpp @@ -1,7 +1,7 @@ #include "server/include/storage/capio_file.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "remote/backend.hpp" #include "server/include/utils/common.hpp" @@ -35,8 +35,8 @@ CapioFile::~CapioFile() { delete[] _buf; } else { if (munmap(_buf, _buf_size) == -1) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, - "WARN: unable to unmap CapioFile: %s", strerror(errno)); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "WARN: unable to unmap CapioFile: %s", + strerror(errno)); } } } else { diff --git a/capio/server/src/cli_parser.cpp b/capio/server/src/cli_parser.cpp index 4c1e68caa..4ba8c357a 100644 --- a/capio/server/src/cli_parser.cpp +++ b/capio/server/src/cli_parser.cpp @@ -1,7 +1,7 @@ #include "utils/cli_parser.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "common/constants.hpp" #include "utils/common.hpp" @@ -54,24 +54,24 @@ CapioParsedConfig parseCLI(int argc, char **argv) { #ifdef CAPIO_LOG continue_on_error = true; for (const auto line : CAPIO_LOG_SERVER_CLI_CONT_ON_ERR_WARNING) { - CAPTURA_PRINT("%s", line); + CALF_PRINT("%s", line); } #else - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, - "--continue-on-error flag given, but logger is not compiled into " - "CAPIO. Flag is ignored."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, + "--continue-on-error flag given, but logger is not compiled into " + "CAPIO. Flag is ignored."); #endif } #ifdef CAPIO_LOG for (const auto line : CAPIO_LOG_SERVER_CLI_LOGGING_ENABLED_WARNING) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "%s", line); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "%s", line); } auto log = new Logger(__func__, __FILE__, __LINE__, gettid(), "Created new log file"); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "started logging to logfile %s", - log->getLogFileName().c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "started logging to logfile %s", + log->getLogFileName().c_str()); #endif if (mem_only_flag) { @@ -83,8 +83,8 @@ CapioParsedConfig parseCLI(int argc, char **argv) { capio_config.capio_cl_config_path = args::get(config); if (std::string token = args::get(config); token == "dynamic") { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, - "Starting CAPIO-CL engine with dynamic configuration"); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, + "Starting CAPIO-CL engine with dynamic configuration"); capio_config.capio_cl_dynamic_config = true; } else { @@ -96,13 +96,13 @@ CapioParsedConfig parseCLI(int argc, char **argv) { } } else if (noConfigFile) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "skipping config file parsing."); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, - "Obtained from environment variable current workflow name: %s", - get_capio_workflow_name().c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "skipping config file parsing."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, + "Obtained from environment variable current workflow name: %s", + get_capio_workflow_name().c_str()); } else { - CAPTURA_PRINT_COLOR( - CAPTURA_CLI_LEVEL_ERROR, + CALF_PRINT_COLOR( + CALF_CLI_LEVEL_ERROR, "Error: no config file provided. To skip config file use --no-config option!"); #ifdef CAPIO_LOG log->log("no config file provided, and --no-config not provided"); @@ -110,7 +110,7 @@ CapioParsedConfig parseCLI(int argc, char **argv) { exit(EXIT_FAILURE); } - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "CAPIO_DIR=%s", get_capio_dir().c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "CAPIO_DIR=%s", get_capio_dir().c_str()); // Backend selection phase if (backend_flag) { capio_config.backend_name = args::get(backend_flag); diff --git a/capio/server/src/client_manager.cpp b/capio/server/src/client_manager.cpp index 380a0df18..34b30001f 100644 --- a/capio/server/src/client_manager.cpp +++ b/capio/server/src/client_manager.cpp @@ -2,8 +2,8 @@ #include "client-manager/client_manager.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "common/constants.hpp" #include "common/queue.hpp" #include "utils/capiocl_adapter.hpp" @@ -18,12 +18,10 @@ ClientManager::ClientDataBuffers::ClientDataBuffers(const std::string &clientToS ClientManager::ClientManager() : requests{SHM_COMM_CHAN_NAME, CAPIO_REQ_BUFF_CNT, CAPIO_REQ_MAX_SIZE, CapioCLEngine::get().getWorkflowName()} { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "initialization completed."); } -ClientManager::~ClientManager() { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "teardown completed."); -} +ClientManager::~ClientManager() { CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "teardown completed."); } void ClientManager::registerClient(pid_t tid, const std::string &app_name, const bool wait) { START_LOG(gettid(), "call(tid=%ld, app_name=%s)", tid, app_name.c_str()); @@ -39,8 +37,8 @@ void ClientManager::registerClient(pid_t tid, const std::string &app_name, const sizeof(off_t), CapioCLEngine::get().getWorkflowName()); DBG(tid, [](const int tid_app, const std::string &app_name_) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "Registered PID %d with app name %s", tid_app, - app_name_.c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "Registered PID %d with app name %s", tid_app, + app_name_.c_str()); }(tid, app_name)); if (wait) { @@ -81,8 +79,8 @@ void ClientManager::removeClient(const pid_t tid) { responses.erase(response_buffer); } DBG(tid, [&](const int tid_app, const std::string &app_name) { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_WARNING, "Removed PID %d with app name %s", tid_app, - app_name.c_str()); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_WARNING, "Removed PID %d with app name %s", tid_app, + app_name.c_str()); }(tid, app_name)); } @@ -186,9 +184,9 @@ int ClientManager::readNextRequest(char *str) { strcpy(str, ptr + 1); } else { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, "Received invalid request: %s", str); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_ERROR, - "Code: %d is not mapped to a valid request handler", code); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, "Received invalid request: %s", str); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_ERROR, "Code: %d is not mapped to a valid request handler", + code); return -1; } diff --git a/capio/server/src/mpi_backend.cpp b/capio/server/src/mpi_backend.cpp index 13af5286c..b77b004cb 100644 --- a/capio/server/src/mpi_backend.cpp +++ b/capio/server/src/mpi_backend.cpp @@ -1,7 +1,7 @@ #include "remote/backend/mpi.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "common/constants.hpp" #include "utils/capiocl_adapter.hpp" @@ -26,13 +26,13 @@ MPIBackend::MPIBackend(int argc, char **argv) : Backend(MPI_MAX_PROCESSOR_NAME) nodes.emplace(node_name); rank_to_hostname[rank] = node_name; hostname_to_rank[node_name] = rank; - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "initialization completed."); } MPIBackend::~MPIBackend() { START_LOG(gettid(), "Call()"); MPI_Finalize(); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "teardown completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "teardown completed."); } const std::set MPIBackend::get_nodes() { return nodes; } @@ -125,13 +125,13 @@ void MPIBackend::recv_file(char *shm, const std::string &source, long int bytes_ MPISYNCBackend::MPISYNCBackend(int argc, char *argv[]) : MPIBackend(argc, argv) { START_LOG(gettid(), "call()"); LOG("Wrapped MPI backend with MPISYC backend"); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "initialization completed."); } MPISYNCBackend::~MPISYNCBackend() { START_LOG(gettid(), "Call()"); MPI_Finalize(); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "teardown completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "teardown completed."); } RemoteRequest MPISYNCBackend::read_next_request() { diff --git a/capio/server/src/none_backend.cpp b/capio/server/src/none_backend.cpp index e08f74dc0..121710602 100644 --- a/capio/server/src/none_backend.cpp +++ b/capio/server/src/none_backend.cpp @@ -2,13 +2,13 @@ #include "remote/backend/none.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "utils/capiocl_adapter.hpp" NoneBackend::NoneBackend(int argc, char **argv) : Backend(HOST_NAME_MAX) { START_LOG(gettid(), "call()"); - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "initialization completed."); } RemoteRequest NoneBackend::read_next_request() { diff --git a/capio/server/src/remote_request.cpp b/capio/server/src/remote_request.cpp index 882d96927..101da514a 100644 --- a/capio/server/src/remote_request.cpp +++ b/capio/server/src/remote_request.cpp @@ -1,7 +1,7 @@ #include "common/constants.hpp" #include "remote/backend.hpp" -#include "captura/StlLogger.h" +#include "calf/StlLogger.h" RemoteRequest::RemoteRequest(char *buf_recv, const std::string &source) : _source(source) { START_LOG(gettid(), "call(buf_recv=%s, source=%s)", buf_recv, source.c_str()); diff --git a/capio/server/src/storage_manager.cpp b/capio/server/src/storage_manager.cpp index 98dab3eef..c2fe73bf6 100644 --- a/capio/server/src/storage_manager.cpp +++ b/capio/server/src/storage_manager.cpp @@ -5,8 +5,8 @@ #include "storage/manager.hpp" -#include "captura/StdOutLogger.h" -#include "captura/StlLogger.h" +#include "calf/StdOutLogger.h" +#include "calf/StlLogger.h" #include "common/dirent.hpp" #include "common/filesystem.hpp" @@ -70,7 +70,7 @@ void StorageManager::addDirectoryEntry(const pid_t tid, const std::filesystem::p } } StorageManager::StorageManager() { - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_STATUS, "initialization completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_STATUS, "initialization completed."); } StorageManager::~StorageManager() { @@ -85,7 +85,7 @@ StorageManager::~StorageManager() { _removeFromTid(tid, fd); } } - CAPTURA_PRINT_COLOR(CAPTURA_CLI_LEVEL_INFO, "teardown completed."); + CALF_PRINT_COLOR(CALF_CLI_LEVEL_INFO, "teardown completed."); } std::optional> diff --git a/capio/tests/unit/posix/CMakeLists.txt b/capio/tests/unit/posix/CMakeLists.txt index cbfbe68c9..96125bda9 100644 --- a/capio/tests/unit/posix/CMakeLists.txt +++ b/capio/tests/unit/posix/CMakeLists.txt @@ -46,7 +46,7 @@ target_include_directories(${TARGET_NAME} PRIVATE # Link libraries ##################################### target_link_directories(${TARGET_NAME} PRIVATE ${SYSCALL_INTERCEPT_LIB_FOLDER}) -target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept GTest::gtest_main captura::syscall) +target_link_libraries(${TARGET_NAME} PRIVATE rt syscall_intercept GTest::gtest_main calf::syscall) ##################################### # Configure tests diff --git a/capio/tests/unit/posix/src/realpath.cpp b/capio/tests/unit/posix/src/realpath.cpp index dd750385e..02600a906 100644 --- a/capio/tests/unit/posix/src/realpath.cpp +++ b/capio/tests/unit/posix/src/realpath.cpp @@ -4,7 +4,7 @@ #include -#include "captura/SyscallLogger.h" +#include "calf/SyscallLogger.h" #include "utils/filesystem.hpp" diff --git a/capio/tests/unit/server/CMakeLists.txt b/capio/tests/unit/server/CMakeLists.txt index 494fa5ff2..487e2d887 100644 --- a/capio/tests/unit/server/CMakeLists.txt +++ b/capio/tests/unit/server/CMakeLists.txt @@ -37,7 +37,7 @@ target_include_directories(${TARGET_NAME} PRIVATE ##################################### # Link libraries ##################################### -target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest_main rt libcapio_cl MPI::MPI_CXX args captura::stl) +target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest_main rt libcapio_cl MPI::MPI_CXX args calf::stl) ##################################### # Configure tests diff --git a/capio/tests/unit/syscall/CMakeLists.txt b/capio/tests/unit/syscall/CMakeLists.txt index 89517a0e1..87a24c003 100644 --- a/capio/tests/unit/syscall/CMakeLists.txt +++ b/capio/tests/unit/syscall/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable(${TARGET_NAME} ${TARGET_SOURCES}) ##################################### # Link libraries ##################################### -target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest captura::syscall) +target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest calf::syscall) ##################################### # Configure tests From 44342bcf28c301ce439554bca35aa27173a57cfe Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Thu, 21 May 2026 15:56:22 +0200 Subject: [PATCH 25/26] Component name for logger --- capio/posix/libcapio_posix.cpp | 3 ++- capio/server/capio_server.cpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/capio/posix/libcapio_posix.cpp b/capio/posix/libcapio_posix.cpp index f326430f8..91039a589 100644 --- a/capio/posix/libcapio_posix.cpp +++ b/capio/posix/libcapio_posix.cpp @@ -403,6 +403,8 @@ static int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, } static __attribute__((constructor)) void init() { + SET_CALF_COMPONENT_NAME("posix"); + SET_CALF_SYSCALL_HANDLER(syscall_no_intercept); const long tid = syscall_no_intercept(SYS_gettid); @@ -418,6 +420,5 @@ static __attribute__((constructor)) void init() { intercept_hook_point_clone_parent = hook_clone_parent; intercept_hook_point = hook; - SET_CALF_SYSCALL_HANDLER(syscall_no_intercept); ENABLE_LOGGER(); } \ No newline at end of file diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index b0c75b237..f0be6ffc2 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -111,6 +111,7 @@ static constexpr std::array build_request_handle } int main(int argc, char **argv) { + SET_CALF_COMPONENT_NAME("server"); StdoutLogger::options.componentName = "SERVER"; StdoutLogger::options.workflowName = ""; From 1902b46275b46e229a5844a686598453c89c2eec Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Tue, 26 May 2026 11:50:36 +0200 Subject: [PATCH 26/26] Cleanup of explicit CALF usage --- capio/server/capio_server.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/capio/server/capio_server.cpp b/capio/server/capio_server.cpp index f0be6ffc2..b29c81326 100644 --- a/capio/server/capio_server.cpp +++ b/capio/server/capio_server.cpp @@ -112,9 +112,7 @@ static constexpr std::array build_request_handle int main(int argc, char **argv) { SET_CALF_COMPONENT_NAME("server"); - - StdoutLogger::options.componentName = "SERVER"; - StdoutLogger::options.workflowName = ""; + UPDATE_CALF_CLI_CONFIG("SERVER", ""); Semaphore internal_server_sem(0); @@ -136,7 +134,7 @@ int main(int argc, char **argv) { capio_cl_engine->setWorkflowName(get_capio_workflow_name()); } - StdoutLogger::options.workflowName = capio_cl_engine->getWorkflowName(); + UPDATE_CALF_CLI_CONFIG("SERVER", capio_cl_engine->getWorkflowName()); if (configuration.store_all_in_memory) { capio_cl_engine->setAllStoreInMemory();