| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 2.1 or version 3 as published by the Free |
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and |
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the |
| 22 | ** following information to ensure the GNU Lesser General Public License |
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and |
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 25 | ** |
| 26 | ** As a special exception, The Qt Company gives you certain additional |
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception |
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 29 | ** |
| 30 | ** $QT_END_LICENSE$ |
| 31 | ** |
| 32 | ****************************************************************************/ |
| 33 | #ifndef QSERVICEDEBUGLOG_P_H |
| 34 | #define QSERVICEDEBUGLOG_P_H |
| 35 | |
| 36 | // |
| 37 | // W A R N I N G |
| 38 | // ------------- |
| 39 | // |
| 40 | // This file is not part of the Qt API. It exists purely as an |
| 41 | // implementation detail. This header file may change from version to |
| 42 | // version without notice, or even be removed. |
| 43 | // |
| 44 | // We mean it. |
| 45 | // |
| 46 | |
| 47 | #include <QString> |
| 48 | #include <QStringList> |
| 49 | #include <QBuffer> |
| 50 | #include <QDataStream> |
| 51 | #include <QSharedPointer> |
| 52 | #include <QMutex> |
| 53 | #include <QVector> |
| 54 | |
| 55 | QT_BEGIN_NAMESPACE |
| 56 | |
| 57 | class QServiceDebugMessage |
| 58 | { |
| 59 | public: |
| 60 | enum DataType { |
| 61 | Int32Type = 1, |
| 62 | FloatType = 2, |
| 63 | StringType = 3 |
| 64 | }; |
| 65 | |
| 66 | QServiceDebugMessage(); |
| 67 | ~QServiceDebugMessage(); |
| 68 | |
| 69 | #ifdef QT_SFW_IPC_DEBUG |
| 70 | QBuffer *buffer; |
| 71 | QDataStream ds; |
| 72 | #endif |
| 73 | }; |
| 74 | |
| 75 | class QServiceDebugValue; |
| 76 | class QServiceDebugKey; |
| 77 | class QUdpSocket; |
| 78 | |
| 79 | class QServiceDebugLog |
| 80 | { |
| 81 | public: |
| 82 | QServiceDebugLog(); |
| 83 | |
| 84 | static QServiceDebugLog* instance(); |
| 85 | |
| 86 | QServiceDebugValue operator<<(const char *key); |
| 87 | void logMessage(QServiceDebugMessage *msg); |
| 88 | |
| 89 | private: |
| 90 | QList<QBuffer *> queue; |
| 91 | QVector<QUdpSocket *> sockets; |
| 92 | void makeSockets(); |
| 93 | QMutex socketLock; |
| 94 | }; |
| 95 | |
| 96 | inline QServiceDebugLog &qServiceLog() |
| 97 | { |
| 98 | return (*QServiceDebugLog::instance()); |
| 99 | } |
| 100 | |
| 101 | class QServiceDebugKey |
| 102 | { |
| 103 | public: |
| 104 | #ifdef QT_SFW_IPC_DEBUG |
| 105 | inline QServiceDebugKey(const QSharedPointer<QServiceDebugMessage> &ptr) |
| 106 | : ptr(ptr) {} |
| 107 | |
| 108 | #else |
| 109 | inline QServiceDebugKey() {} |
| 110 | #endif |
| 111 | QServiceDebugValue operator<<(const char *key); |
| 112 | private: |
| 113 | #ifdef QT_SFW_IPC_DEBUG |
| 114 | QSharedPointer<QServiceDebugMessage> ptr; |
| 115 | #endif |
| 116 | }; |
| 117 | |
| 118 | class QServiceDebugValue |
| 119 | { |
| 120 | public: |
| 121 | #ifdef QT_SFW_IPC_DEBUG |
| 122 | inline QServiceDebugValue(const QSharedPointer<QServiceDebugMessage> &ptr) |
| 123 | : ptr(ptr) {} |
| 124 | #else |
| 125 | inline QServiceDebugValue() {} |
| 126 | #endif |
| 127 | QServiceDebugKey operator<<(const qint32 &val); |
| 128 | QServiceDebugKey operator<<(const float &val); |
| 129 | QServiceDebugKey operator<<(const QString &val); |
| 130 | QServiceDebugKey operator<<(const char *val); |
| 131 | private: |
| 132 | #ifdef QT_SFW_IPC_DEBUG |
| 133 | QSharedPointer<QServiceDebugMessage> ptr; |
| 134 | #endif |
| 135 | }; |
| 136 | |
| 137 | inline QServiceDebugValue QServiceDebugKey::operator<<(const char *key) |
| 138 | { |
| 139 | #ifdef QT_SFW_IPC_DEBUG |
| 140 | ptr->ds.writeBytes(key, ::strlen(key)); |
| 141 | return QServiceDebugValue(ptr); |
| 142 | #else |
| 143 | Q_UNUSED(key) |
| 144 | return QServiceDebugValue(); |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | inline QServiceDebugKey QServiceDebugValue::operator<<(const qint32 &val) |
| 149 | { |
| 150 | #ifdef QT_SFW_IPC_DEBUG |
| 151 | ptr->ds << (qint8)QServiceDebugMessage::Int32Type; |
| 152 | ptr->ds << val; |
| 153 | return QServiceDebugKey(ptr); |
| 154 | #else |
| 155 | Q_UNUSED(val) |
| 156 | return QServiceDebugKey(); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | inline QServiceDebugKey QServiceDebugValue::operator<<(const float &val) |
| 161 | { |
| 162 | #ifdef QT_SFW_IPC_DEBUG |
| 163 | ptr->ds << (qint8)QServiceDebugMessage::FloatType; |
| 164 | ptr->ds << val; |
| 165 | return QServiceDebugKey(ptr); |
| 166 | #else |
| 167 | Q_UNUSED(val) |
| 168 | return QServiceDebugKey(); |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | inline QServiceDebugKey QServiceDebugValue::operator<<(const QString &val) |
| 173 | { |
| 174 | #ifdef QT_SFW_IPC_DEBUG |
| 175 | ptr->ds << (qint8)QServiceDebugMessage::StringType; |
| 176 | QByteArray ba = val.toLatin1(); |
| 177 | ptr->ds.writeBytes(ba.constData(), ba.size()); |
| 178 | return QServiceDebugKey(ptr); |
| 179 | #else |
| 180 | Q_UNUSED(val) |
| 181 | return QServiceDebugKey(); |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | inline QServiceDebugKey QServiceDebugValue::operator<<(const char *val) |
| 186 | { |
| 187 | #ifdef QT_SFW_IPC_DEBUG |
| 188 | ptr->ds << (qint8)QServiceDebugMessage::StringType; |
| 189 | ptr->ds.writeBytes(val, ::strlen(val)); |
| 190 | return QServiceDebugKey(ptr); |
| 191 | #else |
| 192 | Q_UNUSED(val) |
| 193 | return QServiceDebugKey(); |
| 194 | #endif |
| 195 | } |
| 196 | |
| 197 | inline QServiceDebugValue QServiceDebugLog::operator<<(const char *key) |
| 198 | { |
| 199 | #ifdef QT_SFW_IPC_DEBUG |
| 200 | QSharedPointer<QServiceDebugMessage> msg(new QServiceDebugMessage()); |
| 201 | return (QServiceDebugKey(msg) << key); |
| 202 | #else |
| 203 | Q_UNUSED(key) |
| 204 | return QServiceDebugValue(); |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | QT_END_NAMESPACE |
| 209 | |
| 210 | #endif // QSERVICEDEBUGLOG_P_H |
| 211 | |