blob: 5a6b59c3f792646d6eda6c1550c6bab17fdb99b4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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"
henrike@webrtc.org315282c2011-12-09 17:46:20 +000017#include "system_wrappers/interface/static_instance.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018#include "system_wrappers/interface/trace.h"
19#include "system_wrappers/interface/thread_wrapper.h"
20
21namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
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.
sjlee@webrtc.org414fa7f2012-09-11 17:25:46 +000027#if defined(WEBRTC_IOS)
niklase@google.com470e71d2011-07-07 08:21:25 +000028 #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
44class TraceImpl : public Trace
45{
46public:
47 virtual ~TraceImpl();
48
henrike@webrtc.org315282c2011-12-09 17:46:20 +000049 static TraceImpl* CreateInstance();
niklase@google.com470e71d2011-07-07 08:21:25 +000050 static TraceImpl* GetTrace(const TraceLevel level = kTraceAll);
51
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000052 WebRtc_Word32 SetTraceFileImpl(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +000053 const bool addFileCounter);
54 WebRtc_Word32 TraceFileImpl(
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000055 char fileName[FileWrapper::kMaxFileNameSize]);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
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
66protected:
67 TraceImpl();
68
henrike@webrtc.org315282c2011-12-09 17:46:20 +000069 static TraceImpl* StaticInstance(CountOperation count_operation,
70 const TraceLevel level = kTraceAll);
71
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000072 WebRtc_Word32 AddThreadId(char* traceMessage) const;
73
niklase@google.com470e71d2011-07-07 08:21:25 +000074 // OS specific implementations
niklase@google.com470e71d2011-07-07 08:21:25 +000075 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
84private:
henrike@webrtc.org315282c2011-12-09 17:46:20 +000085 friend class Trace;
86
niklase@google.com470e71d2011-07-07 08:21:25 +000087 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(
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000102 const char fileNameUTF8[FileWrapper::kMaxFileNameSize],
103 char fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize],
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 const WebRtc_UWord32 newCount) const;
105
106 bool CreateFileName(
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000107 const char fileNameUTF8[FileWrapper::kMaxFileNameSize],
108 char fileNameWithCounterUTF8[FileWrapper::kMaxFileNameSize],
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 const WebRtc_UWord32 newCount) const;
110
111 void WriteToFile();
112
henrike@webrtc.orgbfa80ce2011-12-15 17:59:58 +0000113 CriticalSectionWrapper* _critsectInterface;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114 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
henrike@webrtc.orgbfa80ce2011-12-15 17:59:58 +0000123 CriticalSectionWrapper* _critsectArray;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 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];
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000127 char* _messageQueue[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 WebRtc_UWord8 _activeQueue;
129};
130} // namespace webrtc
131
132#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_