diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 20:31:18 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 20:31:18 +0100 |
commit | d8daa3e045ca2f41edcbed533bc5a9fef1363a17 (patch) | |
tree | 6a4c0883d05607476b609e68bcac7d854e281aea /backend/print.cpp | |
parent | 6e1d62955c7a7f39bc9126d709a42a70e02a1d30 (diff) |
print to backend
Diffstat (limited to 'backend/print.cpp')
-rw-r--r-- | backend/print.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/backend/print.cpp b/backend/print.cpp new file mode 100644 index 0000000..fc8ad9b --- /dev/null +++ b/backend/print.cpp @@ -0,0 +1,67 @@ +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <unistd.h> + +#include "print.h" +#include "util.h" + +static String va_stringf(va_list args, const char * fmt) { + va_list args_copy; + va_copy(args_copy, args); + + size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; + char * msg = (char *) malloc(sz); + va_end(args_copy); + + vsnprintf(msg, sz, fmt, args); + + String out = msg; + free(msg); + + va_end(args); + return out; +} + +void lprtf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + String formatted = String::va_fmt(args, fmt); + va_end(args); + + fwrite(formatted.c_str(), 1, formatted.size(), stdout); + fflush(stdout); + + SessionLog::get().append(formatted); +} + +SessionLog & SessionLog::get() { + static SessionLog instance; + return instance; +} + +SessionLog::SessionLog() { + if (!this->enable) return; + + String filename = String::fmt("%lu.log", getpid()); + FILE * file = fopen(filename.c_str(), "w+"); +} + +SessionLog::~SessionLog() { + safe_free(this->file); +} + +void SessionLog::append(const String & str) const { + this->append(str.data(), str.size()); +} + +void SessionLog::append(const char * str) const { + this->append(str, strlen(str)); +} + +void SessionLog::append(const char * buf, size_t buf_size) const { + if (this->file == nullptr) return; + if (buf_size == 0) return; + fwrite(buf, 1, buf_size, this->file); +} + |