blob: d99f888d8c608479a39584e40f6556fe97a1730f [file] [log] [blame]
The Android Open Source Project2eb6af62009-03-03 19:30:11 -08001/* ------------------------------------------------------------------
2 * Copyright (C) 2008 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18
19#ifndef ANDROID_LOG_APPENDER_H_INCLUDED
20#define ANDROID_LOG_APPENDER_H_INCLUDED
21
22#ifndef PVLOGGERACCESSORIES_H_INCLUDED
23#include "pvlogger_accessories.h"
24#endif
25
26#ifndef OSCLCONFIG_UTIL_H_INCLUDED
27#include "osclconfig_util.h"
28#endif
29
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070030#ifndef OSCL_MEM_H_INCLUDED
31#include "oscl_mem.h"
32#endif
33
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080034#include <utils/Log.h>
35#undef LOG_TAG
36#define LOG_TAG "PV"
37
38/**
39 * Class: AndroidLogAppender
40 *
41 */
42template<class Layout, int32 LayoutBufferSize, class Lock = OsclNullLock>
43class AndroidLogAppender : public PVLoggerAppender {
44public:
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070045 typedef PVLoggerAppender::message_id_type message_id_type;
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080046
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070047 AndroidLogAppender()
48 {
49 stringbuf=NULL;
50 wstringbuf=NULL;
51 }
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080052 virtual ~AndroidLogAppender()
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070053 {
54 if(stringbuf)
55 OSCL_DEFAULT_FREE(stringbuf);
56 if(wstringbuf)
57 OSCL_DEFAULT_FREE((OsclAny*)wstringbuf);
58 }
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080059
60 void AppendString(message_id_type msgID, const char *fmt, va_list va)
61 {
62 _lock.Lock();
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070063
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080064 int32 size;
65
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070066 if(!stringbuf)
67 {
68 stringbuf=(char*)OSCL_DEFAULT_MALLOC(LayoutBufferSize);
69 if(!stringbuf)
70 return;//allocation failed-- just exit gracefully.
71 }
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080072
73 size = _layout.FormatString(stringbuf, LayoutBufferSize, msgID, fmt, va );
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070074
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080075 LOGE(stringbuf);
76
77 _lock.Unlock();
78 }
79 void AppendBuffers(message_id_type msgID, int32 numPairs, va_list va)
80 {
Jianhong Jiang5665cbc2009-04-14 11:08:10 -070081 OSCL_UNUSED_ARG(msgID);
The Android Open Source Project2eb6af62009-03-03 19:30:11 -080082
83 for (int32 i = 0; i < numPairs; i++)
84 {
85 int32 length = va_arg (va, int32);
86 uint8* buffer = va_arg (va, uint8*);
87
88 int32 jj;
89 for( jj=10; jj<length; jj+=10 ) {
90 AppendStringA(0," %x %x %x %x %x %x %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
91 buffer += 10;
92 }
93
94 uint8 remainderbuf[10];
95 uint32 remainder = length - (jj-10);
96 if( remainder > 0 && remainder <= 10 ) {
97 oscl_memcpy( remainderbuf, buffer, remainder );
98 oscl_memset( remainderbuf+remainder, 0, 10-remainder );
99 buffer = remainderbuf;
100 AppendStringA(0," %x %x %x %x %x %x %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9]);
101 }
102 }
103 va_end(va);
Jianhong Jiang5665cbc2009-04-14 11:08:10 -0700104 }
The Android Open Source Project2eb6af62009-03-03 19:30:11 -0800105
106private:
107 void AppendStringA(message_id_type msgID, const char *fmt, ...) {
108 va_list arguments;
109 va_start(arguments, fmt);
110 AppendString(msgID, fmt, arguments);
111 va_end(arguments);
112 }
113
114 Layout _layout;
115 Lock _lock;
Jianhong Jiang5665cbc2009-04-14 11:08:10 -0700116 char* stringbuf;
117 oscl_wchar* wstringbuf;
The Android Open Source Project2eb6af62009-03-03 19:30:11 -0800118
119};
120
121#endif // ANDROID_LOG_APPENDER_H_INCLUDED
122