Skip to content

Commit

Permalink
Add LOG/DEBUG_LOG/PROFILE_LOG using Integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Apr 20, 2021
1 parent 443b7b6 commit 053ab21
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 97 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ PRIVATE
base/concurrent_timer.cpp
base/concurrent_timer.h
base/const_string.h
base/debug_log.cpp
base/debug_log.h
base/enum_mask.h
base/event_filter.cpp
base/event_filter.h
Expand Down
37 changes: 37 additions & 0 deletions base/debug_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "base/debug_log.h"

#include "base/integration.h"

namespace base {

void LogWriteMain(const QString &message) {
if (Integration::Exists()) {
Integration::Instance().logMessage(message);
}
}

void LogWriteDebug(const QString &message, const char *file, int line) {
Expects(!LogSkipDebug());

Integration::Instance().logMessageDebug(QString("%1 (%2 : %3)").arg(
message,
QString::fromUtf8(file),
QString::number(__LINE__)));
}

bool LogSkipDebug() {
return !Integration::Exists() || Integration::Instance().logSkipDebug();
}

QString LogProfilePrefix() {
const auto now = crl::profile();
return '[' + QString::number(now / 1000., 'f', 3) + "] ";
}

} // namespace base
36 changes: 36 additions & 0 deletions base/debug_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#pragma once

#include "base/assertion.h" // SOURCE_FILE_BASENAME

namespace base {

void LogWriteMain(const QString &message);
void LogWriteDebug(const QString &message, const char *file, int line);
[[nodiscard]] bool LogSkipDebug();

[[nodiscard]] QString LogProfilePrefix();

} // namespace base

#define LOG(message) (::base::LogWriteMain(QString message))
//usage LOG(("log: %1 %2").arg(1).arg(2))

#define PROFILE_LOG(message) {\
if (!::base::LogSkipDebug()) {\
::base::LogWriteMain(::base::LogProfilePrefix() + QString msg);\
}\
}
//usage PROFILE_LOG(("step: %1 %2").arg(1).arg(2))

#define DEBUG_LOG(msg) {\
if (!::base::LogSkipDebug()) {\
::base::LogWriteDebug(QString msg, SOURCE_FILE_BASENAME, __LINE__);\
}\
}
//usage DEBUG_LOG(("log: %1 %2").arg(1).arg(2))
1 change: 1 addition & 0 deletions base/integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "base/integration.h"
#include "base/platform/base_platform_file_utilities.h"
#include "base/debug_log.h"

#include <QtCore/QFileInfo>
#include <QtCore/QDir>
Expand Down
2 changes: 2 additions & 0 deletions base/integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Integration {

virtual void enterFromEventLoop(FnMut<void()> &&method) = 0;

virtual bool logSkipDebug() = 0;
virtual void logMessageDebug(const QString &message) = 0;
virtual void logMessage(const QString &message) = 0;
virtual void logAssertionViolation(const QString &info);

Expand Down
16 changes: 7 additions & 9 deletions base/platform/linux/base_global_shortcuts_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#include "base/const_string.h"
#include "base/global_shortcuts_generic.h"
#include "base/integration.h"
#include "base/platform/base_platform_info.h" // IsWayland
#include "base/unique_qptr.h"
#include "base/debug_log.h"

#ifndef DESKTOP_APP_DISABLE_X11_INTEGRATION
#include "base/platform/linux/base_linux_xcb_utilities.h" // CustomConnection, IsExtensionPresent
Expand Down Expand Up @@ -71,18 +71,16 @@ X11Manager::X11Manager()
: _object(make_unique_q<QObject>())
, _keySymbols(xcb_key_symbols_alloc(_connection)) {

using Log = Integration;

if (xcb_connection_has_error(_connection)) {
Log::Instance().logMessage(
"Global Shortcuts Manager: Error to open local display!");
DEBUG_LOG((
"Global Shortcuts Manager: Error to open local display!"));
_isAvailable = false;
return;
}

if (!XCB::IsExtensionPresent(_connection, &xcb_record_id)) {
Log::Instance().logMessage("Global Shortcuts Manager: "
"RECORD extension not supported on this X server!");
DEBUG_LOG(("Global Shortcuts Manager: "
"RECORD extension not supported on this X server!"));
_isAvailable = false;
return;
}
Expand Down Expand Up @@ -111,8 +109,8 @@ X11Manager::X11Manager()
&clientSpec,
&recordRange);
if (xcb_request_check(_connection, createCookie)) {
Log::Instance().logMessage(
"Global Shortcuts Manager: Could not create a record context!");
DEBUG_LOG((
"Global Shortcuts Manager: Could not create a record context!"));
_isAvailable = false;
return;
}
Expand Down
30 changes: 13 additions & 17 deletions base/platform/linux/base_last_input_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
#include "base/platform/linux/base_last_input_linux.h"

#include "base/integration.h"
#include "base/platform/base_platform_info.h"
#include "base/debug_log.h"

#ifndef DESKTOP_APP_DISABLE_X11_INTEGRATION
#include "base/platform/linux/base_linux_xcb_utilities.h"
Expand Down Expand Up @@ -103,15 +103,13 @@ std::optional<crl::time> FreedesktopDBusLastUserInputTime() {
NotSupported = true;
}

Integration::Instance().logMessage(
QString("Unable to get last user input time "
"from org.freedesktop.ScreenSaver: %1")
.arg(QString::fromStdString(e.what())));
DEBUG_LOG(("Unable to get last user input time "
"from org.freedesktop.ScreenSaver: %1")
.arg(QString::fromStdString(e.what())));
} catch (const std::exception &e) {
Integration::Instance().logMessage(
QString("Unable to get last user input time "
"from org.freedesktop.ScreenSaver: %1")
.arg(QString::fromStdString(e.what())));
DEBUG_LOG(("Unable to get last user input time "
"from org.freedesktop.ScreenSaver: %1")
.arg(QString::fromStdString(e.what())));
}

return std::nullopt;
Expand Down Expand Up @@ -155,15 +153,13 @@ std::optional<crl::time> MutterDBusLastUserInputTime() {
NotSupported = true;
}

Integration::Instance().logMessage(
QString("Unable to get last user input time "
"from org.gnome.Mutter.IdleMonitor: %1")
.arg(QString::fromStdString(e.what())));
DEBUG_LOG(("Unable to get last user input time "
"from org.gnome.Mutter.IdleMonitor: %1")
.arg(QString::fromStdString(e.what())));
} catch (const std::exception &e) {
Integration::Instance().logMessage(
QString("Unable to get last user input time "
"from org.gnome.Mutter.IdleMonitor: %1")
.arg(QString::fromStdString(e.what())));
DEBUG_LOG(("Unable to get last user input time "
"from org.gnome.Mutter.IdleMonitor: %1")
.arg(QString::fromStdString(e.what())));
}

return std::nullopt;
Expand Down
97 changes: 37 additions & 60 deletions base/platform/linux/base_linux_gtk_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/platform/linux/base_linux_gtk_integration_p.h"
#include "base/platform/base_platform_info.h"
#include "base/integration.h"
#include "base/debug_log.h"

#ifndef DESKTOP_APP_DISABLE_X11_INTEGRATION
#include "base/platform/linux/base_linux_xlib_helper.h"
Expand All @@ -24,35 +25,29 @@ bool LoadLibrary(QLibrary &lib, const char *name, int version) {
#ifdef LINK_TO_GTK
return true;
#else // LINK_TO_GTK
Integration::Instance().logMessage(
QString("Loading '%1' with version %2...")
.arg(QLatin1String(name))
.arg(version));
DEBUG_LOG(("Loading '%1' with version %2...")
.arg(QLatin1String(name))
.arg(version));
lib.setFileNameAndVersion(QLatin1String(name), version);
if (lib.load()) {
Integration::Instance().logMessage(
QString("Loaded '%1' with version %2!")
.arg(QLatin1String(name))
.arg(version));
DEBUG_LOG(("Loaded '%1' with version %2!")
.arg(QLatin1String(name))
.arg(version));
return true;
} else {
Integration::Instance().logMessage(
QString("Could not load '%1' with version %2! Error: %3")
.arg(QLatin1String(name))
.arg(version)
.arg(lib.errorString()));
DEBUG_LOG(("Could not load '%1' with version %2! Error: %3")
.arg(QLatin1String(name))
.arg(version)
.arg(lib.errorString()));
}
lib.setFileNameAndVersion(QLatin1String(name), QString());
if (lib.load()) {
Integration::Instance().logMessage(
QString("Loaded '%1' without version!")
.arg(QLatin1String(name)));
DEBUG_LOG(("Loaded '%1' without version!").arg(QLatin1String(name)));
return true;
} else {
Integration::Instance().logMessage(
QString("Could not load '%1' without version! Error: %2")
.arg(QLatin1String(name))
.arg(lib.errorString()));
DEBUG_LOG(("Could not load '%1' without version! Error: %2")
.arg(QLatin1String(name))
.arg(lib.errorString()));
}
return false;
#endif // !LINK_TO_GTK
Expand Down Expand Up @@ -90,12 +85,10 @@ bool SetupGtkBase(QLibrary &lib) {
// See https://github.com/telegramdesktop/tdesktop/issues/3176
// See https://github.com/telegramdesktop/tdesktop/issues/3162
if (::Platform::IsWayland()) {
Integration::Instance().logMessage(
"Limit allowed GDK backends to wayland,x11");
DEBUG_LOG(("Limit allowed GDK backends to wayland,x11"));
gdk_set_allowed_backends("wayland,x11");
} else if (::Platform::IsX11()) {
Integration::Instance().logMessage(
"Limit allowed GDK backends to x11,wayland");
DEBUG_LOG(("Limit allowed GDK backends to x11,wayland"));
gdk_set_allowed_backends("x11,wayland");
}
}
Expand All @@ -109,15 +102,14 @@ bool SetupGtkBase(QLibrary &lib) {
XErrorHandlerRestorer handlerRestorer;
#endif // !DESKTOP_APP_DISABLE_X11_INTEGRATION

Integration::Instance().logMessage("Library gtk functions loaded!");
DEBUG_LOG(("Library gtk functions loaded!"));
TriedToInit = true;
if (!gtk_init_check(0, 0)) {
gtk_init_check = nullptr;
Integration::Instance().logMessage(
"Failed to gtk_init_check(0, 0)!");
DEBUG_LOG(("Failed to gtk_init_check(0, 0)!"));
return false;
}
Integration::Instance().logMessage("Checked gtk with gtk_init_check!");
DEBUG_LOG(("Checked gtk with gtk_init_check!"));

// Use our custom log handler.
g_log_set_handler("Gtk", G_LOG_LEVEL_MESSAGE, GtkMessageHandler, nullptr);
Expand Down Expand Up @@ -181,18 +173,15 @@ void SetIconTheme() {
return;
}

Integration::Instance().logMessage("Setting GTK icon theme");
DEBUG_LOG(("Setting GTK icon theme"));

QIcon::setThemeName(*themeName);
QIcon::setFallbackThemeName(*fallbackThemeName);

Integration::Instance().logMessage(
QString("New icon theme: %1")
.arg(QIcon::themeName()));
DEBUG_LOG(("New icon theme: %1").arg(QIcon::themeName()));

Integration::Instance().logMessage(
QString("New fallback icon theme: %1")
.arg(QIcon::fallbackThemeName()));
DEBUG_LOG(("New fallback icon theme: %1")
.arg(QIcon::fallbackThemeName()));
};

if (QCoreApplication::instance()) {
Expand All @@ -216,10 +205,9 @@ void SetCursorSize() {
return;
}

Integration::Instance().logMessage("Setting GTK cursor size");
DEBUG_LOG(("Setting GTK cursor size"));
qputenv("XCURSOR_SIZE", QByteArray::number(*newCursorSize));
Integration::Instance().logMessage(
QString("New cursor size: %1").arg(*newCursorSize));
DEBUG_LOG(("New cursor size: %1").arg(*newCursorSize));
};

if (QCoreApplication::instance()) {
Expand Down Expand Up @@ -279,15 +267,9 @@ void GtkIntegration::prepareEnvironment() {
void GtkIntegration::load() {
Expects(!loaded());

Integration::Instance().logMessage("Loading GTK");

Integration::Instance().logMessage(
QString("Icon theme: %1")
.arg(QIcon::themeName()));

Integration::Instance().logMessage(
QString("Fallback icon theme: %1")
.arg(QIcon::fallbackThemeName()));
DEBUG_LOG(("Loading GTK"));
DEBUG_LOG(("Icon theme: %1").arg(QIcon::themeName()));
DEBUG_LOG(("Fallback icon theme: %1").arg(QIcon::fallbackThemeName()));

_lib.setLoadHints(QLibrary::DeepBindHint);

Expand All @@ -304,7 +286,7 @@ void GtkIntegration::load() {
LOAD_GTK_SYMBOL(_lib, gtk_check_version);
LOAD_GTK_SYMBOL(_lib, gtk_settings_get_default);
} else {
Integration::Instance().logMessage("Could not load gtk-3 or gtk-x11-2.0!");
DEBUG_LOG(("Could not load gtk-3 or gtk-x11-2.0!"));
}
}

Expand Down Expand Up @@ -335,21 +317,19 @@ std::optional<bool> GtkIntegration::getBoolSetting(
if (!value.has_value()) {
return std::nullopt;
}
Integration::Instance().logMessage(
QString("Getting GTK setting, %1: %2").arg(
propertyName,
*value ? "[TRUE]" : "[FALSE]"));
DEBUG_LOG(("Getting GTK setting, %1: %2").arg(
propertyName,
*value ? "[TRUE]" : "[FALSE]"));
return *value;
}

std::optional<int> GtkIntegration::getIntSetting(
const QString &propertyName) const {
const auto value = GtkSetting<gint>(propertyName);
if (value.has_value()) {
Integration::Instance().logMessage(
QString("Getting GTK setting, %1: %2")
.arg(propertyName)
.arg(*value));
DEBUG_LOG(("Getting GTK setting, %1: %2")
.arg(propertyName)
.arg(*value));
}
return value;
}
Expand All @@ -362,10 +342,7 @@ std::optional<QString> GtkIntegration::getStringSetting(
}
const auto str = QString::fromUtf8(*value);
g_free(*value);
Integration::Instance().logMessage(
QString("Getting GTK setting, %1: '%2'").arg(
propertyName,
str));
DEBUG_LOG(("Getting GTK setting, %1: '%2'").arg(propertyName, str));
return str;
}

Expand Down
Loading

0 comments on commit 053ab21

Please sign in to comment.