1 /*
2  * Copyright (c) 2017, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *    * Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *    * Redistributions in binary form must reproduce the above
10  *      copyright notice, this list of conditions and the following
11  *      disclaimer in the documentation and/or other materials provided
12  *      with the distribution.
13  *    * Neither the name of The Linux Foundation nor the names of its
14  *      contributors may be used to endorse or promote products derived
15  *      from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #define LOG_TAG "IPAHALService/dump"
30 
31 /* External Includes */
32 #include <cutils/log.h>
33 #include <deque>
34 #include <string>
35 #include <sys/types.h>
36 #include <vector>
37 
38 /* Internal Includes */
39 #include "LocalLogBuffer.h"
40 
41 /* Namespace pollution avoidance */
42 using ::std::deque;
43 using ::std::string;
44 using ::std::vector;
45 
46 
FunctionLog(string funcName)47 LocalLogBuffer::FunctionLog::FunctionLog(string funcName) : mName(funcName) {
48     mArgsProvided = false;
49 } /* FunctionLog */
50 
FunctionLog(const FunctionLog & other)51 LocalLogBuffer::FunctionLog::FunctionLog(const FunctionLog& other) :
52         mName(other.mName) {
53     mArgsProvided = other.mArgsProvided;
54     /* Is this right? How do you copy stringstreams without wizardry? */
55     mSSArgs.str(other.mSSArgs.str());
56     mSSReturn.str(other.mSSReturn.str());
57 } /* FunctionLog */
58 
addArg(string kw,string arg)59 void LocalLogBuffer::FunctionLog::addArg(string kw, string arg) {
60     maybeAddArgsComma();
61     mSSArgs << kw << "=" << arg;
62 } /* addArg */
63 
addArg(string kw,vector<string> args)64 void LocalLogBuffer::FunctionLog::addArg(string kw, vector<string> args) {
65     maybeAddArgsComma();
66     mSSArgs << kw << "=[";
67     for (size_t i = 0; i < args.size(); i++) {
68         mSSArgs << args[i];
69         if (i < (args.size() - 1))
70             mSSArgs << ", ";
71     }
72     mSSArgs << "]";
73 } /* addArg */
74 
addArg(string kw,uint64_t arg)75 void LocalLogBuffer::FunctionLog::addArg(string kw, uint64_t arg) {
76     maybeAddArgsComma();
77     mSSArgs << kw << "=" << arg;
78 } /* addArg */
79 
maybeAddArgsComma()80 void LocalLogBuffer::FunctionLog::maybeAddArgsComma() {
81     if (!mArgsProvided) {
82         mArgsProvided = true;
83     } else {
84         mSSArgs << ", ";
85     }
86 } /* maybeAddArgsComma */
87 
setResult(bool success,string msg)88 void LocalLogBuffer::FunctionLog::setResult(bool success, string msg) {
89     mSSReturn << "[" << ((success) ? "success" : "failure") << ", " << msg
90               << "]";
91 } /* setResult */
92 
setResult(vector<unsigned int> ret)93 void LocalLogBuffer::FunctionLog::setResult(vector<unsigned int> ret) {
94     mSSReturn << "[";
95     for (size_t i = 0; i < ret.size(); i++) {
96         mSSReturn << ret[i];
97         if (i < (ret.size() - 1))
98             mSSReturn << ", ";
99     }
100     mSSReturn << "]";
101 } /* setResult */
102 
setResult(uint64_t rx,uint64_t tx)103 void LocalLogBuffer::FunctionLog::setResult(uint64_t rx, uint64_t tx) {
104     mSSReturn << "[rx=" << rx << ", tx=" << tx << "]";
105 } /* setResult */
106 
toString()107 string LocalLogBuffer::FunctionLog::toString() {
108     stringstream ret;
109     ret << mName << "(" << mSSArgs.str() << ") returned " << mSSReturn.str();
110     return ret.str();
111 } /* toString */
112 
LocalLogBuffer(string name,int maxLogs)113 LocalLogBuffer::LocalLogBuffer(string name, int maxLogs) : mName(name),
114         mMaxLogs(maxLogs) {
115 } /* LocalLogBuffer */
116 
addLog(FunctionLog log)117 void LocalLogBuffer::addLog(FunctionLog log) {
118     while (mLogs.size() > mMaxLogs)
119         mLogs.pop_front();
120     mLogs.push_back(log);
121 } /* addLog */
122 
toLogcat()123 void LocalLogBuffer::toLogcat() {
124     for (size_t i = 0; i < mLogs.size(); i++)
125         ALOGD("%s: %s", mName.c_str(), mLogs[i].toString().c_str());
126 } /* toLogcat */
127