Add some advanced logging

This commit is contained in:
Michael Theall 2017-07-25 21:55:16 -05:00
parent c763e2d52f
commit 6d042c525a
5 changed files with 107 additions and 34 deletions

9
delog.py Executable file
View File

@ -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())

View File

@ -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);

View File

@ -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
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)
{
}

View File

@ -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
}

View File

@ -1,6 +1,8 @@
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#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 */