History

Initial Commit

Ethan Hugg committed 12 years ago

Commit 70e5e621 parent bd509b2308 files · +97095 −-5Browse files

Showing the changes to codec/decoder/plus/src/welsCodecTrace.cpp (1 of 308 files). Show all

Unified · Split

codec/decoder/plus/src/welsCodecTrace.cpp+419−0
@@ -0,0 +1,419 @@
1+/*!
2+ * \copy
3+ * Copyright (c) 2013, Cisco Systems
4+ * All rights reserved.
5+ *
6+ * Redistribution and use in source and binary forms, with or without
7+ * modification, are permitted provided that the following conditions
8+ * are met:
9+ *
10+ * * Redistributions of source code must retain the above copyright
11+ * notice, this list of conditions and the following disclaimer.
12+ *
13+ * * Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in
15+ * the documentation and/or other materials provided with the
16+ * distribution.
17+ *
18+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+ * POSSIBILITY OF SUCH DAMAGE.
30+ *
31+ */
32+
33+#ifdef WIN32
34+#include <windows.h>
35+#include <tchar.h>
36+#endif
37+
38+#include <stdio.h>
39+#include <stdarg.h>
40+#include <string.h>
41+
42+#include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms
43+#include "utils.h"
44+
45+#include "welsCodecTrace.h"
46+#include "utils.h"
47+#if defined LINUX || defined SOLARIS || defined UNIX || defined MACOS //LINUX/SOLARIS/UNIX
48+#include <dlfcn.h>
49+#endif
50+
51+#if defined(MACOS)
52+#include <carbon/carbon.h>
53+#include <CoreFoundation/CFBundle.h>
54+#endif//MACOS
55+
56+//using namespace WelsDec;
57+
58+namespace WelsDec {
59+
60+#ifdef MACOS
61+static CFBundleRef LoadLibrary(const char* lpszbundle)
62+{
63+ // 1.get bundle path
64+ char cBundlePath[PATH_MAX];
65+ memset(cBundlePath, 0, PATH_MAX);
66+
67+ Dl_info dlInfo;
68+ static int sDummy;
69+ dladdr((void_t*)&sDummy, &dlInfo);
70+
71+ strlcpy(cBundlePath, dlInfo.dli_fname, PATH_MAX);
72+
73+ char * pPath = NULL;
74+ for(int i = 4; i > 0; i--)
75+ {
76+ pPath = strrchr(cBundlePath,'/');//confirmed_safe_unsafe_usage
77+ if(pPath)
78+ {
79+ *pPath = 0;
80+ }
81+ else
82+ {
83+ break;
84+ }
85+ }
86+ if(pPath)
87+ {
88+ strlcat(cBundlePath, "/", PATH_MAX);
89+ }
90+ else
91+ {
92+ return NULL;
93+ }
94+
95+ strlcat(cBundlePath, lpszbundle, PATH_MAX);
96+
97+ FSRef bundlePath;
98+ OSStatus iStatus = FSPathMakeRef((unsigned char*)cBundlePath, &bundlePath, NULL);
99+ if(noErr != iStatus)
100+ return NULL;
101+
102+ CFURLRef bundleURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &bundlePath);
103+ if(NULL == bundleURL)
104+ return NULL;
105+
106+ // 2.get bundle ref
107+ CFBundleRef bundleRef = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
108+ CFRelease(bundleURL);
109+
110+// Boolean bReturn = FALSE;
111+ if(NULL != bundleRef)
112+ {
113+ // bReturn = CFBundleLoadExecutable(bundleRef);
114+ }
115+
116+ return bundleRef;
117+}
118+
119+static Boolean FreeLibrary(CFBundleRef bundle)
120+{
121+ if(NULL != bundle)
122+ {
123+ // CFBundleUnloadExecutable(bundle);
124+ CFRelease(bundle);
125+ }
126+
127+ return TRUE;
128+}
129+
130+static void_t* GetProcessAddress(CFBundleRef bundle, const char* lpszprocname)
131+{
132+ if(NULL == bundle)
133+ return NULL;
134+
135+ CFStringRef cfprocname = CFStringCreateWithCString(NULL,lpszprocname,CFStringGetSystemEncoding());
136+ void_t *processAddress = CFBundleGetFunctionPointerForName(bundle,cfprocname);
137+ CFRelease(cfprocname);
138+
139+ return processAddress;
140+}
141+#endif
142+
143+
144+
145+int32_t CWelsTraceBase::SetTraceLevel(int iLevel)
146+{
147+ m_iLevel = iLevel;
148+
149+ return 0;
150+}
151+
152+int32_t CWelsTraceBase::Trace(const int kLevel, const str_t *kpFormat, va_list pVl)
153+{
154+ if( kLevel & m_iLevel ){
155+ str_t chWStrFormat[MAX_LOG_SIZE] = {0};
156+ str_t chBuf[MAX_LOG_SIZE] = {0};
157+ str_t chResult[MAX_LOG_SIZE] = {0};
158+ const int32_t kLen = WelsStrnlen((const str_t *)"[DECODER]: ", MAX_LOG_SIZE);
159+
160+ WelsStrncpy(chWStrFormat, MAX_LOG_SIZE, (const str_t *)kpFormat, WelsStrnlen((const str_t *)kpFormat, MAX_LOG_SIZE));
161+
162+ WelsStrncpy(chBuf, MAX_LOG_SIZE, (const str_t *)"[DECODER]: ", kLen);
163+
164+ WelsVsprintf((chBuf + kLen), MAX_LOG_SIZE - kLen, (const str_t *)kpFormat, pVl);
165+ WelsStrncpy(chResult, MAX_LOG_SIZE, (const str_t *)chBuf, WelsStrnlen((const str_t *)chBuf, MAX_LOG_SIZE));
166+
167+ WriteString(kLevel, chResult);
168+ }
169+
170+ return 0;
171+}
172+
173+CWelsTraceFile::CWelsTraceFile(const str_t * pFileName)
174+{
175+ m_pTraceFile = WelsFopen(pFileName, (const str_t *)"wt");
176+}
177+
178+CWelsTraceFile::~CWelsTraceFile()
179+{
180+ if( m_pTraceFile ){
181+ WelsFclose(m_pTraceFile);
182+ m_pTraceFile = NULL;
183+ }
184+}
185+
186+int32_t CWelsTraceFile::WriteString(int32_t iLevel, const str_t * pStr)
187+{
188+ int iRC = 0;
189+ const static str_t chEnter[16] = "\n";
190+ if( m_pTraceFile ){
191+ iRC += WelsFwrite(pStr, 1, WelsStrnlen(pStr, MAX_LOG_SIZE), m_pTraceFile);
192+ iRC += WelsFwrite(chEnter, 1, WelsStrnlen(chEnter, 16), m_pTraceFile);
193+ WelsFflush(m_pTraceFile);
194+ }
195+ return iRC;
196+}
197+
198+
199+#ifdef WIN32
200+
201+int32_t CWelsTraceWinDgb::WriteString(int32_t iLevel, const str_t * pStr)
202+{
203+ OutputDebugStringA(pStr);
204+
205+ return WelsStrnlen(pStr, MAX_LOG_SIZE);//strnlen(pStr, MAX_LOG_SIZE);
206+}
207+
208+#endif
209+
210+CWelsCodecTrace::CWelsCodecTrace()
211+{
212+ m_hTraceHandle = NULL;
213+ m_fpDebugTrace = NULL;
214+ m_fpInfoTrace = NULL;
215+ m_fpWarnTrace = NULL;
216+ m_fpErrorTrace = NULL;
217+
218+ LoadWelsTraceModule();
219+}
220+
221+CWelsCodecTrace::~CWelsCodecTrace()
222+{
223+ UnloadWelsTraceModule();
224+}
225+
226+int32_t CWelsCodecTrace::LoadWelsTraceModule()
227+{
228+#if defined WIN32
229+ HMODULE hHandle = ::LoadLibrary("welstrace.dll");
230+// HMODULE handle = ::LoadLibrary("contrace.dll"); // for c7 trace
231+ if ( NULL == hHandle )
232+ return -1;
233+
234+ CHAR chPath[ _MAX_PATH]= {0};
235+ GetModuleFileName( (HMODULE)hHandle, chPath, _MAX_PATH);
236+
237+ m_hTraceHandle = ::LoadLibrary(chPath);
238+
239+ OutputDebugStringA(chPath);
240+ if( m_hTraceHandle) {
241+ m_fpDebugTrace = ( CM_WELS_TRACE)::GetProcAddress( ( HMODULE)m_hTraceHandle, "WELSDEBUGA");
242+ m_fpInfoTrace = ( CM_WELS_TRACE)::GetProcAddress( ( HMODULE)m_hTraceHandle, "WELSINFOA");
243+ m_fpWarnTrace = ( CM_WELS_TRACE)::GetProcAddress( ( HMODULE)m_hTraceHandle, "WELSWARNA");
244+ m_fpErrorTrace = ( CM_WELS_TRACE)::GetProcAddress( ( HMODULE)m_hTraceHandle, "WELSERRORA");
245+ }
246+
247+ // coverity scan uninitial
248+ if (hHandle != NULL)
249+ {
250+ ::FreeLibrary(hHandle);
251+ hHandle = NULL;
252+ }
253+#elif defined MACOS
254+ m_hTraceHandle = LoadLibrary("welstrace.bundle");
255+ if(m_hTraceHandle) {
256+ m_fpDebugTrace = ( CM_WELS_TRACE)GetProcessAddress( (CFBundleRef)m_hTraceHandle, "WELSDEBUG2");
257+ m_fpInfoTrace = ( CM_WELS_TRACE)GetProcessAddress( (CFBundleRef)m_hTraceHandle, "WELSINFO2");
258+ m_fpWarnTrace = ( CM_WELS_TRACE)GetProcessAddress( (CFBundleRef)m_hTraceHandle, "WELSWARN2");
259+ m_fpErrorTrace = ( CM_WELS_TRACE)GetProcessAddress( (CFBundleRef)m_hTraceHandle, "WELSERROR2");
260+ }
261+#elif defined LINUX || defined SOLARIS || defined UNIX
262+//#else
263+// CCmString cmPath;
264+ str_t chPath[255]= {0};
265+ Dl_info sDlInfo;
266+ static int iMmTPAddress;
267+ dladdr( &iMmTPAddress, &sDlInfo);
268+
269+ if (NULL == sDlInfo.dli_fname)
270+ return -1;
271+ WelsStrncpy(chPath, 255, (const str_t*)sDlInfo.dli_fname, WelsStrnlen((const str_t*)sDlInfo.dli_fname, 255));
272+ str_t* p = strrchr(chPath, '/');//confirmed_safe_unsafe_usage
273+ if ( NULL == p )
274+ return -1;
275+ const int iLenTraceName = WelsStrnlen((const str_t*)"/libwelstrace.so", 15);
276+ const int iCurPos = p - chPath;
277+ if ( iCurPos + iLenTraceName < 255 )
278+ WelsStrncpy(p, 254-iCurPos, (const str_t*)"/libwelstrace.so", iLenTraceName );
279+ else
280+ return -1;
281+
282+ m_hTraceHandle = dlopen( chPath, RTLD_LAZY);
283+ if (m_hTraceHandle == NULL)
284+ {
285+ WelsFileHandle* fp = WelsFopen((const str_t*)"/tmp/trace.txt", (const str_t*)"a");
286+ if(fp)
287+ {
288+ fprintf(fp, "welsCodecTrace::welsCodecTrace ===> dlopen %s fail, %s\n", chPath, dlerror());
289+ WelsFclose(fp);
290+ }
291+ return -1;
292+ }
293+ if (m_hTraceHandle) {
294+ m_fpDebugTrace = ( CM_WELS_TRACE)dlsym( m_hTraceHandle, "WELSDEBUG2");
295+ m_fpInfoTrace = ( CM_WELS_TRACE)dlsym( m_hTraceHandle, "WELSINFO2");
296+ m_fpWarnTrace = ( CM_WELS_TRACE)dlsym( m_hTraceHandle, "WELSWARN2");
297+ m_fpErrorTrace = ( CM_WELS_TRACE)dlsym( m_hTraceHandle, "WELSERROR2");
298+ if(m_fpDebugTrace == NULL)
299+ {
300+ WelsFileHandle* fp = WelsFopen((const str_t*)"/tmp/trace.txt", (const str_t*)"a");
301+ if(fp)
302+ {
303+ printf("welsCodecTrace::welsCodecTrace ===> dlsym failed (WELSDEBUG2) , dlerror = %s\n", dlerror());
304+ WelsFclose(fp);
305+ }
306+ return -1;
307+ }
308+ }
309+#endif
310+ return 0;
311+}
312+
313+int32_t CWelsCodecTrace::UnloadWelsTraceModule()
314+{
315+#if defined WIN32
316+ if( m_hTraceHandle) {
317+ ::FreeLibrary( ( HMODULE)m_hTraceHandle);
318+ }
319+#elif defined MACOS
320+ if (m_hTraceHandle) {
321+ FreeLibrary( (CFBundleRef)m_hTraceHandle);
322+ }
323+#elif defined LINUX || defined SOLARIS || defined UNIX
324+ if (m_hTraceHandle) {
325+ ::dlclose( m_hTraceHandle);
326+ }
327+#endif
328+
329+ m_hTraceHandle = NULL;
330+ m_fpDebugTrace = NULL;
331+ m_fpInfoTrace = NULL;
332+ m_fpWarnTrace = NULL;
333+ m_fpErrorTrace = NULL;
334+ return 0;
335+}
336+
337+int32_t CWelsCodecTrace::WriteString(int32_t iLevel, const str_t * pStr)
338+{
339+ if( m_hTraceHandle )
340+ {
341+#ifdef WIN32
342+ switch(iLevel)
343+ {
344+ case WELS_LOG_ERROR:
345+ if(m_fpErrorTrace)
346+ m_fpErrorTrace("%s", pStr);
347+ break;
348+ case WELS_LOG_WARNING:
349+ if(m_fpWarnTrace)
350+ m_fpWarnTrace("%s", pStr);
351+ break;
352+ case WELS_LOG_INFO:
353+ if(m_fpInfoTrace)
354+ m_fpInfoTrace("%s", pStr);
355+ break;
356+ case WELS_LOG_DEBUG:
357+ if(m_fpDebugTrace)
358+ m_fpDebugTrace("%s", pStr);
359+ break;
360+ default:
361+ if(m_fpDebugTrace)
362+ m_fpInfoTrace("%s", pStr);
363+ break;
364+ }
365+#else
366+ switch(iLevel)
367+ {
368+ case WELS_LOG_ERROR:
369+ if(m_fpErrorTrace)
370+ m_fpErrorTrace("CODEC", "%s", pStr);
371+ break;
372+ case WELS_LOG_WARNING:
373+ if(m_fpWarnTrace)
374+ m_fpWarnTrace("CODEC", "%s", pStr);
375+ break;
376+ case WELS_LOG_INFO:
377+ if(m_fpInfoTrace)
378+ m_fpInfoTrace("CODEC", "%s", pStr);
379+ break;
380+ case WELS_LOG_DEBUG:
381+ if(m_fpInfoTrace)
382+ m_fpInfoTrace("CODEC", "%s", pStr);
383+ break;
384+ default:
385+ if(m_fpInfoTrace)
386+ m_fpInfoTrace("CODEC", "%s", pStr);
387+ break;
388+ }
389+#endif
390+ }
391+
392+ return 0;
393+}
394+
395+
396+IWelsTrace * CreateWelsTrace(EWelsTraceType eType, void_t * pParam)
397+{
398+ IWelsTrace * pTrace = NULL;
399+ switch(eType)
400+ {
401+ case Wels_Trace_Type:
402+ pTrace = new CWelsCodecTrace();
403+ break;
404+ case Wels_Trace_Type_File:
405+ pTrace = new CWelsTraceFile();
406+ break;
407+#ifdef WIN32
408+ case Wels_Trace_Type_WinDgb:
409+ pTrace = new CWelsTraceWinDgb();
410+ break;
411+#endif
412+ default:
413+ break;
414+ }
415+
416+ return pTrace;
417+}
418+
419+} // namespace WelsDec

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t