Skip to content

Commit c2dc0bd

Browse files
committed
fix: networking.c: replace DEBUG_OUT toggle with CMake opt-in; fix fprintf(stderr) and format specifiers
- Remove #define DEBUG_OUT 0 from networking.c and replace all #if DEBUG_OUT guards with #ifdef NETWORKING_DEBUG, consistent with how other debug flags work in the codebase (see #2168) - Add CMake option -DNETWORKING_DEBUG=ON for opt-in debug builds - Replace 3 error messages using printf() with fprintf(stderr): 'Can't send BIN header' 'Can't send BIN data' 'Unable to send data' - Fix format specifiers in debug logs (%u -> %zu for size_t, %u -> %d for int) - Remove duplicate NETWORKING_DEBUG option and if-block in CMakeLists.txt Fixes #2174
1 parent 9f250b1 commit c2dc0bd

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ option (WITH_FFMPEG "Build using FFmpeg demuxer and decoder" OFF)
77
option (WITH_OCR "Build with OCR (Optical Character Recognition) feature" OFF)
88
option (WITH_HARDSUBX "Build with support for burned-in subtitles" OFF)
99
option (VBI_DEBUG "Enable VBI decoder debug output" OFF)
10+
option (NETWORKING_DEBUG "Enable networking debug output" OFF)
1011

1112
# Version number
1213
set (CCEXTRACTOR_VERSION_MAJOR 0)
@@ -151,6 +152,11 @@ if (VBI_DEBUG)
151152
add_definitions(-DVBI_DEBUG)
152153
message(STATUS "VBI debug output enabled")
153154
endif (VBI_DEBUG)
155+
156+
if (NETWORKING_DEBUG)
157+
add_definitions(-DNETWORKING_DEBUG)
158+
message(STATUS "Networking debug output enabled")
159+
endif (NETWORKING_DEBUG)
154160
add_subdirectory (lib_ccx)
155161

156162
aux_source_directory(${PROJECT_SOURCE_DIR} SOURCEFILE)

src/lib_ccx/networking.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <errno.h>
99
#include <assert.h>
1010

11-
#define DEBUG_OUT 0
12-
1311
/* Protocol constants: */
1412
#define INT_LEN 10
1513
#define OK 1
@@ -93,7 +91,7 @@ ssize_t read_byte(int fd, char *status);
9391

9492
void init_sockets(void);
9593

96-
#if DEBUG_OUT
94+
#ifdef NETWORKING_DEBUG
9795
void pr_command(char c);
9896
#endif
9997

@@ -143,15 +141,15 @@ void net_send_header(const unsigned char *data, size_t len)
143141
#endif
144142
assert(srv_sd > 0);
145143

146-
#if DEBUG_OUT
147-
fprintf(stderr, "Sending header (len = %u): \n", len);
144+
#ifdef NETWORKING_DEBUG
145+
fprintf(stderr, "Sending header (len = %zu): \n", len);
148146
fprintf(stderr, "File created by %02X version %02X%02X\n", data[3], data[4], data[5]);
149147
fprintf(stderr, "File format revision: %02X%02X\n", data[6], data[7]);
150148
#endif
151149

152150
if (write_block(srv_sd, BIN_HEADER, data, len) <= 0)
153151
{
154-
printf("Can't send BIN header\n");
152+
fprintf(stderr, "Can't send BIN header\n");
155153
return;
156154
}
157155

@@ -172,13 +170,13 @@ int net_send_cc(const unsigned char *data, int len, void *private_data, struct c
172170
#endif
173171
assert(srv_sd > 0);
174172

175-
#if DEBUG_OUT
176-
fprintf(stderr, "[C] Sending %u bytes\n", len);
173+
#ifdef NETWORKING_DEBUG
174+
fprintf(stderr, "[C] Sending %d bytes\n", len);
177175
#endif
178176

179177
if (write_block(srv_sd, BIN_DATA, data, len) <= 0)
180178
{
181-
printf("Can't send BIN data\n");
179+
fprintf(stderr, "Can't send BIN data\n");
182180
return -1;
183181
}
184182

@@ -212,7 +210,7 @@ void net_check_conn()
212210
rc = read_byte(srv_sd, &c);
213211
if (c == PING)
214212
{
215-
#if DEBUG_OUT
213+
#ifdef NETWORKING_DEBUG
216214
fprintf(stderr, "[S] Received PING\n");
217215
#endif
218216
last_ping = now;
@@ -238,7 +236,7 @@ void net_check_conn()
238236
{
239237
if (write_block(srv_sd, PING, NULL, 0) < 0)
240238
{
241-
printf("Unable to send data\n");
239+
fprintf(stderr, "Unable to send data\n");
242240
exit(EXIT_FAILURE);
243241
}
244242

@@ -324,8 +322,8 @@ void net_send_epg(
324322
memcpy(end, category, c);
325323
end += c;
326324

327-
#if DEBUG_OUT
328-
fprintf(stderr, "[C] Sending EPG: %u bytes\n", len);
325+
#ifdef NETWORKING_DEBUG
326+
fprintf(stderr, "[C] Sending EPG: %zu bytes\n", len);
329327
#endif
330328

331329
if (write_block(srv_sd, EPG_DATA, epg, len) <= 0)
@@ -420,7 +418,7 @@ int net_udp_read(int socket, void *buffer, size_t length, const char *src_str, c
420418
ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
421419
{
422420
assert(fd > 0);
423-
#if DEBUG_OUT
421+
#ifdef NETWORKING_DEBUG
424422
fprintf(stderr, "[C] ");
425423
#endif
426424

@@ -433,7 +431,7 @@ ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
433431
return 0;
434432
nwritten++;
435433

436-
#if DEBUG_OUT
434+
#ifdef NETWORKING_DEBUG
437435
pr_command(command);
438436
fprintf(stderr, " ");
439437
#endif
@@ -446,7 +444,7 @@ ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
446444
return 0;
447445
nwritten += rc;
448446

449-
#if DEBUG_OUT
447+
#ifdef NETWORKING_DEBUG
450448
fwrite(len_str, sizeof(char), INT_LEN, stderr);
451449
fprintf(stderr, " ");
452450
#endif
@@ -460,7 +458,7 @@ ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
460458
nwritten += rc;
461459
}
462460

463-
#if DEBUG_OUT
461+
#ifdef NETWORKING_DEBUG
464462
if (buf != NULL && command != BIN_HEADER && command != BIN_DATA)
465463
{
466464
fwrite(buf, sizeof(char), buf_len, stderr);
@@ -474,7 +472,7 @@ ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
474472
return 0;
475473
nwritten++;
476474

477-
#if DEBUG_OUT
475+
#ifdef NETWORKING_DEBUG
478476
fprintf(stderr, "\\r");
479477
#endif
480478

@@ -484,7 +482,7 @@ ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
484482
return 0;
485483
nwritten++;
486484

487-
#if DEBUG_OUT
485+
#ifdef NETWORKING_DEBUG
488486
fprintf(stderr, "\\n\n");
489487
#endif
490488

@@ -667,11 +665,11 @@ int check_password(int fd, const char *pwd)
667665
return 1;
668666
}
669667

670-
#if DEBUG_OUT
668+
#ifdef NETWORKING_DEBUG
671669
fprintf(stderr, "[C] Wrong password\n");
672670
#endif
673671

674-
#if DEBUG_OUT
672+
#ifdef NETWORKING_DEBUG
675673
fprintf(stderr, "[S] PASSWORD\n");
676674
#endif
677675
if (write_byte(fd, PASSWORD) < 0)
@@ -792,7 +790,7 @@ ssize_t read_block(int fd, char *command, char *buf, size_t *buf_len)
792790
return 0;
793791
nread += rc;
794792

795-
#if DEBUG_OUT
793+
#ifdef NETWORKING_DEBUG
796794
fprintf(stderr, "[C] ");
797795
pr_command(*command);
798796
fprintf(stderr, " ");
@@ -805,7 +803,7 @@ ssize_t read_block(int fd, char *command, char *buf, size_t *buf_len)
805803
return 0;
806804
nread += rc;
807805

808-
#if DEBUG_OUT
806+
#ifdef NETWORKING_DEBUG
809807
fwrite(len_str, sizeof(char), INT_LEN, stderr);
810808
fprintf(stderr, " ");
811809
#endif
@@ -836,7 +834,7 @@ ssize_t read_block(int fd, char *command, char *buf, size_t *buf_len)
836834
return 0;
837835
nread += rc;
838836

839-
#if DEBUG_OUT
837+
#ifdef NETWORKING_DEBUG
840838
if (*command != BIN_DATA && *command != BIN_HEADER)
841839
{
842840
fwrite(buf, sizeof(char), len, stderr);
@@ -854,21 +852,21 @@ ssize_t read_block(int fd, char *command, char *buf, size_t *buf_len)
854852

855853
if (end[0] != '\r' || end[1] != '\n')
856854
{
857-
#if DEBUG_OUT
855+
#ifdef NETWORKING_DEBUG
858856
fprintf(stderr, "read_block(): No end marker present\n");
859857
fprintf(stderr, "Closing connection\n");
860858
#endif
861859
return 0;
862860
}
863861

864-
#if DEBUG_OUT
862+
#ifdef NETWORKING_DEBUG
865863
fprintf(stderr, "\\r\\n\n");
866864
#endif
867865

868866
return nread;
869867
}
870868

871-
#if DEBUG_OUT
869+
#ifdef NETWORKING_DEBUG
872870
void pr_command(char c)
873871
{
874872
switch (c)

0 commit comments

Comments
 (0)