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.
5 #include "ui/events/ozone/evdev/libgestures_glue/gesture_feedback.h"
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/process/launch.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.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"
25 const char kGzipCommand
[] = "/bin/gzip";
27 const size_t kTouchLogTimestampMaxSize
= 80;
29 // Return the values in an array in one string. Used for touch logging.
31 std::string
DumpArrayProperty(const std::vector
<T
>& value
, const char* format
) {
33 for (size_t i
= 0; i
< value
.size(); ++i
) {
36 ret
.append(base::StringPrintf(format
, value
[i
]));
41 // Return the values in a gesture property in one string. Used for touch
43 std::string
DumpGesturePropertyValue(GesturesProp
* property
) {
44 switch (property
->type()) {
45 case GesturePropertyProvider::PT_INT
:
46 return DumpArrayProperty(property
->GetIntValue(), "%d");
48 case GesturePropertyProvider::PT_SHORT
:
49 return DumpArrayProperty(property
->GetShortValue(), "%d");
51 case GesturePropertyProvider::PT_BOOL
:
52 return DumpArrayProperty(property
->GetBoolValue(), "%d");
54 case GesturePropertyProvider::PT_STRING
:
55 return "\"" + property
->GetStringValue() + "\"";
57 case GesturePropertyProvider::PT_REAL
:
58 return DumpArrayProperty(property
->GetDoubleValue(), "%lf");
67 // Compress dumped event logs in place.
68 void CompressDumpedLog(scoped_ptr
<std::vector
<std::string
>> log_paths
) {
69 for (size_t i
= 0; i
< log_paths
->size(); ++i
) {
71 base::CommandLine command
= base::CommandLine(base::FilePath(kGzipCommand
));
72 command
.AppendArg("-f");
73 command
.AppendArg((*log_paths
)[i
]);
75 base::GetAppOutput(command
, &output
);
77 // Replace the original file with the zipped one.
78 base::Move(base::FilePath((*log_paths
)[i
] + ".gz"),
79 base::FilePath((*log_paths
)[i
]));
83 // Get the current time in a string.
84 std::string
GetCurrentTimeForLogging() {
87 char buffer
[kTouchLogTimestampMaxSize
];
90 if (!localtime_r(&rawtime
, &timeinfo
)) {
91 PLOG(ERROR
) << "localtime_r failed";
94 if (!strftime(buffer
, kTouchLogTimestampMaxSize
, "%Y%m%d-%H%M%S", &timeinfo
))
96 return std::string(buffer
);
99 // Canonize the device name for logging.
100 std::string
GetCanonicalDeviceName(const std::string
& name
) {
101 std::string
ret(name
);
102 for (size_t i
= 0; i
< ret
.size(); ++i
)
103 if (!base::IsAsciiAlpha(ret
[i
]))
108 // Name event logs in a way that is compatible with existing toolchain.
109 std::string
GenerateEventLogName(const base::FilePath
& out_dir
,
110 const std::string
& prefix
,
111 const std::string
& now
,
113 return out_dir
.value() + "/" + prefix
+ now
+ "." + base::IntToString(id
);
115 // Name event logs in a way that is compatible with existing toolchain.
116 std::string
GenerateEventLogName(GesturePropertyProvider
* provider
,
117 const base::FilePath
& out_dir
,
118 const std::string
& prefix
,
119 const std::string
& now
,
121 return out_dir
.value() + "/" + prefix
+ now
+ "." + base::IntToString(id
) +
122 "." + GetCanonicalDeviceName(provider
->GetDeviceNameById(id
));
125 // Set the logging properties to dump event logs.
126 void StartToDumpEventLog(GesturePropertyProvider
* provider
,
127 const int device_id
) {
129 GesturesProp
* property
= provider
->GetProperty(device_id
, "Log Path");
130 property
->SetStringValue(kTouchpadGestureLogPath
);
131 property
= provider
->GetProperty(device_id
, "Logging Notify");
132 property
->SetIntValue(std::vector
<int>(1, 1));
135 property
= provider
->GetProperty(device_id
, "Dump Debug Log");
136 property
->SetBoolValue(std::vector
<bool>(1, true));
141 // Dump touch device property values to a string.
142 void DumpTouchDeviceStatus(GesturePropertyProvider
* provider
,
143 std::string
* status
) {
144 // We use DT_ALL since we want gesture property values for all devices that
145 // run with the gesture library, not just mice or touchpads.
146 std::vector
<int> ids
;
147 provider
->GetDeviceIdsByType(DT_ALL
, &ids
);
149 // Dump the property names and values for each device.
150 for (size_t i
= 0; i
< ids
.size(); ++i
) {
151 std::vector
<std::string
> names
= provider
->GetPropertyNamesById(ids
[i
]);
152 status
->append("\n");
153 status
->append(base::StringPrintf("ID %d:\n", ids
[i
]));
154 status
->append(base::StringPrintf(
155 "Device \'%s\':\n", provider
->GetDeviceNameById(ids
[i
]).c_str()));
157 // Note that, unlike X11, we don't maintain the "atom" concept here.
158 // Therefore, the property name indices we output here shouldn't be treated
159 // as unique identifiers of the properties.
160 std::sort(names
.begin(), names
.end());
161 for (size_t j
= 0; j
< names
.size(); ++j
) {
162 status
->append(base::StringPrintf("\t%s (%zu):", names
[j
].c_str(), j
));
163 GesturesProp
* property
= provider
->GetProperty(ids
[i
], names
[j
]);
164 status
->append("\t" + DumpGesturePropertyValue(property
) + '\n');
169 // Dump touch event logs.
170 void DumpTouchEventLog(
171 std::map
<base::FilePath
, EventConverterEvdev
*>& converters
,
172 GesturePropertyProvider
* provider
,
173 const base::FilePath
& out_dir
,
174 scoped_ptr
<std::vector
<base::FilePath
>> log_paths
,
175 const GetTouchEventLogReply
& reply
) {
177 std::vector
<int> ids
;
178 provider
->GetDeviceIdsByType(DT_ALL
, &ids
);
180 // Get current time stamp.
181 std::string now
= GetCurrentTimeForLogging();
183 // Dump event logs for gesture devices.
184 scoped_ptr
<std::vector
<std::string
>> log_paths_to_be_compressed(
185 new std::vector
<std::string
>);
186 for (size_t i
= 0; i
< ids
.size(); ++i
) {
187 // First, see if the device actually uses the gesture library by checking
188 // if it has any gesture property.
189 std::vector
<std::string
> names
= provider
->GetPropertyNamesById(ids
[i
]);
190 if (names
.size() == 0)
193 // Set the logging properties to dump event logs. This needs to be done
194 // synchronously for now or we might have race conditions on the debug
195 // buffer. If the performance becomes a concern then, we can fork and
198 // TODO(sheckylin): Make sure this has no performance impact for user
200 StartToDumpEventLog(provider
, ids
[i
]);
202 // Rename/move the file to another place since each device's log is
203 // always dumped using the same name.
204 std::string gesture_log_filename
= GenerateEventLogName(
205 provider
, out_dir
, "touchpad_activity_", now
, ids
[i
]);
206 base::Move(base::FilePath(kTouchpadGestureLogPath
),
207 base::FilePath(gesture_log_filename
));
208 std::string evdev_log_filename
= GenerateEventLogName(
209 provider
, out_dir
, "cmt_input_events_", now
, ids
[i
]);
210 base::Move(base::FilePath(kTouchpadEvdevLogPath
),
211 base::FilePath(evdev_log_filename
));
213 // Historically, we compress touchpad/mouse logs with gzip before tarring
214 // them up. We DONT compress touchscreen logs though.
215 log_paths_to_be_compressed
->push_back(gesture_log_filename
);
216 log_paths
->push_back(base::FilePath(gesture_log_filename
));
217 log_paths_to_be_compressed
->push_back(evdev_log_filename
);
218 log_paths
->push_back(base::FilePath(evdev_log_filename
));
221 for (auto it
= converters
.begin(); it
!= converters
.end(); ++it
) {
222 EventConverterEvdev
* converter
= it
->second
;
223 if (converter
->HasTouchscreen()) {
224 converter
->DumpTouchEventLog(kInputEventsLogFile
);
225 std::string touch_evdev_log_filename
= GenerateEventLogName(
226 out_dir
, "evdev_input_events_", now
, converter
->id());
227 base::Move(base::FilePath(kInputEventsLogFile
),
228 base::FilePath(touch_evdev_log_filename
));
229 log_paths
->push_back(base::FilePath(touch_evdev_log_filename
));
233 // Compress touchpad/mouse logs on another thread and return.
234 base::WorkerPool::PostTaskAndReply(
236 base::Bind(&CompressDumpedLog
, base::Passed(&log_paths_to_be_compressed
)),
237 base::Bind(reply
, base::Passed(&log_paths
)), true /* task_is_slow */);