From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:56:29 +0000 (+0000) Subject: Make debug_print_buffer check the debug level before doing any work. Set suggested... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0742bba8ed37159b6a79d7d1321a3b83de6e0bab;p=thirdparty%2Fnqptp.git Make debug_print_buffer check the debug level before doing any work. Set suggested compiler optimisation level to -O3. --- diff --git a/configure.ac b/configure.ac index 503b805..d861117 100644 --- a/configure.ac +++ b/configure.ac @@ -3,6 +3,8 @@ AC_PREREQ([2.68]) AC_INIT([nqptp], [1.2.5], [4265913+mikebrady@users.noreply.github.com]) +: ${CFLAGS="-O3"} +: ${CXXFLAGS="-O3"} AM_INIT_AUTOMAKE AC_CANONICAL_HOST diff --git a/debug.c b/debug.c index 50a7705..62108fd 100644 --- a/debug.c +++ b/debug.c @@ -59,6 +59,10 @@ void debug_init(int level, int show_elapsed_time, int show_relative_time, int sh debugger_show_file_and_line = show_file_and_line; } +int debug_level() { + return debuglev; +} + char *generate_preliminary_string(char *buffer, size_t buffer_length, double tss, double tsl, const char *filename, const int linenumber, const char *prefix) { size_t space_remaining = buffer_length; diff --git a/debug.h b/debug.h index 7415c97..8f7ffe4 100644 --- a/debug.h +++ b/debug.h @@ -22,6 +22,7 @@ // level 0 is no messages, level 3 is most messages void debug_init(int level, int show_elapsed_time, int show_relative_time, int show_file_and_line); +int debug_level(); // get current debug level void _die(const char *filename, const int linenumber, const char *format, ...); void _warn(const char *filename, const int linenumber, const char *format, ...); diff --git a/nqptp-utilities.c b/nqptp-utilities.c index 18e5a9c..39cb53b 100644 --- a/nqptp-utilities.c +++ b/nqptp-utilities.c @@ -114,52 +114,54 @@ void open_sockets_at_port(const char *node, uint16_t port, } void debug_print_buffer(int level, char *buf, size_t buf_len) { - // printf("Received %u bytes in a packet from %s:%d\n", buf_len, inet_ntoa(si_other.sin_addr), - // ntohs(si_other.sin_port)); - char *obf = - malloc(buf_len * 4 + 1); // to be on the safe side -- 4 characters on average for each byte - if (obf != NULL) { - char *obfp = obf; - unsigned int obfc; - for (obfc = 0; obfc < buf_len; obfc++) { - snprintf(obfp, 3, "%02X", buf[obfc]); - obfp += 2; - if (obfc != buf_len - 1) { - if (obfc % 32 == 31) { - snprintf(obfp, 5, " || "); - obfp += 4; - } else if (obfc % 16 == 15) { - snprintf(obfp, 4, " | "); - obfp += 3; - } else if (obfc % 4 == 3) { - snprintf(obfp, 2, " "); - obfp += 1; + if (debug_level() >= level) { + // printf("Received %u bytes in a packet from %s:%d\n", buf_len, inet_ntoa(si_other.sin_addr), + // ntohs(si_other.sin_port)); + char *obf = + malloc(buf_len * 4 + 1); // to be on the safe side -- 4 characters on average for each byte + if (obf != NULL) { + char *obfp = obf; + unsigned int obfc; + for (obfc = 0; obfc < buf_len; obfc++) { + snprintf(obfp, 3, "%02X", buf[obfc]); + obfp += 2; + if (obfc != buf_len - 1) { + if (obfc % 32 == 31) { + snprintf(obfp, 5, " || "); + obfp += 4; + } else if (obfc % 16 == 15) { + snprintf(obfp, 4, " | "); + obfp += 3; + } else if (obfc % 4 == 3) { + snprintf(obfp, 2, " "); + obfp += 1; + } } + }; + *obfp = 0; + switch (buf[0]) { + + case 0x10: + debug(level, "SYNC: \"%s\".", obf); + break; + case 0x18: + debug(level, "FLUP: \"%s\".", obf); + break; + case 0x19: + debug(level, "DRSP: \"%s\".", obf); + break; + case 0x1B: + debug(level, "ANNC: \"%s\".", obf); + break; + case 0x1C: + debug(level, "SGNL: \"%s\".", obf); + break; + default: + debug(1, "XXXX \"%s\".", obf); // output this at level 1 + break; } - }; - *obfp = 0; - switch (buf[0]) { - - case 0x10: - debug(level, "SYNC: \"%s\".", obf); - break; - case 0x18: - debug(level, "FLUP: \"%s\".", obf); - break; - case 0x19: - debug(level, "DRSP: \"%s\".", obf); - break; - case 0x1B: - debug(level, "ANNC: \"%s\".", obf); - break; - case 0x1C: - debug(level, "SGNL: \"%s\".", obf); - break; - default: - debug(1, "XXXX \"%s\".", obf); // output this at level 1 - break; + free(obf); } - free(obf); } }