summaryrefslogtreecommitdiff
path: root/DebugEngine.h
blob: e39470e118a6b5f6674b2bbe3dcbeca841e1fba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/** \file DebugEngine.h
\brief Define the class for the debug
\author alpha_one_x86
\note This class don't need be thread safe because ultracopier is done with one thread, but I have implement some basic thread protection
\licence GPL3, see the file COPYING */

#ifndef DEBUG_ENGINE_H
#define DEBUG_ENGINE_H

#include <QObject>
#include <QString>
#include <QFile>
#include <QMutex>
#include <QTime>
#include <QTimer>
#include <QList>
#include <QCoreApplication>
#include <QAbstractTableModel>
#include <QRegularExpression>

#include "Variable.h"
#include "PlatformMacro.h"
#include "StructEnumDefinition.h"
#include "StructEnumDefinition_UltracopierSpecific.h"

#ifdef ULTRACOPIER_DEBUG

class DebugModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    /// \brief the transfer item displayed
    struct DebugItem
    {
        int time;
        DebugLevel_custom level;
        QString function;
        QString text;
        QString file;
        QString location;
    };

    static DebugModel *debugModel;
    DebugModel();
    ~DebugModel();

    virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
    virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    virtual bool setData(const QModelIndex&, const QVariant&, int = Qt::EditRole);

    void addDebugInformation(const int &time, const DebugLevel_custom &level, const QString& function, const QString& text, const QString &file="", const int& ligne=-1, const QString& location="Core");
    void setupTheTimer();
    QTimer *updateDisplayTimer;
    bool displayed;
    bool inWaitOfDisplay;
private:
    QList<DebugItem> list;
private slots:
    void updateDisplay();
};

/** \brief Define the class for the debug

This class provide all needed for the debug mode of ultracopier */
class DebugEngine : public QObject
{
    Q_OBJECT
    public:
        /// \brief Initiate the ultracopier event dispatcher and check if no other session is running
        DebugEngine();
        /** \brief Destroy the ultracopier event dispatcher
        \note This function is thread safe */
        ~DebugEngine();
        /** \brief Get the html text info for re-show it
        \note This function is thread safe */
        QString getTheDebugHtml();
        /// \brief Enumeration of backend
        enum Backend
        {
            Memory,		//Do intensive usage of memory, used only if the file backend is not available
            File		//Store all directly into file, at crash the backtrace is into the file
        };
        /// \brief return the current backend
        Backend getCurrentBackend();
        /// \brief Get the html end
        QString getTheDebugEnd();
        /** \brief For add message info, this function
        \note This function is reentrant */
        static void addDebugInformationStatic(const Ultracopier::DebugLevel &level,const QString& function,const QString& text,const QString& file="",const int& ligne=-1,const QString& location="Core");
        static void addDebugNote(const QString& text);
        static DebugEngine *debugEngine;
    public slots:
        /** \brief ask to the user where save the bug report
        \warning This function can be only call by the graphical thread */
        void saveBugReport();
        void addDebugInformation(const DebugLevel_custom &level,const QString& fonction,const QString& text,QString file="",const int& ligne=-1,const QString& location="Core");
    private:
        /// \brief Path for log file
        QFile logFile;
        /// \brief Path for lock file
        QFile lockFile;
        /// \brief Internal function to remove the lock file
        bool removeTheLockFile();
        /** \brief Do thread safe part for the addDebugInformation()
        \see addDebugInformation() */
        QMutex mutex;
        QMutex mutexList;
        /// \brief For record the start time
        QTime startTime;
        /// \brief String for the end of log file
        QString endOfLogFile;
        /// \brief Drop the html entities
        QString htmlEntities(const QString &text);
        /// \brief To store the debug informations
        QString debugHtmlContent;
        /// \brief The current backend
        Backend currentBackend;
        /// try connect to send to the current running instance the arguements
        bool tryConnect();
        int addDebugInformationCallNumber;
        bool quit;
        QRegularExpression fileNameCleaner;
};

#endif // ULTRACOPIER_DEBUG

#endif // DEBUG_ENGINE_H