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 "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"
20
21namespace 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(WEBRTC_IOS)
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
44class TraceImpl : public Trace {
45 public:
46  virtual ~TraceImpl();
47
48  static TraceImpl* CreateInstance();
49  static TraceImpl* GetTrace(const TraceLevel level = kTraceAll);
50
51  int32_t SetTraceFileImpl(const char* file_name, const bool add_file_counter);
52  int32_t TraceFileImpl(char file_name[FileWrapper::kMaxFileNameSize]);
53
54  int32_t SetTraceCallbackImpl(TraceCallback* callback);
55
56  void AddImpl(const TraceLevel level, const TraceModule module,
57               const int32_t id, const char* msg);
58
59  bool StopThread();
60
61  bool TraceCheck(const TraceLevel level) const;
62
63 protected:
64  TraceImpl();
65
66  static TraceImpl* StaticInstance(CountOperation count_operation,
67                                   const TraceLevel level = kTraceAll);
68
69  int32_t AddThreadId(char* trace_message) const;
70
71  // OS specific implementations.
72  virtual int32_t AddTime(char* trace_message,
73                          const TraceLevel level) const = 0;
74
75  virtual int32_t AddBuildInfo(char* trace_message) const = 0;
76  virtual int32_t AddDateTimeInfo(char* trace_message) const = 0;
77
78  static bool Run(void* obj);
79  bool Process();
80
81 private:
82  friend class Trace;
83
84  int32_t AddLevel(char* sz_message, const TraceLevel level) const;
85
86  int32_t AddModuleAndId(char* trace_message, const TraceModule module,
87                         const int32_t id) const;
88
89  int32_t AddMessage(char* trace_message,
90                     const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
91                     const uint16_t written_so_far) const;
92
93  void AddMessageToList(
94    const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
95    const uint16_t length,
96    const TraceLevel level);
97
98  bool UpdateFileName(
99    const char file_name_utf8[FileWrapper::kMaxFileNameSize],
100    char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
101    const uint32_t new_count) const;
102
103  bool CreateFileName(
104    const char file_name_utf8[FileWrapper::kMaxFileNameSize],
105    char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
106    const uint32_t new_count) const;
107
108  void WriteToFile();
109
110  CriticalSectionWrapper* critsect_interface_;
111  TraceCallback* callback_;
112  uint32_t row_count_text_;
113  uint32_t file_count_text_;
114
115  FileWrapper& trace_file_;
116  ThreadWrapper& thread_;
117  EventWrapper& event_;
118
119  // critsect_array_ protects active_queue_.
120  CriticalSectionWrapper* critsect_array_;
121  uint16_t next_free_idx_[WEBRTC_TRACE_NUM_ARRAY];
122  TraceLevel level_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
123  uint16_t length_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
124  char* message_queue_[WEBRTC_TRACE_NUM_ARRAY][WEBRTC_TRACE_MAX_QUEUE];
125  uint8_t active_queue_;
126};
127
128}  // namespace webrtc
129
130#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_
131