blob: 5548e984f4a4fce60d119e00b3847b9d11623234 [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
pbos@webrtc.org9fed0992014-10-27 09:31:05 +000033#define WEBRTC_TRACE_MAX_MESSAGE_SIZE 1024
niklase@google.com470e71d2011-07-07 08:21:25 +000034// 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
pbos@webrtc.org046deb92013-04-09 09:06:11 +000051 int32_t SetTraceFileImpl(const char* file_name, const bool add_file_counter);
52 int32_t TraceFileImpl(char file_name[FileWrapper::kMaxFileNameSize]);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
pbos@webrtc.org046deb92013-04-09 09:06:11 +000054 int32_t SetTraceCallbackImpl(TraceCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000055
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000056 void AddImpl(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +000057 const int32_t id, const char* msg);
niklase@google.com470e71d2011-07-07 08:21:25 +000058
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000059 bool StopThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000060
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000061 bool TraceCheck(const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000062
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000063 protected:
64 TraceImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000065
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000066 static TraceImpl* StaticInstance(CountOperation count_operation,
67 const TraceLevel level = kTraceAll);
henrike@webrtc.org315282c2011-12-09 17:46:20 +000068
pbos@webrtc.org046deb92013-04-09 09:06:11 +000069 int32_t AddThreadId(char* trace_message) const;
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000070
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000071 // OS specific implementations.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000072 virtual int32_t AddTime(char* trace_message,
73 const TraceLevel level) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000074
pbos@webrtc.org046deb92013-04-09 09:06:11 +000075 virtual int32_t AddBuildInfo(char* trace_message) const = 0;
76 virtual int32_t AddDateTimeInfo(char* trace_message) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000077
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000078 static bool Run(void* obj);
79 bool Process();
niklase@google.com470e71d2011-07-07 08:21:25 +000080
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000081 private:
82 friend class Trace;
henrike@webrtc.org315282c2011-12-09 17:46:20 +000083
pbos@webrtc.org046deb92013-04-09 09:06:11 +000084 int32_t AddLevel(char* sz_message, const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
pbos@webrtc.org046deb92013-04-09 09:06:11 +000086 int32_t AddModuleAndId(char* trace_message, const TraceModule module,
87 const int32_t id) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
pbos@webrtc.org046deb92013-04-09 09:06:11 +000089 int32_t AddMessage(char* trace_message,
90 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
91 const uint16_t written_so_far) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000093 void AddMessageToList(
94 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +000095 const uint16_t length,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000096 const TraceLevel level);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000098 bool UpdateFileName(
99 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
100 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000101 const uint32_t new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000103 bool CreateFileName(
104 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
105 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000106 const uint32_t new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000108 void WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000110 CriticalSectionWrapper* critsect_interface_;
111 TraceCallback* callback_;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000112 uint32_t row_count_text_;
113 uint32_t file_count_text_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000115 FileWrapper& trace_file_;
116 ThreadWrapper& thread_;
117 EventWrapper& event_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000119 // critsect_array_ protects active_queue_.
120 CriticalSectionWrapper* critsect_array_;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000121 uint16_t next_free_idx_[WEBRTC_TRACE_NUM_ARRAY];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000122 TraceLevel level_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000123 uint16_t length_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000124 char* message_queue_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000125 uint8_t active_queue_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126};
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000128} // namespace webrtc
129
130#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_