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;
60 //! Convert message gravity into syslog() enumeration.
61 static int getSysLogPriority (const Message_Gravity theGravity)
65 case Message_Trace: return LOG_DEBUG;
66 case Message_Info: return LOG_INFO;
67 case Message_Warning: return LOG_WARNING;
68 case Message_Alarm: return LOG_ERR;
69 case Message_Fail: return LOG_ERR;
75 IMPLEMENT_STANDARD_RTTIEXT(Message_PrinterSystemLog, Message_Printer)
77 //=======================================================================
78 //function : Constructor
80 //=======================================================================
81 Message_PrinterSystemLog::Message_PrinterSystemLog (const TCollection_AsciiString& theEventSourceName,
82 const Message_Gravity theTraceLevel)
83 : myEventSourceName (theEventSourceName)
85 myTraceLevel = theTraceLevel;
89 const TCollection_ExtendedString aWideSrcName (theEventSourceName);
90 myEventSource = (Standard_Address )RegisterEventSourceW (NULL, aWideSrcName.ToWideString());
91 #elif defined(__ANDROID__)
94 openlog (myEventSourceName.ToCString(), LOG_PID | LOG_NDELAY, LOG_USER);
98 //=======================================================================
99 //function : ~Message_PrinterSystemLog
101 //=======================================================================
102 Message_PrinterSystemLog::~Message_PrinterSystemLog()
105 if (myEventSource != NULL)
107 #if !defined(OCCT_UWP)
108 DeregisterEventSource ((HANDLE )myEventSource);
111 #elif defined(__ANDROID__)
118 //=======================================================================
121 //=======================================================================
122 void Message_PrinterSystemLog::Send (const Standard_CString theString,
123 const Message_Gravity theGravity,
124 const Standard_Boolean ) const
126 if (theGravity < myTraceLevel)
132 Send (TCollection_ExtendedString (theString), theGravity, true);
133 #elif defined(__ANDROID__)
134 __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString);
136 syslog (getSysLogPriority (theGravity), "%s", theString);
140 //=======================================================================
143 //=======================================================================
144 void Message_PrinterSystemLog::Send (const TCollection_AsciiString& theString,
145 const Message_Gravity theGravity,
146 const Standard_Boolean ) const
148 if (theGravity < myTraceLevel)
154 Send (TCollection_ExtendedString (theString), theGravity, true);
155 #elif defined(__ANDROID__)
156 __android_log_write (getAndroidLogPriority (theGravity), myEventSourceName.ToCString(), theString.ToCString());
158 syslog (getSysLogPriority (theGravity), "%s", theString.ToCString());
162 //=======================================================================
165 //=======================================================================
166 void Message_PrinterSystemLog::Send (const TCollection_ExtendedString& theString,
167 const Message_Gravity theGravity,
168 const Standard_Boolean ) const
170 if (theGravity < myTraceLevel)
176 if (myEventSource != NULL)
178 #if !defined(OCCT_UWP)
179 const WORD aLogType = getEventLogPriority (theGravity);
180 const wchar_t* aMessage[1] = { theString.ToWideString() };
181 ReportEventW ((HANDLE )myEventSource, aLogType, 0, 0, NULL,
182 1, 0, aMessage, NULL);
188 Send (TCollection_AsciiString (theString), theGravity, true);