From 34cebb1019d48559e091f9c697001518aea743d4 Mon Sep 17 00:00:00 2001 From: Christian Burger Date: Fri, 8 Apr 2022 22:03:12 +0200 Subject: [PATCH] refactored some debug output --- Debug.hpp | 42 ++++++++++++++++++------------- NCursesPtyWindow.cpp | 60 +++++++++++++++----------------------------- NCursesPtyWindow.hpp | 2 +- 3 files changed, 45 insertions(+), 59 deletions(-) diff --git a/Debug.hpp b/Debug.hpp index 8c965de..8d68390 100644 --- a/Debug.hpp +++ b/Debug.hpp @@ -67,42 +67,47 @@ namespace krikkel } #define __debug_stringify_get_string(closureName) __debug_stringify__##closureName() -#define __debug_bytes_printable_table(unprintable, numberOfBytes) ([](const char *bytes, size_t length) \ +#define __debug_make_bytes_printable_table(__bytes) ([](std::string bytes) -> std::string \ { \ std::locale loc; \ - size_t index, targetIndex = 0; \ - size_t targetLength = (length / 16 + 1) * (3 + 3 * 16 + 1) + 1; \ - char targetBytes[targetLength]; \ - for(index = 0; index < length; index++) \ + std::string result = "\n"; \ + int index; \ + for(index = 0; index < bytes.length(); ++index) \ { \ + static const uint8_t TRANSLATION_BUFFER_SIZE = 4; \ + char translationBuffer[TRANSLATION_BUFFER_SIZE]; \ if(std::isprint(bytes[index], loc)) \ - targetIndex += snprintf(targetBytes + targetIndex, targetLength - targetIndex, index % 16 == 0 ? " | %c" : " %c", bytes[index]); \ + { \ + translationBuffer[0] = translationBuffer[1] = ' '; \ + translationBuffer[2] = bytes[index]; \ + translationBuffer[3] = '\0'; \ + } \ else \ - targetIndex += snprintf(targetBytes + targetIndex, targetLength - targetIndex, index % 16 == 0 ? " | %02x" : " %02x", bytes[index]); \ + std::snprintf(translationBuffer, TRANSLATION_BUFFER_SIZE, " %02x", bytes[index]); \ + result += (index % 16 == 0 ? " | " : " "); \ + result += translationBuffer; \ if(index % 16 == 15) \ - targetIndex += snprintf(targetBytes + targetIndex, targetLength - targetIndex, "\n"); \ + result += '\n'; \ } \ if(index % 16) \ - snprintf(targetBytes + targetIndex, targetLength - targetIndex, "\n"); \ - return std::string(targetBytes); \ + result += '\n'; \ + return result; \ } \ - )(unprintable, numberOfBytes) + )(__bytes) -#define __debug_bytes_printable(bytes, length) ([](std::string unprintable, size_t numberOfBytes) -> std::string \ +#define __debug_make_bytes_printable(__bytes) ([](std::string bytes) -> std::string \ { \ std::locale loc; \ std::string result; \ - for(char character : unprintable) \ + for(char character : bytes) \ { \ - if(!numberOfBytes--) \ - break; \ if(std::isprint(character, loc) && character != '<') \ result += character; \ else \ - result += "<" + std::to_string(character) + ">"; \ + result += '<' + std::to_string(character) + '>'; \ } \ return result; \ - })(bytes, length) + })(__bytes) #else @@ -114,7 +119,8 @@ namespace krikkel #define __debug_stringify_switch_case_end_string(value) #define __debug_stringify_switch_case_end_number(value) #define __debug_stringify_switch_end() -#define __debug_bytes_printable(bytes) +#define __debug_make_bytes_printable_table(bytes) +#define __debug_make_bytes_printable(bytes) #endif /* NDEBUG */ diff --git a/NCursesPtyWindow.cpp b/NCursesPtyWindow.cpp index 0356e94..bed2651 100644 --- a/NCursesPtyWindow.cpp +++ b/NCursesPtyWindow.cpp @@ -67,8 +67,7 @@ namespace krikkel::NCursesPtyWindow void NCursesPtyWindow::writeToClient(const char *output, size_t length) const { write(fdPtyHost, output, length); - __debug_log("written to PTY client: "); - __debug_log(__debug_bytes_printable_table(output, length)); + __debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\''); } void NCursesPtyWindow::readFromPtyClientThreadMethod() @@ -93,7 +92,7 @@ namespace krikkel::NCursesPtyWindow size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE); if(bytesRead != -1 && bytesRead != 0) vterm_input_write(pseudoTerminal, ptyClientOutputBuffer, bytesRead); - __debug_log("read from PTY client: '" + __debug_bytes_printable(ptyClientOutputBuffer, bytesRead) + "'"); + __debug_log("read from PTY client: '" + __debug_make_bytes_printable(std::string(ptyClientOutputBuffer, bytesRead)) + '\''); } VTermScreenCallbacks NCursesPtyWindow::screenCallbacks = @@ -147,43 +146,24 @@ namespace krikkel::NCursesPtyWindow int NCursesPtyWindow::handlerSetTermProp(VTermProp prop, VTermValue *val) { + /// @todo maybe use "vterm_get_prop_type() —> bool, number, string"? + __debug_stringify_switch_begin(handlerSetTermProp, prop, val); + __debug_stringify_switch_case(VTERM_PROP_CURSORVISIBLE); + __debug_stringify_switch_case(VTERM_PROP_CURSORBLINK); + __debug_stringify_switch_case(VTERM_PROP_ALTSCREEN); + __debug_stringify_switch_case(VTERM_PROP_REVERSE); + __debug_stringify_switch_case_end_bool(val->boolean); + __debug_stringify_switch_case(VTERM_PROP_TITLE); + __debug_stringify_switch_case(VTERM_PROP_ICONNAME); + __debug_stringify_switch_case_end_string(val->string); + __debug_stringify_switch_case(VTERM_PROP_CURSORSHAPE); + __debug_stringify_switch_case(VTERM_PROP_MOUSE); + __debug_stringify_switch_case_end_number(val->number); + __debug_stringify_switch_end(prop); - __debug_log(std::string("(unimplemented) set terminal property: ") - + (prop == VTERM_PROP_CURSORVISIBLE - ? std::string("cursor visible = ") - + (val->boolean ? "true" : "false") - : "") - + (prop == VTERM_PROP_CURSORBLINK - ? std::string("cursor blink = ") - + (val->boolean ? "true" : "false") - : "") - + (prop == VTERM_PROP_ALTSCREEN - ? std::string("alt screen = ") - + (val->boolean ? "true" : "false") - : "") - + (prop == VTERM_PROP_TITLE - ? std::string("title = ") - + val->string - : "") - + (prop == VTERM_PROP_ICONNAME - ? std::string("icon name = ") - + val->string - : "") - + (prop == VTERM_PROP_REVERSE - ? std::string("alt screen = ") - + (val->boolean ? "true" : "false") - : "") - + (prop == VTERM_PROP_CURSORSHAPE - ? std::string("icon name = ") - + std::to_string(val->number) - : "") - + (prop == VTERM_PROP_MOUSE - ? std::string("mouse = ") - + std::to_string(val->number) - : "") - + (prop == VTERM_N_PROPS - ? "n props" : "") - ); + __debug_log(std::string("unimplemented handler called: ") + + __debug_stringify_get_string(handlerSetTermProp) + ); return 0; } @@ -248,7 +228,7 @@ namespace krikkel::NCursesPtyWindow add_wch(&converted); } - int NCursesPtyWindow::add_wch(cchar_t *character) + int NCursesPtyWindow::add_wch(const cchar_t *character) { return ::wadd_wch(w, character); } diff --git a/NCursesPtyWindow.hpp b/NCursesPtyWindow.hpp index 7d9fe5a..404e956 100644 --- a/NCursesPtyWindow.hpp +++ b/NCursesPtyWindow.hpp @@ -26,7 +26,7 @@ namespace krikkel::NCursesPtyWindow int getFdPtyClient() const; void writeToClient(const char * string, size_t length) const; - int add_wch(cchar_t *character); + int add_wch(const cchar_t *character); int refresh() override; int wresize(int rows, int cols);