medfall

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

commit 3ab6d178dff9abbfc204eddc0336a4abd7010154
parent 0f854b14c7631b024a094e32fb2bb7384395c4b9
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Dec 28 22:25:58 +0200

Create a log directory per run and store profiler output in profiler.log

Diffstat:
log.cc | 24+++++++++++++++++-------
log.h | 11+++++++----
profiler.cc | 30+++++++++++++++++++++---------
3 files changed, 45 insertions(+), 20 deletions(-)
diff --git a/log.cc b/log.cc @@ -4,10 +4,12 @@ #include "intrinsics.h" #include "log.h" +#include "str.h" #include "platform_io.h" static bool initialised = false; static FILE * streams[ LOGLEVEL_COUNT + 1 ]; +static str< 256 > logs_dir; static const char * names[ LOGLEVEL_COUNT + 1 ] = { "fatals", @@ -17,24 +19,27 @@ static const char * names[ LOGLEVEL_COUNT + 1 ] = { }; // this might lead to trouble with threads -static void log_init() { +void logger_init() { if( initialised ) return; mkdir( "logs", 0755 ); + u64 now = checked_cast< u64 >( time( NULL ) ); + logs_dir.sprintf( "logs/%llu", now ); + mkdir( logs_dir.c_str(), 0755 ); + for( u32 i = 0; i <= LOGLEVEL_COUNT; i++ ) { - char path[ 128 ]; - snprintf( path, sizeof( path ), "logs/%llu-%s.log", ( long long unsigned int ) time( NULL ), names[ i ] ); - streams[ i ] = fopen( path, "w" ); - assert( streams[ i ] != NULL ); + const str< 256 > path( "%s/%s.log", logs_dir.c_str(), names[ i ] ); + streams[ i ] = fopen( path.c_str(), "w" ); + ASSERT( streams[ i ] != NULL ); } initialised = true; } // TODO: print time? -void log_generic( LogLevel level, const char * fmt, ... ) { - log_init(); +void logger_log( LogLevel level, const char * fmt, ... ) { + logger_init(); va_list argp; va_start( argp, fmt ); @@ -51,3 +56,8 @@ void log_generic( LogLevel level, const char * fmt, ... ) { fflush( streams[ level ] ); fflush( streams[ LOGLEVEL_COUNT ] ); } + +const char * logger_get_logs_dir() { + logger_init(); + return logs_dir.c_str(); +} diff --git a/log.h b/log.h @@ -13,10 +13,13 @@ enum LogLevel { LOGLEVEL_COUNT, }; -void log_generic( LogLevel level, const char * fmt, ... ); +void logger_init(); +void logger_log( LogLevel level, const char * fmt, ... ); -#define INFO( form, ... ) log_generic( LOGLEVEL_INFO, "[INFO] [thread %u] " form "\n", thread_getid(), ##__VA_ARGS__ ) -#define WARN( form, ... ) log_generic( LOGLEVEL_WARNING, "[WARN] [thread %u] " form "\n", thread_getid(), ##__VA_ARGS__ ) -#define FATAL( form, ... ) do { log_generic( LOGLEVEL_FATAL, "[FATAL] [thread %u] " form "\n", thread_getid(), ##__VA_ARGS__ ); abort(); } while( 0 ) +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 ) #endif // _LOG_H_ diff --git a/profiler.cc b/profiler.cc @@ -7,6 +7,7 @@ #include "stats.h" #include "str.h" #include "strlcpy.h" +#include "log.h" struct Timer { const char * name; @@ -61,22 +62,32 @@ void profiler_push( u32 timer_idx ) { timer_stack_top++; } -static void print_node( u32 idx, double frame_dt ) { +static FILE * log_file = NULL; +static void profiler_logger_init() { + if( log_file != NULL ) return; + logger_init(); + + const str< 256 > log_path( "%s/profiler.log", logger_get_logs_dir() ); + log_file = fopen( log_path.c_str(), "w" ); +} + +static void write_node( u32 idx, double frame_dt ) { const LogEntry & entry = logs[ idx ]; if( entry.prev_sibling != 0 ) { - print_node( entry.prev_sibling, frame_dt ); + write_node( entry.prev_sibling, frame_dt ); } - printf( "%s (%.2f%%)\n", entry.message.c_str(), entry.dt * 100.0 / frame_dt ); + fprintf( log_file, "%s (%.2f%%)\n", entry.message.c_str(), entry.dt * 100.0 / frame_dt ); if( entry.first_child != 0 ) { - print_node( entry.first_child, frame_dt ); + write_node( entry.first_child, frame_dt ); } } -static void print_frame( double frame_dt ) { - print_node( num_log_entries - 1, frame_dt ); - printf( "\n" ); +static void write_frame( double frame_dt ) { + profiler_logger_init(); + write_node( num_log_entries - 1, frame_dt ); + fprintf( log_file, "\n" ); } void profiler_pop( const char * name ) { @@ -108,13 +119,14 @@ void profiler_pop( const char * name ) { num_log_entries++; if( timer_stack_top == 0 ) { - print_frame( dt ); + write_frame( dt ); num_log_entries = 1; memset( last_at_depth, 0, sizeof( last_at_depth ) ); } } -void profiler_print_all() { +void profiler_write_summary() { + profiler_logger_init(); for( u32 i = 0; i < num_timers; i++ ) { Timer * timer = &all_timers[ i ];