e17b7df791690e9362c2ff5e831ecf722c5d4519
[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 TCollection_AsciiString& theString,
156                                      const Message_Gravity theGravity) const
157 {
158   if (theGravity < myTraceLevel)
159   {
160     return;
161   }
162
163 #if defined(_WIN32)
164   if (myEventSource != NULL)
165   {
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);
172   #else
173     (void )theString;
174   #endif
175   }
176 #elif defined(__ANDROID__)
177   __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString.ToCString());
178 #elif defined(__EMSCRIPTEN__)
179   if (theGravity == Message_Trace)
180   {
181     debugMsgToConsole (theString.ToCString());
182   }
183   else
184   {
185     emscripten_log (getEmscriptenPriority (theGravity), "%s", theString.ToCString());
186   }
187 #else
188   syslog (getSysLogPriority (theGravity), "%s", theString.ToCString());
189 #endif
190 }