From: kgv Date: Fri, 14 Feb 2020 18:27:58 +0000 (+0300) Subject: 0031206: Foundation Classes, Message_PrinterSystemLog - log messages to Browser conso... X-Git-Tag: V7_5_0_beta~274 X-Git-Url: http://git.dev.opencascade.org/gitweb/?p=occt.git;a=commitdiff_plain;h=b380b06c5db8e55ae8ca05eddba4df5e5ff92970 0031206: Foundation Classes, Message_PrinterSystemLog - log messages to Browser console within Emscripten Message_PrinterOStream::SetConsoleTextColor() skips color tags in case of Emscripten. Message_PrinterSystemLog now implements log via emscripten_log(). --- diff --git a/samples/webgl/main.cpp b/samples/webgl/main.cpp index 44206bb0cc..8f55322501 100644 --- a/samples/webgl/main.cpp +++ b/samples/webgl/main.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -56,6 +57,8 @@ static void onFileReadFailed (void* theOpaque) int main() { Message::DefaultMessenger()->Printers().First()->SetTraceLevel (Message_Trace); + Handle(Message_PrinterSystemLog) aJSConsolePrinter = new Message_PrinterSystemLog ("webgl-sample", Message_Trace); + Message::DefaultMessenger()->AddPrinter (aJSConsolePrinter); // open JavaScript console within the Browser to see this output Message::DefaultMessenger()->Send (TCollection_AsciiString("NbLogicalProcessors: ") + OSD_Parallel::NbLogicalProcessors(), Message_Trace); aViewer.run(); Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace); diff --git a/src/Message/Message_PrinterOStream.cxx b/src/Message/Message_PrinterOStream.cxx index e28b23909b..ce7bb1ec35 100644 --- a/src/Message/Message_PrinterOStream.cxx +++ b/src/Message/Message_PrinterOStream.cxx @@ -243,6 +243,12 @@ void Message_PrinterOStream::SetConsoleTextColor (Standard_OStream* theOStream, } SetConsoleTextAttribute (anStdOut, aFlags); } +#elif defined(__EMSCRIPTEN__) + // Terminal capabilities are undefined on this platform. + // std::cout could be redirected to HTML page, into terminal or somewhere else. + (void )theOStream; + (void )theTextColor; + (void )theIsIntenseText; #else if (theOStream == NULL) { diff --git a/src/Message/Message_PrinterSystemLog.cxx b/src/Message/Message_PrinterSystemLog.cxx index 8d2be63398..b87d4b410d 100644 --- a/src/Message/Message_PrinterSystemLog.cxx +++ b/src/Message/Message_PrinterSystemLog.cxx @@ -54,6 +54,35 @@ } return ANDROID_LOG_DEBUG; } +#elif defined(__EMSCRIPTEN__) + #include + + // actual version of Emscripten does not define these yet + #ifndef EM_LOG_INFO + #define EM_LOG_INFO 0 + #endif + #ifndef EM_LOG_DEBUG + #define EM_LOG_DEBUG 0 + #endif + + //! Convert message gravity into emscripten_log() flags. + static int getEmscriptenPriority (const Message_Gravity theGravity) + { + switch (theGravity) + { + case Message_Trace: return EM_LOG_CONSOLE | EM_LOG_DEBUG; + case Message_Info: return EM_LOG_CONSOLE | EM_LOG_INFO; + case Message_Warning: return EM_LOG_CONSOLE | EM_LOG_WARN; + case Message_Alarm: return EM_LOG_CONSOLE | EM_LOG_ERROR; + case Message_Fail: return EM_LOG_CONSOLE | EM_LOG_ERROR; + } + return EM_LOG_CONSOLE; + } + + //! Print message to console.debug(). + EM_JS(void, debugMsgToConsole, (const char* theStr), { + console.debug(UTF8ToString(theStr)); + }); #else #include @@ -90,6 +119,8 @@ Message_PrinterSystemLog::Message_PrinterSystemLog (const TCollection_AsciiStrin myEventSource = (Standard_Address )RegisterEventSourceW (NULL, aWideSrcName.ToWideString()); #elif defined(__ANDROID__) // +#elif defined(__EMSCRIPTEN__) + // #else openlog (myEventSourceName.ToCString(), LOG_PID | LOG_NDELAY, LOG_USER); #endif @@ -110,6 +141,8 @@ Message_PrinterSystemLog::~Message_PrinterSystemLog() } #elif defined(__ANDROID__) // +#elif defined(__EMSCRIPTEN__) + // #else closelog(); #endif @@ -132,6 +165,15 @@ void Message_PrinterSystemLog::Send (const Standard_CString theString, Send (TCollection_ExtendedString (theString), theGravity, true); #elif defined(__ANDROID__) __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString); +#elif defined(__EMSCRIPTEN__) + if (theGravity == Message_Trace) + { + debugMsgToConsole (theString); + } + else + { + emscripten_log (getEmscriptenPriority (theGravity), "%s", theString); + } #else syslog (getSysLogPriority (theGravity), "%s", theString); #endif @@ -154,6 +196,15 @@ void Message_PrinterSystemLog::Send (const TCollection_AsciiString& theString, Send (TCollection_ExtendedString (theString), theGravity, true); #elif defined(__ANDROID__) __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString.ToCString()); +#elif defined(__EMSCRIPTEN__) + if (theGravity == Message_Trace) + { + debugMsgToConsole (theString.ToCString()); + } + else + { + emscripten_log (getEmscriptenPriority (theGravity), "%s", theString.ToCString()); + } #else syslog (getSysLogPriority (theGravity), "%s", theString.ToCString()); #endif