1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 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 #ifndef PVLOGGER_STDERR_APPENDER_H_INCLUDED 19 #define PVLOGGER_STDERR_APPENDER_H_INCLUDED 20 21 #ifndef PVLOGGERACCESSORIES_H_INCLUDED 22 #include "pvlogger_accessories.h" 23 #endif 24 25 #ifndef OSCLCONFIG_UTIL_H_INCLUDED 26 #include "osclconfig_util.h" 27 #endif 28 #ifndef OSCL_MEM_H_INCLUDED 29 #include "oscl_mem.h" 30 #endif 31 32 /** 33 * Class: StdErrAppender 34 * 35 */ 36 template < class Layout, int32 LayoutBufferSize, class Lock = OsclNullLock > 37 class StdErrAppender : public PVLoggerAppender 38 { 39 public: 40 typedef PVLoggerAppender::message_id_type message_id_type; 41 StdErrAppender()42 StdErrAppender() 43 { 44 stringbuf = NULL; 45 wstringbuf = NULL; 46 } ~StdErrAppender()47 virtual ~StdErrAppender() 48 { 49 if (stringbuf) 50 OSCL_DEFAULT_FREE(stringbuf); 51 if (wstringbuf) 52 OSCL_DEFAULT_FREE((OsclAny*)wstringbuf); 53 } 54 AppendString(message_id_type msgID,const char * fmt,va_list va)55 void AppendString(message_id_type msgID, const char *fmt, va_list va) 56 { 57 _lock.Lock(); 58 59 int32 size; 60 61 if (!stringbuf) 62 { 63 stringbuf = (char*)OSCL_DEFAULT_MALLOC(LayoutBufferSize); 64 if (!stringbuf) 65 return;//allocation failed-- just exit gracefully. 66 } 67 68 size = _layout.FormatString(stringbuf, LayoutBufferSize, msgID, fmt, va); 69 70 { 71 fprintf(stderr, "%s", stringbuf); 72 fprintf(stderr, "\n"); 73 } 74 75 _lock.Unlock(); 76 } AppendBuffers(message_id_type msgID,int32 numPairs,va_list va)77 void AppendBuffers(message_id_type msgID, int32 numPairs, va_list va) 78 { 79 OSCL_UNUSED_ARG(msgID); 80 81 for (int32 i = 0; i < numPairs; i++) 82 { 83 int32 length = va_arg(va, int32); 84 uint8* buffer = va_arg(va, uint8*); 85 86 int32 jj; 87 for (jj = 10; jj < length; jj += 10) 88 { 89 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]); 90 buffer += 10; 91 } 92 93 uint8 remainderbuf[10]; 94 uint32 remainder = length - (jj - 10); 95 if (remainder > 0 && remainder <= 10) 96 { 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); 104 } 105 106 private: AppendStringA(message_id_type msgID,const char * fmt,...)107 void AppendStringA(message_id_type msgID, const char *fmt, ...) 108 { 109 va_list arguments; 110 va_start(arguments, fmt); 111 AppendString(msgID, fmt, arguments); 112 va_end(arguments); 113 } 114 115 Layout _layout; 116 Lock _lock; 117 char* stringbuf; 118 oscl_wchar* wstringbuf; 119 120 }; 121 122 #endif // PVLOGGER_STDERR_APPENDER_H_INCLUDED 123 124