| 1 |
// Copyright © 2016 Canonical Ltd. |
| 2 |
// Author: Loïc Molinari <[email protected]> |
| 3 |
// |
| 4 |
// This file is part of Lomiri UI Toolkit. |
| 5 |
// |
| 6 |
// Lomiri UI Toolkit is free software: you can redistribute it and/or modify it |
| 7 |
// under the terms of the GNU Lesser General Public License as published by the |
| 8 |
// Free Software Foundation; version 3. |
| 9 |
// |
| 10 |
// Lomiri UI Toolkit is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
// for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#include "logger_p.h" |
| 19 |
|
| 20 |
#include <dlfcn.h> |
| 21 |
|
| 22 |
#include <QtCore/QDir> |
| 23 |
#include <QtCore/QTime> |
| 24 |
|
| 25 |
#include "events.h" |
| 26 |
#include "lomirimetricsglobal_p.h" |
| 27 |
#if defined(Q_OS_LINUX) |
| 28 |
#define TRACEPOINT_DEFINE |
| 29 |
#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE |
| 30 |
#include "lttng/lttng_p.h" |
| 31 |
#endif // defined(Q_OS_LINUX) |
| 32 |
|
| 33 |
UMFileLogger::UMFileLogger(const QString& fileName, bool parsable) |
| 34 |
: d_ptr(new UMFileLoggerPrivate(fileName, parsable)) |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
UMFileLoggerPrivate::UMFileLoggerPrivate(const QString& fileName, bool parsable) |
| 39 |
{ |
| 40 |
if (QDir::isRelativePath(fileName)) { |
| 41 |
m_file.setFileName(QString(QDir::currentPath() + QDir::separator() + fileName)); |
| 42 |
} else { |
| 43 |
m_file.setFileName(fileName); |
| 44 |
} |
| 45 |
|
| 46 |
if (m_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Unbuffered)) { |
| 47 |
m_textStream.setDevice(&m_file); |
| 48 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 49 |
m_textStream.setCodec("ISO 8859-1"); |
| 50 |
#endif |
| 51 |
m_textStream.setRealNumberPrecision(2); |
| 52 |
m_textStream.setRealNumberNotation(QTextStream::FixedNotation); |
| 53 |
m_flags = Open | Parsable; |
| 54 |
if (parsable) { |
| 55 |
m_flags |= Parsable; |
| 56 |
} |
| 57 |
} else { |
| 58 |
m_flags = 0; |
| 59 |
WARN("FileLogger: Can't open file %s '%s'.", fileName.toLatin1().constData(), |
| 60 |
m_file.errorString().toLatin1().constData()); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
UMFileLogger::UMFileLogger(FILE* fileHandle, bool parsable) |
| 65 |
: d_ptr(new UMFileLoggerPrivate(fileHandle, parsable)) |
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
UMFileLoggerPrivate::UMFileLoggerPrivate(FILE* fileHandle, bool parsable) |
| 70 |
{ |
| 71 |
if (m_file.open(fileHandle, QIODevice::WriteOnly | QIODevice::Text | QIODevice::Unbuffered)) { |
| 72 |
m_textStream.setDevice(&m_file); |
| 73 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 74 |
m_textStream.setCodec("ISO 8859-1"); |
| 75 |
#endif |
| 76 |
m_textStream.setRealNumberPrecision(2); |
| 77 |
m_textStream.setRealNumberNotation(QTextStream::FixedNotation); |
| 78 |
if ((fileHandle == stdout || fileHandle == stderr) && |
| 79 |
!qEnvironmentVariableIsSet("UM_NO_LOGGER_COLOR")) { |
| 80 |
m_flags = Open | Colored; |
| 81 |
} else { |
| 82 |
m_flags = Open; |
| 83 |
} |
| 84 |
if (parsable) { |
| 85 |
m_flags |= Parsable; |
| 86 |
} |
| 87 |
} else { |
| 88 |
m_flags = 0; |
| 89 |
WARN("FileLogger: Can't open file handle '%s'.", |
| 90 |
m_file.errorString().toLatin1().constData()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
UMFileLogger::~UMFileLogger() |
| 95 |
{ |
| 96 |
delete d_ptr; |
| 97 |
} |
| 98 |
|
| 99 |
bool UMFileLogger::isOpen() |
| 100 |
{ |
| 101 |
return !!(d_func()->m_flags & UMFileLoggerPrivate::Open); |
| 102 |
} |
| 103 |
|
| 104 |
// FIXME(loicm) We should maybe get rid of QTextStream and directly write to the |
| 105 |
// device for efficiency reasons. |
| 106 |
|
| 107 |
void UMFileLogger::log(const UMEvent& event) |
| 108 |
{ |
| 109 |
d_func()->log(event); |
| 110 |
} |
| 111 |
|
| 112 |
void UMFileLoggerPrivate::log(const UMEvent& event) |
| 113 |
{ |
| 114 |
if (m_flags & Open) { |
| 115 |
// ANSI/VT100 terminal codes. |
| 116 |
const char* const dim = m_flags & Colored ? "\033[02m" : ""; |
| 117 |
const char* const reset = m_flags & Colored ? "\033[00m" : ""; |
| 118 |
const char* const dimColon = m_flags & Colored ? "\033[02m:\033[00m" : "="; |
| 119 |
|
| 120 |
QTime timeStamp = QTime(0, 0).addMSecs(event.timeStamp / 1000000); |
| 121 |
QString timeString = !timeStamp.hour() |
| 122 |
? timeStamp.toString(QStringLiteral("mm:ss:zzz")) |
| 123 |
: timeStamp.toString(QStringLiteral("hh:mm:ss:zzz")); |
| 124 |
|
| 125 |
switch (event.type) { |
| 126 |
case UMEvent::Process: { |
| 127 |
if (m_flags & Parsable) { |
| 128 |
m_textStream |
| 129 |
<< "P " |
| 130 |
<< event.timeStamp << ' ' |
| 131 |
<< event.process.cpuUsage << ' ' |
| 132 |
<< event.process.vszMemory << ' ' |
| 133 |
<< event.process.rssMemory << ' ' |
| 134 |
<< event.process.threadCount << '\n' |
| 135 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 136 |
<< flush; |
| 137 |
#else |
| 138 |
<< Qt::flush; |
| 139 |
#endif |
| 140 |
} else { |
| 141 |
m_textStream |
| 142 |
<< (m_flags & Colored ? "\033[33mP\033[00m " : "P ") |
| 143 |
<< dim << timeString << reset << ' ' |
| 144 |
<< "CPU" << dimColon << event.process.cpuUsage << "% " |
| 145 |
<< "VSZ" << dimColon << event.process.vszMemory << "kB " |
| 146 |
<< "RSS" << dimColon << event.process.rssMemory << "kB " |
| 147 |
<< "Threads" << dimColon << event.process.threadCount |
| 148 |
<< '\n' |
| 149 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 150 |
<< flush; |
| 151 |
#else |
| 152 |
<< Qt::flush; |
| 153 |
#endif |
| 154 |
} |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
case UMEvent::Frame: |
| 159 |
if (m_flags & Parsable) { |
| 160 |
m_textStream |
| 161 |
<< "F " |
| 162 |
<< event.timeStamp << ' ' |
| 163 |
<< event.frame.window << ' ' |
| 164 |
<< event.frame.number << ' ' |
| 165 |
<< event.frame.deltaTime << ' ' |
| 166 |
<< event.frame.syncTime << ' ' |
| 167 |
<< event.frame.renderTime << ' ' |
| 168 |
<< event.frame.gpuTime << ' ' |
| 169 |
<< event.frame.swapTime << '\n' |
| 170 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 171 |
<< flush; |
| 172 |
#else |
| 173 |
<< Qt::flush; |
| 174 |
#endif |
| 175 |
} else { |
| 176 |
m_textStream |
| 177 |
<< (m_flags & Colored ? "\033[36mF\033[00m " : "F ") |
| 178 |
<< dim << timeString << reset << ' ' |
| 179 |
<< "Win" << dimColon << event.frame.window << ' ' |
| 180 |
<< "N" << dimColon << event.frame.number << ' ' |
| 181 |
<< "Delta" << dimColon << event.frame.deltaTime / 1000000.0f << "ms " |
| 182 |
<< "Sync" << dimColon << event.frame.syncTime / 1000000.0f << "ms " |
| 183 |
<< "Render" << dimColon << event.frame.renderTime / 1000000.0f << "ms " |
| 184 |
<< "GPU" << dimColon << event.frame.gpuTime / 1000000.0f << "ms " |
| 185 |
<< "Swap" << dimColon << event.frame.swapTime / 1000000.0f << "ms\n" |
| 186 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 187 |
<< flush; |
| 188 |
#else |
| 189 |
<< Qt::flush; |
| 190 |
#endif |
| 191 |
} |
| 192 |
break; |
| 193 |
|
| 194 |
case UMEvent::Window: { |
| 195 |
if (m_flags & Parsable) { |
| 196 |
m_textStream |
| 197 |
<< "W " |
| 198 |
<< event.timeStamp << ' ' |
| 199 |
<< event.window.id << ' ' |
| 200 |
<< event.window.state << ' ' |
| 201 |
<< event.window.width << ' ' |
| 202 |
<< event.window.height << '\n' |
| 203 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 204 |
<< flush; |
| 205 |
#else |
| 206 |
<< Qt::flush; |
| 207 |
#endif |
| 208 |
} else { |
| 209 |
const char* const stateString[] = { "Hidden", "Shown", "Resized" }; |
| 210 |
Q_STATIC_ASSERT(ARRAY_SIZE(stateString) == UMWindowEvent::StateCount); |
| 211 |
m_textStream |
| 212 |
<< (m_flags & Colored ? "\033[35mW\033[00m " : "W ") |
| 213 |
<< dim << timeString << reset << ' ' |
| 214 |
<< "Id" << dimColon << event.window.id << ' ' |
| 215 |
<< "State" << dimColon << stateString[event.window.state] << ' ' |
| 216 |
<< "Size" << dimColon << event.window.width << 'x' << event.window.height |
| 217 |
<< '\n' |
| 218 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 219 |
<< flush; |
| 220 |
#else |
| 221 |
<< Qt::flush; |
| 222 |
#endif |
| 223 |
} |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
case UMEvent::Generic: { |
| 228 |
if (m_flags & Parsable) { |
| 229 |
m_textStream |
| 230 |
<< "G " |
| 231 |
<< event.timeStamp << ' ' |
| 232 |
<< event.generic.id << ' ' |
| 233 |
<< event.generic.string << '\n' |
| 234 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 235 |
<< flush; |
| 236 |
#else |
| 237 |
<< Qt::flush; |
| 238 |
#endif |
| 239 |
} else { |
| 240 |
m_textStream |
| 241 |
<< (m_flags & Colored ? "\033[32mG\033[00m " : "G ") |
| 242 |
<< dim << timeString << reset << ' ' |
| 243 |
<< "Id" << dimColon << event.generic.id << ' ' |
| 244 |
<< "String" << dimColon << '"' << event.generic.string << '"' |
| 245 |
<< '\n' |
| 246 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 247 |
<< flush; |
| 248 |
#else |
| 249 |
<< Qt::flush; |
| 250 |
#endif |
| 251 |
} |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
default: |
| 256 |
DNOT_REACHED(); |
| 257 |
break; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void UMFileLogger::setParsable(bool parsable) |
| 263 |
{ |
| 264 |
Q_D(UMFileLogger); |
| 265 |
|
| 266 |
if (parsable) { |
| 267 |
d->m_flags |= UMFileLoggerPrivate::Parsable; |
| 268 |
} else { |
| 269 |
d->m_flags &= ~UMFileLoggerPrivate::Parsable; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
bool UMFileLogger::parsable() |
| 274 |
{ |
| 275 |
return !!(d_func()->m_flags & UMFileLoggerPrivate::Parsable); |
| 276 |
} |
| 277 |
|
| 278 |
#if defined(Q_OS_LINUX) && defined(LTTNG_PLUGIN_BUILD_PATH) |
| 279 |
|
| 280 |
UMLTTNGPlugin* UMLTTNGLogger::m_plugin = nullptr; |
| 281 |
bool UMLTTNGLogger::m_error = false; |
| 282 |
|
| 283 |
UMLTTNGLogger::UMLTTNGLogger() |
| 284 |
{ |
| 285 |
// The LTTng tracepoints are dlopen'd so that we don't directly link to |
| 286 |
// liblttng-ust which spawns two threads when the lib is loaded. That allows |
| 287 |
// to avoid the cost of it at startup when the user doesn't use LTTng (as |
| 288 |
// well as not showing them in the metrics). |
| 289 |
|
| 290 |
if (!m_error && !m_plugin) { |
| 291 |
// Ensure the plugin is first loaded from the build path to ease |
| 292 |
// development on the toolkit from uninstalled sources. |
| 293 |
// FIXME(loicm) Security concerns? |
| 294 |
void* handle = dlopen(LTTNG_PLUGIN_BUILD_PATH, RTLD_LAZY); |
| 295 |
if (!handle) { |
| 296 |
handle = dlopen(LTTNG_PLUGIN_INSTALL_PATH, RTLD_LAZY); |
| 297 |
if (!handle) { |
| 298 |
WARN("ApplicationMonitor: %s", dlerror()); |
| 299 |
m_error = true; |
| 300 |
return; |
| 301 |
} |
| 302 |
} |
| 303 |
m_plugin = static_cast<UMLTTNGPlugin*>(dlsym(handle, "umLttngPlugin")); |
| 304 |
if (!m_plugin) { |
| 305 |
WARN("ApplicationMonitor: %s", dlerror()); |
| 306 |
m_error = true; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
void UMLTTNGLogger::log(const UMEvent& event) |
| 312 |
{ |
| 313 |
if (Q_LIKELY(m_plugin)) { |
| 314 |
switch (event.type) { |
| 315 |
|
| 316 |
case UMEvent::Process: { |
| 317 |
UMLTTNGProcessEvent processEvent = { |
| 318 |
.vszMemory = event.process.vszMemory, |
| 319 |
.rssMemory = event.process.rssMemory, |
| 320 |
.cpuUsage = event.process.cpuUsage, |
| 321 |
.threadCount = event.process.threadCount |
| 322 |
}; |
| 323 |
m_plugin->logProcessEvent(&processEvent); |
| 324 |
break; |
| 325 |
} |
| 326 |
|
| 327 |
case UMEvent::Frame: { |
| 328 |
UMLTTNGFrameEvent frameEvent = { |
| 329 |
.window = event.frame.window, |
| 330 |
.number = event.frame.number, |
| 331 |
.deltaTime = event.frame.deltaTime * 0.000001f, |
| 332 |
.syncTime = event.frame.syncTime * 0.000001f, |
| 333 |
.renderTime = event.frame.renderTime * 0.000001f, |
| 334 |
.gpuTime = event.frame.gpuTime * 0.000001f, |
| 335 |
.swapTime = event.frame.swapTime * 0.000001f |
| 336 |
}; |
| 337 |
m_plugin->logFrameEvent(&frameEvent); |
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
case UMEvent::Window: { |
| 342 |
const char* stateString[] = { "Hidden", "Shown", "Resized" }; |
| 343 |
Q_STATIC_ASSERT(ARRAY_SIZE(stateString) == UMWindowEvent::StateCount); |
| 344 |
UMLTTNGWindowEvent windowEvent = { |
| 345 |
.state = stateString[event.window.state], |
| 346 |
.id = event.window.id, |
| 347 |
.width = event.window.width, |
| 348 |
.height = event.window.height |
| 349 |
}; |
| 350 |
m_plugin->logWindowEvent(&windowEvent); |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
case UMEvent::Generic: { |
| 355 |
UMLTTNGGenericEvent genericEvent; |
| 356 |
genericEvent.id = event.generic.id; |
| 357 |
DASSERT(event.generic.stringSize < UMGenericEvent::maxStringSize); |
| 358 |
memcpy(genericEvent.string, event.generic.string, event.generic.stringSize); |
| 359 |
m_plugin->logGenericEvent(&genericEvent); |
| 360 |
break; |
| 361 |
} |
| 362 |
|
| 363 |
default: |
| 364 |
DNOT_REACHED(); |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
#endif // defined(Q_OS_LINUX) && defined(LTTNG_PLUGIN_BUILD_PATH) |