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