From 5d5fd32fe67be087e1f8d9ff74f80b7dd3731ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 16 Nov 2018 21:55:32 +0100 Subject: [PATCH] Log API --- lib/CMakeLists.txt | 6 +++-- lib/include/chiaki/log.h | 47 ++++++++++++++++++++++++++++++++ lib/include/chiaki/session.h | 3 +++ lib/src/log.c | 52 ++++++++++++++++++++++++++++++++++++ lib/src/session.c | 23 +++++++++++----- 5 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 lib/include/chiaki/log.h create mode 100644 lib/src/log.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 09b5d50..a1d611f 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -4,14 +4,16 @@ set(HEADER_FILES include/chiaki/common.h include/chiaki/thread.h include/chiaki/base64.h - include/chiaki/http.h) + include/chiaki/http.h + include/chiaki/log.h) set(SOURCE_FILES src/common.c src/session.c src/thread.c src/base64.c - src/http.c) + src/http.c + src/log.c) add_library(chiaki-lib ${HEADER_FILES} ${SOURCE_FILES}) set_target_properties(chiaki-lib PROPERTIES OUTPUT_NAME chiaki) diff --git a/lib/include/chiaki/log.h b/lib/include/chiaki/log.h new file mode 100644 index 0000000..04a7bcd --- /dev/null +++ b/lib/include/chiaki/log.h @@ -0,0 +1,47 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_LOG_H +#define CHIAKI_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + CHIAKI_LOG_DEBUG, + CHIAKI_LOG_INFO, + CHIAKI_LOG_WARNING, + CHIAKI_LOG_ERROR +} ChiakiLogLevel; + +typedef struct chiaki_log_t +{ +} ChiakiLog; + +void chiaki_log(ChiakiLog *log, ChiakiLogLevel level, const char *fmt, ...); + +#define CHIAKI_LOGD(log, ...) do { chiaki_log((log), CHIAKI_LOG_DEBUG, __VA_ARGS__); } while(0); +#define CHIAKI_LOGI(log, ...) do { chiaki_log((log), CHIAKI_LOG_INFO, __VA_ARGS__); } while(0); +#define CHIAKI_LOGW(log, ...) do { chiaki_log((log), CHIAKI_LOG_WARNING, __VA_ARGS__); } while(0); +#define CHIAKI_LOGE(log, ...) do { chiaki_log((log), CHIAKI_LOG_ERROR, __VA_ARGS__); } while(0); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_LOG_H diff --git a/lib/include/chiaki/session.h b/lib/include/chiaki/session.h index da8ae25..5f69181 100644 --- a/lib/include/chiaki/session.h +++ b/lib/include/chiaki/session.h @@ -20,6 +20,7 @@ #include "common.h" #include "thread.h" +#include "log.h" #include #include @@ -88,6 +89,8 @@ typedef struct chiaki_session_t void *event_cb_user; ChiakiThread session_thread; + + ChiakiLog log; } ChiakiSession; CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info); diff --git a/lib/src/log.c b/lib/src/log.c new file mode 100644 index 0000000..76216ff --- /dev/null +++ b/lib/src/log.c @@ -0,0 +1,52 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +#include +#include + +void chiaki_log(ChiakiLog *log, ChiakiLogLevel level, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + char c; + switch(level) + { + case CHIAKI_LOG_DEBUG: + c = 'D'; + break; + case CHIAKI_LOG_INFO: + c = 'I'; + break; + case CHIAKI_LOG_WARNING: + c = 'W'; + break; + case CHIAKI_LOG_ERROR: + c = 'E'; + break; + default: + c = '?'; + break; + } + + printf("[%c] ", c); + vprintf(fmt, args); + + va_end(args); +} \ No newline at end of file diff --git a/lib/src/session.c b/lib/src/session.c index 732286e..7845221 100644 --- a/lib/src/session.c +++ b/lib/src/session.c @@ -106,11 +106,13 @@ static void *session_thread_func(void *arg) ChiakiSession *session = arg; bool success; + CHIAKI_LOGI(&session->log, "Starting session request\n"); + success = session_thread_request_session(session); if(!success) goto quit; - printf("Connected!\n"); + CHIAKI_LOGI(&session->log, "Session request successful\n"); ChiakiEvent quit_event; quit: @@ -188,13 +190,17 @@ static bool session_thread_request_session(ChiakiSession *session) continue; } + CHIAKI_LOGI(&session->log, "Trying to request session from %s:%d\n", host_buf, SESSION_PORT); + session_sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if(session_sock < 0) continue; r = connect(session_sock, sa, ai->ai_addrlen); if(r < 0) { - if(errno == ECONNREFUSED) + int errsv = errno; + CHIAKI_LOGE(&session->log, "Session request connect failed: %s\n", strerror(errsv)); + if(errsv == ECONNREFUSED) session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_CONNECTION_REFUSED; else session->quit_reason = CHIAKI_QUIT_REASON_NONE; @@ -212,13 +218,13 @@ static bool session_thread_request_session(ChiakiSession *session) if(session_sock < 0) { - printf("Session Connection Failed.\n"); + CHIAKI_LOGE(&session->log, "Session request connect failed eventually.\n"); if(session->quit_reason == CHIAKI_QUIT_REASON_NONE) session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN; return false; } - printf("Connected to %s:%u\n", host_buf, SESSION_PORT); + CHIAKI_LOGI(&session->log, "Connected to %s:%d\n", host_buf, SESSION_PORT); static const char session_request_fmt[] = "GET /sce/rp/session HTTP/1.1\r\n" @@ -240,11 +246,12 @@ static bool session_thread_request_session(ChiakiSession *session) return false; } - printf("sending\n%s\n", buf); + CHIAKI_LOGI(&session->log, "Sending session request\n"); ssize_t sent = send(session_sock, buf, (size_t)request_len, 0); if(sent < 0) { + CHIAKI_LOGE(&session->log, "Failed to send session request\n"); close(session_sock); session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN; return false; @@ -255,18 +262,19 @@ static bool session_thread_request_session(ChiakiSession *session) ChiakiErrorCode err = chiaki_recv_http_header(session_sock, buf, sizeof(buf), &header_size, &received_size); if(err != CHIAKI_ERR_SUCCESS) { + CHIAKI_LOGE(&session->log, "Failed to receive session request response\n"); close(session_sock); session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN; return false; } buf[received_size] = '\0'; - printf("received\n%s\n", buf); ChiakiHttpResponse http_response; err = chiaki_http_response_parse(&http_response, buf, header_size); if(err != CHIAKI_ERR_SUCCESS) { + CHIAKI_LOGE(&session->log, "Failed to parse session request response\n"); close(session_sock); session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN; return false; @@ -281,6 +289,7 @@ static bool session_thread_request_session(ChiakiSession *session) err = chiaki_base64_decode(response.nonce, strlen(response.nonce), session->nonce, &nonce_len); if(err != CHIAKI_ERR_SUCCESS || nonce_len != CHIAKI_KEY_BYTES) { + CHIAKI_LOGE(&session->log, "Nonce invalid\n"); response.success = false; session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN; } @@ -290,9 +299,11 @@ static bool session_thread_request_session(ChiakiSession *session) switch(response.error_code) { case RP_APPLICATION_REASON_IN_USE: + CHIAKI_LOGE(&session->log, "Remote is already in use\n"); session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_RP_IN_USE; break; case RP_APPLICATION_REASON_CRASH: + CHIAKI_LOGE(&session->log, "Remote seems to have crashed\n"); session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_RP_CRASH; break; default: