Skip to content

Commit a9850fd

Browse files
authored
Merge pull request #1399 from johnhaddon/messagePrefixes
OStreamMessageHandler : Prefix every line with message level
2 parents e7b5481 + 06a7086 commit a9850fd

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/IECore/OStreamMessageHandler.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,24 @@ OStreamMessageHandler::~OStreamMessageHandler()
6767

6868
void OStreamMessageHandler::handle( Level level, const std::string &context, const std::string &message )
6969
{
70-
*m_stream << levelAsString( level ) << " : " << context << " : " << message << endl;
70+
const string levelString = levelAsString( level );
71+
// Output the message a line at a time.
72+
for( size_t lineBegin = 0; lineBegin < message.size(); )
73+
{
74+
// Find span to the next newline.
75+
const size_t f = message.find( '\n', lineBegin );
76+
const size_t lineEnd = f == string::npos ? message.size() : f;
77+
// Prefix every line with the level
78+
*m_stream << levelString << " : ";
79+
// Only prefix the first line with the context
80+
if( lineBegin == 0 )
81+
{
82+
*m_stream << context << " : ";
83+
}
84+
// Output line and set up for next one.
85+
*m_stream << std::string_view( message.data() + lineBegin, lineEnd - lineBegin ) << endl;
86+
lineBegin = lineEnd + 1;
87+
}
7188
}
7289

7390
///////////////////////////////////////////////////////////////////////////////////////

test/IECore/MessageHandler.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434

3535
from __future__ import with_statement
3636
import gc
37+
import inspect
3738
import unittest
39+
import pathlib
40+
import subprocess
41+
import sys
3842
import threading
3943
import time
4044
import weakref
@@ -266,5 +270,26 @@ def handle( self, level, context, msg ):
266270
# `ExceptionAlgo::translatePythonException()`.
267271
self.assertEqual( [ o for o in gc.get_objects() if isinstance( o, Exception ) ], [] )
268272

273+
def testOStreamLineSplitting( self ) :
274+
275+
output = subprocess.check_output(
276+
[ sys.executable, str( pathlib.Path( __file__ ).parent / "scripts" / "messages.py" ) ],
277+
text = True, stderr = subprocess.STDOUT
278+
)
279+
280+
self.assertEqual(
281+
output.split( "\n" ),
282+
[
283+
"ERROR : Context : Simple message",
284+
"ERROR : Context : Two line",
285+
"ERROR : message",
286+
"ERROR : Context : Terminating newline",
287+
"ERROR : Context : Blank",
288+
"ERROR : ",
289+
"ERROR : lines",
290+
"",
291+
]
292+
)
293+
269294
if __name__ == "__main__":
270295
unittest.main()

test/IECore/scripts/messages.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import IECore
2+
3+
IECore.msg( IECore.Msg.Level.Error, "Context", "Simple message" )
4+
IECore.msg( IECore.Msg.Level.Error, "Context", "Two line\nmessage" )
5+
IECore.msg( IECore.Msg.Level.Error, "Context", "Terminating newline\n" )
6+
IECore.msg( IECore.Msg.Level.Error, "Context", "Blank\n\nlines" )

0 commit comments

Comments
 (0)