10 #include "statusMessageReporting.h"
12 #if defined __cplusplus
17 #define SMR_InitialMessageSize 1024
18 #define SMR_IncrementMessageSize 1024
21 static char smr_mallocFailed[] =
"statusMessageReporting could not allocate memory for message";
36 static int smr_setReport( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
37 enum smr_status status,
char const *fmt, va_list *args );
38 static int smr_setAllocationFailure( statusMessageReport *report,
char const *file,
int line,
char const *
function,
char const *fmt, va_list *args );
46 if( smrIsSetup )
return( 0 );
48 for( i = 0; i < smr_maximumNumberOfRegisteredLibraries; ++i ) registeredLibraries[i] = NULL;
68 if( smrIsSetup == 0 )
return( 0 );
70 numberOfRegisteredLibraries = 0;
82 if( smrIsSetup == 0 )
return( -1 );
83 if( numberOfRegisteredLibraries == smr_maximumNumberOfRegisteredLibraries )
return( smr_tooManyIDs );
85 if( strcmp( libraryName, registeredLibraries[i] ) == 0 )
return( i );
87 if( ( registeredLibraries[numberOfRegisteredLibraries] = strdup( libraryName ) ) == NULL )
return( -2 );
89 return( numberOfRegisteredLibraries - 1 );
96 return( numberOfRegisteredLibraries );
103 if( ( ID < 0 ) || ( ID >= smr_maximumNumberOfRegisteredLibraries ) )
return( NULL );
104 return( registeredLibraries[ID] );
109 statusMessageReporting *
smr_new( statusMessageReporting *smr,
enum smr_status verbosity,
int append ) {
111 statusMessageReporting *new_SMR;
113 if( ( new_SMR = (statusMessageReporting *) smr_malloc2( smr,
sizeof( statusMessageReporting ), 0,
"new_SMR" ) ) == NULL )
return( NULL );
120 int smr_initialize( statusMessageReporting *smr,
enum smr_status verbosity,
int append ) {
122 if( smr == NULL )
return( 0 );
123 smr->verbosity = verbosity;
124 smr->append = append;
131 statusMessageReporting *
smr_clone( statusMessageReporting *smr ) {
133 if( smr == NULL )
return( NULL );
134 return(
smr_new( NULL, smr->verbosity, smr->append ) );
143 if( smr == NULL )
return;
144 for( current = first; current != NULL; current = next ) {
156 if( smr == NULL )
return( NULL );
168 statusMessageReport *report;
170 if( ( report = (statusMessageReport *) smr_malloc2( NULL,
sizeof( statusMessageReport ), 0,
"report" ) ) == NULL )
return( NULL );
180 report->status = smr_status_Ok;
181 report->libraryID = smr_unknownID;
182 report->code = smr_codeNULL;
184 report->fileName[0] = 0;
185 report->function[0] = 0;
186 report->message = NULL;
194 if( report->message != NULL ) {
195 if( report->message != smr_mallocFailed )
smr_freeMemory( (
void **) &(report->message) );
202 static int smr_setReport( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
203 enum smr_status status,
char const *fmt, va_list *args ) {
206 statusMessageReport *report, *next;
208 if( smr == NULL )
return( 0 );
209 if( (
int) status < (
int) smr->verbosity )
return( 0 );
210 if( status == smr_status_Ok )
return( 0 );
211 if( ( smr->report.status != smr_status_Ok ) && smr->append ) {
213 for( next =
smr_firstReport( smr ); next->next != NULL; next = next->next ) ;
214 next->next = report; }
216 if( status <= smr->report.status )
return( 0 );
218 report = &(smr->report);
220 report->status = status;
222 report->libraryID = libraryID;
225 if( file != NULL ) strncpy( report->fileName, file, smr_maximumFileNameSize );
226 report->fileName[smr_maximumFileNameSize] = 0;
227 if(
function != NULL ) strncpy( report->function,
function, smr_maximumFileNameSize );
228 report->function[smr_maximumFileNameSize] = 0;
231 if( userInterface != NULL ) {
232 if( ( userMsg = (*(smr_userInterface *) userInterface)( (
void *) userInterface ) ) != NULL ) {
233 int userSize = (int) strlen( userMsg );
234 if( ( report->message = (
char *) smr_realloc2( NULL, report->message, strlen( report->message ) + userSize + 2,
"report->message" ) ) == NULL ) {
238 strcat( report->message, userMsg );
247 static int smr_setAllocationFailure( statusMessageReport *report,
char const *file,
int line,
char const *
function,
char const *fmt, va_list *args ) {
249 vfprintf( stderr, fmt, *args );
251 fprintf( stderr,
"\nAt line %d of %s in function %s\n", line, file,
function );
252 if( report != NULL ) {
253 report->status = smr_status_Error;
254 report->message = (
char *) smr_mallocFailed;
262 int smr_setReportInfo( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
char const *fmt, ... ) {
267 va_start( args, fmt );
268 status =
smr_setReport( smr, userInterface, file, line,
function, libraryID, code, smr_status_Info, fmt, &args );
275 int smr_vsetReportInfo( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
char const *fmt, va_list *args ) {
277 return(
smr_setReport( smr, userInterface, file, line,
function, libraryID, code, smr_status_Info, fmt, args ) );
282 int smr_setReportWarning( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
char const *fmt, ... ) {
287 va_start( args, fmt );
288 status =
smr_setReport( smr, userInterface, file, line,
function, libraryID, code, smr_status_Warning, fmt, &args );
295 int smr_vsetReportWarning( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
char const *fmt, va_list *args ) {
297 return(
smr_setReport( smr, userInterface, file, line,
function, libraryID, code, smr_status_Warning, fmt, args ) );
302 int smr_setReportError( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
char const *fmt, ... ) {
307 va_start( args, fmt );
308 status =
smr_setReport( smr, userInterface, file, line,
function, libraryID, code, smr_status_Error, fmt, &args );
315 int smr_vsetReportError( statusMessageReporting *smr,
void *userInterface,
char const *file,
int line,
char const *
function,
int libraryID,
int code,
char const *fmt, va_list *args ) {
317 return(
smr_setReport( smr, userInterface, file, line,
function, libraryID, code, smr_status_Error, fmt, args ) );
324 enum smr_status status = smr_status_Ok;
325 statusMessageReport *report;
327 if( smr == NULL )
return( smr_status_Ok );
328 for( report =
smr_firstReport( smr ); report != NULL; report =
smr_nextReport( report ) )
if( report->status > status ) status = report->status;
366 return( ( status == smr_status_Warning ) || ( status == smr_status_Error ) );
373 if( report == NULL )
return( 0 );
374 return( report->status == smr_status_Ok );
381 if( report == NULL )
return( 0 );
382 return( report->status == smr_status_Info );
389 if( report == NULL )
return( 0 );
390 return( report->status == smr_status_Warning );
397 if( report == NULL )
return( 0 );
398 return( report->status == smr_status_Error );
405 if( report == NULL )
return( 0 );
406 return( ( report->status == smr_status_Warning ) || ( report->status == smr_status_Error ) );
414 statusMessageReport *report;
416 if( smr == NULL )
return( 0 );
417 if( smr->report.status == smr_status_Ok )
return( 0 );
426 if( smr == NULL )
return( NULL );
427 if( smr->report.status == smr_status_Ok )
return( NULL );
428 return( &(smr->report) );
435 if( report == NULL )
return( NULL );
436 return( report->next );
443 if( smr == NULL )
return( smr_status_Ok );
444 return( smr->verbosity );
451 if( smr == NULL )
return( 0 );
452 return( smr->append );
459 if( report == NULL )
return( 0 );
460 return( report->libraryID );
467 if( report == NULL )
return( -1 );
468 return( report->code );
475 if( report == NULL )
return( -1 );
476 return( report->line );
483 if( report == NULL )
return( NULL );
484 return( report->fileName );
491 if( report == NULL )
return( NULL );
492 return( report->function );
499 if( report == NULL )
return( NULL );
500 return( report->message );
507 if( report == NULL )
return( NULL );
508 if( report->status == smr_status_Ok )
return( NULL );
516 if( report == NULL )
return( NULL );
517 if( report->status == smr_status_Ok )
return( NULL );
518 return(
smr_allocateFormatMessage(
"%s\nAt line %d of %s in function %s", report->message, report->line, report->fileName, report->function ) );
523 void smr_print( statusMessageReporting *smr,
int clear ) {
530 void smr_write( statusMessageReporting *smr, FILE *f,
int clear ) {
532 statusMessageReport *report;
534 if( smr == NULL )
return;
550 if( report->message != NULL ) fprintf( f,
"%s\nAt line %d of %s in function %s\n", report->message, report->line, report->fileName, report->function );
558 case smr_status_Ok :
return( statusStringOk );
573 va_start( args, fmt );
588 va_copy( args_, *args );
589 n = vsnprintf( message, size, fmt, args_ );
591 if( ( n > -1 ) && ( n < size ) )
break;
597 if( message == buffer ) message = NULL;
598 if( ( message = (
char *) realloc( message, size ) ) == NULL )
return( NULL );
600 if( message == buffer ) {
601 if( ( message = (
char *) malloc( n + 1 ) ) == NULL )
return( NULL );
602 strcpy( message, buffer ); }
604 if( ( message = (
char *) realloc( message, n + 1 ) ) == NULL )
return( NULL );
611 void *
smr_malloc( statusMessageReporting *smr,
size_t size,
int zero,
char const *forItem,
char const *file,
int line,
char const *
function ) {
613 void *p =
smr_realloc( smr, NULL, size, forItem, file, line,
function );
618 if( ( p != NULL ) && zero ) {
619 for( i = 0, l = (
long long *) p; i < size /
sizeof(
long long ); i++, l++ ) *l = 0;
620 for( i *=
sizeof(
long long ), c = (
char *) l; i < size; i++, c++ ) *c = 0;
628 void *
smr_realloc( statusMessageReporting *smr,
void *pOld,
size_t size,
char const *forItem,
char const *file,
int line,
char const *
function ) {
630 void *p = realloc( pOld, size );
632 if( ( p == NULL ) && ( smr != NULL ) ) {
633 smr_setReportError( smr, NULL, file, line,
function, smr_smrID, -1,
" smr_realloc: failed to realloc size = %z for variable %s\n", size, forItem );
642 if( p == NULL )
return( NULL );
652 char *
smr_allocateCopyString( statusMessageReporting *smr,
char const *
s,
char const *forItem,
char const *file,
int line,
char const *
function ) {
656 char *c = strdup( s );
658 if( c == NULL )
smr_setReportError( smr, NULL, file, line,
function, smr_smrID, -1,
" smr_allocateCopyString: strdup failed for strlen( s ) = %z for variable %s",
659 strlen( s ), forItem );
665 char *
smr_allocateCopyStringN( statusMessageReporting *smr,
char const *
s,
size_t n,
char const *forItem,
char const *file,
int line,
char const *
function ) {
669 size_t l = strlen( s );
673 if( ( c = (
char *)
smr_malloc( smr, l + 1, 0, forItem, file, line,
function ) ) != NULL ) {
689 #if defined __cplusplus
int smr_vsetReportError(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, va_list *args)
void smr_print(statusMessageReporting *smr, int clear)
static char statusStringWarning[]
int smr_vsetReportInfo(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, va_list *args)
static char statusStringError[]
static char * registeredLibraries[smr_maximumNumberOfRegisteredLibraries]
char const * smr_getFile(statusMessageReport *report)
static constexpr double s
statusMessageReport * smr_nextReport(statusMessageReport *report)
static char unknownLibrary[]
static char statusStringInvalid[]
#define SMR_IncrementMessageSize
char * smr_allocateCopyString(statusMessageReporting *smr, char const *s, char const *forItem, char const *file, int line, char const *function)
static char statusStringInfo[]
int smr_isError(statusMessageReporting *smr)
int smr_isReportOk(statusMessageReport *report)
static char smr_mallocFailed[]
int smr_initialize(statusMessageReporting *smr, enum smr_status verbosity, int append)
void smr_reportPrint(statusMessageReport *report)
void smr_release(statusMessageReporting *smr)
void * smr_free(statusMessageReporting **smr)
char * smr_allocateFormatMessage(char const *fmt,...)
enum smr_status smr_getVerbosity(statusMessageReporting *smr)
statusMessageReporting * smr_clone(statusMessageReporting *smr)
char * smr_vallocateFormatMessage(char const *fmt, va_list *args)
static char tooManyLibrary[]
int smr_setReportError(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt,...)
char const * smr_statusToString(enum smr_status status)
int smr_isOk(statusMessageReporting *smr)
int smr_isWarning(statusMessageReporting *smr)
static int smr_setReport(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, enum smr_status status, char const *fmt, va_list *args)
#define SMR_InitialMessageSize
int smr_getAppend(statusMessageReporting *smr)
void * smr_freeMemory(void **p)
int smr_isReportWarningOrError(statusMessageReport *report)
int smr_isReportError(statusMessageReport *report)
static char invalidLibrary[]
int smr_vsetReportWarning(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt, va_list *args)
static char statusStringOk[]
int smr_isReportWarning(statusMessageReport *report)
int smr_getCode(statusMessageReport *report)
void * smr_malloc(statusMessageReporting *smr, size_t size, int zero, char const *forItem, char const *file, int line, char const *function)
char * smr_copyFullMessage(statusMessageReport *report)
static char errnoLibrary[]
static int smr_setAllocationFailure(statusMessageReport *report, char const *file, int line, char const *function, char const *fmt, va_list *args)
char const * smr_getRegisteredLibrariesName(int ID)
char const * smr_getFunction(statusMessageReport *report)
int smr_setReportWarning(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt,...)
char * smr_allocateCopyStringN(statusMessageReporting *smr, char const *s, size_t n, char const *forItem, char const *file, int line, char const *function)
static int smr_reportInitialize(statusMessageReport *report)
void smr_write(statusMessageReporting *smr, FILE *f, int clear)
int smr_setReportInfo(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt,...)
int smr_isInfo(statusMessageReporting *smr)
int smr_getLibraryID(statusMessageReport *report)
static statusMessageReport * smr_reportNew(void)
static int numberOfRegisteredLibraries
int smr_isWarningOrError(statusMessageReporting *smr)
void smr_reportWrite(statusMessageReport *report, FILE *f)
char * smr_copyMessage(statusMessageReport *report)
statusMessageReporting * smr_new(statusMessageReporting *smr, enum smr_status verbosity, int append)
statusMessageReport * smr_firstReport(statusMessageReporting *smr)
int smr_registerLibrary(char const *libraryName)
int smr_isReportInfo(statusMessageReport *report)
int smr_getLine(statusMessageReport *report)
void * smr_realloc(statusMessageReporting *smr, void *pOld, size_t size, char const *forItem, char const *file, int line, char const *function)
int smr_numberOfRegisteredLibraries(void)
char const * smr_getMessage(statusMessageReport *report)
static void smr_reportRelease(statusMessageReport *report)
int smr_numberOfReports(statusMessageReporting *smr)
enum smr_status smr_highestStatus(statusMessageReporting *smr)