From 073ad235ae62a0f18b5aea931ed6900f65de32cf Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Fri, 10 Feb 2023 23:24:10 +0800 Subject: [PATCH] Improve logging Use our logging mechanism and remove dependency on SDL. --- TODO.md | 4 +-- src/packages/log/IZ_intercept.c | 61 ++++++++++++++++++++++++++++++--- src/packages/log/IZ_intercept.h | 4 ++- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index 4384186..755941a 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,5 @@ +- [X] Improve logging (remove SDL/lws dependency) +- [ ] Unify memset/memcpy/free/malloc functions (remove SDL/lws dependency) - [ ] Fix gamepad mapping - [ ] Provide default mapping - [ ] Allow customization of button mappings @@ -5,5 +7,3 @@ - [ ] server - [ ] client (frontend) - [ ] Proof-of-concept for fast SVG rendering (we're going to use SVG instead of Spine) -- [ ] Improve logging (remove SDL/lws dependency) -- [ ] Unify memset/memcpy/free/malloc functions (remove SDL/lws dependency) diff --git a/src/packages/log/IZ_intercept.c b/src/packages/log/IZ_intercept.c index 2504032..57171e3 100644 --- a/src/packages/log/IZ_intercept.c +++ b/src/packages/log/IZ_intercept.c @@ -1,22 +1,75 @@ #include "IZ_intercept.h" +typedef enum { + IZ_LOG_INTERCEPT_EXTRACT_STATE_INITIAL, + IZ_LOG_INTERCEPT_EXTRACT_STATE_OPEN_BRACKET, + IZ_LOG_INTERCEPT_EXTRACT_STATE_CLOSE_BRACKET, + IZ_LOG_INTERCEPT_EXTRACT_STATE_SPACE, + IZ_LOG_INTERCEPT_EXTRACT_STATE_MESSAGE, +} IZ_LogInterceptExtractState; + +void IZ_LogInterceptExtractWSMessageToFormatted(const char* raw_line, char* formatted_line) { + IZ_LogInterceptExtractState state = IZ_LOG_INTERCEPT_EXTRACT_STATE_INITIAL; + unsigned int offset = 0; + + IZ_memset(formatted_line, 0, 1024); + for (unsigned int i = 0; i < 1024; i += 1) { + if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_INITIAL && raw_line[i] == '[') { + state = IZ_LOG_INTERCEPT_EXTRACT_STATE_OPEN_BRACKET; + continue; + } + + if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_OPEN_BRACKET && raw_line[i] == ']') { + state = IZ_LOG_INTERCEPT_EXTRACT_STATE_CLOSE_BRACKET; + continue; + } + + if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_CLOSE_BRACKET && raw_line[i] == ' ') { + offset = i + 1; + state = IZ_LOG_INTERCEPT_EXTRACT_STATE_SPACE; + continue; + } + + if (state == IZ_LOG_INTERCEPT_EXTRACT_STATE_SPACE) { + if (isspace(raw_line[i])) { + continue; + } + state = IZ_LOG_INTERCEPT_EXTRACT_STATE_MESSAGE; + } + + if (raw_line[i] == '\r' || raw_line[i] == '\n' || raw_line[i] == '\0') { + formatted_line[i - offset] = '\0'; + break; + } + + if (iscntrl(raw_line[i])) { + continue; + } + + formatted_line[i - offset] = raw_line[i]; + } +} + void IZ_LogInterceptHandleFromWS(i32 level, const char* line) { + static char buffer[1024]; + IZ_LogInterceptExtractWSMessageToFormatted(line, buffer); switch (level) { + // TODO level is a bit field...check if each level is OR'd. case LLL_ERR: - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", line); + IZ_LogError("%s", buffer); return; case LLL_WARN: - SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "%s", line); + IZ_LogWarn(false, "%s", buffer); return; case LLL_NOTICE: - SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", line); + IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "%s", buffer); return; case LLL_USER: default: break; } - SDL_Log("%s", line); + IZ_Log("%s", buffer); } void IZ_LogInterceptWSMessages(i32 level) { diff --git a/src/packages/log/IZ_intercept.h b/src/packages/log/IZ_intercept.h index 1ca9811..511cd72 100644 --- a/src/packages/log/IZ_intercept.h +++ b/src/packages/log/IZ_intercept.h @@ -1,9 +1,11 @@ #ifndef IZ_INTERCEPT_H #define IZ_INTERCEPT_H -#include "SDL_log.h" +#include #include "libwebsockets.h" #include "../common/IZ_common.h" +#include "../string/IZ_string.h" +#include "IZ_log.h" void IZ_LogInterceptWSMessages(i32);