Update logging to also print to stdout + replace printf to logging.
This commit is contained in:
		
							parent
							
								
									988bc52e57
								
							
						
					
					
						commit
						f9cf93eaee
					
				
							
								
								
									
										14
									
								
								audio.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								audio.c
									
									
									
									
									
								
							| @ -1,6 +1,8 @@ | |||||||
| #include "audio.h" | #include "audio.h" | ||||||
| #include "log.h" | #include "log.h" | ||||||
| 
 | 
 | ||||||
|  | #include <SDL2/SDL.h> | ||||||
|  | 
 | ||||||
| static SDL_AudioSpec recording_spec; | static SDL_AudioSpec recording_spec; | ||||||
| static SDL_AudioSpec playback_spec; | static SDL_AudioSpec playback_spec; | ||||||
| static SDL_AudioSpec received_recording_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) | 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); | 	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) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	state->recording_device_id = device_id; | 	state->recording_device_id = device_id; | ||||||
| @ -65,13 +67,13 @@ void audio_destroy(audio_state* state) | |||||||
| int audio_playback_init(audio_state* state) | int audio_playback_init(audio_state* state) | ||||||
| { | { | ||||||
| 	if (!state->recording_buffer) { | 	if (!state->recording_buffer) { | ||||||
| 		printf("Audio buffer not initialized"); | 		log_message(LOG_INFO, "Audio buffer not initialized"); | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, &playback_spec, &received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); | 	SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, &playback_spec, &received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); | ||||||
| 	if (!device_id) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	state->playback_device_id = device_id; | 	state->playback_device_id = device_id; | ||||||
| @ -79,7 +81,7 @@ int audio_playback_init(audio_state* state) | |||||||
| 	return 1; | 	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]; | 	char string[64]; | ||||||
| 	audio_state* state = userdata; | 	audio_state* state = userdata; | ||||||
| @ -103,7 +105,7 @@ void audio_recording_callback(void* userdata, Uint8* stream, int len) | |||||||
| 	log_message(LOG_INFO, string); | 	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]; | 	char string[64]; | ||||||
| 	audio_state* state = userdata; | 	audio_state* state = userdata; | ||||||
|  | |||||||
							
								
								
									
										9
									
								
								audio.h
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								audio.h
									
									
									
									
									
								
							| @ -1,14 +1,13 @@ | |||||||
| #ifndef AUDIO_H | #ifndef AUDIO_H | ||||||
| #define AUDIO_H | #define AUDIO_H | ||||||
| 
 | #include <stdint.h> | ||||||
| #include <SDL2/SDL.h> |  | ||||||
| 
 | 
 | ||||||
| #define MAX_RECORDING_SECONDS 2 | #define MAX_RECORDING_SECONDS 2 | ||||||
| 
 | 
 | ||||||
| typedef struct audio_state { | typedef struct audio_state { | ||||||
| 	int recording_device_id; | 	int recording_device_id; | ||||||
| 	int playback_device_id; | 	int playback_device_id; | ||||||
| 	Uint8* recording_buffer; | 	uint8_t* recording_buffer; | ||||||
| 	unsigned int recording_buffer_size; | 	unsigned int recording_buffer_size; | ||||||
| 	unsigned int recording_buffer_position; | 	unsigned int recording_buffer_position; | ||||||
| } audio_state; | } audio_state; | ||||||
| @ -17,7 +16,7 @@ int audio_init(audio_state* state); | |||||||
| void audio_destroy(audio_state* state); | void audio_destroy(audio_state* state); | ||||||
| int audio_recording_init(audio_state* state, int index); | int audio_recording_init(audio_state* state, int index); | ||||||
| int audio_playback_init(audio_state* state); | int audio_playback_init(audio_state* state); | ||||||
| void audio_recording_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* stream, int len); | void audio_playback_callback(void* userdata, uint8_t* stream, int len); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										21
									
								
								log.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								log.c
									
									
									
									
									
								
							| @ -1,5 +1,8 @@ | |||||||
| // Chat GPT code
 |  | ||||||
| #include "log.h" | #include "log.h" | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <stdarg.h> | ||||||
|  | #include <time.h> | ||||||
| 
 | 
 | ||||||
| const char* get_current_time() | const char* get_current_time() | ||||||
| { | { | ||||||
| @ -10,7 +13,7 @@ const char* get_current_time() | |||||||
| 	return buffer; | 	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" }; | 	const char* level_strings[] = { "INFO", "WARNING", "ERROR" }; | ||||||
| 
 | 
 | ||||||
| @ -21,8 +24,20 @@ void log_message(LogLevel level, const char* message) | |||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	va_list args; | ||||||
|  | 
 | ||||||
| 	// Write the log message to the file
 | 	// 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
 | 	// Close the log file
 | ||||||
| 	fclose(log_file); | 	fclose(log_file); | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								log.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								log.h
									
									
									
									
									
								
							| @ -2,10 +2,6 @@ | |||||||
| #ifndef LOG_H | #ifndef LOG_H | ||||||
| #define LOG_H | #define LOG_H | ||||||
| 
 | 
 | ||||||
| #include <stdio.h> |  | ||||||
| #include <time.h> |  | ||||||
| #include <stdlib.h> |  | ||||||
| 
 |  | ||||||
| typedef enum { | typedef enum { | ||||||
| 	LOG_INFO, | 	LOG_INFO, | ||||||
| 	LOG_WARNING, | 	LOG_WARNING, | ||||||
| @ -14,6 +10,6 @@ typedef enum { | |||||||
| 
 | 
 | ||||||
| const char* get_current_time(); | const char* get_current_time(); | ||||||
| 
 | 
 | ||||||
| void log_message(LogLevel level, const char* message); | void log_message(LogLevel level, const char* format, ...); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										30
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								main.c
									
									
									
									
									
								
							| @ -4,6 +4,7 @@ | |||||||
| 
 | 
 | ||||||
| #include "texture.h" | #include "texture.h" | ||||||
| #include "audio.h" | #include "audio.h" | ||||||
|  | #include "log.h" | ||||||
| 
 | 
 | ||||||
| #define SCREEN_WIDTH 640 | #define SCREEN_WIDTH 640 | ||||||
| #define SCREEN_HEIGHT 480 | #define SCREEN_HEIGHT 480 | ||||||
| @ -39,12 +40,13 @@ int main(int argc, char* argv[]) | |||||||
| 
 | 
 | ||||||
| 	num_devices = SDL_GetNumAudioDevices(1); | 	num_devices = SDL_GetNumAudioDevices(1); | ||||||
| 	printf("Num audio devices: %i\n", num_devices); | 	printf("Num audio devices: %i\n", num_devices); | ||||||
|  | 	log_message(LOG_INFO, "Num audio devices: %i", num_devices); | ||||||
| 	char char_buffer[64]; | 	char char_buffer[64]; | ||||||
| 	char default_fmt[] = "Device selected: %i"; | 	char default_fmt[] = "Device selected: %i"; | ||||||
| 
 | 
 | ||||||
| 	state state; | 	state state; | ||||||
| 	state.application_state = SELECTING_DEVICE; | 	state.application_state = SELECTING_DEVICE; | ||||||
| 	audio_init(&state.audio); | 	if (!audio_init(&state.audio)) return 1; | ||||||
| 	state.device_index = -1; | 	state.device_index = -1; | ||||||
| 
 | 
 | ||||||
| 	text_texture display_text_texture; | 	text_texture display_text_texture; | ||||||
| @ -111,16 +113,16 @@ int main(int argc, char* argv[]) | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| cleanup: | cleanup: | ||||||
| 	printf("Cleanup start\n"); | 	log_message(LOG_INFO, "Cleanup start"); | ||||||
| 	audio_destroy(&state.audio); | 	audio_destroy(&state.audio); | ||||||
| 	printf("Audio done\n"); | 	log_message(LOG_INFO, "Audio done"); | ||||||
| 	text_texture_free(&display_text_texture); | 	text_texture_free(&display_text_texture); | ||||||
| 	for (int i = 0; i < num_devices; i++) { | 	for (int i = 0; i < num_devices; i++) { | ||||||
| 		text_texture_free(&device_textures[i]); | 		text_texture_free(&device_textures[i]); | ||||||
| 	} | 	} | ||||||
| 	printf("Texture done\n"); | 	log_message(LOG_INFO, "Texture done"); | ||||||
| 	destroy(); | 	destroy(); | ||||||
| 	printf("Done\n"); | 	log_message(LOG_INFO, "Done"); | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -224,12 +226,12 @@ void handle_task(state* state) | |||||||
| int init() | int init() | ||||||
| { | { | ||||||
| 	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (TTF_Init() == -1) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -238,13 +240,13 @@ int init() | |||||||
| 	                          SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | 	                          SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | ||||||
| 
 | 
 | ||||||
| 	if (!window) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | 	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | ||||||
| 	if (!renderer) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -254,11 +256,11 @@ int init() | |||||||
| void destroy() | void destroy() | ||||||
| { | { | ||||||
| 	SDL_DestroyRenderer(renderer); | 	SDL_DestroyRenderer(renderer); | ||||||
| 	printf("Renderer done\n"); | 	log_message(LOG_INFO, "Renderer done"); | ||||||
| 	SDL_DestroyWindow(window); | 	SDL_DestroyWindow(window); | ||||||
| 	printf("Window done\n"); | 	log_message(LOG_INFO, "Window done"); | ||||||
| 	TTF_Quit(); | 	TTF_Quit(); | ||||||
| 	printf("TTF done\n"); | 	log_message(LOG_INFO, "TTF done"); | ||||||
| 	SDL_Quit(); | 	SDL_Quit(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -271,10 +273,10 @@ int select_device(SDL_KeyboardEvent* key_event, int max_index) | |||||||
| 	/*int no_mod = (mod == 0);*/ | 	/*int no_mod = (mod == 0);*/ | ||||||
| 	/*if (!keycode_is_num || !no_mod) {*/ | 	/*if (!keycode_is_num || !no_mod) {*/ | ||||||
| 	if (!keycode_is_num) { | 	if (!keycode_is_num) { | ||||||
| 		printf("Not number: %c <%u>\n", keycode, keycode); | 		log_message(LOG_INFO, "Not number: %c <%u>", keycode, keycode); | ||||||
| 		return -1; | 		return -1; | ||||||
| 	} else { | 	} else { | ||||||
| 		printf("Is number: %c <%u>\n", keycode, keycode); | 		log_message(LOG_INFO, "Is number: %c <%u>", keycode, keycode); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	int index = keycode - SDLK_0; | 	int index = keycode - SDLK_0; | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								texture.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								texture.c
									
									
									
									
									
								
							| @ -1,4 +1,5 @@ | |||||||
| #include "texture.h" | #include "texture.h" | ||||||
|  | #include "log.h" | ||||||
| 
 | 
 | ||||||
| int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer) | 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->renderer = renderer; | ||||||
| 	text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", 24); | 	text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", 24); | ||||||
| 	if (!text_texture->font) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	text_texture->width = 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_Color text_color = { 255, 255, 255 }; | ||||||
| 	SDL_Surface* text_surface = TTF_RenderText_Solid(text_texture->font, string, text_color); | 	SDL_Surface* text_surface = TTF_RenderText_Solid(text_texture->font, string, text_color); | ||||||
| 	if (!text_surface) { | 	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; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	SDL_Texture* texture = SDL_CreateTextureFromSurface(text_texture->renderer, text_surface); | 	SDL_Texture* texture = SDL_CreateTextureFromSurface(text_texture->renderer, text_surface); | ||||||
| 	if (!texture) { | 	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; | 		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) | int text_texture_render(text_texture* text_texture, int x, int y) | ||||||
| { | { | ||||||
| 	if (!text_texture) { | 	if (!text_texture) { | ||||||
| 		printf("text_texture uninitialized"); | 		log_message(LOG_ERROR, "text_texture uninitialized"); | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	if (!text_texture->texture) { | 	if (!text_texture->texture) { | ||||||
| 		printf("text_texture->texture uninitialized"); | 		log_message(LOG_ERROR, "text_texture->texture uninitialized"); | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user