medfall

A super great game engine
Log | Files | Refs

commit a21906b19cbe0bd0ff289a4357c51da28fe91ba7
parent c7ede2c7989221dc79a221452bdb13274045b9a4
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Jun  3 02:04:17 +0300

Explicit logger/profiler initialisation and some minor cleanup

Diffstat:
launcher/main.cc | 4++++
log.cc | 37++++++++-----------------------------
log.h | 12++++++++++--
main.cc | 9+++++++--
profiler.cc | 11+++++------
profiler.h | 3+++
work_queue.cc | 2+-
7 files changed, 38 insertions(+), 40 deletions(-)
diff --git a/launcher/main.cc b/launcher/main.cc @@ -536,6 +536,8 @@ static void asdf() { } int main() { + logger_init(); + logger_thread_name( "main" ); net_init(); static u8 memory[ megabytes( 512 ) ]; @@ -749,5 +751,7 @@ int main() { ImGui_ImplGlfwGL3_Shutdown(); glfwTerminate(); + logger_term(); + return 0; } diff --git a/log.cc b/log.cc @@ -1,18 +1,12 @@ #include <stdio.h> -#include <stdarg.h> -#include <time.h> #include "intrinsics.h" #include "log.h" #include "str.h" -#include "strlcpy.h" -#include "strlcat.h" -#include "platform_io.h" #include "platform_atomic.h" #include "platform_thread.h" #include "platform_mutex.h" -static bool initialised = false; static FILE * streams[ LOGLEVEL_COUNT + 1 ]; static str< 256 > logs_dir; @@ -24,11 +18,10 @@ static const char * file_names[ LOGLEVEL_COUNT + 1 ] = { }; #define MAX_THREADS 32 -#define THREAD_NAME_LENGTH 32 struct LoggerThread { u64 id; - char name[ THREAD_NAME_LENGTH ]; + str< 32 > name; }; static LoggerThread threads[ MAX_THREADS ]; @@ -36,12 +29,7 @@ static atomic_u32 num_threads; static Mutex mutex; -// this might lead to trouble with threads void logger_init() { - if( initialised ) return; - - mutex_init( &mutex ); - mkdir( "logs", 0755 ); u64 now = checked_cast< u64 >( time( NULL ) ); @@ -53,33 +41,26 @@ void logger_init() { streams[ i ] = fopen( path.c_str(), "w" ); ASSERT( streams[ i ] != NULL ); } +} - initialised = true; +void logger_term() { + for( u32 i = 0; i <= LOGLEVEL_COUNT; i++ ) { + fclose( streams[ i ] ); + } } // TODO: this should really use ggformat too -void logger_thread_name( const char * fmt, ... ) { +void logger_thread_name( const char * name ) { u32 i = fetch_add_acqrel( &num_threads, 1 ); ASSERT( i < MAX_THREADS ); LoggerThread & thread = threads[ i ]; thread.id = thread_getid(); - - thread.name[ 0 ] = '\0'; - strlcat( thread.name, "[", sizeof( thread.name ) ); - - va_list argp; - va_start( argp, fmt ); - vsnprintf( thread.name + 1, sizeof( thread.name ) - 1, fmt, argp ); - va_end( argp ); - - strlcat( thread.name, "]", sizeof( thread.name ) ); + thread.name.sprintf( "[{}] ", name ); } // TODO: print time? void logger_log( LogLevel level, const char * message ) { - logger_init(); - str< 1024 > buf; u64 thread_id = thread_getid(); @@ -94,7 +75,6 @@ void logger_log( LogLevel level, const char * message ) { buf.appendf( "[thread {}] ", thread_id ); } - buf += " "; buf += message; if( buf[ buf.len() - 1 ] != '\n' ) { buf += "\n"; @@ -113,6 +93,5 @@ void logger_log( LogLevel level, const char * message ) { } const char * logger_get_logs_dir() { - logger_init(); return logs_dir.c_str(); } diff --git a/log.h b/log.h @@ -2,8 +2,8 @@ #include <stdlib.h> -#include "platform_backtrace.h" #include "str.h" +#include "platform_backtrace.h" enum LogLevel { LOGLEVEL_FATAL, @@ -14,10 +14,18 @@ enum LogLevel { }; void logger_init(); -void logger_thread_name( const char * fmt, ... ); +void logger_term(); + +void logger_thread_name( const char * name ); void logger_log( LogLevel level, const char * message ); template< typename... Rest > +void logger_thread_name( const char * fmt, Rest... rest ) { + str< 32 > buf( fmt, rest... ); + logger_thread_name( buf.c_str() ); +} + +template< typename... Rest > void logger_log( LogLevel level, const char * fmt, Rest... rest ) { str< 1024 > buf( fmt, rest... ); logger_log( level, buf.c_str() ); diff --git a/main.cc b/main.cc @@ -23,8 +23,6 @@ extern "C" GameFrame game_frame; #endif int main( int argc, char ** argv ) { - logger_thread_name( "main" ); - #if PLATFORM_LINUX install_debug_signal_handlers( true ); #endif @@ -41,6 +39,10 @@ int main( int argc, char ** argv ) { static GameState state; mem.state = &state; + logger_init(); + logger_thread_name( "main" ); + profiler_init(); + GLFWwindow * window = gl_init(); shaders_init(); text_renderer_init(); @@ -124,6 +126,9 @@ int main( int argc, char ** argv ) { profiler_write_summary(); + profiler_term(); + logger_term(); + printf( "FPS: %.1f\n", total_frames / program_run_time ); return 0; diff --git a/profiler.cc b/profiler.cc @@ -64,14 +64,15 @@ void profiler_push( u32 timer_idx ) { } static FILE * log_file = NULL; -static void profiler_logger_init() { - if( log_file != NULL ) return; - logger_init(); - +void profiler_init() { const str< 256 > log_path( "{}/profiler.log", logger_get_logs_dir() ); log_file = fopen( log_path.c_str(), "w" ); } +void profiler_term() { + fclose( log_file ); +} + static void write_node( u32 idx, u64 frame_clocks ) { const LogEntry & entry = logs[ idx ]; if( entry.prev_sibling != 0 ) { @@ -86,7 +87,6 @@ static void write_node( u32 idx, u64 frame_clocks ) { } static void write_frame( u64 frame_clocks ) { - profiler_logger_init(); write_node( num_log_entries - 1, frame_clocks ); ggprint_to_file( log_file, "\n" ); fflush( log_file ); @@ -133,7 +133,6 @@ void profiler_pop( const char * name ) { } void profiler_write_summary() { - profiler_logger_init(); for( u32 i = 0; i < num_timers; i++ ) { Timer * timer = &all_timers[ i ]; ggprint_to_file( log_file, "{} ({}:{}): {}\n", timer->name, timer->file, timer->line, timer->stats ); diff --git a/profiler.h b/profiler.h @@ -3,6 +3,9 @@ #include "intrinsics.h" #include "platform_io.h" +void profiler_init(); +void profiler_term(); + u32 profiler_new_timer( const char * name, const char * file, int line ); void profiler_push( u32 timer_idx ); void profiler_pop( const char * name = NULL ); diff --git a/work_queue.cc b/work_queue.cc @@ -39,7 +39,7 @@ static THREAD( workqueue_worker ) { WorkQueue * queue = info->queue; u32 thread_id = info->thread_id; - logger_thread_name( "worker %u", thread_id ); + logger_thread_name( "worker {}", thread_id ); while( load_acquire( &queue->shutting_down ) == 0 ) { if( !workqueue_step( thread_id, queue ) ) {