KWin
Loading...
Searching...
No Matches
ftrace.h
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2021 David Edmundson <davidedmundson@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#pragma once
11
12#include "effect/globals.h"
13
14#include <QFile>
15#include <QMutex>
16#include <QMutexLocker>
17#include <QObject>
18#include <QTextStream>
19
20namespace KWin
21{
30class KWIN_EXPORT FTraceLogger : public QObject
31{
32 Q_OBJECT
33 Q_CLASSINFO("D-Bus Interface", "org.kde.kwin.FTrace");
34 Q_PROPERTY(bool isEnabled READ isEnabled NOTIFY enabledChanged)
35
36public:
40 bool isEnabled() const;
41
46 template<typename... Args>
47 void trace(Args... args)
48 {
49 Q_ASSERT(isEnabled());
50 QMutexLocker lock(&m_mutex);
51 if (!m_file.isOpen()) {
52 return;
53 }
54 QTextStream stream(&m_file);
55 (stream << ... << args) << Qt::endl;
56 }
57
58Q_SIGNALS:
60
61public Q_SLOTS:
62 Q_SCRIPTABLE void setEnabled(bool enabled);
63
64private:
65 static QString filePath();
66 bool open();
67 QFile m_file;
68 QMutex m_mutex;
70};
71
72class KWIN_EXPORT FTraceDuration
73{
74public:
75 template<typename... Args>
76 FTraceDuration(Args... args)
77 {
78 static QAtomicInteger<quint32> s_context = 0;
79 QTextStream stream(&m_message);
80 (stream << ... << args);
81 stream.flush();
82 m_context = ++s_context;
83 FTraceLogger::self()->trace(m_message, " begin_ctx=", m_context);
84 }
85
87
88private:
89 QByteArray m_message;
90 quint32 m_context;
91};
92
93} // namespace KWin
94
98#define fTrace(...) \
99 if (KWin::FTraceLogger::self()->isEnabled()) \
100 KWin::FTraceLogger::self()->trace(__VA_ARGS__);
101
106#define fTraceDuration(...) \
107 std::unique_ptr<KWin::FTraceDuration> _duration(KWin::FTraceLogger::self()->isEnabled() ? new KWin::FTraceDuration(__VA_ARGS__) : nullptr);
FTraceDuration(Args... args)
Definition ftrace.h:76
void trace(Args... args)
Definition ftrace.h:47
#define KWIN_SINGLETON(ClassName)
Definition globals.h:323