medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 78d71e6ddd2db55c6aa096351a3085b801b8c01d
parent ad86536ce9a9687ad77b7dd65c3ed8b7ca7975de
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Jan  8 22:06:41 +0200

Give threads pretty names in logs

Diffstat:
Makefile | 6+++---
log.cc | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
log.h | 9++++-----
main.cc | 2++
work_queue.cc | 3+++
5 files changed, 73 insertions(+), 18 deletions(-)
diff --git a/Makefile b/Makefile @@ -6,10 +6,10 @@ VISITORS := visitors/linear_algebra.h all: $(VISITORS) $(BINS) $(MODULES) test_lockfree # Binary dependencies -medfall: main.o gl.o log.o memory_arena.o renderer.o immediate.o work_queue.o text_renderer.o stb_truetype.o glad.o +medfall: main.o gl.o log.o strlcpy.o memory_arena.o renderer.o immediate.o work_queue.o text_renderer.o stb_truetype.o glad.o libglfw.a pp: pp.o log.o memory_arena.o stb_image.o lz4.o lz4hc.o strlcpy.o heightmap.o -sound: audio.o mixer.o log.o memory_arena.o wave.o platform_audio_output.o -srv: server/main.o log.o profiler.o stats.o strlcpy.o rng/well512.o +sound: audio.o mixer.o log.o strlcpy.o memory_arena.o wave.o platform_audio_output.o +srv: server/main.o log.o profiler.o stats.o strlcpy.o rng/well512.o libglfw.a launcher/launcher: launcher/main.o log.o http.o sha2.o strlcpy.o test_lockfree: relacy.cc diff --git a/log.cc b/log.cc @@ -5,19 +5,36 @@ #include "intrinsics.h" #include "log.h" #include "str.h" +#include "strlcpy.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; -static const char * names[ LOGLEVEL_COUNT + 1 ] = { +static const char * file_names[ LOGLEVEL_COUNT + 1 ] = { "fatals", "warnings", "info", "all", }; +#define MAX_THREADS 32 +#define THREAD_NAME_LENGTH 32 + +struct LoggerThread { + u64 id; + char name[ THREAD_NAME_LENGTH ]; +}; + +static LoggerThread threads[ MAX_THREADS ]; +static atomic_u32 num_threads; + +static Mutex mutex; + // this might lead to trouble with threads void logger_init() { if( initialised ) return; @@ -29,7 +46,7 @@ void logger_init() { mkdir( logs_dir.c_str(), 0755 ); for( u32 i = 0; i <= LOGLEVEL_COUNT; i++ ) { - const str< 256 > path( "%s/%s.log", logs_dir.c_str(), names[ i ] ); + const str< 256 > path( "%s/%s.log", logs_dir.c_str(), file_names[ i ] ); streams[ i ] = fopen( path.c_str(), "w" ); ASSERT( streams[ i ] != NULL ); } @@ -37,22 +54,56 @@ void logger_init() { initialised = true; } +void logger_thread_name( const char * fmt, ... ) { + u32 i = fetch_add_acqrel( &num_threads, 1 ); + ASSERT( i < MAX_THREADS ); + + LoggerThread & thread = threads[ i ]; + thread.id = thread_getid(); + + va_list argp; + va_start( argp, fmt ); + str< sizeof( thread.name ) > name( "[" ); + name.appendf( fmt, argp ); + name.appendf( "] " ); + va_end( argp ); + + strlcpy( thread.name, name.c_str(), sizeof( thread.name ) ); +} + // TODO: print time? void logger_log( LogLevel level, const char * fmt, ... ) { logger_init(); + str< 1024 > buf; + + u64 thread_id = thread_getid(); + u32 n = load_acquire( &num_threads ); + for( u32 i = 0; i < n; i++ ) { + if( threads[ i ].id == thread_id ) { + buf.appendf( threads[ i ].name ); + } + } + + if( buf.len() == 0 ) { + buf.appendf( "[thread %llu] ", thread_id ); + } + va_list argp; va_start( argp, fmt ); - FILE * stream = level == LOGLEVEL_INFO ? stdout : stderr; - vfprintf( stream, fmt, argp ); - va_end( argp ); - va_start( argp, fmt ); - vfprintf( streams[ level ], fmt, argp ); - va_end( argp ); - va_start( argp, fmt ); - vfprintf( streams[ LOGLEVEL_COUNT ], fmt, argp ); + buf.appendf( fmt, argp ); va_end( argp ); + buf.appendf( "\n" ); + + FILE * console_stream = level == LOGLEVEL_INFO ? stdout : stderr; + { + SCOPED_MUTEX_LOCK( &mutex ); + fputs( buf.c_str(), console_stream ); + fputs( buf.c_str(), streams[ level ] ); + fputs( buf.c_str(), streams[ LOGLEVEL_COUNT ] ); + } + fflush( streams[ level ] ); fflush( streams[ LOGLEVEL_COUNT ] ); } diff --git a/log.h b/log.h @@ -2,8 +2,6 @@ #include <stdlib.h> -#include "platform_thread.h" - enum LogLevel { LOGLEVEL_FATAL, LOGLEVEL_WARNING, @@ -13,10 +11,11 @@ enum LogLevel { }; void logger_init(); +void logger_thread_name( const char * fmt, ... ); void logger_log( LogLevel level, const char * fmt, ... ); const char * logger_get_logs_dir(); -#define INFO( form, ... ) logger_log( LOGLEVEL_INFO, "[INFO] [thread %u] " form "\n", thread_getid(), ##__VA_ARGS__ ) -#define WARN( form, ... ) logger_log( LOGLEVEL_WARNING, "[WARN] [thread %u] " form "\n", thread_getid(), ##__VA_ARGS__ ) -#define FATAL( form, ... ) do { logger_log( LOGLEVEL_FATAL, "[FATAL] [thread %u] " form "\n", thread_getid(), ##__VA_ARGS__ ); abort(); } while( 0 ) +#define INFO( form, ... ) logger_log( LOGLEVEL_INFO, "[INFO] " form, ##__VA_ARGS__ ) +#define WARN( form, ... ) logger_log( LOGLEVEL_WARNING, "[WARN] " form, ##__VA_ARGS__ ) +#define FATAL( form, ... ) do { logger_log( LOGLEVEL_FATAL, "[FATAL] " form, ##__VA_ARGS__ ); abort(); } while( 0 ) diff --git a/main.cc b/main.cc @@ -84,6 +84,8 @@ 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 diff --git a/work_queue.cc b/work_queue.cc @@ -3,6 +3,7 @@ */ #include "intrinsics.h" +#include "log.h" #include "work_queue.h" #include "platform_atomic.h" #include "platform_semaphore.h" @@ -41,6 +42,8 @@ static THREAD( workqueue_worker ) { fetch_add_acqrel( info->started_threads, 1 ); + logger_thread_name( "worker %u", thread_id ); + for( ;; ) { if( !workqueue_step( thread_id, queue ) ) { semaphore_wait( &queue->sem );