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 | * System independent wrapper for logging runtime information to file. |
| 11 | * Note: All log messages will be written to the same trace file. |
| 12 | * Note: If too many messages are written to file there will be a build up of |
| 13 | * messages. Apply filtering to avoid that. |
| 14 | */ |
| 15 | |
| 16 | #ifndef SYSTEM_WRAPPERS_INCLUDE_TRACE_H_ |
| 17 | #define SYSTEM_WRAPPERS_INCLUDE_TRACE_H_ |
| 18 | |
| 19 | #include "common_types.h" // NOLINT(build/include) |
| 20 | #include "typedefs.h" // NOLINT(build/include) |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | #if defined(WEBRTC_RESTRICT_LOGGING) |
| 25 | // Disable all TRACE macros. The LOG macro is still functional. |
| 26 | #define WEBRTC_TRACE true ? (void) 0 : Trace::Add |
| 27 | #else |
| 28 | #define WEBRTC_TRACE Trace::Add |
| 29 | #endif |
| 30 | |
| 31 | class Trace { |
| 32 | public: |
| 33 | // The length of the trace text preceeding the log message. |
| 34 | static const int kBoilerplateLength; |
| 35 | // The position of the timestamp text within a trace. |
| 36 | static const int kTimestampPosition; |
| 37 | // The length of the timestamp (without "delta" field). |
| 38 | static const int kTimestampLength; |
| 39 | |
| 40 | // Increments the reference count to the trace. |
| 41 | static void CreateTrace(); |
| 42 | // Decrements the reference count to the trace. |
| 43 | static void ReturnTrace(); |
| 44 | // Note: any instance that writes to the trace file should increment and |
| 45 | // decrement the reference count on construction and destruction, |
| 46 | // respectively. |
| 47 | |
| 48 | // Specifies what type of messages should be written to the trace file. The |
| 49 | // filter parameter is a bitmask where each message type is enumerated by the |
| 50 | // TraceLevel enumerator. TODO(hellner): why is the TraceLevel enumerator not |
| 51 | // defined in this file? |
| 52 | static void set_level_filter(int filter); |
| 53 | |
| 54 | // Returns what type of messages are written to the trace file. |
| 55 | static int level_filter(); |
| 56 | |
| 57 | // Sets the file name. If add_file_counter is false the same file will be |
| 58 | // reused when it fills up. If it's true a new file with incremented name |
| 59 | // will be used. |
| 60 | static int32_t SetTraceFile(const char* file_name, |
| 61 | const bool add_file_counter = false); |
| 62 | |
| 63 | // Registers callback to receive trace messages. |
| 64 | // TODO(hellner): Why not use OutStream instead? Why is TraceCallback not |
| 65 | // defined in this file? |
| 66 | static int32_t SetTraceCallback(TraceCallback* callback); |
| 67 | |
| 68 | // Adds a trace message for writing to file. The message is put in a queue |
| 69 | // for writing to file whenever possible for performance reasons. I.e. there |
| 70 | // is a crash it is possible that the last, vital logs are not logged yet. |
| 71 | // level is the type of message to log. If that type of messages is |
| 72 | // filtered it will not be written to file. module is an identifier for what |
| 73 | // part of the code the message is coming. |
| 74 | // id is an identifier that should be unique for that set of classes that |
| 75 | // are associated (e.g. all instances owned by an engine). |
| 76 | // msg and the ellipsis are the same as e.g. sprintf. |
| 77 | // TODO(hellner) Why is TraceModule not defined in this file? |
| 78 | static void Add(const TraceLevel level, |
| 79 | const TraceModule module, |
| 80 | const int32_t id, |
| 81 | const char* msg, ...); |
| 82 | |
| 83 | private: |
| 84 | static volatile int level_filter_; |
| 85 | }; |
| 86 | |
| 87 | } // namespace webrtc |
| 88 | |
| 89 | #endif // SYSTEM_WRAPPERS_INCLUDE_TRACE_H_ |