1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Extract values by name from logs.
22 *//*--------------------------------------------------------------------*/
24 #include "xeTestLogParser.hpp"
25 #include "xeTestResultParser.hpp"
26 #include "deFilePath.hpp"
50 vector<string> tagNames;
54 typedef xe::ri::NumericValue Value;
59 xe::TestCaseType caseType;
60 xe::TestStatusCode statusCode;
66 class BatchResultValues
69 BatchResultValues (const vector<string>& tagNames)
70 : m_tagNames(tagNames)
74 ~BatchResultValues (void)
76 for (vector<CaseValues*>::iterator i = m_caseValues.begin(); i != m_caseValues.end(); ++i)
80 void add (const CaseValues& result)
82 CaseValues* copy = new CaseValues(result);
85 m_caseValues.push_back(copy);
94 const vector<string>& getTagNames (void) const { return m_tagNames; }
96 size_t size (void) const { return m_caseValues.size(); }
97 const CaseValues& operator[] (size_t ndx) const { return *m_caseValues[ndx]; }
100 vector<string> m_tagNames;
101 vector<CaseValues*> m_caseValues;
104 static Value findValueByTag (const xe::ri::List& items, const string& tagName)
106 for (int ndx = 0; ndx < items.getNumItems(); ndx++)
108 const xe::ri::Item& item = items.getItem(ndx);
110 if (item.getType() == xe::ri::TYPE_SECTION)
112 const Value value = findValueByTag(static_cast<const xe::ri::Section&>(item).items, tagName);
113 if (value.getType() != Value::TYPE_EMPTY)
116 else if (item.getType() == xe::ri::TYPE_NUMBER)
118 const xe::ri::Number& value = static_cast<const xe::ri::Number&>(item);
126 class TagParser : public xe::TestLogHandler
129 TagParser (BatchResultValues& result)
134 void setSessionInfo (const xe::SessionInfo&)
139 xe::TestCaseResultPtr startTestCaseResult (const char* casePath)
141 return xe::TestCaseResultPtr(new xe::TestCaseResultData(casePath));
144 void testCaseResultUpdated (const xe::TestCaseResultPtr&)
149 void testCaseResultComplete (const xe::TestCaseResultPtr& caseData)
151 const vector<string>& tagNames = m_result.getTagNames();
152 CaseValues tagResult;
154 tagResult.casePath = caseData->getTestCasePath();
155 tagResult.caseType = xe::TESTCASETYPE_SELF_VALIDATE;
156 tagResult.statusCode = caseData->getStatusCode();
157 tagResult.statusDetails = caseData->getStatusDetails();
158 tagResult.values.resize(tagNames.size());
160 if (caseData->getDataSize() > 0 && caseData->getStatusCode() == xe::TESTSTATUSCODE_LAST)
162 xe::TestCaseResult fullResult;
163 xe::TestResultParser::ParseResult parseResult;
165 m_testResultParser.init(&fullResult);
166 parseResult = m_testResultParser.parse(caseData->getData(), caseData->getDataSize());
168 if ((parseResult != xe::TestResultParser::PARSERESULT_ERROR && fullResult.statusCode != xe::TESTSTATUSCODE_LAST) ||
169 (tagResult.statusCode == xe::TESTSTATUSCODE_LAST && fullResult.statusCode != xe::TESTSTATUSCODE_LAST))
171 tagResult.statusCode = fullResult.statusCode;
172 tagResult.statusDetails = fullResult.statusDetails;
174 else if (tagResult.statusCode == xe::TESTSTATUSCODE_LAST)
176 DE_ASSERT(parseResult == xe::TestResultParser::PARSERESULT_ERROR);
177 tagResult.statusCode = xe::TESTSTATUSCODE_INTERNAL_ERROR;
178 tagResult.statusDetails = "Test case result parsing failed";
181 if (parseResult != xe::TestResultParser::PARSERESULT_ERROR)
183 for (int valNdx = 0; valNdx < (int)tagNames.size(); valNdx++)
184 tagResult.values[valNdx] = findValueByTag(fullResult.resultItems, tagNames[valNdx]);
188 m_result.add(tagResult);
192 BatchResultValues& m_result;
193 xe::TestResultParser m_testResultParser;
196 static void readLogFile (BatchResultValues& batchResult, const char* filename)
198 std::ifstream in (filename, std::ifstream::binary|std::ifstream::in);
199 TagParser resultHandler (batchResult);
200 xe::TestLogParser parser (&resultHandler);
205 throw std::runtime_error(string("Failed to open '") + filename + "'");
209 in.read((char*)&buf[0], DE_LENGTH_OF_ARRAY(buf));
210 numRead = (int)in.gcount();
215 parser.parse(&buf[0], numRead);
221 static void printTaggedValues (const CommandLine& cmdLine, std::ostream& dst)
223 BatchResultValues values(cmdLine.tagNames);
225 readLogFile(values, cmdLine.filename.c_str());
230 if (cmdLine.statusCode)
231 dst << ",StatusCode";
233 for (vector<string>::const_iterator tagName = values.getTagNames().begin(); tagName != values.getTagNames().end(); ++tagName)
234 dst << "," << *tagName;
239 for (int resultNdx = 0; resultNdx < (int)values.size(); resultNdx++)
241 const CaseValues& result = values[resultNdx];
243 dst << result.casePath;
244 if (cmdLine.statusCode)
245 dst << "," << xe::getTestStatusCodeName(result.statusCode);
247 for (vector<Value>::const_iterator value = result.values.begin(); value != result.values.end(); ++value)
248 dst << "," << *value;
254 static void printHelp (const char* binName)
256 printf("%s: [filename] [name 1] [[name 2]...]\n", binName);
257 printf(" --statuscode Include status code as first entry.\n");
260 static bool parseCommandLine (CommandLine& cmdLine, int argc, const char* const* argv)
262 for (int argNdx = 1; argNdx < argc; argNdx++)
264 const char* arg = argv[argNdx];
266 if (deStringEqual(arg, "--statuscode"))
267 cmdLine.statusCode = true;
268 else if (!deStringBeginsWith(arg, "--"))
270 if (cmdLine.filename.empty())
271 cmdLine.filename = arg;
273 cmdLine.tagNames.push_back(arg);
279 if (cmdLine.filename.empty())
285 int main (int argc, const char* const* argv)
291 if (!parseCommandLine(cmdLine, argc, argv))
297 printTaggedValues(cmdLine, std::cout);
299 catch (const std::exception& e)
301 printf("FATAL ERROR: %s\n", e.what());