media/mtransport/rlogringbuffer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/rlogringbuffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Some of this code is cut-and-pasted from nICEr. Copyright is:
    1.11 +
    1.12 +/*
    1.13 +Copyright (c) 2007, Adobe Systems, Incorporated
    1.14 +All rights reserved.
    1.15 +
    1.16 +Redistribution and use in source and binary forms, with or without
    1.17 +modification, are permitted provided that the following conditions are
    1.18 +met:
    1.19 +
    1.20 +* Redistributions of source code must retain the above copyright
    1.21 +  notice, this list of conditions and the following disclaimer.
    1.22 +
    1.23 +* Redistributions in binary form must reproduce the above copyright
    1.24 +  notice, this list of conditions and the following disclaimer in the
    1.25 +  documentation and/or other materials provided with the distribution.
    1.26 +
    1.27 +* Neither the name of Adobe Systems, Network Resonance nor the names of its
    1.28 +  contributors may be used to endorse or promote products derived from
    1.29 +  this software without specific prior written permission.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +*/
    1.43 +
    1.44 +/* This Source Code Form is subject to the terms of the Mozilla Public
    1.45 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
    1.46 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.47 +
    1.48 +/* Original author: bcampen@mozilla.com */
    1.49 +
    1.50 +/*
    1.51 +   This file defines an r_dest_vlog that can be used to accumulate log messages
    1.52 +   for later inspection/filtering. The intent is to use this for interactive
    1.53 +   debug purposes on an about:webrtc page or similar.
    1.54 +*/
    1.55 +
    1.56 +#ifndef rlogringbuffer_h__
    1.57 +#define rlogringbuffer_h__
    1.58 +
    1.59 +#include <stdint.h>
    1.60 +
    1.61 +#include <deque>
    1.62 +#include <string>
    1.63 +#include <vector>
    1.64 +
    1.65 +#include "mozilla/Mutex.h"
    1.66 +
    1.67 +#include "m_cpp_utils.h"
    1.68 +
    1.69 +namespace mozilla {
    1.70 +
    1.71 +class RLogRingBuffer {
    1.72 +  public:
    1.73 +    /*
    1.74 +       NB: These are not threadsafe, nor are they safe to call during static
    1.75 +       init/deinit.
    1.76 +    */
    1.77 +    static RLogRingBuffer* CreateInstance();
    1.78 +    static RLogRingBuffer* GetInstance();
    1.79 +    static void DestroyInstance();
    1.80 +
    1.81 +    /*
    1.82 +       Retrieves log statements that match a given substring, subject to a
    1.83 +       limit. |matching_logs| will be filled in chronological order (front()
    1.84 +       is oldest, back() is newest). |limit| == 0 will be interpreted as no
    1.85 +       limit.
    1.86 +    */
    1.87 +    void Filter(const std::string& substring,
    1.88 +                uint32_t limit,
    1.89 +                std::deque<std::string>* matching_logs);
    1.90 +
    1.91 +    void FilterAny(const std::vector<std::string>& substrings,
    1.92 +                   uint32_t limit,
    1.93 +                   std::deque<std::string>* matching_logs);
    1.94 +
    1.95 +    inline void GetAny(uint32_t limit,
    1.96 +                       std::deque<std::string>* matching_logs) {
    1.97 +      Filter("", limit, matching_logs);
    1.98 +    }
    1.99 +
   1.100 +    void SetLogLimit(uint32_t new_limit);
   1.101 +
   1.102 +    void Log(std::string&& log);
   1.103 +
   1.104 +  private:
   1.105 +    RLogRingBuffer();
   1.106 +    ~RLogRingBuffer();
   1.107 +    void RemoveOld();
   1.108 +    static RLogRingBuffer* instance;
   1.109 +
   1.110 +    /*
   1.111 +     * Might be worthwhile making this a circular buffer, but I think it is
   1.112 +     * preferable to take up as little space as possible if no logging is
   1.113 +     * happening/the ringbuffer is not being used.
   1.114 +    */
   1.115 +    std::deque<std::string> log_messages_;
   1.116 +    /* Max size of log buffer (should we use time-depth instead/also?) */
   1.117 +    uint32_t log_limit_;
   1.118 +    OffTheBooksMutex mutex_;
   1.119 +
   1.120 +    DISALLOW_COPY_ASSIGN(RLogRingBuffer);
   1.121 +}; // class RLogRingBuffer
   1.122 +
   1.123 +} // namespace mozilla
   1.124 +
   1.125 +#endif // rlogringbuffer_h__
   1.126 +
   1.127 +

mercurial