49 #include <boost/algorithm/string/predicate.hpp>
50 #include <boost/algorithm/string/trim.hpp>
51 #include <boost/format.hpp>
89 {
Warn,
true,
false,
SCR_STDERR,
false,
"Warning (%s, %s(), %d):\n"},
90 {
UsrErr,
true,
false,
SCR_NONE,
true,
"User Error (%s, %s(), %d):\n"},
91 {
Err,
true,
false,
SCR_NONE,
true,
"Error (%s, %s(), %d):\n"},
92 {
PrgErr,
true,
false,
SCR_NONE,
true,
"Internal Error (%s, %s(), %d):\n"}
106 int _xprintf(
const char *
const xprintf_file,
const char *
const xprintf_func,
const int xprintf_line,
MessageType type,
const char *
const fmt, ... )
112 static int mpi_msg_id = 0;
127 default: screen=NULL;
134 fprintf(screen,
"[mpi_id=%d] ", sys_info.
my_proc );
135 if (mf.
log && sys_info.
log)
136 fprintf(sys_info.
log,
"[mpi_id=%d] ", sys_info.
my_proc );
139 if ( (screen) && ( sys_info.
my_proc != 0 ) )
146 char format_message[1024];
147 va_start( argptr, fmt );
148 vsprintf(format_message, fmt, argptr);
154 BOOST_THROW_EXCEPTION( ExcXprintfMsg()
155 << EI_XprintfHeader( boost::str(boost::format(mf.
head) % xprintf_file % xprintf_func % xprintf_line) )
156 << EI_XprintfMessage( format_message ) );
164 printf(
"MPI_Barrier() error in xprintf()\n");
165 exit( EXIT_FAILURE );
170 fprintf(screen,
"[msg_id=%d] ", mpi_msg_id );
171 if (mf.
log && sys_info.
log)
172 fprintf(sys_info.
log,
"[msg_id=%d] ", mpi_msg_id );
175 #ifndef DEBUG_MESSAGES
176 if (type ==
Warn) mf.
head=
"\nWarning: ";
180 if (screen) fprintf(screen,mf.
head,xprintf_file,xprintf_func,xprintf_line);
181 if (mf.
log && sys_info.
log)
182 fprintf(sys_info.
log,mf.
head,xprintf_file,xprintf_func,xprintf_line);
188 if (mf.
log && sys_info.
log)
190 va_start( argptr, fmt );
191 rc=vfprintf(sys_info.
log,fmt,argptr);
194 fflush(sys_info.
log);
199 va_start( argptr, fmt );
200 rc=vfprintf(screen,fmt,argptr);
224 if (size == 0 ) size++;
227 if ( rc == NULL )
xprintf(
Err ,
"Not enough memory for allocating %u bytes\n", size );
260 rc = realloc( ptr, size );
261 if ( rc == NULL )
xprintf(
Err ,
"Not enough memory for allocating %u bytes\n", size );
294 void *
operator new[] (std::size_t size,
const std::nothrow_t& )
throw() {
298 void operator delete(
void *p)
throw()
303 void operator delete[](
void *p)
throw()
320 INPUT_CHECK(!( rc != 0 ),
"Error executing external command: %s\n", cmd );
334 ASSERT(!( src == NULL ),
"NULL pointer as argument of function xstrcpy()\n");
335 length = strlen( src ) + 1;
336 rc = (
char*)
xmalloc(length *
sizeof(
char));
350 const char *
const whitespace_delim=
" \t\r\n";
354 rc =
xstrtok( s, whitespace_delim, position);
367 char *
xstrtok(
char *s1,
const char *delim,
int position )
370 static char * full_string = NULL;
371 static int token_count;
375 ASSERT(!( delim == NULL ),
"NULL pointer as delimiter in xstrtok()\n");
382 full_string[0] = 0x0;
389 INPUT_CHECK( token_count == position || position < 0,
"Requested position %d dosn't match token position %d", position, token_count);
390 rc = strtok( s1, delim );
393 INPUT_CHECK(!( rc == NULL ),
"Missing token no. %d: original string '%s' with delimiters '%s'\n", token_count, full_string, delim );
410 ASSERT( s,
"Can not chomp NULL string.");
418 while ((p >= s) && ((*p ==
' ') || (*p ==
'\t') || (*p ==
'\r') || (*p ==
'\n')))
438 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xmkdir()\n");
439 rc = mkdir(s, S_IRWXU);
442 INPUT_CHECK(!( rc != 0 ),
"Cannot make directory %s\n", s );
456 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xrmdir()\n");
458 INPUT_CHECK(!( rc != 0 ),
"Cannot delete directory %s\n", s );
471 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xchdir()\n");
473 INPUT_CHECK(!( rc != 0 ),
"Cannot change directory to %s\n", s );
486 ASSERT(!( fname == NULL ),
"NULL pointer as argument of function xremove()\n");
487 if( access( fname , F_OK ) == 0 )
489 rc =
remove( fname );
490 INPUT_CHECK(!( rc != 0 ),
"Cannot remove file %s\n", fname );
493 xprintf(
Warn,
"File '%s' does not exist, can not remove. Ignoring.\n", fname );
508 rc = getcwd( tmp, PATH_MAX );
509 ASSERT( rc,
"Cannot get name of current working directory\n");
522 bool skip_to( istream &in,
const string &pattern )
525 if (! in.good())
xprintf(
PrgErr,
"Input stream is not ready for i/o operations. Perhaps missing check about correct open.\n");
527 for(std::string
line; ! in.eof() ; std::getline(in,
line) ) {
529 if ( boost::iequals(
line, pattern ) )
return true;