1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
18 #include <Message_PrinterSystemLog.hxx>
20 #include <TCollection_ExtendedString.hxx>
25 //! Convert message gravity into EventLog enumeration.
26 static WORD getEventLogPriority (const Message_Gravity theGravity)
32 return EVENTLOG_ERROR_TYPE;
34 return EVENTLOG_WARNING_TYPE;
37 return EVENTLOG_INFORMATION_TYPE;
39 return EVENTLOG_INFORMATION_TYPE;
41 #elif defined(__ANDROID__)
42 #include <android/log.h>
44 //! Convert message gravity into Android log enumeration.
45 static android_LogPriority getAndroidLogPriority (const Message_Gravity theGravity)
49 case Message_Trace: return ANDROID_LOG_DEBUG;
50 case Message_Info: return ANDROID_LOG_INFO;
51 case Message_Warning: return ANDROID_LOG_WARN;
52 case Message_Alarm: return ANDROID_LOG_ERROR;
53 case Message_Fail: return ANDROID_LOG_ERROR;
55 return ANDROID_LOG_DEBUG;
57 #elif defined(__EMSCRIPTEN__)
58 #include <emscripten/emscripten.h>
60 // actual version of Emscripten does not define these yet
65 #define EM_LOG_DEBUG 0
68 //! Convert message gravity into emscripten_log() flags.
69 static int getEmscriptenPriority (const Message_Gravity theGravity)
73 case Message_Trace: return EM_LOG_CONSOLE | EM_LOG_DEBUG;
74 case Message_Info: return EM_LOG_CONSOLE | EM_LOG_INFO;
75 case Message_Warning: return EM_LOG_CONSOLE | EM_LOG_WARN;
76 case Message_Alarm: return EM_LOG_CONSOLE | EM_LOG_ERROR;
77 case Message_Fail: return EM_LOG_CONSOLE | EM_LOG_ERROR;
79 return EM_LOG_CONSOLE;
82 //! Print message to console.debug().
83 EM_JS(void, debugMsgToConsole, (const char* theStr), {
84 console.debug(UTF8ToString(theStr));
89 //! Convert message gravity into syslog() enumeration.
90 static int getSysLogPriority (const Message_Gravity theGravity)
94 case Message_Trace: return LOG_DEBUG;
95 case Message_Info: return LOG_INFO;
96 case Message_Warning: return LOG_WARNING;
97 case Message_Alarm: return LOG_ERR;
98 case Message_Fail: return LOG_ERR;
104 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterSystemLog, Message_Printer)
106 //=======================================================================
107 //function : Constructor
109 //=======================================================================
110 Message_PrinterSystemLog::Message_PrinterSystemLog (const TCollection_AsciiString& theEventSourceName,
111 const Message_Gravity theTraceLevel)
112 : myEventSourceName (theEventSourceName)
114 myTraceLevel = theTraceLevel;
115 #if defined(OCCT_UWP)
116 myEventSource = NULL;
117 #elif defined(_WIN32)
118 const TCollection_ExtendedString aWideSrcName (theEventSourceName);
119 myEventSource = (Standard_Address )RegisterEventSourceW (NULL, aWideSrcName.ToWideString());
120 #elif defined(__ANDROID__)
122 #elif defined(__EMSCRIPTEN__)
125 openlog (myEventSourceName.ToCString(), LOG_PID | LOG_NDELAY, LOG_USER);
129 //=======================================================================
130 //function : ~Message_PrinterSystemLog
132 //=======================================================================
133 Message_PrinterSystemLog::~Message_PrinterSystemLog()
136 if (myEventSource != NULL)
138 #if !defined(OCCT_UWP)
139 DeregisterEventSource ((HANDLE )myEventSource);
142 #elif defined(__ANDROID__)
144 #elif defined(__EMSCRIPTEN__)
151 //=======================================================================
154 //=======================================================================
155 void Message_PrinterSystemLog::send (const TCollection_AsciiString& theString,
156 const Message_Gravity theGravity) const
158 if (theGravity < myTraceLevel)
164 if (myEventSource != NULL)
166 #if !defined(OCCT_UWP)
167 const TCollection_ExtendedString aWideString (theString);
168 const WORD aLogType = getEventLogPriority (theGravity);
169 const wchar_t* aMessage[1] = { aWideString.ToWideString() };
170 ReportEventW ((HANDLE )myEventSource, aLogType, 0, 0, NULL,
171 1, 0, aMessage, NULL);
176 #elif defined(__ANDROID__)
177 __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString.ToCString());
178 #elif defined(__EMSCRIPTEN__)
179 if (theGravity == Message_Trace)
181 debugMsgToConsole (theString.ToCString());
185 emscripten_log (getEmscriptenPriority (theGravity), "%s", theString.ToCString());
188 syslog (getSysLogPriority (theGravity), "%s", theString.ToCString());