Skip to content

Commit

Permalink
OStreamMessageHandler : Prefix every line with message level
Browse files Browse the repository at this point in the history
This allows log parsers to correctly identify the type of each line for syntax highlighting and the like. The immediate motivation is for a new LocalJobs panel
in Gaffer, that captures the output from LocalDispatcher subprocesses and turns it back into messages for display into a GafferUI.MessagesWidget.
  • Loading branch information
johnhaddon committed Dec 6, 2023
1 parent e7b5481 commit 06a7086
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/IECore/OStreamMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,24 @@ OStreamMessageHandler::~OStreamMessageHandler()

void OStreamMessageHandler::handle( Level level, const std::string &context, const std::string &message )
{
*m_stream << levelAsString( level ) << " : " << context << " : " << message << endl;
const string levelString = levelAsString( level );
// Output the message a line at a time.
for( size_t lineBegin = 0; lineBegin < message.size(); )
{
// Find span to the next newline.
const size_t f = message.find( '\n', lineBegin );
const size_t lineEnd = f == string::npos ? message.size() : f;
// Prefix every line with the level
*m_stream << levelString << " : ";
// Only prefix the first line with the context
if( lineBegin == 0 )
{
*m_stream << context << " : ";
}
// Output line and set up for next one.
*m_stream << std::string_view( message.data() + lineBegin, lineEnd - lineBegin ) << endl;
lineBegin = lineEnd + 1;
}
}

///////////////////////////////////////////////////////////////////////////////////////
Expand Down
25 changes: 25 additions & 0 deletions test/IECore/MessageHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@

from __future__ import with_statement
import gc
import inspect
import unittest
import pathlib
import subprocess
import sys
import threading
import time
import weakref
Expand Down Expand Up @@ -266,5 +270,26 @@ def handle( self, level, context, msg ):
# `ExceptionAlgo::translatePythonException()`.
self.assertEqual( [ o for o in gc.get_objects() if isinstance( o, Exception ) ], [] )

def testOStreamLineSplitting( self ) :

output = subprocess.check_output(
[ sys.executable, str( pathlib.Path( __file__ ).parent / "scripts" / "messages.py" ) ],
text = True, stderr = subprocess.STDOUT
)

self.assertEqual(
output.split( "\n" ),
[
"ERROR : Context : Simple message",
"ERROR : Context : Two line",
"ERROR : message",
"ERROR : Context : Terminating newline",
"ERROR : Context : Blank",
"ERROR : ",
"ERROR : lines",
"",
]
)

if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions test/IECore/scripts/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import IECore

IECore.msg( IECore.Msg.Level.Error, "Context", "Simple message" )
IECore.msg( IECore.Msg.Level.Error, "Context", "Two line\nmessage" )
IECore.msg( IECore.Msg.Level.Error, "Context", "Terminating newline\n" )
IECore.msg( IECore.Msg.Level.Error, "Context", "Blank\n\nlines" )

0 comments on commit 06a7086

Please sign in to comment.