blob: 797161557a132ba9fc9f1c2d71e41413663cf0dc [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
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000014#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
15#include "webrtc/system_wrappers/interface/event_wrapper.h"
16#include "webrtc/system_wrappers/interface/file_wrapper.h"
17#include "webrtc/system_wrappers/interface/static_instance.h"
18#include "webrtc/system_wrappers/interface/thread_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000023// 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)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000028#define WEBRTC_TRACE_MAX_QUEUE 2000
niklase@google.com470e71d2011-07-07 08:21:25 +000029#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000030#define WEBRTC_TRACE_MAX_QUEUE 8000
niklase@google.com470e71d2011-07-07 08:21:25 +000031#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) =
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000037// 1 or 4 Mbyte.
niklase@google.com470e71d2011-07-07 08:21:25 +000038
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
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000044class TraceImpl : public Trace {
45 public:
46 virtual ~TraceImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000047
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000048 static TraceImpl* CreateInstance();
49 static TraceImpl* GetTrace(const TraceLevel level = kTraceAll);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000051 WebRtc_Word32 SetTraceFileImpl(const char* file_name,
52 const bool add_file_counter);
53 WebRtc_Word32 TraceFileImpl(
54 char file_name[FileWrapper::kMaxFileNameSize]);
niklase@google.com470e71d2011-07-07 08:21:25 +000055
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000056 WebRtc_Word32 SetTraceCallbackImpl(TraceCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000058 void AddImpl(const TraceLevel level, const TraceModule module,
59 const WebRtc_Word32 id, const char* msg);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000061 bool StopThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000062
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000063 bool TraceCheck(const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000064
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000065 protected:
66 TraceImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000067
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000068 static TraceImpl* StaticInstance(CountOperation count_operation,
69 const TraceLevel level = kTraceAll);
henrike@webrtc.org315282c2011-12-09 17:46:20 +000070
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000071 WebRtc_Word32 AddThreadId(char* trace_message) const;
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000072
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000073 // OS specific implementations.
74 virtual WebRtc_Word32 AddTime(char* trace_message,
75 const TraceLevel level) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000077 virtual WebRtc_Word32 AddBuildInfo(char* trace_message) const = 0;
78 virtual WebRtc_Word32 AddDateTimeInfo(char* trace_message) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000079
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000080 static bool Run(void* obj);
81 bool Process();
niklase@google.com470e71d2011-07-07 08:21:25 +000082
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000083 private:
84 friend class Trace;
henrike@webrtc.org315282c2011-12-09 17:46:20 +000085
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000086 WebRtc_Word32 AddLevel(char* sz_message, const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000087
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000088 WebRtc_Word32 AddModuleAndId(char* trace_message, const TraceModule module,
89 const WebRtc_Word32 id) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000091 WebRtc_Word32 AddMessage(char* trace_message,
92 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
93 const WebRtc_UWord16 written_so_far) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000095 void AddMessageToList(
96 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
97 const WebRtc_UWord16 length,
98 const TraceLevel level);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000100 bool UpdateFileName(
101 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
102 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
103 const WebRtc_UWord32 new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000105 bool CreateFileName(
106 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
107 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
108 const WebRtc_UWord32 new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000110 void WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000112 CriticalSectionWrapper* critsect_interface_;
113 TraceCallback* callback_;
114 WebRtc_UWord32 row_count_text_;
115 WebRtc_UWord32 file_count_text_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000117 FileWrapper& trace_file_;
118 ThreadWrapper& thread_;
119 EventWrapper& event_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000121 // critsect_array_ protects active_queue_.
122 CriticalSectionWrapper* critsect_array_;
123 WebRtc_UWord16 next_free_idx_[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 char* message_queue_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
127 WebRtc_UWord8 active_queue_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128};
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000130} // namespace webrtc
131
132#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_