1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/*
2f6bb77a6f0d250f8c4c2c4dbf059556c00cf8eafpwestin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *
4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Use of this source code is governed by a BSD-style license
5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  that can be found in the LICENSE file in the root of the source
6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  tree. An additional intellectual property rights grant can be found
7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  in the file PATENTS.  All contributing project authors may
8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  be found in the AUTHORS file in the root of the source tree.
9daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org *
10daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org *  System independent wrapper for logging runtime information to file.
11daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org *  Note: All log messages will be written to the same trace file.
12daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org *  Note: If too many messages are written to file there will be a build up of
13daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org *  messages. Apply filtering to avoid that.
14470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */
15470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
16d56d68cd272783ae0cfe11834c2a141ec62519c2kjellander#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_
17d56d68cd272783ae0cfe11834c2a141ec62519c2kjellander#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_
18470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
19daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org#include "webrtc/common_types.h"
20daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org#include "webrtc/typedefs.h"
21470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
22f1a48174d475ad26dc00bcfc2ae2722008f71d78andrew@webrtc.orgnamespace webrtc {
23f1a48174d475ad26dc00bcfc2ae2722008f71d78andrew@webrtc.org
24f1a48174d475ad26dc00bcfc2ae2722008f71d78andrew@webrtc.org#if defined(WEBRTC_RESTRICT_LOGGING)
25f1a48174d475ad26dc00bcfc2ae2722008f71d78andrew@webrtc.org// Disable all TRACE macros. The LOG macro is still functional.
26f1a48174d475ad26dc00bcfc2ae2722008f71d78andrew@webrtc.org#define WEBRTC_TRACE true ? (void) 0 : Trace::Add
277bf7326d0bbfc4bea634f8ef0af15d5ac7464542wjia@webrtc.org#else
284158c35820f0de05cc3a3a3cb7f822f6e7a7d56ehenrike@webrtc.org#define WEBRTC_TRACE Trace::Add
297bf7326d0bbfc4bea634f8ef0af15d5ac7464542wjia@webrtc.org#endif
30470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
31daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.orgclass Trace {
32daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org public:
33c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org  // The length of the trace text preceeding the log message.
34c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org  static const int kBoilerplateLength;
35c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org  // The position of the timestamp text within a trace.
36c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org  static const int kTimestampPosition;
37c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org  // The length of the timestamp (without "delta" field).
38c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org  static const int kTimestampLength;
39c1ffd337f14788d596f2c3b869e20b3cca5b1e8aandrew@webrtc.org
40daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Increments the reference count to the trace.
41daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  static void CreateTrace();
42daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Decrements the reference count to the trace.
43daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  static void ReturnTrace();
44daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Note: any instance that writes to the trace file should increment and
45daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // decrement the reference count on construction and destruction,
46daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // respectively.
47470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
48daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Specifies what type of messages should be written to the trace file. The
49daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // filter parameter is a bitmask where each message type is enumerated by the
50daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // TraceLevel enumerator. TODO(hellner): why is the TraceLevel enumerator not
51daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // defined in this file?
52c7157da599d3d8d5eb4e89d6a85e503da0e344e2tommi@webrtc.org  static void set_level_filter(int filter);
53470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
54daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Returns what type of messages are written to the trace file.
55c7157da599d3d8d5eb4e89d6a85e503da0e344e2tommi@webrtc.org  static int level_filter();
56470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
57daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Sets the file name. If add_file_counter is false the same file will be
58daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // reused when it fills up. If it's true a new file with incremented name
59daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // will be used.
60046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  static int32_t SetTraceFile(const char* file_name,
61046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org                              const bool add_file_counter = false);
62470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
63daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Returns the name of the file that the trace is currently writing to.
64046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  static int32_t TraceFile(char file_name[1024]);
65470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
66daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Registers callback to receive trace messages.
67daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // TODO(hellner): Why not use OutStream instead? Why is TraceCallback not
68daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // defined in this file?
69046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org  static int32_t SetTraceCallback(TraceCallback* callback);
70470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
71daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // Adds a trace message for writing to file. The message is put in a queue
72daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // for writing to file whenever possible for performance reasons. I.e. there
73daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // is a crash it is possible that the last, vital logs are not logged yet.
74daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // level is the type of message to log. If that type of messages is
75daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // filtered it will not be written to file. module is an identifier for what
76daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // part of the code the message is coming.
77daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // id is an identifier that should be unique for that set of classes that
78daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // are associated (e.g. all instances owned by an engine).
79daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // msg and the ellipsis are the same as e.g. sprintf.
80daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  // TODO(hellner) Why is TraceModule not defined in this file?
81daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org  static void Add(const TraceLevel level,
82daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org                  const TraceModule module,
83046deb9b2050ebdf98a41e2d22f852e104dd365apbos@webrtc.org                  const int32_t id,
84daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org                  const char* msg, ...);
859080518a3928285be9f94684adad064c65d2cdf3andrew@webrtc.org
869080518a3928285be9f94684adad064c65d2cdf3andrew@webrtc.org private:
8746ad5426b025eddac8e9232014d347e73d27180epbos  static volatile int level_filter_;
88470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com};
8950419b07772de974964072478886c4c35ab9c8ccandrew@webrtc.org
90daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org}  // namespace webrtc
91daabfd25a6067a62f5c6385353606577b163296bphoglund@webrtc.org
92d56d68cd272783ae0cfe11834c2a141ec62519c2kjellander#endif  // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_
93