1 | #pragma once |
2 | |
3 | /**************************************************************************************** |
4 | ** QLogger is a library to register and print logs into a file. |
5 | ** Copyright (C) 2021 Francesc Martinez |
6 | ** |
7 | ** LinkedIn: www.linkedin.com/in/cescmm/ |
8 | ** Web: www.francescmm.com |
9 | ** |
10 | ** This library is free software; you can redistribute it and/or |
11 | ** modify it under the terms of the GNU Lesser General Public |
12 | ** License as published by the Free Software Foundation; either |
13 | ** version 2 of the License, or (at your option) any later version. |
14 | ** |
15 | ** This library is distributed in the hope that it will be useful, |
16 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | ** Lesser General Public License for more details. |
19 | ** |
20 | ** You should have received a copy of the GNU Lesser General Public |
21 | ** License along with this library; if not, write to the Free Software |
22 | ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | ***************************************************************************************/ |
24 | |
25 | #include <QLoggerLevel.h> |
26 | |
27 | #include <QThread> |
28 | #include <QWaitCondition> |
29 | #include <QMutex> |
30 | #include <QVector> |
31 | |
32 | namespace QLogger |
33 | { |
34 | |
35 | class QLoggerWriter : public QThread |
36 | { |
37 | Q_OBJECT |
38 | |
39 | public: |
40 | /** |
41 | * @brief Constructor that gets the complete path and filename to create the file. It also |
42 | * configures the level of this file to filter the logs depending on the level. |
43 | * @param fileDestination The complete path. |
44 | * @param level The maximum level that is allowed. |
45 | * @param fileFolderDestination The complete folder destination. |
46 | * @param mode The logging mode. |
47 | * @param fileSuffixIfFull The filename suffix if the file is full. |
48 | */ |
49 | explicit QLoggerWriter(const QString &fileDestination, LogLevel level = LogLevel::Warning, |
50 | const QString &fileFolderDestination = QString(), LogMode mode = LogMode::OnlyFile, |
51 | LogFileDisplay fileSuffixIfFull = LogFileDisplay::DateTime, |
52 | LogMessageDisplays messageOptions = LogMessageDisplay::Default); |
53 | |
54 | /** |
55 | * @brief Gets path and folder of the file that will store the logs. |
56 | */ |
57 | QString getFileDestinationFolder() const { return mFileDestinationFolder; } |
58 | /** |
59 | * @brief Path and name of the file that will store the logs. |
60 | */ |
61 | QString getFileDestination() const { return mFileDestination; } |
62 | |
63 | /** |
64 | * @brief Gets the current logging mode. |
65 | * @return The level. |
66 | */ |
67 | LogMode getMode() const { return mMode; } |
68 | |
69 | /** |
70 | * @brief setLogMode Sets the log mode for this destination. |
71 | * @param mode |
72 | */ |
73 | void setLogMode(LogMode mode); |
74 | |
75 | /** |
76 | * @brief Gets the current level threshold. |
77 | * @return The level. |
78 | */ |
79 | LogLevel getLevel() const { return mLevel; } |
80 | |
81 | /** |
82 | * @brief setLogLevel Sets the log level for this destination. |
83 | * @param level The new level threshold. |
84 | */ |
85 | void setLogLevel(LogLevel level) { mLevel = level; } |
86 | |
87 | /** |
88 | * @brief Gets the current max size for the log file. |
89 | * @return The maximum size |
90 | */ |
91 | int getMaxFileSize() const { return mMaxFileSize; } |
92 | |
93 | /** |
94 | * @brief setMaxFileSize Sets the max file size for this destination. |
95 | * @param maxSize The new file size |
96 | */ |
97 | void setMaxFileSize(int maxSize) { mMaxFileSize = maxSize; } |
98 | |
99 | /** |
100 | * @brief getMessageOptions Gets the current message options. |
101 | * @return The current options |
102 | */ |
103 | LogMessageDisplays getMessageOptions() const { return mMessageOptions; } |
104 | |
105 | /** |
106 | * @brief setMessageOptions Specifies what elements are displayed in one line of log message. |
107 | * @param messageOptions The options |
108 | */ |
109 | void setMessageOptions(LogMessageDisplays messageOptions) { mMessageOptions = messageOptions; } |
110 | |
111 | /** |
112 | * @brief enqueue Enqueues a message to be written in the destiantion. |
113 | * @param date The date and time of the log message. |
114 | * @param threadId The thread where the message comes from. |
115 | * @param module The module that writes the message. |
116 | * @param level The log level of the message. |
117 | * @param function The function that prints the log. |
118 | * @param fileName The file name that prints the log. |
119 | * @param line The line of the file name that prints the log. |
120 | * @param message The message to log. |
121 | */ |
122 | void enqueue(const QDateTime &date, const QString &threadId, const QString &module, LogLevel level, |
123 | const QString &function, const QString &fileName, int line, const QString &message); |
124 | |
125 | /** |
126 | * @brief Stops the log writer |
127 | * @param stop True to be stop, otherwise false |
128 | */ |
129 | void stop(bool stop) { mIsStop = stop; } |
130 | |
131 | /** |
132 | * @brief Returns if the log writer is stop from writing. |
133 | * @return True if is stop, otherwise false |
134 | */ |
135 | bool isStop() const { return mIsStop; } |
136 | |
137 | /** |
138 | * @brief run Overloaded method from QThread used to wait for new messages. |
139 | */ |
140 | void run() override; |
141 | |
142 | /** |
143 | * @brief closeDestination Closes the destination. This needs to be called whenever |
144 | */ |
145 | void closeDestination(); |
146 | |
147 | private: |
148 | bool mQuit = false; |
149 | bool mIsStop = false; |
150 | QWaitCondition mQueueNotEmpty; |
151 | QString mFileDestinationFolder; |
152 | QString mFileDestination; |
153 | LogFileDisplay mFileSuffixIfFull; |
154 | LogMode mMode; |
155 | LogLevel mLevel; |
156 | int mMaxFileSize = 1024 * 1024; //! @note 1Mio |
157 | LogMessageDisplays mMessageOptions; |
158 | QVector<QString> mMessages; |
159 | QMutex mutex; |
160 | |
161 | /** |
162 | * @brief renameFileIfFull Truncates the log file in two. Keeps the filename for the new one and renames the old one |
163 | * with the timestamp or with a file number. |
164 | * |
165 | * @return Returns the file name for the old logs. |
166 | */ |
167 | QString renameFileIfFull(); |
168 | |
169 | /** |
170 | * @brief generateDuplicateFilename |
171 | * |
172 | * @param fileDestination The file path and name without the extension. |
173 | * @param fileExtension The file extension |
174 | * @param fileSuffixNumber The file suffix number. |
175 | * @return The complete path of the duplicated file name. |
176 | */ |
177 | static QString generateDuplicateFilename(const QString &fileDestination, const QString &fileExtension, |
178 | int fileSuffixNumber = 1); |
179 | |
180 | /** |
181 | * @brief Writes a message in a file. If the file is full, it truncates it and prints a first line with the |
182 | * information of the old file. |
183 | * |
184 | * @param message Pair of values consistent on the date and the message to be log. |
185 | */ |
186 | void write(QVector<QString> messages); |
187 | }; |
188 | |
189 | } |
190 | |