Seregon/zftpd

Zero-copy FTP/HTTP Daemon compatible with all POSIX systems

C/11.0 KB/No license
src/ftp_log.c
zftpd / src / ftp_log.c
1#include "ftp_log.h"
2#include <stdio.h>
3#include <string.h>
4 
5static const char *level_str(ftp_log_level_t level)
6{
7 switch (level) {
8 case FTP_LOG_WARN:
9 return "WARN";
10 case FTP_LOG_ERROR:
11 return "ERROR";
12 case FTP_LOG_INFO:
13 default:
14 return "INFO";
15 }
16}
17 
18void ftp_log_line(ftp_log_level_t level, const char *line)
19{
20 if (line == NULL) {
21 return;
22 }
23 
24#if defined(PLATFORM_PS4) || defined(PLATFORM_PS5)
25 printf("[FTP][%s] %s\n", level_str(level), line);
26#else
27 fprintf(stderr, "[FTP][%s] %s\n", level_str(level), line);
28#endif
29}
30 
31void ftp_log_session_event(const ftp_session_t *session,
32 const char *event,
33 ftp_error_t result,
34 uint64_t bytes)
35{
36 const uint32_t sid = (session != NULL) ? session->session_id : 0U;
37 const char *ip = (session != NULL) ? session->client_ip : "unknown";
38 char buf[256];
39 (void)snprintf(buf, sizeof(buf),
40 "SID=%u IP=%s EVT=%s RES=%d BYTES=%llu",
41 (unsigned)sid,
42 ip,
43 (event != NULL) ? event : "unknown",
44 (int)result,
45 (unsigned long long)bytes);
46 ftp_log_line((result == FTP_OK) ? FTP_LOG_INFO : FTP_LOG_WARN, buf);
47}
48 
49void ftp_log_session_cmd(const ftp_session_t *session,
50 const char *command,
51 ftp_error_t result)
52{
53 const uint32_t sid = (session != NULL) ? session->session_id : 0U;
54 const char *ip = (session != NULL) ? session->client_ip : "unknown";
55 char buf[256];
56 (void)snprintf(buf, sizeof(buf),
57 "SID=%u IP=%s CMD=%s RES=%d",
58 (unsigned)sid,
59 ip,
60 (command != NULL) ? command : "unknown",
61 (int)result);
62 ftp_log_line((result == FTP_OK) ? FTP_LOG_INFO : FTP_LOG_WARN, buf);
63}
64