Skip to content

Commit a6892e0

Browse files
authored
Merge pull request #3420 from heplesser/move-message-critical
Move critical section so it can be avoided by limiting verbosity.
2 parents dc6789a + 8e23122 commit a6892e0

File tree

1 file changed

+115
-119
lines changed

1 file changed

+115
-119
lines changed

sli/interpret.cc

Lines changed: 115 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -773,54 +773,45 @@ SLIInterpreter::terminate( int returnvalue )
773773
void
774774
SLIInterpreter::message( int level, const char from[], const char text[], const char errorname[] ) const
775775
{
776-
// Only one thread may write at a time.
777-
#ifdef _OPENMP
778-
#pragma omp critical( message )
776+
if ( level >= verbositylevel )
779777
{
780-
#endif
781-
if ( level >= verbositylevel )
778+
if ( level >= M_FATAL )
782779
{
783-
if ( level >= M_FATAL )
784-
{
785-
message( std::cout, M_FATAL_NAME, from, text, errorname );
786-
}
787-
else if ( level >= M_ERROR )
788-
{
789-
message( std::cout, M_ERROR_NAME, from, text, errorname );
790-
}
791-
else if ( level >= M_WARNING )
792-
{
793-
message( std::cout, M_WARNING_NAME, from, text, errorname );
794-
}
795-
else if ( level >= M_DEPRECATED )
796-
{
797-
message( std::cout, M_DEPRECATED_NAME, from, text, errorname );
798-
}
799-
else if ( level >= M_PROGRESS )
800-
{
801-
message( std::cout, M_PROGRESS_NAME, from, text, errorname );
802-
}
803-
else if ( level >= M_INFO )
804-
{
805-
message( std::cout, M_INFO_NAME, from, text, errorname );
806-
}
807-
else if ( level >= M_STATUS )
808-
{
809-
message( std::cout, M_STATUS_NAME, from, text, errorname );
810-
}
811-
else if ( level >= M_DEBUG )
812-
{
813-
message( std::cout, M_DEBUG_NAME, from, text, errorname );
814-
}
815-
else
816-
{
817-
message( std::cout, M_ALL_NAME, from, text, errorname );
818-
}
780+
message( std::cout, M_FATAL_NAME, from, text, errorname );
781+
}
782+
else if ( level >= M_ERROR )
783+
{
784+
message( std::cout, M_ERROR_NAME, from, text, errorname );
785+
}
786+
else if ( level >= M_WARNING )
787+
{
788+
message( std::cout, M_WARNING_NAME, from, text, errorname );
789+
}
790+
else if ( level >= M_DEPRECATED )
791+
{
792+
message( std::cout, M_DEPRECATED_NAME, from, text, errorname );
793+
}
794+
else if ( level >= M_PROGRESS )
795+
{
796+
message( std::cout, M_PROGRESS_NAME, from, text, errorname );
797+
}
798+
else if ( level >= M_INFO )
799+
{
800+
message( std::cout, M_INFO_NAME, from, text, errorname );
801+
}
802+
else if ( level >= M_STATUS )
803+
{
804+
message( std::cout, M_STATUS_NAME, from, text, errorname );
805+
}
806+
else if ( level >= M_DEBUG )
807+
{
808+
message( std::cout, M_DEBUG_NAME, from, text, errorname );
809+
}
810+
else
811+
{
812+
message( std::cout, M_ALL_NAME, from, text, errorname );
819813
}
820-
821-
#ifdef _OPENMP
822814
}
823-
#endif
824815
}
825816

826817
void
@@ -830,94 +821,99 @@ SLIInterpreter::message( std::ostream& out,
830821
const char text[],
831822
const char errorname[] ) const
832823
{
833-
const unsigned buflen = 30;
834-
char timestring[ buflen + 1 ] = "";
835-
const time_t tm = std::time( nullptr );
836-
837-
std::strftime( timestring, buflen, "%b %d %H:%M:%S", std::localtime( &tm ) );
838-
839-
std::string msg = String::compose( "%1 %2 [%3]: ", timestring, from, levelname );
840-
out << std::endl << msg << errorname;
841-
842-
// Set the preferred line indentation.
843-
const size_t indent = 4;
844-
845-
// Get size of the output window. The message text will be
846-
// adapted to the width of the window.
847-
//
848-
// The COLUMNS variable should preferably be extracted
849-
// from the environment dictionary set up by the
850-
// Processes class. getenv("COLUMNS") works only on
851-
// the created NEST executable (not on the messages
852-
// printed by make install).
853-
char const* const columns = std::getenv( "COLUMNS" );
854-
size_t max_width = 78;
855-
if ( columns )
856-
{
857-
max_width = std::atoi( columns );
858-
}
859-
if ( max_width < 3 * indent )
860-
{
861-
max_width = 3 * indent;
862-
}
863-
const size_t width = max_width - indent;
864-
865-
// convert char* to string to be able to use the string functions
866-
std::string text_str( text );
867-
868-
// Indent first message line
869-
if ( text_str.size() != 0 )
824+
// Only one thread may write at a time to ensure tidy output.
825+
#pragma omp critical( message )
870826
{
871-
std::cout << std::endl << std::string( indent, ' ' );
872-
}
827+
const unsigned buflen = 30;
828+
char timestring[ buflen + 1 ] = "";
829+
const time_t tm = std::time( nullptr );
830+
831+
std::strftime( timestring, buflen, "%b %d %H:%M:%S", std::localtime( &tm ) );
832+
833+
std::string msg = String::compose( "%1 %2 [%3]: ", timestring, from, levelname );
834+
out << std::endl << msg << errorname;
835+
836+
// Set the preferred line indentation.
837+
const size_t indent = 4;
838+
839+
// Get size of the output window. The message text will be
840+
// adapted to the width of the window.
841+
//
842+
// The COLUMNS variable should preferably be extracted
843+
// from the environment dictionary set up by the
844+
// Processes class. getenv("COLUMNS") works only on
845+
// the created NEST executable (not on the messages
846+
// printed by make install).
847+
char const* const columns = std::getenv( "COLUMNS" );
848+
size_t max_width = 78;
849+
if ( columns )
850+
{
851+
max_width = std::atoi( columns );
852+
}
853+
if ( max_width < 3 * indent )
854+
{
855+
max_width = 3 * indent;
856+
}
857+
const size_t width = max_width - indent;
873858

874-
size_t pos = 0;
859+
// convert char* to string to be able to use the string functions
860+
std::string text_str( text );
875861

876-
for ( size_t i = 0; i < text_str.size(); ++i )
877-
{
878-
if ( text_str.at( i ) == '\n' and i != text_str.size() - 1 )
862+
// Indent first message line
863+
if ( text_str.size() != 0 )
879864
{
880-
// Print a lineshift followed by an indented whitespace
881-
// Manually inserted lineshift at the end of the message
882-
// are suppressed.
883-
out << std::endl << std::string( indent, ' ' );
884-
pos = 0;
865+
std::cout << std::endl << std::string( indent, ' ' );
885866
}
886-
else
887-
{
888-
// If we've reached the width of the output we'll print
889-
// a lineshift regardless of whether '\n' is found or not.
890-
// The printing is done so that no word splitting occurs.
891-
size_t space = text_str.find( ' ', i ) < text_str.find( '\n' ) ? text_str.find( ' ', i ) : text_str.find( '\n' );
892-
// If no space is found (i.e. the last word) the space
893-
// variable is set to the end of the string.
894-
if ( space == std::string::npos )
895-
{
896-
space = text_str.size();
897-
}
898867

899-
// Start on a new line if the next word is longer than the
900-
// space available (as long as the word is shorter than the
901-
// total width of the printout).
902-
if ( i != 0 and text_str.at( i - 1 ) == ' '
903-
and static_cast< int >( space - i ) > static_cast< int >( width - pos ) )
868+
size_t pos = 0;
869+
870+
for ( size_t i = 0; i < text_str.size(); ++i )
871+
{
872+
if ( text_str.at( i ) == '\n' and i != text_str.size() - 1 )
904873
{
874+
// Print a lineshift followed by an indented whitespace
875+
// Manually inserted lineshift at the end of the message
876+
// are suppressed.
905877
out << std::endl << std::string( indent, ' ' );
906878
pos = 0;
907879
}
908-
909-
// Only print character if we're not at the end of the
910-
// line and the last character is a space.
911-
if ( not( width - pos == 0 and text_str.at( i ) == ' ' ) )
880+
else
912881
{
913-
// Print the actual character.
914-
out << text_str.at( i );
915-
}
882+
// If we've reached the width of the output we'll print
883+
// a lineshift regardless of whether '\n' is found or not.
884+
// The printing is done so that no word splitting occurs.
885+
size_t space =
886+
text_str.find( ' ', i ) < text_str.find( '\n' ) ? text_str.find( ' ', i ) : text_str.find( '\n' );
887+
// If no space is found (i.e. the last word) the space
888+
// variable is set to the end of the string.
889+
if ( space == std::string::npos )
890+
{
891+
space = text_str.size();
892+
}
916893

917-
++pos;
894+
// Start on a new line if the next word is longer than the
895+
// space available (as long as the word is shorter than the
896+
// total width of the printout).
897+
if ( i != 0 and text_str.at( i - 1 ) == ' '
898+
and static_cast< int >( space - i ) > static_cast< int >( width - pos ) )
899+
{
900+
out << std::endl << std::string( indent, ' ' );
901+
pos = 0;
902+
}
903+
904+
// Only print character if we're not at the end of the
905+
// line and the last character is a space.
906+
if ( not( width - pos == 0 and text_str.at( i ) == ' ' ) )
907+
{
908+
// Print the actual character.
909+
out << text_str.at( i );
910+
}
911+
912+
++pos;
913+
}
918914
}
919-
}
920-
out << std::endl;
915+
out << std::endl;
916+
} // #pragma omp critical
921917
}
922918

923919
Name

0 commit comments

Comments
 (0)