From f9cf93eaee0ca294a7dc3e1bcc9ed206015c80e8 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Tue, 15 Oct 2024 01:36:02 +0800 Subject: [PATCH] Update logging to also print to stdout + replace printf to logging. --- audio.c | 14 ++++++++------ audio.h | 9 ++++----- log.c | 21 ++++++++++++++++++--- log.h | 6 +----- main.c | 30 ++++++++++++++++-------------- texture.c | 11 ++++++----- 6 files changed, 53 insertions(+), 38 deletions(-) diff --git a/audio.c b/audio.c index f9d8a8a..1ef0594 100644 --- a/audio.c +++ b/audio.c @@ -1,6 +1,8 @@ #include "audio.h" #include "log.h" +#include + static SDL_AudioSpec recording_spec; static SDL_AudioSpec playback_spec; static SDL_AudioSpec received_recording_spec; @@ -37,9 +39,9 @@ int audio_init(audio_state* state) int audio_recording_init(audio_state* state, int index) { SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(index, 1), 1, &recording_spec, &received_recording_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); - printf("Recording Device: %s\n", SDL_GetAudioDeviceName(index, 1)); + log_message(LOG_INFO, "Recording Device: %s", SDL_GetAudioDeviceName(index, 1)); if (!device_id) { - printf("Failed to open recording device! SDL Error: %s", SDL_GetError()); + log_message(LOG_INFO, "Failed to open recording device! SDL Error: %s", SDL_GetError()); return 0; } state->recording_device_id = device_id; @@ -65,13 +67,13 @@ void audio_destroy(audio_state* state) int audio_playback_init(audio_state* state) { if (!state->recording_buffer) { - printf("Audio buffer not initialized"); + log_message(LOG_INFO, "Audio buffer not initialized"); return 0; } SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, &playback_spec, &received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if (!device_id) { - printf("Failed to open playback device! SDL Error: %s", SDL_GetError()); + log_message(LOG_INFO, "Failed to open playback device! SDL Error: %s", SDL_GetError()); return 0; } state->playback_device_id = device_id; @@ -79,7 +81,7 @@ int audio_playback_init(audio_state* state) return 1; } -void audio_recording_callback(void* userdata, Uint8* stream, int len) +void audio_recording_callback(void* userdata, uint8_t* stream, int len) { char string[64]; audio_state* state = userdata; @@ -103,7 +105,7 @@ void audio_recording_callback(void* userdata, Uint8* stream, int len) log_message(LOG_INFO, string); } -void audio_playback_callback(void* userdata, Uint8* stream, int len) +void audio_playback_callback(void* userdata, uint8_t* stream, int len) { char string[64]; audio_state* state = userdata; diff --git a/audio.h b/audio.h index 7575098..57f96f6 100644 --- a/audio.h +++ b/audio.h @@ -1,14 +1,13 @@ #ifndef AUDIO_H #define AUDIO_H - -#include +#include #define MAX_RECORDING_SECONDS 2 typedef struct audio_state { int recording_device_id; int playback_device_id; - Uint8* recording_buffer; + uint8_t* recording_buffer; unsigned int recording_buffer_size; unsigned int recording_buffer_position; } audio_state; @@ -17,7 +16,7 @@ int audio_init(audio_state* state); void audio_destroy(audio_state* state); int audio_recording_init(audio_state* state, int index); int audio_playback_init(audio_state* state); -void audio_recording_callback(void* userdata, Uint8* stream, int len); -void audio_playback_callback(void* userdata, Uint8* stream, int len); +void audio_recording_callback(void* userdata, uint8_t* stream, int len); +void audio_playback_callback(void* userdata, uint8_t* stream, int len); #endif diff --git a/log.c b/log.c index 6312a28..b2781c9 100644 --- a/log.c +++ b/log.c @@ -1,5 +1,8 @@ -// Chat GPT code #include "log.h" +#include +#include +#include +#include const char* get_current_time() { @@ -10,7 +13,7 @@ const char* get_current_time() return buffer; } -void log_message(LogLevel level, const char* message) +void log_message(LogLevel level, const char* format, ...) { const char* level_strings[] = { "INFO", "WARNING", "ERROR" }; @@ -21,8 +24,20 @@ void log_message(LogLevel level, const char* message) return; } + va_list args; + // Write the log message to the file - fprintf(log_file, "[%s] [%s] %s\n", get_current_time(), level_strings[level], message); + va_start(args, format); + fprintf(log_file, "[%s] [%s] ", get_current_time(), level_strings[level]); + vfprintf(log_file, format, args); + fprintf(log_file, "\n"); + va_end(args); + // Write to stdout + va_start(args, format); + printf("[%s] ", level_strings[level]); + vprintf(format, args); + printf("\n"); + va_end(args); // Close the log file fclose(log_file); diff --git a/log.h b/log.h index 8ed0363..1a312fa 100644 --- a/log.h +++ b/log.h @@ -2,10 +2,6 @@ #ifndef LOG_H #define LOG_H -#include -#include -#include - typedef enum { LOG_INFO, LOG_WARNING, @@ -14,6 +10,6 @@ typedef enum { const char* get_current_time(); -void log_message(LogLevel level, const char* message); +void log_message(LogLevel level, const char* format, ...); #endif diff --git a/main.c b/main.c index 48d588a..0deca74 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include "texture.h" #include "audio.h" +#include "log.h" #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 @@ -39,12 +40,13 @@ int main(int argc, char* argv[]) num_devices = SDL_GetNumAudioDevices(1); printf("Num audio devices: %i\n", num_devices); + log_message(LOG_INFO, "Num audio devices: %i", num_devices); char char_buffer[64]; char default_fmt[] = "Device selected: %i"; state state; state.application_state = SELECTING_DEVICE; - audio_init(&state.audio); + if (!audio_init(&state.audio)) return 1; state.device_index = -1; text_texture display_text_texture; @@ -111,16 +113,16 @@ int main(int argc, char* argv[]) } cleanup: - printf("Cleanup start\n"); + log_message(LOG_INFO, "Cleanup start"); audio_destroy(&state.audio); - printf("Audio done\n"); + log_message(LOG_INFO, "Audio done"); text_texture_free(&display_text_texture); for (int i = 0; i < num_devices; i++) { text_texture_free(&device_textures[i]); } - printf("Texture done\n"); + log_message(LOG_INFO, "Texture done"); destroy(); - printf("Done\n"); + log_message(LOG_INFO, "Done"); return 0; } @@ -224,12 +226,12 @@ void handle_task(state* state) int init() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { - printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); + log_message(LOG_ERROR, "SDL could not initialize! SDL Error: %s", SDL_GetError()); return 0; } if (TTF_Init() == -1) { - printf("TTF could not initialize! TTF Error: %s\n", TTF_GetError()); + log_message(LOG_ERROR, "TTF could not initialize! TTF Error: %s", TTF_GetError()); return 0; } @@ -238,13 +240,13 @@ int init() SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (!window) { - printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); + log_message(LOG_ERROR, "Window could not be created! SDL Error: %s", SDL_GetError()); return 0; } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (!renderer) { - printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError()); + log_message(LOG_ERROR, "Renderer could not be created! SDL_Error: %s", SDL_GetError()); return 0; } @@ -254,11 +256,11 @@ int init() void destroy() { SDL_DestroyRenderer(renderer); - printf("Renderer done\n"); + log_message(LOG_INFO, "Renderer done"); SDL_DestroyWindow(window); - printf("Window done\n"); + log_message(LOG_INFO, "Window done"); TTF_Quit(); - printf("TTF done\n"); + log_message(LOG_INFO, "TTF done"); SDL_Quit(); } @@ -271,10 +273,10 @@ int select_device(SDL_KeyboardEvent* key_event, int max_index) /*int no_mod = (mod == 0);*/ /*if (!keycode_is_num || !no_mod) {*/ if (!keycode_is_num) { - printf("Not number: %c <%u>\n", keycode, keycode); + log_message(LOG_INFO, "Not number: %c <%u>", keycode, keycode); return -1; } else { - printf("Is number: %c <%u>\n", keycode, keycode); + log_message(LOG_INFO, "Is number: %c <%u>", keycode, keycode); } int index = keycode - SDLK_0; diff --git a/texture.c b/texture.c index ba1bb13..b6de90e 100644 --- a/texture.c +++ b/texture.c @@ -1,4 +1,5 @@ #include "texture.h" +#include "log.h" int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer) { @@ -6,7 +7,7 @@ int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer) text_texture->renderer = renderer; text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", 24); if (!text_texture->font) { - printf("Failed to load font! TTF_Error: %s\n", TTF_GetError()); + log_message(LOG_ERROR, "Failed to load font! TTF_Error: %s", TTF_GetError()); return 0; } text_texture->width = 0; @@ -24,13 +25,13 @@ int text_texture_load(text_texture* text_texture, char* string) SDL_Color text_color = { 255, 255, 255 }; SDL_Surface* text_surface = TTF_RenderText_Solid(text_texture->font, string, text_color); if (!text_surface) { - printf("Unable to render text surface! TTF_Error: %s\n", TTF_GetError()); + log_message(LOG_ERROR, "Unable to render text surface! TTF_Error: %s", TTF_GetError()); return 0; } SDL_Texture* texture = SDL_CreateTextureFromSurface(text_texture->renderer, text_surface); if (!texture) { - printf("Unable to create texture from rendered text! SDL_Error: %s\n", SDL_GetError()); + log_message(LOG_ERROR, "Unable to create texture from rendered text! SDL_Error: %s", SDL_GetError()); return 0; } @@ -45,11 +46,11 @@ int text_texture_load(text_texture* text_texture, char* string) int text_texture_render(text_texture* text_texture, int x, int y) { if (!text_texture) { - printf("text_texture uninitialized"); + log_message(LOG_ERROR, "text_texture uninitialized"); return 0; } if (!text_texture->texture) { - printf("text_texture->texture uninitialized"); + log_message(LOG_ERROR, "text_texture->texture uninitialized"); return 0; }