Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: ui/events/ozone/evdev/libgestures_glue/gesture_feedback.cc

Issue 893753002: Dump touchpad event logs for touch log source (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored the code per previous comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/events/ozone/evdev/libgestures_glue/gesture_feedback.h"
6
7 #include <time.h>
8
9 #include <locale>
10
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/process/launch.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/threading/worker_pool.h"
18 #include "ui/events/ozone/evdev/libgestures_glue/gesture_property_provider.h"
19
20 namespace ui {
21
22 namespace {
23
24 // Binary paths.
25 const char kGzipCommand[] = "/bin/gzip";
26 const char kDateCommand[] = "/bin/date";
27
28 const size_t kTouchLogTimestampMaxSize = 80;
29
30 // Return the values in an array in one string. Used for touch logging.
31 template <typename T>
32 std::string DumpArrayProperty(const std::vector<T>& value, const char* format) {
33 std::string ret;
34 for (size_t i = 0; i < value.size(); ++i) {
35 if (i > 0)
36 ret.append(", ");
37 ret.append(base::StringPrintf(format, value[i]));
38 }
39 return ret;
40 }
41
42 // Return the values in a gesture property in one string. Used for touch
43 // logging.
44 std::string DumpGesturePropertyValue(GesturesProp* property) {
45 switch (property->type()) {
46 case GesturePropertyProvider::PT_INT:
47 return DumpArrayProperty(property->GetIntValue(), "%d");
48 break;
49 case GesturePropertyProvider::PT_SHORT:
50 return DumpArrayProperty(property->GetShortValue(), "%d");
51 break;
52 case GesturePropertyProvider::PT_BOOL:
53 return DumpArrayProperty(property->GetBoolValue(), "%d");
54 break;
55 case GesturePropertyProvider::PT_STRING:
56 return "\"" + property->GetStringValue() + "\"";
57 break;
58 case GesturePropertyProvider::PT_REAL:
59 return DumpArrayProperty(property->GetDoubleValue(), "%lf");
60 break;
61 default:
62 NOTREACHED();
63 break;
64 }
65 return std::string();
66 }
67
68 // Compress dumped event logs in place.
69 void CompressDumpedLog(scoped_ptr<std::vector<std::string>> log_paths) {
70 for (size_t i = 0; i < log_paths->size(); ++i) {
71 // Zip the file.
72 base::CommandLine command = base::CommandLine(base::FilePath(kGzipCommand));
73 command.AppendArg("-f");
74 command.AppendArg((*log_paths)[i]);
75 std::string output;
76 base::GetAppOutput(command, &output);
77
78 // Replace the original file with the zipped one.
79 base::Move(base::FilePath((*log_paths)[i] + ".gz"),
80 base::FilePath((*log_paths)[i]));
81 }
82 }
83
84 // Get the current time in a string.
85 std::string GetCurrentTimeForLogging() {
86 time_t rawtime;
87 struct tm* timeinfo;
88 char buffer[kTouchLogTimestampMaxSize];
89
90 time(&rawtime);
91 timeinfo = localtime(&rawtime);
spang 2015/02/11 19:31:46 Use the reentrant variant (localtime_r). The non-r
Shecky Lin 2015/02/12 06:51:54 Done. Thanks!
92 strftime(buffer, kTouchLogTimestampMaxSize, "%Y%m%d-%H%M%S", timeinfo);
93 return std::string(buffer);
94 }
95
96 // Canonize the device name for logging.
97 std::string GetCanonicalDeviceName(const std::string& name) {
98 std::locale loc;
99 std::string ret(name);
100 for (size_t i = 0; i < ret.size(); ++i)
101 if (!std::isalpha(ret[i], loc))
spang 2015/02/11 19:31:46 I don't think we should use std::isalpha or in gen
Shecky Lin 2015/02/12 06:51:54 Done. Thanks!
102 ret[i] = '_';
103 return ret;
104 }
105
106 // Name event logs in a way that is compatible with existing toolchain.
107 std::string GenerateEventLogName(GesturePropertyProvider* provider,
108 const base::FilePath& out_dir,
109 const std::string& prefix,
110 const std::string& now,
111 int id) {
112 return out_dir.MaybeAsASCII() + "/" + prefix + now + "." +
spang 2015/02/11 19:31:46 What's with the MaybeAsASCII()? This says to creat
Shecky Lin 2015/02/12 06:51:54 Yeah, my bad. Thanks!
113 std::to_string(id) + "." +
spang 2015/02/11 19:31:46 C++11 library functions are not allowed. Use base:
Shecky Lin 2015/02/12 06:51:54 Done.
114 GetCanonicalDeviceName(provider->GetDeviceNameById(id));
115 }
116
117 // Set the logging properties to dump event logs.
118 void StartToDumpEventLog(GesturePropertyProvider* provider,
119 const int device_id) {
120 // Dump gesture log.
121 GesturesProp* property = provider->GetProperty(device_id, "Log Path");
122 DCHECK(property);
123 property->SetStringValue(kTouchpadGestureLogPath);
124 property = provider->GetProperty(device_id, "Logging Notify");
125 DCHECK(property);
126 property->SetIntValue(std::vector<int>(1, 1));
127
128 // Dump evdev log.
129 property = provider->GetProperty(device_id, "Dump Debug Log");
130 DCHECK(property);
spang 2015/02/11 19:31:46 These DCHECKs are redundant. If it's null the next
Shecky Lin 2015/02/12 06:51:54 Done.
131 property->SetBoolValue(std::vector<bool>(1, true));
132 }
133
134 } // namespace
135
136 // Dump touch device property values to a string.
137 void DumpTouchDeviceStatus(GesturePropertyProvider* provider,
138 std::string* status) {
139 // We use DT_ALL since we want gesture property values for all devices that
140 // run with the gesture library, not just mice or touchpads.
141 std::vector<int> ids;
142 provider->GetDeviceIdsByType(DT_ALL, &ids);
143
144 // Dump the property names and values for each device.
145 for (size_t i = 0; i < ids.size(); ++i) {
146 std::vector<std::string> names = provider->GetPropertyNamesById(ids[i]);
147 status->append("\n");
148 status->append(base::StringPrintf("ID %d:\n", ids[i]));
149 status->append(base::StringPrintf(
150 "Device \'%s\':\n", provider->GetDeviceNameById(ids[i]).c_str()));
151
152 // Note that, unlike X11, we don't maintain the "atom" concept here.
153 // Therefore, the property name indices we output here shouldn't be treated
154 // as unique identifiers of the properties.
155 std::sort(names.begin(), names.end());
156 for (size_t j = 0; j < names.size(); ++j) {
157 status->append(base::StringPrintf("\t%s (%zu):", names[j].c_str(), j));
158 GesturesProp* property = provider->GetProperty(ids[i], names[j]);
159 status->append("\t" + DumpGesturePropertyValue(property) + '\n');
160 }
161 }
162 }
163
164 // Dump touch event logs.
165 void DumpTouchEventLog(GesturePropertyProvider* provider,
166 const base::FilePath& out_dir,
167 scoped_ptr<std::vector<base::FilePath>> log_paths,
168 const GetTouchEventLogReply& reply) {
169 // Get device ids.
170 std::vector<int> ids;
171 provider->GetDeviceIdsByType(DT_ALL, &ids);
172
173 // Get current time stamp.
174 std::string now = GetCurrentTimeForLogging();
175
176 // Dump event logs for gesture devices.
177 scoped_ptr<std::vector<std::string>> log_paths_to_be_compressed(
178 new std::vector<std::string>);
179 for (size_t i = 0; i < ids.size(); ++i) {
180 // First, see if the device actually uses the gesture library by checking
181 // if it has any gesture property.
182 std::vector<std::string> names = provider->GetPropertyNamesById(ids[i]);
183 if (names.size() == 0)
184 continue;
185
186 // Set the logging properties to dump event logs. This needs to be done
187 // synchronously for now or we might have race conditions on the debug
188 // buffer. If the performance becomes a concern then, we can fork and
189 // synchronize it.
190 //
191 // TODO(sheckylin): Make sure this has no performance impact for user
192 // feedbacks.
193 StartToDumpEventLog(provider, ids[i]);
194
195 // Rename/move the file to another place since each device's log is
196 // always dumped using the same name.
197 std::string gesture_log_filename = GenerateEventLogName(
198 provider, out_dir, "touchpad_activity_", now, ids[i]);
199 base::Move(base::FilePath(kTouchpadGestureLogPath),
200 base::FilePath(gesture_log_filename));
201 std::string evdev_log_filename = GenerateEventLogName(
202 provider, out_dir, "cmt_input_events_", now, ids[i]);
203 base::Move(base::FilePath(kTouchpadEvdevLogPath),
204 base::FilePath(evdev_log_filename));
205
206 // Historically, we compress touchpad/mouse logs with gzip before tarring
207 // them up. We DONT compress touchscreen logs though.
208 log_paths_to_be_compressed->push_back(gesture_log_filename);
209 log_paths->push_back(base::FilePath(gesture_log_filename));
210 log_paths_to_be_compressed->push_back(evdev_log_filename);
211 log_paths->push_back(base::FilePath(evdev_log_filename));
212 }
213
214 // Compress touchpad/mouse logs on another thread and return.
215 base::WorkerPool::PostTaskAndReply(
216 FROM_HERE,
217 base::Bind(&CompressDumpedLog, base::Passed(&log_paths_to_be_compressed)),
218 base::Bind(reply, base::Passed(&log_paths)), true /* task_is_slow */);
219 }
220
221 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698