kodi
NeptuneLoggingBridge.h
1 /*****************************************************************
2 |
3 | Platinum - Managed NeptuneLoggingBridge
4 |
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
13 |
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 |
21 | This program is distributed in the hope that it will be useful,
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | GNU General Public License for more details.
25 |
26 | You should have received a copy of the GNU General Public License
27 | along with this program; see the file LICENSE.txt. If not, write to
28 | the Free Software Foundation, Inc.,
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 | http://www.gnu.org/licenses/gpl-2.0.html
31 |
32 ****************************************************************/
33 #pragma once
34 
35 using namespace log4net;
36 
37 namespace Platinum
38 {
39 
40 /*----------------------------------------------------------------------
41 | NeptuneLoggingBridge
42 +---------------------------------------------------------------------*/
44 {
45 private:
46 
47  NPT_Mutex m_SetLoggerNameLock;
48  gcroot<String^> m_pFormatString;
49  gcroot<ILog^> m_pLogger;
50 
51 public:
52 
53  static void Configure()
54  {
55  static NPT_Mutex lock;
56 
57  lock.Lock();
58 
60 
61  // clear config
62  NPT_LogManager::GetDefault().Configure("plist:.level=ALL;.handlers=;platinum.level=ALL;platinum.handlers=");
63 
64  // get root logger
65  NPT_Logger* rootLogger = NPT_LogManager::GetLogger("platinum");
66 
67  if (rootLogger)
68  {
69  // set handler
70  rootLogger->AddHandler(&instance, false);
71  }
72 
73  lock.Unlock();
74  }
75 
76 public:
77 
78  virtual void Log(const NPT_LogRecord& record)
79  {
80  gcroot<ILog^> log = SetLoggerName(record.m_LoggerName);
81 
82  switch (record.m_Level)
83  {
84  case NPT_LOG_LEVEL_FATAL:
85  if (log->IsFatalEnabled)
86  {
87  log->FatalFormat(
88  m_pFormatString,
89  marshal_as<String^>(NPT_Log::GetLogLevelName(record.m_Level)),
90  marshal_as<String^>(record.m_Message),
91  marshal_as<String^>(record.m_SourceFile),
92  UInt32(record.m_SourceLine)
93  );
94  }
95 
96  break;
97 
98  case NPT_LOG_LEVEL_SEVERE:
99  if (log->IsErrorEnabled)
100  {
101  log->ErrorFormat(
102  m_pFormatString,
103  marshal_as<String^>(NPT_Log::GetLogLevelName(record.m_Level)),
104  marshal_as<String^>(record.m_Message),
105  marshal_as<String^>(record.m_SourceFile),
106  UInt32(record.m_SourceLine)
107  );
108  }
109 
110  break;
111 
112  case NPT_LOG_LEVEL_WARNING:
113  if (log->IsWarnEnabled)
114  {
115  log->WarnFormat(
116  m_pFormatString,
117  marshal_as<String^>(NPT_Log::GetLogLevelName(record.m_Level)),
118  marshal_as<String^>(record.m_Message),
119  marshal_as<String^>(record.m_SourceFile),
120  UInt32(record.m_SourceLine)
121  );
122  }
123 
124  break;
125 
126  case NPT_LOG_LEVEL_INFO:
127  if (log->IsInfoEnabled)
128  {
129  log->InfoFormat(
130  m_pFormatString,
131  marshal_as<String^>(NPT_Log::GetLogLevelName(record.m_Level)),
132  marshal_as<String^>(record.m_Message),
133  marshal_as<String^>(record.m_SourceFile),
134  UInt32(record.m_SourceLine)
135  );
136  }
137 
138  break;
139 
140  case NPT_LOG_LEVEL_FINE:
141  case NPT_LOG_LEVEL_FINER:
142  case NPT_LOG_LEVEL_FINEST:
143  if (log->IsDebugEnabled)
144  {
145  log->DebugFormat(
146  m_pFormatString,
147  marshal_as<String^>(NPT_Log::GetLogLevelName(record.m_Level)),
148  marshal_as<String^>(record.m_Message),
149  marshal_as<String^>(record.m_SourceFile),
150  UInt32(record.m_SourceLine)
151  );
152  }
153 
154  break;
155 
156  default:
157  if (log->IsWarnEnabled)
158  {
159  log->WarnFormat(
160  m_pFormatString,
161  marshal_as<String^>("UNKNOWN_LOG_LEVEL"),
162  marshal_as<String^>(record.m_Message),
163  marshal_as<String^>(record.m_SourceFile),
164  UInt32(record.m_SourceLine)
165  );
166  }
167 
168  break;
169  }
170  }
171 
172 private:
173 
174  gcroot<ILog^> SetLoggerName(const char* name)
175  {
176  m_SetLoggerNameLock.Lock();
177 
178  gcroot<String^> loggerName = gcnew String(name);
179  gcroot<ILog^> logger = m_pLogger;
180 
181  if (logger->Logger->Name != loggerName)
182  {
183  logger = LogManager::GetLogger(loggerName);
184 
185  m_pLogger = logger;
186  }
187 
188  m_SetLoggerNameLock.Unlock();
189 
190  return logger;
191  }
192 
193 public:
194 
196  {
197  m_pLogger = LogManager::GetLogger(gcnew String("NeptuneLoggingBridge"));
198  m_pFormatString = gcnew String("{0}: {2}:{3}: {1}");
199  }
200 
201  virtual ~NeptuneLoggingBridge()
202  {
203  }
204 
205 };
206 
207 }
Definition: NptThreads.h:76
Definition: NptLogging.h:70
Definition: addon_base.h:267
Definition: NeptuneLoggingBridge.h:43
Definition: NptLogging.h:86
Definition: NptLogging.h:58