b87d4b410d82de7340016ee9af81240c3b407bed
[occt.git] / src / Message / Message_PrinterSystemLog.cxx
1 // Copyright (c) 2019 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifdef _WIN32
15   #include <windows.h>
16 #endif
17
18 #include <Message_PrinterSystemLog.hxx>
19
20 #include <TCollection_ExtendedString.hxx>
21
22 #if defined(OCCT_UWP)
23   //
24 #elif defined(_WIN32)
25   //! Convert message gravity into EventLog enumeration.
26   static WORD getEventLogPriority (const Message_Gravity theGravity)
27   {
28     switch (theGravity)
29     {
30       case Message_Alarm:
31       case Message_Fail:
32         return EVENTLOG_ERROR_TYPE;
33       case Message_Warning:
34         return EVENTLOG_WARNING_TYPE;
35       case Message_Info:
36       case Message_Trace:
37         return EVENTLOG_INFORMATION_TYPE;
38     }
39     return EVENTLOG_INFORMATION_TYPE;
40   }
41 #elif defined(__ANDROID__)
42   #include <android/log.h>
43
44   //! Convert message gravity into Android log enumeration.
45   static android_LogPriority getAndroidLogPriority (const Message_Gravity theGravity)
46   {
47     switch (theGravity)
48     {
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;
54     }
55     return ANDROID_LOG_DEBUG;
56   }
57 #elif defined(__EMSCRIPTEN__)
58   #include <emscripten/emscripten.h>
59
60   // actual version of Emscripten does not define these yet
61   #ifndef EM_LOG_INFO
62     #define EM_LOG_INFO 0
63   #endif
64   #ifndef EM_LOG_DEBUG
65     #define EM_LOG_DEBUG 0
66   #endif
67
68   //! Convert message gravity into emscripten_log() flags.
69   static int getEmscriptenPriority (const Message_Gravity theGravity)
70   {
71     switch (theGravity)
72     {
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;
78     }
79     return EM_LOG_CONSOLE;
80   }
81
82   //! Print message to console.debug().
83   EM_JS(void, debugMsgToConsole, (const char* theStr), {
84     console.debug(UTF8ToString(theStr));
85   });
86 #else
87   #include <syslog.h>
88
89   //! Convert message gravity into syslog() enumeration.
90   static int getSysLogPriority (const Message_Gravity theGravity)
91   {
92     switch (theGravity)
93     {
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;
99     }
100     return LOG_DEBUG;
101   }
102 #endif
103
104 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterSystemLog, Message_Printer)
105
106 //=======================================================================
107 //function : Constructor
108 //purpose  :
109 //=======================================================================
110 Message_PrinterSystemLog::Message_PrinterSystemLog (const TCollection_AsciiString& theEventSourceName,
111                                                     const Message_Gravity theTraceLevel)
112 : myEventSourceName (theEventSourceName)
113 {
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__)
121   //
122 #elif defined(__EMSCRIPTEN__)
123   //
124 #else
125   openlog (myEventSourceName.ToCString(), LOG_PID | LOG_NDELAY, LOG_USER);
126 #endif
127 }
128
129 //=======================================================================
130 //function : ~Message_PrinterSystemLog
131 //purpose  :
132 //=======================================================================
133 Message_PrinterSystemLog::~Message_PrinterSystemLog()
134 {
135 #if defined(_WIN32)
136   if (myEventSource != NULL)
137   {
138   #if !defined(OCCT_UWP)
139     DeregisterEventSource ((HANDLE )myEventSource);
140   #endif
141   }
142 #elif defined(__ANDROID__)
143   //
144 #elif defined(__EMSCRIPTEN__)
145   //
146 #else
147   closelog();
148 #endif
149 }
150
151 //=======================================================================
152 //function : Send
153 //purpose  :
154 //=======================================================================
155 void Message_PrinterSystemLog::Send (const Standard_CString theString,
156                                      const Message_Gravity theGravity,
157                                      const Standard_Boolean ) const
158 {
159   if (theGravity < myTraceLevel)
160   {
161     return;
162   }
163
164 #if defined(_WIN32)
165   Send (TCollection_ExtendedString (theString), theGravity, true);
166 #elif defined(__ANDROID__)
167   __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString);
168 #elif defined(__EMSCRIPTEN__)
169   if (theGravity == Message_Trace)
170   {
171     debugMsgToConsole (theString);
172   }
173   else
174   {
175     emscripten_log (getEmscriptenPriority (theGravity), "%s", theString);
176   }
177 #else
178   syslog (getSysLogPriority (theGravity), "%s", theString);
179 #endif
180 }
181
182 //=======================================================================
183 //function : Send
184 //purpose  :
185 //=======================================================================
186 void Message_PrinterSystemLog::Send (const TCollection_AsciiString& theString,
187                                      const Message_Gravity theGravity,
188                                      const Standard_Boolean ) const
189 {
190   if (theGravity < myTraceLevel)
191   {
192     return;
193   }
194
195 #if defined(_WIN32)
196   Send (TCollection_ExtendedString (theString), theGravity, true);
197 #elif defined(__ANDROID__)
198   __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString.ToCString());
199 #elif defined(__EMSCRIPTEN__)
200   if (theGravity == Message_Trace)
201   {
202     debugMsgToConsole (theString.ToCString());
203   }
204   else
205   {
206     emscripten_log (getEmscriptenPriority (theGravity), "%s", theString.ToCString());
207   }
208 #else
209   syslog (getSysLogPriority (theGravity), "%s", theString.ToCString());
210 #endif
211 }
212
213 //=======================================================================
214 //function : Send
215 //purpose  :
216 //=======================================================================
217 void Message_PrinterSystemLog::Send (const TCollection_ExtendedString& theString,
218                                      const Message_Gravity theGravity,
219                                      const Standard_Boolean ) const
220 {
221   if (theGravity < myTraceLevel)
222   {
223     return;
224   }
225
226 #if defined(_WIN32)
227   if (myEventSource != NULL)
228   {
229   #if !defined(OCCT_UWP)
230     const WORD aLogType = getEventLogPriority (theGravity);
231     const wchar_t* aMessage[1] = { theString.ToWideString() };
232     ReportEventW ((HANDLE )myEventSource, aLogType, 0, 0, NULL,
233                   1, 0, aMessage, NULL);
234   #else
235     (void )theString;
236   #endif
237   }
238 #else
239   Send (TCollection_AsciiString (theString), theGravity, true);
240 #endif
241 }