fixed up debug logs and fatal asserts.

This commit is contained in:
Edward R. Gonzalez 2023-03-12 22:38:21 -04:00
parent 71d9aa79eb
commit d01625d464
5 changed files with 109 additions and 66 deletions

11
.editorconfig Normal file
View File

@ -0,0 +1,11 @@
[*.md]
indent_style = tab
indent_size = 4
[*.c]
indent_style = tab
indent_size = 4
[*.cpp]
indent_style = tab
indent_size = 4

View File

@ -97,8 +97,39 @@ namespace Memory
} }
} }
void fatal() sw log_fmt(char const *fmt, ...)
{ {
Memory::cleanup(); #if Build_Debug
assert_crash("FATAL"); sw res;
va_list va;
va_start(va, fmt);
res = zpl_printf_va(fmt, va);
va_end(va);
return res;
#else
return 0;
#endif
}
void fatal(char const *fmt, ...)
{
local_persist thread_local
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
va_list va;
#if Build_Debug
va_start(va, fmt);
zpl_snprintf_va(buf, ZPL_PRINTF_MAXLEN, fmt, va);
va_end(va);
assert_crash(buf);
#else
va_start(va, fmt);
zpl_printf_err_va( fmt, va);
va_end(va);
exit(1);
#endif
} }

View File

@ -6,9 +6,7 @@ namespace File
{ {
string Source = nullptr; string Source = nullptr;
string Destination = nullptr; string Destination = nullptr;
file_contents Content {}; file_contents Content {};
zpl_arena Buffer; zpl_arena Buffer;
void cleanup() void cleanup()
@ -43,8 +41,7 @@ namespace File
if ( Content.data == nullptr ) if ( Content.data == nullptr )
{ {
zpl_printf( "Unable to open source file: %s\n", Source ); fatal( "Unable to open source file: %s\n", Source );
fatal();
} }
} }
@ -58,8 +55,7 @@ namespace File
if ( error != ZPL_FILE_ERROR_NONE ) if ( error != ZPL_FILE_ERROR_NONE )
{ {
zpl_printf( "Unable to open destination file: %s\n", Destination ); fatal( "Unable to open destination file: %s\n", Destination );
fatal();
} }
file_write( & file_dest, refactored, string_length(refactored) ); file_write( & file_dest, refactored, string_length(refactored) );
@ -147,8 +143,7 @@ namespace Spec
if ( length == 0 ) if ( length == 0 )
{ {
zpl_printf("Failed to find valid initial token"); fatal("Failed to find valid initial token");
fatal();
} }
token = string_append_length( token, line, length ); token = string_append_length( token, line, length );
@ -168,16 +163,14 @@ namespace Spec
if ( error != ZPL_FILE_ERROR_NONE ) if ( error != ZPL_FILE_ERROR_NONE )
{ {
zpl_printf("Could not open the specification file: %s", File); fatal("Could not open the specification file: %s", File);
fatal();
} }
sw fsize = scast( sw, file_size( & file ) ); sw fsize = scast( sw, file_size( & file ) );
if ( fsize <= 0 ) if ( fsize <= 0 )
{ {
zpl_printf("No content in specificaiton to process"); fatal("No content in specificaiton to process");
fatal();
} }
arena_init_from_allocator( & Buffer, heap(), (fsize + fsize % 64) * 10 + kilobytes(1) ); arena_init_from_allocator( & Buffer, heap(), (fsize + fsize % 64) * 10 + kilobytes(1) );
@ -197,8 +190,7 @@ namespace Spec
if ( left == 0 ) if ( left == 0 )
{ {
zpl_printf("Spec::process: lines array imporoperly setup"); fatal("Spec::process: lines array imporoperly setup");
fatal();
} }
// Skip the first line as its the version number and we only support __VERSION 1. // Skip the first line as its the version number and we only support __VERSION 1.
@ -404,7 +396,7 @@ namespace Spec
} }
} }
zpl_printf("Specification Line: %d is missing valid keyword", array_count(lines) - left); log_fmt("Specification Line: %d is missing valid keyword", array_count(lines) - left);
lines++; lines++;
} }
} }
@ -465,12 +457,6 @@ void refactor()
u32 sig_length = string_length( ignore->Sig ); u32 sig_length = string_length( ignore->Sig );
current = string_append_length( current, content, sig_length ); current = string_append_length( current, content, sig_length );
// bool match = false;
// if ( str_compare( "zpl_printf", current, sig_length ) == 0 )
// {
// match = true;
// }
if ( string_are_equal( ignore->Sig, current ) ) if ( string_are_equal( ignore->Sig, current ) )
{ {
char after = content[sig_length]; char after = content[sig_length];
@ -480,7 +466,7 @@ void refactor()
continue; continue;
} }
zpl_printf("\nIgnored %-81s line %d", current, line ); log_fmt("\nIgnored %-81s line %d", current, line );
content += sig_length; content += sig_length;
left -= sig_length; left -= sig_length;
@ -521,7 +507,7 @@ void refactor()
string_clear( preview ); string_clear( preview );
preview = string_append_length( preview, content, length ); preview = string_append_length( preview, content, length );
zpl_printf("\nIgnored %-40s %-40s line %d", preview, ignore->Sig, line); log_fmt("\nIgnored %-40s %-40s line %d", preview, ignore->Sig, line);
content += length; content += length;
left -= length; left -= length;
@ -572,7 +558,7 @@ void refactor()
array_append( tokens, entry ); array_append( tokens, entry );
zpl_printf("\nFound %-81s line %d", current, line); log_fmt("\nFound %-81s line %d", current, line);
content += sig_length; content += sig_length;
left -= sig_length; left -= sig_length;
@ -629,7 +615,7 @@ void refactor()
string_clear( preview ); string_clear( preview );
preview = string_append_length( preview, content, length); preview = string_append_length( preview, content, length);
zpl_printf("\nFound %-40s %-40s line %d", preview, nspace->Sig, line); log_fmt("\nFound %-40s %-40s line %d", preview, nspace->Sig, line);
content += length; content += length;
left -= length; left -= length;
@ -722,8 +708,7 @@ void parse_options( int num, char** arguments )
} }
else else
{ {
zpl_printf( "-source not provided\n" ); fatal( "-source not provided\n" );
fatal();
} }
if ( opts_has_arg( & opts, "dst" ) ) if ( opts_has_arg( & opts, "dst" ) )
@ -749,8 +734,7 @@ void parse_options( int num, char** arguments )
} }
else else
{ {
zpl_printf( "Failed to parse arguments\n" ); fatal( "Failed to parse arguments\n" );
fatal();
} }
opts_free( & opts); opts_free( & opts);

View File

@ -108,8 +108,39 @@ namespace Memory
} }
} }
void fatal() sw log_fmt(char const *fmt, ...)
{ {
Memory::cleanup(); #if Build_Debug
zpl_assert_crash("FATAL"); sw res;
va_list va;
va_start(va, fmt);
res = zpl_printf_va(fmt, va);
va_end(va);
return res;
#else
return 0;
#endif
}
void fatal(char const *fmt, ...)
{
zpl_local_persist zpl_thread_local
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
va_list va;
#if Build_Debug
va_start(va, fmt);
zpl_snprintf_va(buf, ZPL_PRINTF_MAXLEN, fmt, va);
va_end(va);
zpl_assert_crash(buf);
#else
va_start(va, fmt);
zpl_printf_err_va( fmt, va);
va_end(va);
exit(1);
#endif
} }

View File

@ -43,8 +43,7 @@ namespace File
if ( Content.data == nullptr ) if ( Content.data == nullptr )
{ {
zpl_printf( "Unable to open source file: %s\n", Source ); fatal( "Unable to open source file: %s\n", Source );
fatal();
} }
} }
@ -58,8 +57,7 @@ namespace File
if ( error != ZPL_FILE_ERROR_NONE ) if ( error != ZPL_FILE_ERROR_NONE )
{ {
zpl_printf( "Unable to open destination file: %s\n", Destination ); fatal( "Unable to open destination file: %s\n", Destination );
fatal();
} }
zpl_file_write( & file_dest, refactored, zpl_string_length(refactored) ); zpl_file_write( & file_dest, refactored, zpl_string_length(refactored) );
@ -147,8 +145,7 @@ namespace Spec
if ( length == 0 ) if ( length == 0 )
{ {
zpl_printf("Failed to find valid initial token"); fatal("Failed to find valid initial token");
fatal();
} }
token = zpl_string_append_length( token, line, length ); token = zpl_string_append_length( token, line, length );
@ -168,16 +165,14 @@ namespace Spec
if ( error != ZPL_FILE_ERROR_NONE ) if ( error != ZPL_FILE_ERROR_NONE )
{ {
zpl_printf("Could not open the specification file: %s", File); fatal("Could not open the specification file: %s", File);
fatal();
} }
sw fsize = scast( sw, zpl_file_size( & file ) ); sw fsize = scast( sw, zpl_file_size( & file ) );
if ( fsize <= 0 ) if ( fsize <= 0 )
{ {
zpl_printf("No content in specificaiton to process"); fatal("No content in specificaiton to process");
fatal();
} }
zpl_arena_init_from_allocator( & Buffer, zpl_heap(), (fsize + fsize % 64) * 10 + zpl_kilobytes(1) ); zpl_arena_init_from_allocator( & Buffer, zpl_heap(), (fsize + fsize % 64) * 10 + zpl_kilobytes(1) );
@ -197,8 +192,7 @@ namespace Spec
if ( left == 0 ) if ( left == 0 )
{ {
zpl_printf("Spec::process: lines array imporoperly setup"); fatal("Spec::process: lines array imporoperly setup");
fatal();
} }
// Skip the first line as its the version number and we only support __VERSION 1. // Skip the first line as its the version number and we only support __VERSION 1.
@ -404,7 +398,7 @@ namespace Spec
} }
} }
zpl_printf("Specification Line: %d is missing valid keyword", zpl_array_count(lines) - left); log_fmt("Specification Line: %d is missing valid keyword", zpl_array_count(lines) - left);
lines++; lines++;
} }
} }
@ -465,12 +459,6 @@ void refactor()
u32 sig_length = zpl_string_length( ignore->Sig ); u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_append_length( current, content, sig_length ); current = zpl_string_append_length( current, content, sig_length );
// bool match = false;
// if ( zpl_strncmp( "zpl_printf", current, sig_length ) == 0 )
// {
// match = true;
// }
if ( zpl_string_are_equal( ignore->Sig, current ) ) if ( zpl_string_are_equal( ignore->Sig, current ) )
{ {
char before = content[-1]; char before = content[-1];
@ -482,7 +470,7 @@ void refactor()
continue; continue;
} }
zpl_printf("\nIgnored %-81s line %d", current, line ); log_fmt("\nIgnored %-81s line %d", current, line );
content += sig_length; content += sig_length;
left -= sig_length; left -= sig_length;
@ -523,7 +511,7 @@ void refactor()
zpl_string_clear( preview ); zpl_string_clear( preview );
preview = zpl_string_append_length( preview, content, length ); preview = zpl_string_append_length( preview, content, length );
zpl_printf("\nIgnored %-40s %-40s line %d", preview, ignore->Sig, line); log_fmt("\nIgnored %-40s %-40s line %d", preview, ignore->Sig, line);
content += length; content += length;
left -= length; left -= length;
@ -576,7 +564,7 @@ void refactor()
zpl_array_append( tokens, entry ); zpl_array_append( tokens, entry );
zpl_printf("\nFound %-81s line %d", current, line); log_fmt("\nFound %-81s line %d", current, line);
content += sig_length; content += sig_length;
left -= sig_length; left -= sig_length;
@ -633,7 +621,7 @@ void refactor()
zpl_string_clear( preview ); zpl_string_clear( preview );
preview = zpl_string_append_length( preview, content, length); preview = zpl_string_append_length( preview, content, length);
zpl_printf("\nFound %-40s %-40s line %d", preview, nspace->Sig, line); log_fmt("\nFound %-40s %-40s line %d", preview, nspace->Sig, line);
content += length; content += length;
left -= length; left -= length;
@ -726,8 +714,7 @@ void parse_options( int num, char** arguments )
} }
else else
{ {
zpl_printf( "-source not provided\n" ); fatal( "-source not provided\n" );
fatal();
} }
if ( zpl_opts_has_arg( & opts, "dst" ) ) if ( zpl_opts_has_arg( & opts, "dst" ) )
@ -753,8 +740,7 @@ void parse_options( int num, char** arguments )
} }
else else
{ {
zpl_printf( "Failed to parse arguments\n" ); fatal( "Failed to parse arguments\n" );
fatal();
} }
zpl_opts_free( & opts); zpl_opts_free( & opts);