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 //! Print message to console.debug().
61 EM_JS(void, occJSConsoleDebug, (const char* theStr), {
62 console.debug(UTF8ToString(theStr));
65 //! Print message to console.info().
66 EM_JS(void, occJSConsoleInfo, (const char* theStr), {
67 console.info(UTF8ToString(theStr));
70 //! Print message to console.warn().
71 EM_JS(void, occJSConsoleWarn, (const char* theStr), {
72 console.warn(UTF8ToString(theStr));
75 //! Print message to console.error().
76 EM_JS(void, occJSConsoleError, (const char* theStr), {
77 console.error(UTF8ToString(theStr));
82 //! Convert message gravity into syslog() enumeration.
83 static int getSysLogPriority (const Message_Gravity theGravity)
87 case Message_Trace: return LOG_DEBUG;
88 case Message_Info: return LOG_INFO;
89 case Message_Warning: return LOG_WARNING;
90 case Message_Alarm: return LOG_ERR;
91 case Message_Fail: return LOG_ERR;
97 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterSystemLog, Message_Printer)
99 //=======================================================================
100 //function : Constructor
102 //=======================================================================
103 Message_PrinterSystemLog::Message_PrinterSystemLog (const TCollection_AsciiString& theEventSourceName,
104 const Message_Gravity theTraceLevel)
105 : myEventSourceName (theEventSourceName)
107 myTraceLevel = theTraceLevel;
108 #if defined(OCCT_UWP)
109 myEventSource = NULL;
110 #elif defined(_WIN32)
111 const TCollection_ExtendedString aWideSrcName (theEventSourceName);
112 myEventSource = (Standard_Address )RegisterEventSourceW (NULL, aWideSrcName.ToWideString());
113 #elif defined(__ANDROID__)
115 #elif defined(__EMSCRIPTEN__)
118 openlog (myEventSourceName.ToCString(), LOG_PID | LOG_NDELAY, LOG_USER);
122 //=======================================================================
123 //function : ~Message_PrinterSystemLog
125 //=======================================================================
126 Message_PrinterSystemLog::~Message_PrinterSystemLog()
129 if (myEventSource != NULL)
131 #if !defined(OCCT_UWP)
132 DeregisterEventSource ((HANDLE )myEventSource);
135 #elif defined(__ANDROID__)
137 #elif defined(__EMSCRIPTEN__)
144 //=======================================================================
147 //=======================================================================
148 void Message_PrinterSystemLog::send (const TCollection_AsciiString& theString,
149 const Message_Gravity theGravity) const
151 if (theGravity < myTraceLevel)
157 if (myEventSource != NULL)
159 #if !defined(OCCT_UWP)
160 const TCollection_ExtendedString aWideString (theString);
161 const WORD aLogType = getEventLogPriority (theGravity);
162 const wchar_t* aMessage[1] = { aWideString.ToWideString() };
163 ReportEventW ((HANDLE )myEventSource, aLogType, 0, 0, NULL,
164 1, 0, aMessage, NULL);
169 #elif defined(__ANDROID__)
170 __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString.ToCString());
171 #elif defined(__EMSCRIPTEN__)
172 // don't use bogus emscripten_log() corrupting UNICODE strings
175 case Message_Trace: occJSConsoleDebug(theString.ToCString()); return;
176 case Message_Info: occJSConsoleInfo (theString.ToCString()); return;
177 case Message_Warning: occJSConsoleWarn (theString.ToCString()); return;
178 case Message_Alarm: occJSConsoleError(theString.ToCString()); return;
179 case Message_Fail: occJSConsoleError(theString.ToCString()); return;
181 occJSConsoleWarn (theString.ToCString());
183 syslog (getSysLogPriority (theGravity), "%s", theString.ToCString());