SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
qtwebapp::FileLogger Class Reference

#include <filelogger.h>

+ Inheritance diagram for qtwebapp::FileLogger:
+ Collaboration diagram for qtwebapp::FileLogger:

Public Member Functions

 FileLogger (QSettings *settings, const int refreshInterval=10000, QObject *parent=0)
 
 FileLogger (const FileLoggerSettings &settings, const int refreshInterval=10000, QObject *parent=0)
 
virtual ~FileLogger ()
 
FileLoggerSettings getFileLoggerSettings () const
 
void setFileLoggerSettings (const FileLoggerSettings &settings)
 
virtual void write (const LogMessage *logMessage)
 
- Public Member Functions inherited from qtwebapp::Logger
 Logger (QObject *parent)
 
 Logger (const QString msgFormat="{timestamp} {type} {msg}", const QString timestampFormat="yyyy-MM-dd HH:mm:ss.zzz", const QtMsgType minLevel=QtDebugMsg, const int bufferSize=0, QObject *parent=0)
 
virtual ~Logger ()
 
virtual void log (const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0)
 
void installMsgHandler ()
 
void setMinMessageLevel (const QtMsgType &minMsgLevel)
 
QtMsgType getMinMessageLevel () const
 
virtual void clear (const bool buffer=true, const bool variables=true)
 

Protected Member Functions

void timerEvent (QTimerEvent *event)
 

Private Member Functions

void open ()
 
void close ()
 
void rotate ()
 
void refreshSettings ()
 
void refreshQtSettings ()
 
void refreshFileLogSettings ()
 

Private Attributes

QString fileName
 
long maxSize
 
int maxBackups
 
QSettings * settings
 
FileLoggerSettings fileLoggerSettings
 
QFile * file
 
QBasicTimer refreshTimer
 
QBasicTimer flushTimer
 
bool useQtSettings
 

Additional Inherited Members

- Static Public Member Functions inherited from qtwebapp::Logger
static void set (const QString &name, const QString &value)
 
- Protected Attributes inherited from qtwebapp::Logger
QString msgFormat
 
QString timestampFormat
 
QtMsgType minLevel
 
int bufferSize
 
- Static Protected Attributes inherited from qtwebapp::Logger
static QMutex mutex
 

Detailed Description

Logger that uses a text file for output. Settings are read from a config file using a QSettings object. Config settings can be changed at runtime.

Example for the configuration settings:

fileName=logs/QtWebApp.log
maxSize=1000000
maxBackups=2
minLevel=0
msgformat={timestamp} {typeNr} {type} thread={thread}: {msg}
timestampFormat=yyyy-MM-dd HH:mm:ss.zzz
bufferSize=0

See also
set() describes how to set logger variables
LogMessage for a description of the message decoration.
Logger for a descrition of the buffer.

Definition at line 54 of file filelogger.h.

Constructor & Destructor Documentation

◆ FileLogger() [1/2]

FileLogger::FileLogger ( QSettings *  settings,
const int  refreshInterval = 10000,
QObject *  parent = 0 
)

Constructor.

Parameters
settingsConfiguration settings, usually stored in an INI file. Must not be 0. Settings are read from the current group, so the caller must have called settings->beginGroup(). Because the group must not change during runtime, it is recommended to provide a separate QSettings instance to the logger that is not used by other parts of the program.
refreshIntervalInterval of checking for changed config settings in msec, or 0=disabled
parentParent object

Definition at line 97 of file filelogger.cpp.

References file, flushTimer, refreshSettings(), refreshTimer, and settings.

97  :
98  Logger(parent), fileName(""), useQtSettings(true)
99 {
100  Q_ASSERT(settings != 0);
101  Q_ASSERT(refreshInterval >= 0);
102 
103  this->settings = settings;
104  file = 0;
105 
106  if (refreshInterval > 0) {
107  refreshTimer.start(refreshInterval, this);
108  }
109 
110  flushTimer.start(1000, this);
111 
112  refreshSettings();
113 }
Logger(QObject *parent)
Definition: logger.cpp:25
QSettings * settings
Definition: filelogger.h:120
QBasicTimer refreshTimer
Definition: filelogger.h:129
QBasicTimer flushTimer
Definition: filelogger.h:132
+ Here is the call graph for this function:

◆ FileLogger() [2/2]

FileLogger::FileLogger ( const FileLoggerSettings settings,
const int  refreshInterval = 10000,
QObject *  parent = 0 
)

Constructor.

Parameters
settingsConfiguration settings as a simple structure (see: FileLoggerSettings).
refreshIntervalInterval of checking for changed config settings in msec, or 0=disabled
parentParent object

Definition at line 115 of file filelogger.cpp.

References file, fileLoggerSettings, flushTimer, refreshSettings(), refreshTimer, and settings.

115  :
116  Logger(parent), fileName(""), useQtSettings(false)
117 {
118  Q_ASSERT(refreshInterval >= 0);
119 
121  file = 0;
122 
123  if (refreshInterval > 0) {
124  refreshTimer.start(refreshInterval, this);
125  }
126 
127  flushTimer.start(1000, this);
128 
129  refreshSettings();
130 }
Logger(QObject *parent)
Definition: logger.cpp:25
QSettings * settings
Definition: filelogger.h:120
QBasicTimer refreshTimer
Definition: filelogger.h:129
FileLoggerSettings fileLoggerSettings
Definition: filelogger.h:123
QBasicTimer flushTimer
Definition: filelogger.h:132
+ Here is the call graph for this function:

◆ ~FileLogger()

FileLogger::~FileLogger ( )
virtual

Destructor. Closes the file.

Definition at line 132 of file filelogger.cpp.

References close().

133 {
134  close();
135 }
+ Here is the call graph for this function:

Member Function Documentation

◆ close()

void FileLogger::close ( )
private

Close the output file

Definition at line 188 of file filelogger.cpp.

References file.

Referenced by refreshFileLogSettings(), refreshQtSettings(), timerEvent(), write(), and ~FileLogger().

189 {
190  if (file)
191  {
192  file->close();
193  delete file;
194  file = 0;
195  }
196 }
+ Here is the caller graph for this function:

◆ getFileLoggerSettings()

FileLoggerSettings qtwebapp::FileLogger::getFileLoggerSettings ( ) const
inline

Get a file logger settings copy

Returns
The current file logger settings

Definition at line 87 of file filelogger.h.

Referenced by qtwebapp::LoggerWithFile::getLogFileName().

87 { return fileLoggerSettings; }
FileLoggerSettings fileLoggerSettings
Definition: filelogger.h:123
+ Here is the caller graph for this function:

◆ open()

void FileLogger::open ( )
private

Open the output file

Definition at line 166 of file filelogger.cpp.

References file, and fileName.

Referenced by refreshFileLogSettings(), refreshQtSettings(), and timerEvent().

167 {
168  if (fileName.isEmpty())
169  {
170  fprintf(stderr, "FileLogger::open: Name of logFile is empty\n");
171  }
172  else
173  {
174  file = new QFile(fileName);
175 
176  if (!file->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
177  {
178  fprintf(stderr, "FileLogger::open: Cannot open log file %s: %s\n", qPrintable(fileName), qPrintable(file->errorString()));
179  file = 0;
180  }
181  else
182  {
183  fprintf(stderr, "FileLogger::open: Opened log file %s\n", qPrintable(fileName));
184  }
185  }
186 }
+ Here is the caller graph for this function:

◆ refreshFileLogSettings()

void FileLogger::refreshFileLogSettings ( )
private

Refreshes the configuration settings with file log settings structure.

Definition at line 68 of file filelogger.cpp.

References qtwebapp::FileLoggerSettings::bufferSize, qtwebapp::Logger::bufferSize, close(), fileLoggerSettings, qtwebapp::FileLoggerSettings::fileName, fileName, qtwebapp::FileLoggerSettings::maxBackups, maxBackups, qtwebapp::FileLoggerSettings::maxSize, maxSize, qtwebapp::FileLoggerSettings::minLevel, qtwebapp::Logger::minLevel, qtwebapp::FileLoggerSettings::msgFormat, qtwebapp::Logger::msgFormat, open(), qtwebapp::FileLoggerSettings::timestampFormat, and qtwebapp::Logger::timestampFormat.

Referenced by refreshSettings().

69 {
70  // Save old file name for later comparison with new settings
71  QString oldFileName = fileName;
72 
73  // Load new config settings
75 
76  // Convert relative fileName to absolute, based on the current working directory
77  if (QDir::isRelativePath(fileName)) {
78  fileName = QFileInfo(QDir::currentPath(), fileName).absoluteFilePath();
79  }
80 
87 
88  // Create new file if the filename has been changed
89  if (oldFileName != fileName)
90  {
91  fprintf(stderr, "FileLogger::refreshQtSettings: Logging to new file %s\n", qPrintable(fileName));
92  close();
93  open();
94  }
95 }
QString timestampFormat
Definition: logger.h:133
QtMsgType minLevel
Definition: logger.h:136
QString msgFormat
Definition: logger.h:130
FileLoggerSettings fileLoggerSettings
Definition: filelogger.h:123
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ refreshQtSettings()

void FileLogger::refreshQtSettings ( )
private

Refreshes the configuration settings with Qt settings.

Definition at line 32 of file filelogger.cpp.

References qtwebapp::Logger::bufferSize, close(), fileName, maxBackups, maxSize, qtwebapp::Logger::minLevel, qtwebapp::Logger::msgFormat, open(), settings, and qtwebapp::Logger::timestampFormat.

Referenced by refreshSettings().

33 {
34  // Save old file name for later comparison with new settings
35  QString oldFileName = fileName;
36 
37  // Load new config settings
38  settings->sync();
39  fileName = settings->value("fileName").toString();
40 
41  // Convert relative fileName to absolute, based on the directory of the config file.
42 #ifdef Q_OS_WIN32
43  if (QDir::isRelativePath(fileName) && settings->format()!=QSettings::NativeFormat)
44 #else
45  if (QDir::isRelativePath(fileName))
46 #endif
47  {
48  QFileInfo configFile(settings->fileName());
49  fileName = QFileInfo(configFile.absolutePath(), fileName).absoluteFilePath();
50  }
51 
52  maxSize = settings->value("maxSize", 0).toLongLong();
53  maxBackups = settings->value("maxBackups", 0).toInt();
54  msgFormat = settings->value("msgFormat", "{timestamp} {type} {msg}").toString();
55  timestampFormat = settings->value("timestampFormat", "yyyy-MM-dd HH:mm:ss.zzz").toString();
56  minLevel = static_cast<QtMsgType>(settings->value("minLevel", 0).toInt());
57  bufferSize = settings->value("bufferSize", 0).toInt();
58 
59  // Create new file if the filename has been changed
60  if (oldFileName != fileName)
61  {
62  fprintf(stderr, "FileLogger::refreshQtSettings: Logging to %s\n", qPrintable(fileName));
63  close();
64  open();
65  }
66 }
QString timestampFormat
Definition: logger.h:133
QtMsgType minLevel
Definition: logger.h:136
QSettings * settings
Definition: filelogger.h:120
QString msgFormat
Definition: logger.h:130
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ refreshSettings()

void FileLogger::refreshSettings ( )
private

Refreshes the configuration settings. This method is thread-safe.

Definition at line 19 of file filelogger.cpp.

References qtwebapp::Logger::mutex, refreshFileLogSettings(), refreshQtSettings(), and useQtSettings.

Referenced by FileLogger(), and timerEvent().

20 {
21  mutex.lock();
22 
23  if (useQtSettings) {
25  } else {
27  }
28 
29  mutex.unlock();
30 }
void refreshFileLogSettings()
Definition: filelogger.cpp:68
static QMutex mutex
Definition: logger.h:142
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rotate()

void FileLogger::rotate ( )
private

Rotate files and delete some backups if there are too many

Definition at line 198 of file filelogger.cpp.

References arg(), fileName, i, and maxBackups.

Referenced by timerEvent().

199 {
200  fprintf(stderr, "FileLogger::rotate\n");
201  // count current number of existing backup files
202  int count = 0;
203 
204  forever
205  {
206  QFile bakFile(QString("%1.%2").arg(fileName).arg(count + 1));
207 
208  if (bakFile.exists()) {
209  ++count;
210  } else {
211  break;
212  }
213  }
214 
215  // Remove all old backup files that exceed the maximum number
216  while (maxBackups > 0 && count >= maxBackups)
217  {
218  QFile::remove(QString("%1.%2").arg(fileName).arg(count));
219  --count;
220  }
221 
222  // Rotate backup files
223  for (int i = count; i > 0; --i)
224  {
225  QFile::rename(QString("%1.%2").arg(fileName).arg(i), QString("%1.%2").arg(fileName).arg(i + 1));
226  }
227 
228  // Backup the current logfile
229  QFile::rename(fileName, fileName + ".1");
230 }
Fixed< IntType, IntBits > arg(const std::complex< Fixed< IntType, IntBits > > &val)
Definition: fixed.h:2401
int32_t i
Definition: decimators.h:244
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFileLoggerSettings()

void qtwebapp::FileLogger::setFileLoggerSettings ( const FileLoggerSettings settings)
inline

Set new file logger settings data

Parameters
Filelogger settings to replace current data

Definition at line 93 of file filelogger.h.

Referenced by qtwebapp::LoggerWithFile::createOrSetFileLogger().

QSettings * settings
Definition: filelogger.h:120
FileLoggerSettings fileLoggerSettings
Definition: filelogger.h:123
+ Here is the caller graph for this function:

◆ timerEvent()

void FileLogger::timerEvent ( QTimerEvent *  event)
protected

Handler for timer events. Refreshes config settings or synchronizes I/O buffer, depending on the event. This method is thread-safe.

Parameters
eventused to distinguish between the two timers.

Definition at line 232 of file filelogger.cpp.

References close(), file, flushTimer, maxSize, qtwebapp::Logger::mutex, open(), refreshSettings(), refreshTimer, and rotate().

233 {
234  if (!event) {
235  return;
236  } else if (event->timerId() == refreshTimer.timerId()) {
237  refreshSettings();
238  }
239  else if (event->timerId() == flushTimer.timerId() && file)
240  {
241  mutex.lock();
242 
243  // Flush the I/O buffer
244  file->flush();
245 
246  // Rotate the file if it is too large
247  if (maxSize > 0 && file->size() >= maxSize)
248  {
249  close();
250  rotate();
251  open();
252  }
253 
254  mutex.unlock();
255  }
256 }
QBasicTimer refreshTimer
Definition: filelogger.h:129
QBasicTimer flushTimer
Definition: filelogger.h:132
static QMutex mutex
Definition: logger.h:142
+ Here is the call graph for this function:

◆ write()

void FileLogger::write ( const LogMessage logMessage)
virtual

Write a message to the log file

Reimplemented from qtwebapp::Logger.

Definition at line 137 of file filelogger.cpp.

References close(), file, fileName, qtwebapp::LogMessage::getType(), qtwebapp::Logger::msgFormat, qtwebapp::Logger::timestampFormat, qtwebapp::LogMessage::toString(), useQtSettings, and qtwebapp::Logger::write().

138 {
139  // Try to write to the file
140  if (file)
141  {
142  // Write the message
143  file->write(qPrintable(logMessage->toString(msgFormat, timestampFormat)));
144 
145  // Flush error messages immediately, to ensure that no important message
146  // gets lost when the program terinates abnormally.
147  if (logMessage->getType() >= QtCriticalMsg) {
148  file->flush();
149  }
150 
151  // Check for success
152  if (file->error())
153  {
154  close();
155  fprintf(stderr, "FileLogger::write: Cannot write to log file %s: %s\n", qPrintable(fileName), qPrintable(file->errorString()));
156  }
157  }
158 
159  // Fall-back to the super class method, if writing failed
160  if (!file && useQtSettings) {
161  Logger::write(logMessage);
162  }
163 
164 }
QString timestampFormat
Definition: logger.h:133
QtMsgType getType() const
Definition: logmessage.cpp:87
virtual void write(const LogMessage *logMessage)
Definition: logger.cpp:104
QString toString(const QString &msgFormat, const QString &timestampFormat) const
Definition: logmessage.cpp:29
QString msgFormat
Definition: logger.h:130
+ Here is the call graph for this function:

Member Data Documentation

◆ file

QFile* qtwebapp::FileLogger::file
private

Output file, or 0=disabled

Definition at line 126 of file filelogger.h.

Referenced by close(), FileLogger(), open(), timerEvent(), and write().

◆ fileLoggerSettings

FileLoggerSettings qtwebapp::FileLogger::fileLoggerSettings
private

Pointer to the configuration settings

Definition at line 123 of file filelogger.h.

Referenced by FileLogger(), and refreshFileLogSettings().

◆ fileName

QString qtwebapp::FileLogger::fileName
private

Configured name of the log file

Definition at line 111 of file filelogger.h.

Referenced by open(), refreshFileLogSettings(), refreshQtSettings(), rotate(), and write().

◆ flushTimer

QBasicTimer qtwebapp::FileLogger::flushTimer
private

Timer for flushing the file I/O buffer

Definition at line 132 of file filelogger.h.

Referenced by FileLogger(), and timerEvent().

◆ maxBackups

int qtwebapp::FileLogger::maxBackups
private

Configured maximum number of backup files, or 0=unlimited

Definition at line 117 of file filelogger.h.

Referenced by refreshFileLogSettings(), refreshQtSettings(), and rotate().

◆ maxSize

long qtwebapp::FileLogger::maxSize
private

Configured maximum size of the file in bytes, or 0=unlimited

Definition at line 114 of file filelogger.h.

Referenced by refreshFileLogSettings(), refreshQtSettings(), and timerEvent().

◆ refreshTimer

QBasicTimer qtwebapp::FileLogger::refreshTimer
private

Timer for refreshing configuration settings

Definition at line 129 of file filelogger.h.

Referenced by FileLogger(), and timerEvent().

◆ settings

QSettings* qtwebapp::FileLogger::settings
private

Pointer to the configuration settings

Definition at line 120 of file filelogger.h.

Referenced by FileLogger(), and refreshQtSettings().

◆ useQtSettings

bool qtwebapp::FileLogger::useQtSettings
private

Use of Qt settings or simple structure settings

Definition at line 135 of file filelogger.h.

Referenced by refreshSettings(), and write().


The documentation for this class was generated from the following files: