12#if defined __cplusplus
17#define SMR_InitialMessageSize 1024
18#define SMR_IncrementMessageSize 1024
20static int smrIsSetup = 0;
21static char smr_mallocFailed[] =
"statusMessageReporting could not allocate memory for message";
22static char statusStringOk[] =
"Ok", statusStringInfo[] =
"Info",
23 statusStringWarning[] =
"Warning", statusStringError[] =
"Error", statusStringInvalid[] =
"Invalid";
25static int numberOfRegisteredLibraries = 0;
26static char unknownLibrary[] =
"unknownID";
27static char tooManyLibrary[] =
"tooManyIDs";
28static char invalidLibrary[] =
"invalidID";
29static char errnoLibrary[] =
"errnoID";
30static char smrLibrary[] =
"statusMessageReporting";
37 enum smr_status status,
char const *fmt, va_list *args );
38static int smr_setAllocationFailure(
statusMessageReport *report,
char const *file,
int line,
char const *
function,
char const *fmt, va_list *args );
46 if( smrIsSetup )
return( 0 );
50 ++numberOfRegisteredLibraries;
52 ++numberOfRegisteredLibraries;
54 ++numberOfRegisteredLibraries;
56 ++numberOfRegisteredLibraries;
57 registeredLibraries[
smr_smrID] = smrLibrary;
58 ++numberOfRegisteredLibraries;
68 if( smrIsSetup == 0 )
return( 0 );
69 for( i =
smr_smrID + 1; i < numberOfRegisteredLibraries; ++i )
smr_freeMemory( (
void **) &(registeredLibraries[i]) );
70 numberOfRegisteredLibraries = 0;
82 if( smrIsSetup == 0 )
return( -1 );
84 for( i = 0; i < numberOfRegisteredLibraries; ++i ) {
85 if( strcmp( libraryName, registeredLibraries[i] ) == 0 )
return( i );
87 if( ( registeredLibraries[numberOfRegisteredLibraries] = strdup( libraryName ) ) == NULL )
return( -2 );
88 ++numberOfRegisteredLibraries;
89 return( numberOfRegisteredLibraries - 1 );
96 return( numberOfRegisteredLibraries );
104 return( registeredLibraries[ID] );
122 if( smr == NULL )
return( 0 );
125 smr_reportInitialize( &(smr->
report) );
133 if( smr == NULL )
return( NULL );
143 if( smr == NULL )
return;
144 for( current = first; current != NULL; current = next ) {
146 smr_reportRelease( current );
156 if( smr == NULL )
return( NULL );
171 smr_reportInitialize( report );
194 if( report->
message != NULL ) {
197 smr_reportInitialize( report );
203 enum smr_status status,
char const *fmt, va_list *args ) {
208 if( smr == NULL )
return( 0 );
209 if( (
int) status < (
int) smr->
verbosity )
return( 0 );
212 if( ( report = smr_reportNew( ) ) == NULL )
return( smr_setAllocationFailure( NULL, file, line,
function, fmt, args ) );
214 next->
next = report; }
216 if( status <= smr->report.
status )
return( 0 );
221 if( ( libraryID < 0 ) || ( libraryID >= numberOfRegisteredLibraries ) ) libraryID =
smr_invalidID;
231 if( userInterface != NULL ) {
232 if( ( userMsg = (*(
smr_userInterface *) userInterface)( (
void *) userInterface ) ) != NULL ) {
233 int userSize = (int) strlen( userMsg );
236 return( smr_setAllocationFailure( report, file, line,
function, fmt, args ) );
238 strcat( report->
message, userMsg );
247static 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 ) {
254 report->
message = (
char *) smr_mallocFailed;
267 va_start( args, fmt );
287 va_start( args, fmt );
307 va_start( args, fmt );
373 if( report == NULL )
return( 0 );
381 if( report == NULL )
return( 0 );
389 if( report == NULL )
return( 0 );
397 if( report == NULL )
return( 0 );
405 if( report == NULL )
return( 0 );
416 if( smr == NULL )
return( 0 );
426 if( smr == NULL )
return( NULL );
435 if( report == NULL )
return( NULL );
436 return( report->
next );
451 if( smr == NULL )
return( 0 );
459 if( report == NULL )
return( 0 );
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 );
491 if( report == NULL )
return( NULL );
499 if( report == NULL )
return( NULL );
507 if( report == NULL )
return( NULL );
516 if( report == NULL )
return( NULL );
534 if( smr == NULL )
return;
563 return( statusStringInvalid );
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 );
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 );
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;
630 void *p = realloc( pOld, size );
632 if( ( p == NULL ) && ( smr != NULL ) ) {
642 if( p == NULL )
return( NULL );
656 char *c = strdup( s );
659 strlen( s ), forItem );
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
G4double(* function)(G4double)
#define SMR_InitialMessageSize
#define SMR_IncrementMessageSize
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)
int smr_registerLibrary(char const *libraryName)
char * smr_copyMessage(statusMessageReport *report)
int smr_isInfo(statusMessageReporting *smr)
int smr_initialize(statusMessageReporting *smr, enum smr_status verbosity, int append)
char * smr_allocateCopyString(statusMessageReporting *smr, char const *s, char const *forItem, char const *file, int line, char const *function)
char * smr_vallocateFormatMessage(char const *fmt, va_list *args)
int smr_isError(statusMessageReporting *smr)
int smr_isReportOk(statusMessageReport *report)
char * smr_copyFullMessage(statusMessageReport *report)
int smr_isReportWarningOrError(statusMessageReport *report)
int smr_getAppend(statusMessageReporting *smr)
statusMessageReport * smr_nextReport(statusMessageReport *report)
int smr_isReportWarning(statusMessageReport *report)
#define smr_maximumFileNameSize
char const * smr_getFile(statusMessageReport *report)
#define smr_realloc2(smr, old, size, forItem)
statusMessageReporting * smr_new(statusMessageReporting *smr, enum smr_status verbosity, int append)
int smr_numberOfReports(statusMessageReporting *smr)
void * smr_realloc(statusMessageReporting *smr, void *pOld, size_t size, char const *forItem, char const *file, int line, char const *function)
int smr_isWarningOrError(statusMessageReporting *smr)
statusMessageReporting * smr_clone(statusMessageReporting *smr)
statusMessageReport * smr_firstReport(statusMessageReporting *smr)
enum smr_status smr_highestStatus(statusMessageReporting *smr)
int smr_getLibraryID(statusMessageReport *report)
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)
void * smr_free(statusMessageReporting **smr)
int smr_setReportWarning(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt,...)
void smr_print(statusMessageReporting *smr, int clear)
char *(* smr_userInterface)(void *userData)
int smr_isReportInfo(statusMessageReport *report)
char const * smr_getMessage(statusMessageReport *report)
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)
char const * smr_statusToString(enum smr_status status)
void * smr_freeMemory(void **p)
char const * smr_getRegisteredLibrariesName(int ID)
int smr_isWarning(statusMessageReporting *smr)
void smr_write(statusMessageReporting *smr, FILE *f, int clear)
int smr_getLine(statusMessageReport *report)
void smr_reportWrite(statusMessageReport *report, FILE *f)
void * smr_malloc(statusMessageReporting *smr, size_t size, int zero, char const *forItem, char const *file, int line, char const *function)
int smr_setReportInfo(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt,...)
int smr_setReportError(statusMessageReporting *smr, void *userInterface, char const *file, int line, char const *function, int libraryID, int code, char const *fmt,...)
enum smr_status smr_getVerbosity(statusMessageReporting *smr)
void smr_release(statusMessageReporting *smr)
int smr_isOk(statusMessageReporting *smr)
#define smr_maximumNumberOfRegisteredLibraries
void smr_reportPrint(statusMessageReport *report)
char * smr_allocateFormatMessage(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)
int smr_numberOfRegisteredLibraries(void)
int smr_getCode(statusMessageReport *report)
char const * smr_getFunction(statusMessageReport *report)
#define smr_malloc2(smr, size, zero, forItem)
int smr_isReportError(statusMessageReport *report)
char function[smr_maximumFileNameSize+1]
struct statusMessageReport * next
char fileName[smr_maximumFileNameSize+1]
enum smr_status verbosity
statusMessageReport report