blob: 5fa6ce3871c058186ba6fa9701bec056c453096e [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 AddDateTimeInfo(char* trace_message) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000077 static bool Run(void* obj);
78 bool Process();
niklase@google.com470e71d2011-07-07 08:21:25 +000079
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000080 private:
81 friend class Trace;
henrike@webrtc.org315282c2011-12-09 17:46:20 +000082
pbos@webrtc.org046deb92013-04-09 09:06:11 +000083 int32_t AddLevel(char* sz_message, const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
pbos@webrtc.org046deb92013-04-09 09:06:11 +000085 int32_t AddModuleAndId(char* trace_message, const TraceModule module,
86 const int32_t id) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000087
pbos@webrtc.org046deb92013-04-09 09:06:11 +000088 int32_t AddMessage(char* trace_message,
89 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
90 const uint16_t written_so_far) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000091
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000092 void AddMessageToList(
93 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +000094 const uint16_t length,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000095 const TraceLevel level);
niklase@google.com470e71d2011-07-07 08:21:25 +000096
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000097 bool UpdateFileName(
98 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
99 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000100 const uint32_t new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000102 bool CreateFileName(
103 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
104 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000105 const uint32_t new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000107 void WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000109 CriticalSectionWrapper* critsect_interface_;
110 TraceCallback* callback_;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000111 uint32_t row_count_text_;
112 uint32_t file_count_text_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000114 FileWrapper& trace_file_;
115 ThreadWrapper& thread_;
116 EventWrapper& event_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000118 // critsect_array_ protects active_queue_.
119 CriticalSectionWrapper* critsect_array_;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000120 uint16_t next_free_idx_[WEBRTC_TRACE_NUM_ARRAY];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000121 TraceLevel level_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000122 uint16_t length_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000123 char* message_queue_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000124 uint8_t active_queue_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125};
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000127} // namespace webrtc
128
129#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_