Debian Codegraph @

src/FileAppender.cpp

raw
1 /*
2 Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com)
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License version 2.1
6 as published by the Free Software Foundation and appearing in the file
7 LICENSE.LGPL included in the packaging of this file.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 */
14
15 #include "FileAppender.h"
16
17 #include <QFileInfo>
18
19 #include "rollingfilesink_p.h"
20
21 #include <iostream>
22
23 DLOG_CORE_BEGIN_NAMESPACE
24
25 std::string loggerName(const QFile &logFile)
26 {
27 return QFileInfo(logFile).fileName().toStdString();
28 }
29 /*!
30 @~english
31 @class Dtk::Core::FileAppender
32 @ingroup dtkcore
33
34 @brief Simple appender that writes the log records to the plain text file.
35 */
36
37 /*!
38 @~english
39 @brief Constructs the new file appender assigned to file with the given \a fileName.
40 */
41 FileAppender::FileAppender(const QString &fileName)
42 {
43 setFileName(fileName);
44 }
45
46 FileAppender::~FileAppender()
47 {
48 closeFile();
49 }
50
51 /*!
52 @~english
53 \brief Returns the name set by setFileName() or to the FileAppender constructor.
54
55 \sa setFileName()
56 */
57 QString FileAppender::fileName() const
58 {
59 QMutexLocker locker(&m_logFileMutex);
60 return m_logFile.fileName();
61 }
62
63 /*!
64 \brief Sets the \a s name of the file. The name can have no path, a relative path, or an absolute path.
65
66 \sa fileName()
67 */
68 void FileAppender::setFileName(const QString &s)
69 {
70 QMutexLocker locker(&m_logFileMutex);
71
72 if (s == m_logFile.fileName())
73 return;
74
75 closeFile();
76
77 m_logFile.setFileName(s);
78
79 if (!spdlog::get(loggerName(QFile(s))))
80 rolling_logger_mt(loggerName(QFile(s)),
81 m_logFile.fileName().toStdString(),
82 1024 * 1024 * 20, 0);
83 }
84
85 qint64 FileAppender::size() const
86 {
87 QMutexLocker locker(&m_logFileMutex);
88
89 if (auto *bs = get_sink<rolling_file_sink_mt>(loggerName(m_logFile)))
90 {
91 return qint64(bs->filesize());
92 }
93
94 return m_logFile.size();
95 }
96
97 bool FileAppender::openFile()
98 {
99 auto fl = spdlog::get(loggerName(m_logFile));
100
101 return fl.get();
102 }
103
104 /*!
105 @~english
106 \brief Write the log record to the file.
107 \reimp
108
109 The \a time parameter indicates the time stamp.
110 The \a level parameter describes the LogLevel.
111 The \a file parameter is the current file name.
112 The \a line parameter indicates the number of lines to output.
113 The \a func parameter indicates the func name to output.
114 The \a category parameter indicates the log category.
115 The \a msg parameter indicates the output message.
116
117 \sa fileName()
118 \sa AbstractStringAppender::format()
119 */
120 void FileAppender::append(const QDateTime &time, Logger::LogLevel level, const char *file, int line,
121 const char *func, const QString &category, const QString &msg)
122 {
123
124 if (!openFile())
125 return;
126
127 auto fl = spdlog::get(loggerName(m_logFile));
128 if (Q_UNLIKELY(!fl))
129 return;
130 fl->set_level(spdlog::level::level_enum(detailsLevel()));
131
132 const auto &formatted = formattedString(time, level, file, line, func, category, msg);
133 fl->log(spdlog::level::level_enum(level), formatted.toStdString());
134 fl->flush();
135 }
136
137 void FileAppender::closeFile()
138 {
139 spdlog::drop(loggerName(m_logFile));
140 }
141
142 DLOG_CORE_END_NAMESPACE