• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <errno.h>
20 
21 #include <UniquePtr.h>
22 
23 #include "Log.h"
24 #include "FileUtil.h"
25 #include "Report.h"
26 #include "StringUtil.h"
27 #include "task/TaskCase.h"
28 #include "task/TaskGeneric.h"
29 #include "task/TaskSave.h"
30 
31 static const android::String8 STR_FILE("file");
32 static const android::String8 STR_REPORT("report");
33 
TaskSave()34 TaskSave::TaskSave()
35     : TaskGeneric(TaskGeneric::ETaskSave)
36 {
37     const android::String8* list[] = {&STR_FILE, &STR_REPORT, NULL};
38     registerSupportedStringAttributes(list);
39 }
40 
~TaskSave()41 TaskSave::~TaskSave()
42 {
43 
44 }
45 
handleFile()46 bool TaskSave::handleFile()
47 {
48     android::String8 fileValue;
49     if (!findStringAttribute(STR_FILE, fileValue)) {
50         LOGI("no saving to file");
51         return true; // true as there is no need to save
52     }
53 
54     UniquePtr<std::vector<android::String8> > list(StringUtil::split(fileValue, ','));
55     std::vector<android::String8>* listp = list.get();
56     if (listp == NULL) {
57         LOGE("alloc failed");
58         return false;
59     }
60 
61     android::String8 dirName;
62     if (!FileUtil::prepare(dirName)) {
63         LOGE("cannot prepare report dir");
64         return false;
65     }
66     android::String8 caseName;
67     if (!getTestCase()->getCaseName(caseName)) {
68         return false;
69     }
70     dirName.appendPath(caseName);
71     int result = mkdir(dirName.string(), S_IRWXU);
72     if ((result == -1) && (errno != EEXIST)) {
73         LOGE("mkdir of save dir %s failed, error %d", dirName.string(), errno);
74         return false;
75     }
76 
77     for (size_t i = 0; i < listp->size(); i++) {
78         UniquePtr<std::list<TaskCase::BufferPair> > buffers(
79                 getTestCase()->findAllBuffers((*listp)[i]));
80         std::list<TaskCase::BufferPair>* buffersp = buffers.get();
81         if (buffersp == NULL) {
82             LOGE("no buffer for given pattern %s", ((*listp)[i]).string());
83             return false;
84         }
85         std::list<TaskCase::BufferPair>::iterator it = buffersp->begin();
86         std::list<TaskCase::BufferPair>::iterator end = buffersp->end();
87         for (; it != end; it++) {
88             android::String8 fileName(dirName);
89             fileName.appendPath(it->first);
90             if (!it->second->saveToFile(fileName)) {
91                 LOGE("save failed");
92                 return false;
93             }
94         }
95     }
96     return true;
97 }
98 
handleReport()99 bool TaskSave::handleReport()
100 {
101     android::String8 reportValue;
102     if (!findStringAttribute(STR_REPORT, reportValue)) {
103         LOGI("no saving to report");
104         return true; // true as there is no need to save
105     }
106 
107     UniquePtr<std::vector<android::String8> > list(StringUtil::split(reportValue, ','));
108     std::vector<android::String8>* listp = list.get();
109     if (listp == NULL) {
110         LOGE("alloc failed");
111         return false;
112     }
113     Report::Instance()->printf("=== Values stored ===");
114     for (size_t i = 0; i < listp->size(); i++) {
115         UniquePtr<std::list<TaskCase::ValuePair> > values(
116                 getTestCase()->findAllValues((*listp)[i]));
117         std::list<TaskCase::ValuePair>* valuesp = values.get();
118         if (valuesp == NULL) {
119             LOGE("no value for given pattern %s", ((*listp)[i]).string());
120             return false;
121         }
122         std::list<TaskCase::ValuePair>::iterator it = values->begin();
123         std::list<TaskCase::ValuePair>::iterator end = values->end();
124         for (; it != end; it++) {
125             if (it->second.getType() == TaskCase::Value::ETypeDouble) {
126                 Report::Instance()->printf("   %s: %f", it->first.string(),
127                         it->second.getDouble());
128             } else { //64bit int
129                 Report::Instance()->printf("   %s: %lld", it->first.string(),
130                         it->second.getInt64());
131             }
132         }
133     }
134     return true;
135 }
136 
run()137 TaskGeneric::ExecutionResult TaskSave::run()
138 {
139     bool failed = false;
140     if (!handleFile()) {
141         failed = true;
142     }
143     if (!handleReport()) {
144         failed = true;
145     }
146     if (failed) {
147         return TaskGeneric::EResultError;
148     } else {
149         return TaskGeneric::EResultOK;
150     }
151 }
152 
153 
154