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);
153 BOOST_THROW_EXCEPTION( ExcXprintfMsg()
154 << EI_XprintfHeader( boost::str(boost::format(mf.
head) % xprintf_file % xprintf_func % xprintf_line) )
155 << EI_XprintfMessage( format_message ) );
163 printf(
"MPI_Barrier() error in xprintf()\n");
164 exit( EXIT_FAILURE );
169 fprintf(screen,
"[msg_id=%d] ", mpi_msg_id );
170 if (mf.
log && sys_info.
log)
171 fprintf(sys_info.
log,
"[msg_id=%d] ", mpi_msg_id );
174 #ifndef DEBUG_MESSAGES
175 if (type ==
Warn) mf.
head=
"\nWarning: ";
179 if (screen) fprintf(screen,mf.
head,xprintf_file,xprintf_func,xprintf_line);
180 if (mf.
log && sys_info.
log)
181 fprintf(sys_info.
log,mf.
head,xprintf_file,xprintf_func,xprintf_line);
187 if (mf.
log && sys_info.
log)
189 va_start( argptr, fmt );
190 rc=vfprintf(sys_info.
log,fmt,argptr);
193 fflush(sys_info.
log);
198 va_start( argptr, fmt );
199 rc=vfprintf(screen,fmt,argptr);
223 if (size == 0 ) size++;
226 if ( rc == NULL )
xprintf(
Err ,
"Not enough memory for allocating %u bytes\n", size );
259 rc = realloc( ptr, size );
260 if ( rc == NULL )
xprintf(
Err ,
"Not enough memory for allocating %u bytes\n", size );
293 void *
operator new[] (std::size_t size,
const std::nothrow_t& )
throw() {
297 void operator delete(
void *p)
throw()
302 void operator delete[](
void *p)
throw()
319 INPUT_CHECK(!( rc != 0 ),
"Error executing external command: %s\n", cmd );
333 ASSERT(!( src == NULL ),
"NULL pointer as argument of function xstrcpy()\n");
334 length = strlen( src ) + 1;
335 rc = (
char*)
xmalloc(length *
sizeof(
char));
349 const char *
const whitespace_delim=
" \t\r\n";
353 rc =
xstrtok( s, whitespace_delim, position);
366 char *
xstrtok(
char *s1,
const char *delim,
int position )
369 static char * full_string = NULL;
370 static int token_count;
374 ASSERT(!( delim == NULL ),
"NULL pointer as delimiter in xstrtok()\n");
381 full_string[0] = 0x0;
388 INPUT_CHECK( token_count == position || position < 0,
"Requested position %d dosn't match token position %d", position, token_count);
389 rc = strtok( s1, delim );
392 INPUT_CHECK(!( rc == NULL ),
"Missing token no. %d: original string '%s' with delimiters '%s'\n", token_count, full_string, delim );
409 ASSERT( s,
"Can not chomp NULL string.");
417 while ((p >= s) && ((*p ==
' ') || (*p ==
'\t') || (*p ==
'\r') || (*p ==
'\n')))
437 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xmkdir()\n");
438 rc = mkdir(s, S_IRWXU);
441 INPUT_CHECK(!( rc != 0 ),
"Cannot make directory %s\n", s );
455 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xrmdir()\n");
457 INPUT_CHECK(!( rc != 0 ),
"Cannot delete directory %s\n", s );
470 ASSERT(!( s == NULL ),
"NULL pointer as argument of function xchdir()\n");
472 INPUT_CHECK(!( rc != 0 ),
"Cannot change directory to %s\n", s );
485 ASSERT(!( fname == NULL ),
"NULL pointer as argument of function xremove()\n");
486 if( access( fname , F_OK ) == 0 )
488 rc =
remove( fname );
489 INPUT_CHECK(!( rc != 0 ),
"Cannot remove file %s\n", fname );
492 xprintf(
Warn,
"File '%s' does not exist, can not remove. Ignoring.\n", fname );
507 rc = getcwd( tmp, PATH_MAX );
508 ASSERT( rc,
"Cannot get name of current working directory\n");
521 bool skip_to( istream &in,
const string &pattern )
524 if (! in.good())
xprintf(
PrgErr,
"Input stream is not ready for i/o operations. Perhaps missing check about correct open.\n");
526 for(std::string
line; ! in.eof() ; std::getline(in,
line) ) {
528 if ( boost::iequals(
line, pattern ) )
return true;