1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_ 12 #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_ 13 14 #include "system_wrappers/interface/critical_section_wrapper.h" 15 #include "system_wrappers/interface/event_wrapper.h" 16 #include "system_wrappers/interface/file_wrapper.h" 17 #include "system_wrappers/interface/static_instance.h" 18 #include "system_wrappers/interface/trace.h" 19 #include "system_wrappers/interface/thread_wrapper.h" 20 21 namespace webrtc { 22 23 // TODO (pwestin) WEBRTC_TRACE_MAX_QUEUE needs to be tweaked 24 // TODO (hellner) the buffer should be close to how much the system can write to 25 // file. Increasing the buffer will not solve anything. Sooner or 26 // later the buffer is going to fill up anyways. 27 #if defined(MAC_IPHONE) 28 #define WEBRTC_TRACE_MAX_QUEUE 2000 29 #else 30 #define WEBRTC_TRACE_MAX_QUEUE 8000 31 #endif 32 #define WEBRTC_TRACE_NUM_ARRAY 2 33 #define WEBRTC_TRACE_MAX_MESSAGE_SIZE 256 34 // Total buffer size is WEBRTC_TRACE_NUM_ARRAY (number of buffer partitions) * 35 // WEBRTC_TRACE_MAX_QUEUE (number of lines per buffer partition) * 36 // WEBRTC_TRACE_MAX_MESSAGE_SIZE (number of 1 byte charachters per line) = 37 // 1 or 4 Mbyte 38 39 #define WEBRTC_TRACE_MAX_FILE_SIZE 100*1000 40 // Number of rows that may be written to file. On average 110 bytes per row (max 41 // 256 bytes per row). So on average 110*100*1000 = 11 Mbyte, max 256*100*1000 = 42 // 25.6 Mbyte 43 44 class TraceImpl : public Trace 45 { 46 public: 47 virtual ~TraceImpl(); 48 49 static TraceImpl* CreateInstance(); 50 static TraceImpl* GetTrace(const TraceLevel level = kTraceAll); 51 52 WebRtc_Word32 SetTraceFileImpl(const WebRtc_Word8* fileName, 53 const bool addFileCounter); 54 WebRtc_Word32 TraceFileImpl( 55 WebRtc_Word8 fileName[FileWrapper::kMaxFileNameSize]); 56 57 WebRtc_Word32 SetTraceCallbackImpl(TraceCallback* callback); 58 59 void AddImpl(const TraceLevel level, const TraceModule module, 60 const WebRtc_Word32 id, const char* msg); 61 62 bool StopThread(); 63 64 bool TraceCheck(const TraceLevel level) const; 65 66 protected: 67 TraceImpl(); 68 69 static TraceImpl* StaticInstance(CountOperation count_operation, 70 const TraceLevel level = kTraceAll); 71 72 // OS specific implementations 73 virtual WebRtc_Word32 AddThreadId(char* traceMessage) const = 0; 74 virtual WebRtc_Word32 AddTime(char* traceMessage, 75 const TraceLevel level) const = 0; 76 77 virtual WebRtc_Word32 AddBuildInfo(char* traceMessage) const = 0; 78 virtual WebRtc_Word32 AddDateTimeInfo(char* traceMessage) const = 0; 79 80 static bool Run(void* obj); 81 bool Process(); 82 83 private: 84 friend class Trace; 85 86 WebRtc_Word32 AddLevel(char* szMessage, const TraceLevel level) const; 87 88 WebRtc_Word32 AddModuleAndId(char* traceMessage, const TraceModule module, 89 const WebRtc_Word32 id) const; 90 91 WebRtc_Word32 AddMessage(char* traceMessage, 92 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE], 93 const WebRtc_UWord16 writtenSoFar) const; 94 95 void AddMessageToList( 96 const char traceMessage[WEBRTC_TRACE_MAX_MESSAGE_SIZE], 97 const WebRtc_UWord16 length, 98 const TraceLevel level); 99 100 bool UpdateFileName( 101 const WebRtc_Word8 fileNameUTF8[FileWrapper::kMaxFileNameSize], 102 WebRtc_Word8 fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize], 103 const WebRtc_UWord32 newCount) const; 104 105 bool CreateFileName( 106 const WebRtc_Word8 fileNameUTF8[FileWrapper::kMaxFileNameSize], 107 WebRtc_Word8 fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize], 108 const WebRtc_UWord32 newCount) const; 109 110 void WriteToFile(); 111 112 CriticalSectionWrapper* _critsectInterface; 113 TraceCallback* _callback; 114 WebRtc_UWord32 _rowCountText; 115 WebRtc_UWord32 _fileCountText; 116 117 FileWrapper& _traceFile; 118 ThreadWrapper& _thread; 119 EventWrapper& _event; 120 121 // _critsectArray protects _activeQueue 122 CriticalSectionWrapper* _critsectArray; 123 WebRtc_UWord16 _nextFreeIdx[WEBRTC_TRACE_NUM_ARRAY]; 124 TraceLevel _level[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE]; 125 WebRtc_UWord16 _length[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE]; 126 WebRtc_Word8* _messageQueue[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE]; 127 WebRtc_UWord8 _activeQueue; 128 }; 129 } // namespace webrtc 130 131 #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_ 132