From 6d042c525a3a58544e475c8b03b28dcf5fa92bc6 Mon Sep 17 00:00:00 2001 From: Michael Theall Date: Tue, 25 Jul 2017 21:55:16 -0500 Subject: [PATCH] Add some advanced logging --- delog.py | 9 +++++++++ include/console.h | 3 +++ source/console.c | 42 ++++++++++++++++++++++++++++++++++++++++- source/ftp.c | 48 ++++++++++++++++------------------------------- source/main.c | 39 +++++++++++++++++++++++++++++++++++++- 5 files changed, 107 insertions(+), 34 deletions(-) create mode 100755 delog.py diff --git a/delog.py b/delog.py new file mode 100755 index 0000000..bfe3b77 --- /dev/null +++ b/delog.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +import sys +import re + +if __name__ == '__main__': + regex = re.compile('\x1b\[[0-9]*;[0-9]*H') + for line in sys.stdin: + print(regex.sub('', line).strip()) diff --git a/include/console.h b/include/console.h index 4e3f42b..78aef52 100644 --- a/include/console.h +++ b/include/console.h @@ -33,4 +33,7 @@ void console_set_status(const char *fmt, ...); __attribute__((format(printf,1,2))) void console_print(const char *fmt, ...); +__attribute__((format(printf,1,2))) +void debug_print(const char *fmt, ...); + void console_render(void); diff --git a/source/console.c b/source/console.c index dfad8b3..e4618b9 100644 --- a/source/console.c +++ b/source/console.c @@ -12,6 +12,9 @@ static PrintConsole status_console; static PrintConsole main_console; static PrintConsole tcp_console; +#if ENABLE_LOGGING +static bool disable_logging = false; +#endif /*! initialize console subsystem */ void @@ -61,11 +64,29 @@ console_print(const char *fmt, ...) va_start(ap, fmt); vprintf(fmt, ap); #ifdef ENABLE_LOGGING - vfprintf(stderr, fmt, ap); + if(!disable_logging) + vfprintf(stderr, fmt, ap); #endif va_end(ap); } +/*! print debug message + * + * @param[in] fmt format string + * @param[in] ... format arguments + */ +void +debug_print(const char *fmt, ...) +{ +#ifdef ENABLE_LOGGING + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +#endif +} + /*! print tcp tables */ static void print_tcp_table(void) @@ -75,6 +96,10 @@ print_tcp_table(void) size_t i; int rc, lines = 0; +#ifdef ENABLE_LOGGING + disable_logging = true; +#endif + consoleSelect(&tcp_console); console_print("\x1b[0;0H\x1b[K"); optlen = sizeof(tcp_entries); @@ -148,6 +173,10 @@ print_tcp_table(void) console_print("\x1b[2J"); consoleSelect(&main_console); + +#ifdef ENABLE_LOGGING + disable_logging = false; +#endif } /*! draw console to screen */ @@ -190,6 +219,17 @@ console_print(const char *fmt, ...) va_end(ap); } +void +debug_print(const char *fmt, ...) +{ +#ifdef ENABLE_LOGGING + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +#endif +} + void console_render(void) { } diff --git a/source/ftp.c b/source/ftp.c index 586ba93..3a9bd77 100644 --- a/source/ftp.c +++ b/source/ftp.c @@ -968,7 +968,10 @@ ftp_session_new(int listen_fd) /* send initiator response */ rc = ftp_send_response(session, 220, "Hello!\r\n"); if(rc <= 0) + { + debug_print("failed to send initiator response\n"); ftp_session_destroy(session); + } } /*! accept PASV connection for ftp session @@ -1175,6 +1178,7 @@ ftp_session_read_command(ftp_session_t *session, if(rc == 0) { /* peer closed connection */ + debug_print("peer closed connection\n"); ftp_session_close_cmd(session); return; } @@ -1386,7 +1390,10 @@ ftp_session_poll(ftp_session_t *session) /* we need to read a new command */ if(pollinfo[0].revents & (POLLERR|POLLHUP)) + { + debug_print("cmd revents=0x%x\n", pollinfo[0].revents); ftp_session_close_cmd(session); + } else if(pollinfo[0].revents & (POLLIN | POLLPRI)) ftp_session_read_command(session, pollinfo[0].revents); } @@ -1449,6 +1456,7 @@ ftp_session_poll(ftp_session_t *session) return session->next; /* disconnected from peer; destroy it and return next session */ + debug_print("disconnected from peer\n"); return ftp_session_destroy(session); } @@ -1504,6 +1512,9 @@ update_status(void) { #ifdef _3DS console_set_status("\n" GREEN STATUS_STRING " " +#ifdef ENABLE_LOGGING + "DEBUG " +#endif CYAN "%s:%u" RESET, inet_ntoa(serv_addr.sin_addr), ntohs(serv_addr.sin_port)); @@ -1528,6 +1539,9 @@ update_status(void) } console_set_status(GREEN STATUS_STRING " " +#ifdef ENABLE_LOGGING + "DEBUG " +#endif YELLOW "IP:" CYAN "%s " YELLOW "Port:" CYAN "%u" RESET, @@ -1623,23 +1637,6 @@ ftp_init(void) console_print(GREEN "Ready!\n" RESET); -#ifdef ENABLE_LOGGING - /* open log file */ - FILE *fp = freopen("/ftpd.log", "wb", stderr); - if(fp == NULL) - { - console_print(RED "freopen: 0x%08X\n" RESET, errno); - goto log_fail; - } - - /* truncate log file */ - if(ftruncate(fileno(fp), 0) != 0) - { - console_print(RED "ftruncate: 0x%08X\n" RESET, errno); - goto ftruncate_fail; - } -#endif - /* allocate buffer for SOC service */ SOCU_buffer = (u32*)memalign(SOCU_ALIGN, SOCU_BUFFERSIZE); if(SOCU_buffer == NULL) @@ -1722,13 +1719,6 @@ soc_fail: SOCU_buffer = NULL; memalign_fail: -#ifdef ENABLE_LOGGING -ftruncate_fail: - if(fclose(stderr) != 0) - console_print(RED "fclose: 0x%08X\n" RESET, errno); - -log_fail: -#endif return -1; #endif } @@ -1741,6 +1731,8 @@ ftp_exit(void) Result ret; #endif + debug_print("exiting ftp server\n"); + /* clean up all sessions */ while(sessions != NULL) ftp_session_destroy(sessions); @@ -1761,14 +1753,6 @@ ftp_exit(void) console_print(RED "socExit: 0x%08X\n" RESET, (unsigned int)ret); free(SOCU_buffer); } - -#ifdef ENABLE_LOGGING - /* close log file */ - if(fclose(stderr) != 0) - console_print(RED "fclose: 0x%08X\n" RESET, errno); - -#endif - #endif } diff --git a/source/main.c b/source/main.c index 7d0d433..60a0bd7 100644 --- a/source/main.c +++ b/source/main.c @@ -1,6 +1,8 @@ +#include #include #include #include +#include #ifdef _3DS #include <3ds.h> #endif @@ -78,7 +80,33 @@ main(int argc, /* initialize console subsystem */ console_init(); - console_set_status("\n" GREEN STATUS_STRING RESET); + +#ifdef ENABLE_LOGGING + /* open log file */ +#ifdef _3DS + FILE *fp = freopen("/ftpd.log", "wb", stderr); +#else + FILE *fp = freopen("ftpd.log", "wb", stderr); +#endif + if(fp == NULL) + { + console_print(RED "freopen: 0x%08X\n" RESET, errno); + goto log_fail; + } + + /* truncate log file */ + if(ftruncate(fileno(fp), 0) != 0) + { + console_print(RED "ftruncate: 0x%08X\n" RESET, errno); + goto log_fail; + } +#endif + + console_set_status("\n" GREEN STATUS_STRING +#ifdef ENABLE_LOGGING + " DEBUG" +#endif + RESET); while(status == LOOP_RESTART) { @@ -97,6 +125,15 @@ main(int argc, #ifdef _3DS console_print("Press B to exit\n"); +#endif + +#ifdef ENABLE_LOGGING +log_fail: + if(fclose(stderr) != 0) + console_print(RED "fclose(%d): 0x%08X\n" RESET, fileno(stderr), errno); +#endif + +#ifdef _3DS loop(wait_for_b); /* deinitialize 3DS services */