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 <unistd.h>
37 #include <vector>
38
39 /* Internal Includes */
40 #include "LocalLogBuffer.h"
41
42 /* Namespace pollution avoidance */
43 using ::std::deque;
44 using ::std::string;
45 using ::std::vector;
46
47
FunctionLog(string funcName)48 LocalLogBuffer::FunctionLog::FunctionLog(string funcName) : mName(funcName) {
49 mArgsProvided = false;
50 } /* FunctionLog */
51
FunctionLog(const FunctionLog & other)52 LocalLogBuffer::FunctionLog::FunctionLog(const FunctionLog& other) :
53 mName(other.mName) {
54 mArgsProvided = other.mArgsProvided;
55 /* Is this right? How do you copy stringstreams without wizardry? */
56 mSSArgs.str(other.mSSArgs.str());
57 mSSReturn.str(other.mSSReturn.str());
58 } /* FunctionLog */
59
addArg(string kw,string arg)60 void LocalLogBuffer::FunctionLog::addArg(string kw, string arg) {
61 maybeAddArgsComma();
62 mSSArgs << kw << "=" << arg;
63 } /* addArg */
64
addArg(string kw,vector<string> args)65 void LocalLogBuffer::FunctionLog::addArg(string kw, vector<string> args) {
66 maybeAddArgsComma();
67 mSSArgs << kw << "=[";
68 for (size_t i = 0; i < args.size(); i++) {
69 mSSArgs << args[i];
70 if (i < (args.size() - 1))
71 mSSArgs << ", ";
72 }
73 mSSArgs << "]";
74 } /* addArg */
75
addArg(string kw,uint64_t arg)76 void LocalLogBuffer::FunctionLog::addArg(string kw, uint64_t arg) {
77 maybeAddArgsComma();
78 mSSArgs << kw << "=" << arg;
79 } /* addArg */
80
maybeAddArgsComma()81 void LocalLogBuffer::FunctionLog::maybeAddArgsComma() {
82 if (!mArgsProvided) {
83 mArgsProvided = true;
84 } else {
85 mSSArgs << ", ";
86 }
87 } /* maybeAddArgsComma */
88
setResult(bool success,string msg)89 void LocalLogBuffer::FunctionLog::setResult(bool success, string msg) {
90 mSSReturn << "[" << ((success) ? "success" : "failure") << ", " << msg
91 << "]";
92 } /* setResult */
93
setResult(vector<unsigned int> ret)94 void LocalLogBuffer::FunctionLog::setResult(vector<unsigned int> ret) {
95 mSSReturn << "[";
96 for (size_t i = 0; i < ret.size(); i++) {
97 mSSReturn << ret[i];
98 if (i < (ret.size() - 1))
99 mSSReturn << ", ";
100 }
101 mSSReturn << "]";
102 } /* setResult */
103
setResult(uint64_t rx,uint64_t tx)104 void LocalLogBuffer::FunctionLog::setResult(uint64_t rx, uint64_t tx) {
105 mSSReturn << "[rx=" << rx << ", tx=" << tx << "]";
106 } /* setResult */
107
toString()108 string LocalLogBuffer::FunctionLog::toString() {
109 stringstream ret;
110 ret << mName << "(" << mSSArgs.str() << ") returned " << mSSReturn.str();
111 return ret.str();
112 } /* toString */
113
LocalLogBuffer(string name,int maxLogs)114 LocalLogBuffer::LocalLogBuffer(string name, int maxLogs) : mName(name),
115 mMaxLogs(maxLogs) {
116 } /* LocalLogBuffer */
117
addLog(FunctionLog log)118 void LocalLogBuffer::addLog(FunctionLog log) {
119 while (mLogs.size() > mMaxLogs)
120 mLogs.pop_front();
121 mLogs.push_back(log);
122 } /* addLog */
123
toLogcat()124 void LocalLogBuffer::toLogcat() {
125 for (size_t i = 0; i < mLogs.size(); i++)
126 ALOGD("%s: %s", mName.c_str(), mLogs[i].toString().c_str());
127 } /* toLogcat */
128
toFd(int fd)129 void LocalLogBuffer::toFd(int fd) {
130 for (size_t i = 0; i < mLogs.size(); i++) {
131 string line = mName + " " + mLogs[i].toString();
132 write(fd, line.c_str(), line.size());
133 write(fd, "\n", 1);
134 }
135 } /* toFd */
136