1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 // Some of this code is cut-and-pasted from nICEr. Copyright is:
8 
9 /*
10 Copyright (c) 2007, Adobe Systems, Incorporated
11 All rights reserved.
12 
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16 
17 * Redistributions of source code must retain the above copyright
18   notice, this list of conditions and the following disclaimer.
19 
20 * Redistributions in binary form must reproduce the above copyright
21   notice, this list of conditions and the following disclaimer in the
22   documentation and/or other materials provided with the distribution.
23 
24 * Neither the name of Adobe Systems, Network Resonance nor the names of its
25   contributors may be used to endorse or promote products derived from
26   this software without specific prior written permission.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40 
41 /* This Source Code Form is subject to the terms of the Mozilla Public
42  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
43  * You can obtain one at http://mozilla.org/MPL/2.0/. */
44 
45 /* Original author: bcampen@mozilla.com */
46 
47 /*
48    This file defines an r_dest_vlog that can be used to accumulate log messages
49    for later inspection/filtering. The intent is to use this for interactive
50    debug purposes on an about:webrtc page or similar.
51 */
52 
53 #ifndef rlogconnector_h__
54 #define rlogconnector_h__
55 
56 #include <stdint.h>
57 
58 #include <deque>
59 #include <string>
60 #include <vector>
61 
62 #include "mozilla/Mutex.h"
63 
64 #include "m_cpp_utils.h"
65 
66 namespace mozilla {
67 
68 class RLogConnector {
69  public:
70   /*
71      NB: These are not threadsafe, nor are they safe to call during static
72      init/deinit.
73   */
74   static RLogConnector* CreateInstance();
75   static RLogConnector* GetInstance();
76   static void DestroyInstance();
77 
78   /*
79      Retrieves log statements that match a given substring, subject to a
80      limit. |matching_logs| will be filled in chronological order (front()
81      is oldest, back() is newest). |limit| == 0 will be interpreted as no
82      limit.
83   */
84   void Filter(const std::string& substring, uint32_t limit,
85               std::deque<std::string>* matching_logs);
86 
87   void FilterAny(const std::vector<std::string>& substrings, uint32_t limit,
88                  std::deque<std::string>* matching_logs);
89 
GetAny(uint32_t limit,std::deque<std::string> * matching_logs)90   inline void GetAny(uint32_t limit, std::deque<std::string>* matching_logs) {
91     Filter("", limit, matching_logs);
92   }
93 
94   void SetLogLimit(uint32_t new_limit);
95   bool ShouldLog(int level) const;
96   void Log(int level, std::string&& log);
97   void Clear();
98 
99   // Methods to signal when a PeerConnection exists in a Private Window.
100   void EnterPrivateMode();
101   void ExitPrivateMode();
102 
103  private:
104   RLogConnector();
105   ~RLogConnector();
106   void RemoveOld();
107   void AddMsg(std::string&& msg);
108 
109   static RLogConnector* instance;
110 
111   /*
112    * Might be worthwhile making this a circular buffer, but I think it is
113    * preferable to take up as little space as possible if no logging is
114    * happening/the ringbuffer is not being used.
115    */
116   std::deque<std::string> log_messages_;
117   /* Max size of log buffer (should we use time-depth instead/also?) */
118   uint32_t log_limit_;
119   OffTheBooksMutex mutex_;
120   uint32_t disableCount_;
121 
122   DISALLOW_COPY_ASSIGN(RLogConnector);
123 };  // class RLogConnector
124 
125 }  // namespace mozilla
126 
127 #endif  // rlogconnector_h__
128