refactored some debug output

This commit is contained in:
Christian Burger 2022-04-08 22:03:12 +02:00
parent c7138f3df9
commit 34cebb1019
3 changed files with 45 additions and 59 deletions

View file

@ -67,42 +67,47 @@ namespace krikkel
} }
#define __debug_stringify_get_string(closureName) __debug_stringify__##closureName() #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; \ std::locale loc; \
size_t index, targetIndex = 0; \ std::string result = "\n"; \
size_t targetLength = (length / 16 + 1) * (3 + 3 * 16 + 1) + 1; \ int index; \
char targetBytes[targetLength]; \ for(index = 0; index < bytes.length(); ++index) \
for(index = 0; index < length; index++) \
{ \ { \
static const uint8_t TRANSLATION_BUFFER_SIZE = 4; \
char translationBuffer[TRANSLATION_BUFFER_SIZE]; \
if(std::isprint(bytes[index], loc)) \ 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 \ 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) \ if(index % 16 == 15) \
targetIndex += snprintf(targetBytes + targetIndex, targetLength - targetIndex, "\n"); \ result += '\n'; \
} \ } \
if(index % 16) \ if(index % 16) \
snprintf(targetBytes + targetIndex, targetLength - targetIndex, "\n"); \ result += '\n'; \
return std::string(targetBytes); \ 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::locale loc; \
std::string result; \ std::string result; \
for(char character : unprintable) \ for(char character : bytes) \
{ \ { \
if(!numberOfBytes--) \
break; \
if(std::isprint(character, loc) && character != '<') \ if(std::isprint(character, loc) && character != '<') \
result += character; \ result += character; \
else \ else \
result += "<" + std::to_string(character) + ">"; \ result += '<' + std::to_string(character) + '>'; \
} \ } \
return result; \ return result; \
})(bytes, length) })(__bytes)
#else #else
@ -114,7 +119,8 @@ namespace krikkel
#define __debug_stringify_switch_case_end_string(value) #define __debug_stringify_switch_case_end_string(value)
#define __debug_stringify_switch_case_end_number(value) #define __debug_stringify_switch_case_end_number(value)
#define __debug_stringify_switch_end() #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 */ #endif /* NDEBUG */

View file

@ -67,8 +67,7 @@ namespace krikkel::NCursesPtyWindow
void NCursesPtyWindow::writeToClient(const char *output, size_t length) const void NCursesPtyWindow::writeToClient(const char *output, size_t length) const
{ {
write(fdPtyHost, output, length); write(fdPtyHost, output, length);
__debug_log("written to PTY client: "); __debug_log("written to PTY client: '" + __debug_make_bytes_printable(std::string(output, length)) + '\'');
__debug_log(__debug_bytes_printable_table(output, length));
} }
void NCursesPtyWindow::readFromPtyClientThreadMethod() void NCursesPtyWindow::readFromPtyClientThreadMethod()
@ -93,7 +92,7 @@ namespace krikkel::NCursesPtyWindow
size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE); size_t bytesRead = read(fdPtyHost, ptyClientOutputBuffer, PTY_CLIENT_OUTPUT_BUFFER_SIZE);
if(bytesRead != -1 && bytesRead != 0) if(bytesRead != -1 && bytesRead != 0)
vterm_input_write(pseudoTerminal, ptyClientOutputBuffer, bytesRead); 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 = VTermScreenCallbacks NCursesPtyWindow::screenCallbacks =
@ -147,42 +146,23 @@ namespace krikkel::NCursesPtyWindow
int NCursesPtyWindow::handlerSetTermProp(VTermProp prop, VTermValue *val) 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: ") __debug_log(std::string("unimplemented handler called: ")
+ (prop == VTERM_PROP_CURSORVISIBLE + __debug_stringify_get_string(handlerSetTermProp)
? 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" : "")
); );
return 0; return 0;
} }
@ -248,7 +228,7 @@ namespace krikkel::NCursesPtyWindow
add_wch(&converted); add_wch(&converted);
} }
int NCursesPtyWindow::add_wch(cchar_t *character) int NCursesPtyWindow::add_wch(const cchar_t *character)
{ {
return ::wadd_wch(w, character); return ::wadd_wch(w, character);
} }

View file

@ -26,7 +26,7 @@ namespace krikkel::NCursesPtyWindow
int getFdPtyClient() const; int getFdPtyClient() const;
void writeToClient(const char * string, size_t length) 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 refresh() override;
int wresize(int rows, int cols); int wresize(int rows, int cols);