Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
statusMessageReporting.cc
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4# Produced at the Lawrence Livermore National Laboratory
5# Written by Bret R. Beck, beck6@llnl.gov.
6# CODE-461393
7# All rights reserved.
8#
9# This file is part of GIDI. For details, see nuclear.llnl.gov.
10# Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11#
12# Redistribution and use in source and binary forms, with or without modification,
13# are permitted provided that the following conditions are met:
14#
15# 1) Redistributions of source code must retain the above copyright notice,
16# this list of conditions and the disclaimer below.
17# 2) Redistributions in binary form must reproduce the above copyright notice,
18# this list of conditions and the disclaimer (as noted below) in the
19# documentation and/or other materials provided with the distribution.
20# 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21# used to endorse or promote products derived from this software without
22# specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27# SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34# <<END-copyright>>
35*/
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <stdarg.h>
40
42
43#if defined __cplusplus
44namespace GIDI {
45using namespace GIDI;
46#endif
47
48static const char smr_mallocFailed[] = "statusMessageReporting could not allocate memory for message";
49
50static int smr_setAllocationFailure( statusMessageReporting *smr, const char *fmt, va_list *args );
51static int smr_setMessage( statusMessageReporting *smr, void *userInterface, const char *file, int line, int code,
52 enum smr_status status, const char *fmt, va_list *args );
53static char *smr_getFullMessage2( char const *fmt, ... );
54/*
55************************************************************
56*/
58
59 smr->status = smr_status_Ok;
60 smr->packageName[0] = 0;
61 smr->line= -1;
62 smr->code = 0;
63 smr->message = NULL;
64 return( 0 );
65}
66/*
67************************************************************
68*/
70
71 if( smr->message != NULL ) {
72 if( smr->message != smr_mallocFailed ) free( smr->message );
73 }
74 return( smr_initialize( smr ) );
75}
76/*
77************************************************************
78*/
79int smr_setMessageInfo( statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt, ... ) {
80
81 int status;
82 va_list args;
83
84 va_start( args, fmt );
85 status = smr_setMessage( smr, userInterface, file, line, code, smr_status_Info, fmt, &args );
86 va_end( args );
87 return( status );
88}
89/*
90************************************************************
91*/
92int smr_vsetMessageInfo( statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt, va_list *args ) {
93
94 int status = smr_setMessage( smr, userInterface, file, line, code, smr_status_Info, fmt, args );
95 return( status );
96}
97/*
98************************************************************
99*/
100int smr_setMessageError( statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt, ... ) {
101
102 int status;
103 va_list args;
104
105 va_start( args, fmt );
106 status = smr_setMessage( smr, userInterface, file, line, code, smr_status_Error, fmt, &args );
107 va_end( args );
108 return( status );
109}
110/*
111************************************************************
112*/
113int smr_vsetMessageError( statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt, va_list *args ) {
114
115 int status = smr_setMessage( smr, userInterface, file, line, code, smr_status_Error, fmt, args );
116 return( status );
117}
118/*
119************************************************************
120*/
121char *smr_allocateFormatMessage( const char *fmt, ... ) {
122
123 char *s;
124 va_list args;
125
126 va_start( args, fmt );
127 s = smr_vallocateFormatMessage( fmt, &args );
128 va_end( args );
129 return( s );
130}
131/*
132************************************************************
133*/
134char *smr_vallocateFormatMessage( const char *fmt, va_list *args ) {
135
136 int n, size = 128;
137 char *message = NULL;
138 va_list args_;
139
140 while( 1 ) {
141 if( ( message = (char *) realloc( message, size ) ) == NULL ) return( NULL );
142 //TK110426
143#if defined WIN32
144 args_ = *args;
145#elif defined __IBMCPP__
146 va_copy( args_, *args );
147#else
148 __va_copy( args_, *args );
149#endif
150 n = vsnprintf( message, size, fmt, args_ );
151 va_end( args_ );
152 if( ( n > -1 ) && ( n < size ) ) break;
153 if( n > -1 ) { /* glibc 2.1 */
154 size = n + 3; }
155 else { /* glibc 2.0 */
156 size += 128;
157 }
158 }
159 return( message );
160}
161/*
162************************************************************
163*/
164static int smr_setMessage( statusMessageReporting *smr, void *userInterface, const char *file, int line, int code,
165 enum smr_status status, const char *fmt, va_list *args ) {
166
167 char *userMsg = NULL;
168 int userSize;
169
170 if( smr == NULL ) return( 0 );
171 smr_release( smr );
172 smr->status = status;
173 if( file != NULL ) strncpy( smr->packageName, file, smr_maximumPackageNameSize );
175 smr->line= line;
176 smr->code = code;
177
178 if( ( smr->message = smr_vallocateFormatMessage( fmt, args ) ) == NULL ) return( smr_setAllocationFailure( smr, fmt, args ) );
179 if( userInterface != NULL ) {
180 if( ( userSize = (*(smr_userInterface *) userInterface)( (void *) userInterface, NULL ) ) > 0 ) {
181 //if( (smr->message = realloc(smr->message, strlen( smr->message ) + userSize + 2)) == NULL ) return( smr_setAllocationFailure( smr, fmt, args ) );
182 if( (smr->message = (char*) realloc(smr->message, strlen( smr->message ) + userSize + 2)) == NULL ) return( smr_setAllocationFailure( smr, fmt, args ) );
183 strcat( smr->message, "\n" );
184 userSize = (*(smr_userInterface *) userInterface)( (void *) userInterface, &userMsg );
185 if( userSize < 0 ) return( smr_setAllocationFailure( smr, fmt, args ) );
186 if( userSize > 0 ) {
187 strcat( smr->message, userMsg );
188 free( userMsg );
189 }
190 }
191 }
192 return( 0 );
193}
194/*
195************************************************************
196*/
197static int smr_setAllocationFailure( statusMessageReporting *smr, const char *fmt, va_list *args ) {
198
199 vfprintf( stderr, fmt, *args ); /* Assume calling routine calls va_end( args ). */
200 fprintf( stderr, "\nAt line %d of %s\n", smr->line, smr->packageName );
202 smr->message = (char *) smr_mallocFailed;
203 return( 1 );
204}
205/*
206************************************************************
207*/
209
210 if( smr == NULL ) return( 1 );
211 return( smr->status == smr_status_Ok );
212}
213/*
214************************************************************
215*/
217
218 if( smr == NULL ) return( 1 );
219 return( smr->status == smr_status_Info );
220}
221/*
222************************************************************
223*/
225
226 if( smr == NULL ) return( 1 );
227 return( ( smr->status == smr_status_Error ) || ( smr->status == smr_status_Fatal ) );
228}
229/*
230************************************************************
231*/
233
234 if( smr == NULL ) return( 1 );
235 return( smr->status == smr_status_Fatal );
236}
237/*
238************************************************************
239*/
241
242 return( smr->message );
243}
244/*
245************************************************************
246*/
248
249 return( smr_getFullMessage2( "%s\nAt line %d of %s", smr->message, smr->line, smr->packageName ) );
250}
251/*
252************************************************************
253*/
254static char *smr_getFullMessage2( char const *fmt, ... ) {
255
256 va_list args;
257 char *message;
258
259 va_start( args, fmt );
260 message = smr_vallocateFormatMessage( fmt, &args );
261 va_end( args );
262 return( message );
263}
264/*
265************************************************************
266*/
267void smr_print( statusMessageReporting *smr, FILE *f, int clear ) {
268
269 if( smr->message != NULL ) fprintf( f, "%s\nAt line %d of %s\n", smr->message, smr->line, smr->packageName );
270 if( clear ) smr_release( smr );
271}
272
273#if defined __cplusplus
274}
275#endif
int smr_vsetMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt, va_list *args)
int smr_isInfo(statusMessageReporting *smr)
int smr_isError(statusMessageReporting *smr)
int smr_vsetMessageInfo(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt, va_list *args)
char * smr_vallocateFormatMessage(const char *fmt, va_list *args)
int smr_isFatal(statusMessageReporting *smr)
int smr_initialize(statusMessageReporting *smr)
char * smr_getFullMessage(statusMessageReporting *smr)
#define smr_maximumPackageNameSize
void smr_print(statusMessageReporting *smr, FILE *f, int clear)
int smr_release(statusMessageReporting *smr)
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
char * smr_allocateFormatMessage(const char *fmt,...)
const char * smr_getMessage(statusMessageReporting *smr)
int smr_setMessageInfo(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
int smr_isOk(statusMessageReporting *smr)
@ smr_status_Error
@ smr_status_Fatal
@ smr_status_Info
@ smr_status_Ok
int(* smr_userInterface)(void *userData, char **smr)
char packageName[smr_maximumPackageNameSize]