1 /*
2  *  Copyright (c) 2012 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 char* fileName,
53                                    const bool addFileCounter);
54     WebRtc_Word32 TraceFileImpl(
55         char 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     WebRtc_Word32 AddThreadId(char* traceMessage) const;
73 
74     // OS specific implementations
75     virtual WebRtc_Word32 AddTime(char* traceMessage,
76                                   const TraceLevel level) const = 0;
77 
78     virtual WebRtc_Word32 AddBuildInfo(char* traceMessage) const = 0;
79     virtual WebRtc_Word32 AddDateTimeInfo(char* traceMessage) const = 0;
80 
81     static bool Run(void* obj);
82     bool Process();
83 
84 private:
85     friend class Trace;
86 
87     WebRtc_Word32 AddLevel(char* szMessage, const TraceLevel level) const;
88 
89     WebRtc_Word32 AddModuleAndId(char* traceMessage, const TraceModule module,
90                                  const WebRtc_Word32 id) const;
91 
92     WebRtc_Word32 AddMessage(char* traceMessage,
93                              const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
94                              const WebRtc_UWord16 writtenSoFar) const;
95 
96     void AddMessageToList(
97         const char traceMessage[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
98         const WebRtc_UWord16 length,
99         const TraceLevel level);
100 
101     bool UpdateFileName(
102         const char fileNameUTF8[FileWrapper::kMaxFileNameSize],
103         char fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize],
104         const WebRtc_UWord32 newCount) const;
105 
106     bool CreateFileName(
107         const char fileNameUTF8[FileWrapper::kMaxFileNameSize],
108         char fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize],
109         const WebRtc_UWord32 newCount) const;
110 
111     void WriteToFile();
112 
113     CriticalSectionWrapper* _critsectInterface;
114     TraceCallback* _callback;
115     WebRtc_UWord32 _rowCountText;
116     WebRtc_UWord32 _fileCountText;
117 
118     FileWrapper& _traceFile;
119     ThreadWrapper& _thread;
120     EventWrapper& _event;
121 
122     // _critsectArray protects _activeQueue
123     CriticalSectionWrapper* _critsectArray;
124     WebRtc_UWord16 _nextFreeIdx[WEBRTC_TRACE_NUM_ARRAY];
125     TraceLevel _level[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
126     WebRtc_UWord16 _length[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
127     char* _messageQueue[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
128     WebRtc_UWord8 _activeQueue;
129 };
130 } // namespace webrtc
131 
132 #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_
133