• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <audio_utils/SimpleLog.h>
20 #include <map>
21 #include <mutex>
22 #include <sstream>
23 
24 namespace android::mediametrics {
25 
26 class StatsdLog {
27 public:
StatsdLog(size_t lines)28     explicit StatsdLog(size_t lines) : mSimpleLog(lines) {}
29 
log(int atom,const std::string & string)30     void log(int atom, const std::string& string) {
31         {
32             std::lock_guard lock(mLock);
33             ++mCountMap[atom];
34         }
35         mSimpleLog.log("%s", string.c_str());
36     }
37 
38    std::string dumpToString(const char *prefix = "", size_t logLines = 0) const {
39        std::stringstream ss;
40 
41        {   // first print out the atom counts
42            std::lock_guard lock(mLock);
43 
44            size_t col = 0;
45            for (const auto& count : mCountMap) {
46                if (col == 8) {
47                    col = 0;
48                    ss << "\n" << prefix;
49                } else {
50                    ss << " ";
51                }
52                ss << "[ " << count.first << " : " << count.second << " ]";
53                ++col;
54            }
55            ss << "\n";
56        }
57 
58        // then print out the log lines
59        ss << mSimpleLog.dumpToString(prefix, logLines);
60        return ss.str();
61    }
62 
63 private:
64     SimpleLog mSimpleLog; // internally locked
65     std::map<int /* atom */, size_t /* count */> mCountMap GUARDED_BY(mLock); // sorted
66     mutable std::mutex mLock;
67 };
68 
69 } // namespace android::mediametrics
70