47 #include <boost/algorithm/string/predicate.hpp>
48 #include <boost/algorithm/string/trim.hpp>
49 #include <boost/format.hpp>
80 {
Warn,
true,
false,
SCR_STDERR,
false,
"Warning (%s, %s(), %d):\n"},
81 {
UsrErr,
true,
false,
SCR_NONE,
true,
"User Error (%s, %s(), %d):\n"},
82 {
Err,
true,
false,
SCR_NONE,
true,
"Error (%s, %s(), %d):\n"},
83 {
PrgErr,
true,
false,
SCR_NONE,
true,
"Internal Error (%s, %s(), %d):\n"}
97 int _xprintf(
const char *
const xprintf_file,
const char *
const xprintf_func,
const int xprintf_line,
MessageType type,
const char *
const fmt, ... )
103 static int mpi_msg_id = 0;
118 default: screen=NULL;
125 fprintf(screen,
"[mpi_id=%d] ", sys_info.
my_proc );
126 if (mf.
log && sys_info.
log)
127 fprintf(sys_info.
log,
"[mpi_id=%d] ", sys_info.
my_proc );
130 if ( (screen) && ( sys_info.
my_proc != 0 ) )
137 char format_message[1024];
138 va_start( argptr, fmt );
139 vsprintf(format_message, fmt, argptr);
144 BOOST_THROW_EXCEPTION( ExcXprintfMsg()
145 << EI_XprintfHeader( boost::str(boost::format(mf.
head) % xprintf_file % xprintf_func % xprintf_line) )
146 << EI_XprintfMessage( format_message ) );
154 printf(
"MPI_Barrier() error in xprintf()\n");
155 exit( EXIT_FAILURE );
160 fprintf(screen,
"[msg_id=%d] ", mpi_msg_id );
161 if (mf.
log && sys_info.
log)
162 fprintf(sys_info.
log,
"[msg_id=%d] ", mpi_msg_id );
165 #ifndef DEBUG_MESSAGES
166 if (type ==
Warn) mf.
head=
"\nWarning: ";
170 if (screen) fprintf(screen,mf.
head,xprintf_file,xprintf_func,xprintf_line);
171 if (mf.
log && sys_info.
log)
172 fprintf(sys_info.
log,mf.
head,xprintf_file,xprintf_func,xprintf_line);
178 if (mf.
log && sys_info.
log)
180 va_start( argptr, fmt );
181 rc=vfprintf(sys_info.
log,fmt,argptr);
184 fflush(sys_info.
log);
189 va_start( argptr, fmt );
190 rc=vfprintf(screen,fmt,argptr);
214 if (size == 0 ) size++;
216 if ( rc == NULL )
xprintf(
Err ,
"Not enough memory for allocating %u bytes\n", size );
247 rc = realloc( ptr, size );
248 if ( rc == NULL )
xprintf(
Err ,
"Not enough memory for allocating %u bytes\n", size );
281 void *
operator new[] (std::size_t size,
const std::nothrow_t& )
throw() {
285 void operator delete(
void *p)
throw()
290 void operator delete[](
void *p)
throw()
305 INPUT_CHECK(!( rc != 0 ),
"Error executing external command: %s\n", cmd );
317 ASSERT(!( src == NULL ),
"NULL pointer as argument of function xstrcpy()\n");
318 length = strlen( src ) + 1;
319 rc = (
char*)
xmalloc(length *
sizeof(
char));
333 const char *
const whitespace_delim=
" \t\r\n";
335 rc =
xstrtok( s, whitespace_delim, position);
348 char *
xstrtok(
char *s1,
const char *delim,
int position )
351 static char * full_string = NULL;
352 static int token_count;
354 ASSERT(!( delim == NULL ),
"NULL pointer as delimiter in xstrtok()\n");
361 full_string[0] = 0x0;
368 INPUT_CHECK( token_count == position || position < 0,
"Requested position %d dosn't match token position %d", position, token_count);
369 rc = strtok( s1, delim );
372 INPUT_CHECK(!( rc == NULL ),
"Missing token no. %d: original string '%s' with delimiters '%s'\n", token_count, full_string, delim );
387 ASSERT( s,
"Can not chomp NULL string.");
395 while ((p >= s) && ((*p ==
' ') || (*p ==
'\t') || (*p ==
'\r') || (*p ==
'\n')))
413 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xmkdir()\n");
414 rc = mkdir(s, S_IRWXU);
417 INPUT_CHECK(!( rc != 0 ),
"Cannot make directory %s\n", s );
429 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xrmdir()\n");
431 INPUT_CHECK(!( rc != 0 ),
"Cannot delete directory %s\n", s );
442 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xchdir()\n");
444 INPUT_CHECK(!( rc != 0 ),
"Cannot change directory to %s\n", s );
455 ASSERT(!( fname == NULL ),
"NULL pointer as argument of function xremove()\n");
456 if( access( fname , F_OK ) == 0 )
458 rc =
remove( fname );
459 INPUT_CHECK(!( rc != 0 ),
"Cannot remove file %s\n", fname );
462 xprintf(
Warn,
"File '%s' does not exist, can not remove. Ignoring.\n", fname );
475 rc = getcwd( tmp, PATH_MAX );
476 ASSERT( rc,
"Cannot get name of current working directory\n");
bool log
log the message - YES/NO
#define OPERATOR_NEW_THROW_EXCEPTION
int xremove(const char *fname)
DELETE a FILE with error handling.
char * xstrcpy(const char *src)
MAKE BRAND NEW COPY OF STRING.
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
void * xrealloc(void *ptr, size_t size)
Reallocation of memory block with checking.
System structure for various global variables.
int xchomp(char *s)
Delete trailing whitespace characters (space,tab,CR,NL).
int xrmdir(const char *s)
RMDIR with err handling.
bool stop
terminate the program
int xmkdir(const char *s)
MKDIR WITH ERROR HANDLING.
int _xprintf(const char *const xprintf_file, const char *const xprintf_func, const int xprintf_line, MessageType type, const char *const fmt,...)
Multi-purpose printing routine: messages, warnings, errors.
int screen
print to stdout,stderr,NULL
void * xmalloc(size_t size)
Memory allocation with checking.
#define INPUT_CHECK(i,...)
Debugging macros.
INTERNAL DEFINITIONS FOR XPRINTF.
MessageType
Identifiers for various output messages.
int xchdir(const char *s)
CHDIR WITH ERROR HANDLING.
static struct MsgFmt msg_fmt[]
char * xstrtok(char *s, int position)
STRTOK WITH ERROR HANDLING and whitespace delimiters.
const char * head
message formating string
char * xgetcwd(void)
GET CURRENT WORKING DIRECTORY with error handling.
#define MPI_Barrier(comm)
int xsystem(const char *cmd)
SYSTEM with err handling.
bool mpi
treat as global message (invoke MPI_Barrier() when printing)