Home | History | Annotate | Download | only in source
      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/trace.h"
     18 #include "system_wrappers/interface/thread_wrapper.h"
     19 
     20 namespace webrtc {
     21 enum TraceCount
     22 {
     23     WEBRTC_TRACE_DEC    = 0,
     24     WEBRTC_TRACE_INC    = 1,
     25     WEBRTC_TRACE_INC_NO_CREATE = 2
     26 };
     27 
     28 enum TraceCreate
     29 {
     30     WEBRTC_TRACE_EXIST    = 0,
     31     WEBRTC_TRACE_CREATE   = 1,
     32     WEBRTC_TRACE_DESTROY  = 2
     33 };
     34 
     35 // TODO (pwestin) WEBRTC_TRACE_MAX_QUEUE needs to be tweaked
     36 // TODO (hellner) the buffer should be close to how much the system can write to
     37 //                file. Increasing the buffer will not solve anything. Sooner or
     38 //                later the buffer is going to fill up anyways.
     39 #if defined(MAC_IPHONE)
     40     #define WEBRTC_TRACE_MAX_QUEUE  2000
     41 #else
     42     #define WEBRTC_TRACE_MAX_QUEUE  8000
     43 #endif
     44 #define WEBRTC_TRACE_NUM_ARRAY 2
     45 #define WEBRTC_TRACE_MAX_MESSAGE_SIZE 256
     46 // Total buffer size is WEBRTC_TRACE_NUM_ARRAY (number of buffer partitions) *
     47 // WEBRTC_TRACE_MAX_QUEUE (number of lines per buffer partition) *
     48 // WEBRTC_TRACE_MAX_MESSAGE_SIZE (number of 1 byte charachters per line) =
     49 // 1 or 4 Mbyte
     50 
     51 #define WEBRTC_TRACE_MAX_FILE_SIZE 100*1000
     52 // Number of rows that may be written to file. On average 110 bytes per row (max
     53 // 256 bytes per row). So on average 110*100*1000 = 11 Mbyte, max 256*100*1000 =
     54 // 25.6 Mbyte
     55 
     56 class TraceImpl : public Trace
     57 {
     58 public:
     59     virtual ~TraceImpl();
     60 
     61     static Trace* CreateTrace();
     62     static TraceImpl* GetTrace(const TraceLevel level = kTraceAll);
     63 
     64     static Trace* StaticInstance(TraceCount inc,
     65                                  const TraceLevel level = kTraceAll);
     66 
     67     WebRtc_Word32 SetTraceFileImpl(const WebRtc_Word8* fileName,
     68                                    const bool addFileCounter);
     69     WebRtc_Word32 TraceFileImpl(
     70         WebRtc_Word8 fileName[FileWrapper::kMaxFileNameSize]);
     71 
     72     WebRtc_Word32 SetTraceCallbackImpl(TraceCallback* callback);
     73 
     74     void AddImpl(const TraceLevel level, const TraceModule module,
     75                  const WebRtc_Word32 id, const char* msg);
     76 
     77     bool StopThread();
     78 
     79     bool TraceCheck(const TraceLevel level) const;
     80 
     81 protected:
     82     TraceImpl();
     83 
     84     // OS specific implementations
     85     virtual WebRtc_Word32 AddThreadId(char* traceMessage) const = 0;
     86     virtual WebRtc_Word32 AddTime(char* traceMessage,
     87                                   const TraceLevel level) const = 0;
     88 
     89     virtual WebRtc_Word32 AddBuildInfo(char* traceMessage) const = 0;
     90     virtual WebRtc_Word32 AddDateTimeInfo(char* traceMessage) const = 0;
     91 
     92     static bool Run(void* obj);
     93     bool Process();
     94 
     95 private:
     96     WebRtc_Word32 AddLevel(char* szMessage, const TraceLevel level) const;
     97 
     98     WebRtc_Word32 AddModuleAndId(char* traceMessage, const TraceModule module,
     99                                  const WebRtc_Word32 id) const;
    100 
    101     WebRtc_Word32 AddMessage(char* traceMessage,
    102                              const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
    103                              const WebRtc_UWord16 writtenSoFar) const;
    104 
    105     void AddMessageToList(
    106         const char traceMessage[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
    107         const WebRtc_UWord16 length,
    108         const TraceLevel level);
    109 
    110     bool UpdateFileName(
    111         const WebRtc_Word8 fileNameUTF8[FileWrapper::kMaxFileNameSize],
    112         WebRtc_Word8 fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize],
    113         const WebRtc_UWord32 newCount) const;
    114 
    115     bool CreateFileName(
    116         const WebRtc_Word8 fileNameUTF8[FileWrapper::kMaxFileNameSize],
    117         WebRtc_Word8 fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize],
    118         const WebRtc_UWord32 newCount) const;
    119 
    120     void WriteToFile();
    121 
    122     CriticalSectionWrapper& _critsectInterface;
    123     TraceCallback* _callback;
    124     WebRtc_UWord32 _rowCountText;
    125     WebRtc_UWord32 _fileCountText;
    126 
    127     FileWrapper& _traceFile;
    128     ThreadWrapper& _thread;
    129     EventWrapper& _event;
    130 
    131     // _critsectArray protects _activeQueue
    132     CriticalSectionWrapper& _critsectArray;
    133     WebRtc_UWord16 _nextFreeIdx[WEBRTC_TRACE_NUM_ARRAY];
    134     TraceLevel _level[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
    135     WebRtc_UWord16 _length[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
    136     WebRtc_Word8* _messageQueue[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
    137     WebRtc_UWord8 _activeQueue;
    138 };
    139 } // namespace webrtc
    140 
    141 #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_
    142