blob: 182f5809a505d7fde3d5b078aaaca69d79f66e05 [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
kwibergbfefb032016-05-01 14:53:46 -070014#include <memory>
15
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000016#include "webrtc/base/criticalsection.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/event_wrapper.h"
18#include "webrtc/system_wrappers/include/file_wrapper.h"
19#include "webrtc/system_wrappers/include/static_instance.h"
pbos12411ef2015-11-23 14:47:56 -080020#include "webrtc/base/platform_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/system_wrappers/include/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
pbos@webrtc.org9fed0992014-10-27 09:31:05 +000025#define WEBRTC_TRACE_MAX_MESSAGE_SIZE 1024
niklase@google.com470e71d2011-07-07 08:21:25 +000026// 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) =
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000029// 1 or 4 Mbyte.
niklase@google.com470e71d2011-07-07 08:21:25 +000030
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
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000036class TraceImpl : public Trace {
37 public:
38 virtual ~TraceImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000039
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000040 static TraceImpl* CreateInstance();
41 static TraceImpl* GetTrace(const TraceLevel level = kTraceAll);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
pbos@webrtc.org046deb92013-04-09 09:06:11 +000043 int32_t SetTraceFileImpl(const char* file_name, const bool add_file_counter);
44 int32_t TraceFileImpl(char file_name[FileWrapper::kMaxFileNameSize]);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
pbos@webrtc.org046deb92013-04-09 09:06:11 +000046 int32_t SetTraceCallbackImpl(TraceCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000048 void AddImpl(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +000049 const int32_t id, const char* msg);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000051 bool TraceCheck(const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000053 protected:
54 TraceImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000055
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000056 static TraceImpl* StaticInstance(CountOperation count_operation,
57 const TraceLevel level = kTraceAll);
henrike@webrtc.org315282c2011-12-09 17:46:20 +000058
pbos@webrtc.org046deb92013-04-09 09:06:11 +000059 int32_t AddThreadId(char* trace_message) const;
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000060
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000061 // OS specific implementations.
pbos@webrtc.org046deb92013-04-09 09:06:11 +000062 virtual int32_t AddTime(char* trace_message,
63 const TraceLevel level) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000064
pbos@webrtc.org046deb92013-04-09 09:06:11 +000065 virtual int32_t AddDateTimeInfo(char* trace_message) const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000066
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000067 private:
68 friend class Trace;
henrike@webrtc.org315282c2011-12-09 17:46:20 +000069
pbos@webrtc.org046deb92013-04-09 09:06:11 +000070 int32_t AddLevel(char* sz_message, const TraceLevel level) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
pbos@webrtc.org046deb92013-04-09 09:06:11 +000072 int32_t AddModuleAndId(char* trace_message, const TraceModule module,
73 const int32_t id) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000074
pbos@webrtc.org046deb92013-04-09 09:06:11 +000075 int32_t AddMessage(char* trace_message,
76 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
77 const uint16_t written_so_far) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000079 void AddMessageToList(
80 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +000081 const uint16_t length,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000082 const TraceLevel level);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000084 bool UpdateFileName(
85 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
86 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +000087 const uint32_t new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000089 bool CreateFileName(
90 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
91 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +000092 const uint32_t new_count) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000093
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000094 void WriteToFile(const char* msg, uint16_t length)
95 EXCLUSIVE_LOCKS_REQUIRED(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +000096
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000097 TraceCallback* callback_ GUARDED_BY(crit_);
98 uint32_t row_count_text_ GUARDED_BY(crit_);
99 uint32_t file_count_text_ GUARDED_BY(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
kwibergbfefb032016-05-01 14:53:46 -0700101 const std::unique_ptr<FileWrapper> trace_file_ GUARDED_BY(crit_);
tommi@webrtc.orgd3125052015-02-28 00:01:11 +0000102 rtc::CriticalSection crit_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103};
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000105} // namespace webrtc
106
107#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_TRACE_IMPL_H_