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/base/criticalsection.h"
15 #include "webrtc/base/scoped_ptr.h"
16 #include "webrtc/system_wrappers/include/event_wrapper.h"
17 #include "webrtc/system_wrappers/include/file_wrapper.h"
18 #include "webrtc/system_wrappers/include/static_instance.h"
19 #include "webrtc/base/platform_thread.h"
20 #include "webrtc/system_wrappers/include/trace.h"
21 
22 namespace webrtc {
23 
24 #define WEBRTC_TRACE_MAX_MESSAGE_SIZE 1024
25 // Total buffer size is WEBRTC_TRACE_NUM_ARRAY (number of buffer partitions) *
26 // WEBRTC_TRACE_MAX_QUEUE (number of lines per buffer partition) *
27 // WEBRTC_TRACE_MAX_MESSAGE_SIZE (number of 1 byte charachters per line) =
28 // 1 or 4 Mbyte.
29 
30 #define WEBRTC_TRACE_MAX_FILE_SIZE 100*1000
31 // Number of rows that may be written to file. On average 110 bytes per row (max
32 // 256 bytes per row). So on average 110*100*1000 = 11 Mbyte, max 256*100*1000 =
33 // 25.6 Mbyte
34 
35 class TraceImpl : public Trace {
36  public:
37   virtual ~TraceImpl();
38 
39   static TraceImpl* CreateInstance();
40   static TraceImpl* GetTrace(const TraceLevel level = kTraceAll);
41 
42   int32_t SetTraceFileImpl(const char* file_name, const bool add_file_counter);
43   int32_t TraceFileImpl(char file_name[FileWrapper::kMaxFileNameSize]);
44 
45   int32_t SetTraceCallbackImpl(TraceCallback* callback);
46 
47   void AddImpl(const TraceLevel level, const TraceModule module,
48                const int32_t id, const char* msg);
49 
50   bool TraceCheck(const TraceLevel level) const;
51 
52  protected:
53   TraceImpl();
54 
55   static TraceImpl* StaticInstance(CountOperation count_operation,
56                                    const TraceLevel level = kTraceAll);
57 
58   int32_t AddThreadId(char* trace_message) const;
59 
60   // OS specific implementations.
61   virtual int32_t AddTime(char* trace_message,
62                           const TraceLevel level) const = 0;
63 
64   virtual int32_t AddDateTimeInfo(char* trace_message) const = 0;
65 
66  private:
67   friend class Trace;
68 
69   int32_t AddLevel(char* sz_message, const TraceLevel level) const;
70 
71   int32_t AddModuleAndId(char* trace_message, const TraceModule module,
72                          const int32_t id) const;
73 
74   int32_t AddMessage(char* trace_message,
75                      const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
76                      const uint16_t written_so_far) const;
77 
78   void AddMessageToList(
79     const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
80     const uint16_t length,
81     const TraceLevel level);
82 
83   bool UpdateFileName(
84     const char file_name_utf8[FileWrapper::kMaxFileNameSize],
85     char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
86     const uint32_t new_count) const;
87 
88   bool CreateFileName(
89     const char file_name_utf8[FileWrapper::kMaxFileNameSize],
90     char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
91     const uint32_t new_count) const;
92 
93   void WriteToFile(const char* msg, uint16_t length)
94       EXCLUSIVE_LOCKS_REQUIRED(crit_);
95 
96   TraceCallback* callback_ GUARDED_BY(crit_);
97   uint32_t row_count_text_ GUARDED_BY(crit_);
98   uint32_t file_count_text_ GUARDED_BY(crit_);
99 
100   const rtc::scoped_ptr<FileWrapper> trace_file_ GUARDED_BY(crit_);
101   rtc::CriticalSection crit_;
102 };
103 
104 }  // namespace webrtc
105 
106 #endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_
107