übernahme Code Shortcut

This commit is contained in:
georg0480
2026-01-31 15:28:10 +01:00
parent 6f4d6b9301
commit ef46c21291
1787 changed files with 1126465 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1
as published by the Free Software Foundation and appearing in the file
LICENSE.LGPL included in the packaging of this file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#ifndef ABSTRACTAPPENDER_H
#define ABSTRACTAPPENDER_H
// Local
#include "CuteLogger_global.h"
#include <Logger.h>
// Qt
#include <QMutex>
class CUTELOGGERSHARED_EXPORT AbstractAppender
{
public:
AbstractAppender();
virtual ~AbstractAppender();
Logger::LogLevel detailsLevel() const;
void setDetailsLevel(Logger::LogLevel level);
void setDetailsLevel(const QString& level);
void write(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, const char* function,
const QString& category, const QString& message);
protected:
virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message) = 0;
private:
QMutex m_writeMutex;
Logger::LogLevel m_detailsLevel;
mutable QMutex m_detailsLevelMutex;
};
#endif // ABSTRACTAPPENDER_H

View File

@@ -0,0 +1,46 @@
/*
Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1
as published by the Free Software Foundation and appearing in the file
LICENSE.LGPL included in the packaging of this file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#ifndef ABSTRACTSTRINGAPPENDER_H
#define ABSTRACTSTRINGAPPENDER_H
// Local
#include "CuteLogger_global.h"
#include <AbstractAppender.h>
// Qt
#include <QReadWriteLock>
class CUTELOGGERSHARED_EXPORT AbstractStringAppender : public AbstractAppender
{
public:
AbstractStringAppender();
virtual QString format() const;
void setFormat(const QString&);
static QString stripFunctionName(const char*);
protected:
QString formattedString(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message) const;
private:
static QByteArray qCleanupFuncinfo(const char*);
QString m_format;
mutable QReadWriteLock m_formatLock;
};
#endif // ABSTRACTSTRINGAPPENDER_H

View File

@@ -0,0 +1,21 @@
#ifndef ANDROIDAPPENDER_H
#define ANDROIDAPPENDER_H
// Local
#include <AbstractStringAppender.h>
class AndroidAppender : public AbstractStringAppender
{
public:
AndroidAppender();
static int androidLogPriority(Logger::LogLevel);
protected:
void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message);
};
#endif // ANDROIDAPPENDER_H

View File

@@ -0,0 +1,36 @@
/*
Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1
as published by the Free Software Foundation and appearing in the file
LICENSE.LGPL included in the packaging of this file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#ifndef CONSOLEAPPENDER_H
#define CONSOLEAPPENDER_H
#include "CuteLogger_global.h"
#include <AbstractStringAppender.h>
class CUTELOGGERSHARED_EXPORT ConsoleAppender : public AbstractStringAppender
{
public:
ConsoleAppender();
virtual QString format() const;
void ignoreEnvironmentPattern(bool ignore);
protected:
virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message);
private:
bool m_ignoreEnvPattern;
};
#endif // CONSOLEAPPENDER_H

View File

@@ -0,0 +1,12 @@
#ifndef CUTELOGGER_GLOBAL_H
#define CUTELOGGER_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(CUTELOGGER_LIBRARY)
# define CUTELOGGERSHARED_EXPORT Q_DECL_EXPORT
#else
# define CUTELOGGERSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // CUTELOGGER_GLOBAL_H

View File

@@ -0,0 +1,55 @@
/*
Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1
as published by the Free Software Foundation and appearing in the file
LICENSE.LGPL included in the packaging of this file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#ifndef FILEAPPENDER_H
#define FILEAPPENDER_H
// Logger
#include "CuteLogger_global.h"
#include <AbstractStringAppender.h>
// Qt
#include <QFile>
#include <QTextStream>
class CUTELOGGERSHARED_EXPORT FileAppender : public AbstractStringAppender
{
public:
FileAppender(const QString& fileName = QString());
~FileAppender();
QString fileName() const;
void setFileName(const QString&);
bool flushOnWrite() const;
void setFlushOnWrite(bool);
bool flush();
bool reopenFile();
protected:
virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message);
bool openFile();
void closeFile();
private:
QFile m_logFile;
bool m_flushOnWrite;
QTextStream m_logStream;
mutable QMutex m_logFileMutex;
};
#endif // FILEAPPENDER_H

238
CuteLogger/include/Logger.h Normal file
View File

@@ -0,0 +1,238 @@
/*
Copyright (c) 2012 Boris Moiseev (cyberbobs at gmail dot com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1
as published by the Free Software Foundation and appearing in the file
LICENSE.LGPL included in the packaging of this file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#ifndef LOGGER_H
#define LOGGER_H
// Qt
#include <QString>
#include <QDebug>
#include <QDateTime>
#include <QElapsedTimer>
// Local
#include "CuteLogger_global.h"
class AbstractAppender;
class Logger;
CUTELOGGERSHARED_EXPORT Logger* cuteLoggerInstance();
#define cuteLogger cuteLoggerInstance()
#define LOG_TRACE CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO).write
#define LOG_DEBUG CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO).write
#define LOG_INFO CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO).write
#define LOG_WARNING CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO).write
#define LOG_ERROR CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO).write
#define LOG_FATAL CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO).write
#define LOG_CTRACE(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
#define LOG_CDEBUG(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
#define LOG_CINFO(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
#define LOG_CWARNING(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
#define LOG_CERROR(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
#define LOG_CFATAL(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO, category).write()
#define LOG_TRACE_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
#define LOG_DEBUG_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
#define LOG_INFO_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start
#define LOG_ASSERT(cond) ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, #cond) : qt_noop())
#define LOG_ASSERT_X(cond, msg) ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, msg) : qt_noop())
#if (__cplusplus >= 201103L)
#include <functional>
#define LOG_CATEGORY(category) \
Logger customCuteLoggerInstance{category};\
std::function<Logger*()> cuteLoggerInstance = [&customCuteLoggerInstance]() {\
return &customCuteLoggerInstance;\
};\
#define LOG_GLOBAL_CATEGORY(category) \
Logger customCuteLoggerInstance{category, true};\
std::function<Logger*()> cuteLoggerInstance = [&customCuteLoggerInstance]() {\
return &customCuteLoggerInstance;\
};\
#else
#define LOG_CATEGORY(category) \
Logger* cuteLoggerInstance()\
{\
static Logger customCuteLoggerInstance(category);\
return &customCuteLoggerInstance;\
}\
#define LOG_GLOBAL_CATEGORY(category) \
Logger* cuteLoggerInstance()\
{\
static Logger customCuteLoggerInstance(category);\
customCuteLoggerInstance.logToGlobalInstance(category, true);\
return &customCuteLoggerInstance;\
}\
#endif
class LoggerPrivate;
class CUTELOGGERSHARED_EXPORT Logger
{
Q_DISABLE_COPY(Logger)
public:
Logger();
Logger(const QString& defaultCategory, bool writeToGlobalInstance = false);
~Logger();
//! Describes the possible severity levels of the log records
enum LogLevel
{
Trace, //!< Trace level. Can be used for mostly unneeded records used for internal code tracing.
Debug, //!< Debug level. Useful for non-necessary records used for the debugging of the software.
Info, //!< Info level. Can be used for informational records, which may be interesting for not only developers.
Warning, //!< Warning. May be used to log some non-fatal warnings detected by your application.
Error, //!< Error. May be used for a big problems making your application work wrong but not crashing.
Fatal //!< Fatal. Used for unrecoverable errors, crashes the application right after the log record is written.
};
//! Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros
enum TimingMode
{
TimingAuto, //!< Show time in seconds, if it exceeds 10s (default)
TimingMs //!< Always use milliseconds to display
};
static QString levelToString(LogLevel logLevel);
static LogLevel levelFromString(const QString& s);
static Logger* globalInstance();
void registerAppender(AbstractAppender* appender);
void registerCategoryAppender(const QString& category, AbstractAppender* appender);
void removeAppender(AbstractAppender* appender);
void logToGlobalInstance(const QString& category, bool logToGlobal = false);
void setDefaultCategory(const QString& category);
QString defaultCategory() const;
void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
const QString& message);
void write(LogLevel logLevel, const char* file, int line, const char* function, const char* category, const QString& message);
void writeAssert(const char* file, int line, const char* function, const char* condition);
private:
void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
const QString& message, bool fromLocalInstance);
Q_DECLARE_PRIVATE(Logger)
LoggerPrivate* d_ptr;
};
class CUTELOGGERSHARED_EXPORT CuteMessageLogger
{
Q_DISABLE_COPY(CuteMessageLogger)
public:
CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function)
: m_l(l),
m_level(level),
m_file(file),
m_line(line),
m_function(function),
m_category(nullptr)
{}
CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function, const char* category)
: m_l(l),
m_level(level),
m_file(file),
m_line(line),
m_function(function),
m_category(category)
{}
~CuteMessageLogger();
void write(const char* msg, ...)
#if defined(Q_CC_GNU) && !defined(__INSURE__)
# if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
__attribute__ ((format (gnu_printf, 2, 3)))
# else
__attribute__ ((format (printf, 2, 3)))
# endif
#endif
;
void write(const QString& msg);
QDebug write();
private:
Logger* m_l;
Logger::LogLevel m_level;
const char* m_file;
int m_line;
const char* m_function;
const char* m_category;
QString m_message;
};
class CUTELOGGERSHARED_EXPORT LoggerTimingHelper
{
Q_DISABLE_COPY(LoggerTimingHelper)
public:
inline explicit LoggerTimingHelper(Logger* l, Logger::LogLevel logLevel, const char* file, int line,
const char* function)
: m_logger(l),
m_logLevel(logLevel),
m_timingMode(Logger::TimingAuto),
m_file(file),
m_line(line),
m_function(function)
{}
void start(const char* msg, ...)
#if defined(Q_CC_GNU) && !defined(__INSURE__)
# if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
__attribute__ ((format (gnu_printf, 2, 3)))
# else
__attribute__ ((format (printf, 2, 3)))
# endif
#endif
;
void start(const QString& msg = QString());
void start(Logger::TimingMode mode, const QString& msg);
~LoggerTimingHelper();
private:
Logger* m_logger;
QElapsedTimer m_time;
Logger::LogLevel m_logLevel;
Logger::TimingMode m_timingMode;
const char* m_file;
int m_line;
const char* m_function;
QString m_block;
};
#endif // LOGGER_H

View File

@@ -0,0 +1,29 @@
/*
Copyright (c) 2010 Karl-Heinz Reichel (khreichel at googlemail dot com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1
as published by the Free Software Foundation and appearing in the file
LICENSE.LGPL included in the packaging of this file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
*/
#ifndef OUTPUTDEBUGAPPENDER_H
#define OUTPUTDEBUGAPPENDER_H
#include "CuteLogger_global.h"
#include <AbstractStringAppender.h>
class CUTELOGGERSHARED_EXPORT OutputDebugAppender : public AbstractStringAppender
{
protected:
virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message);
};
#endif // OUTPUTDEBUGAPPENDER_H

View File

@@ -0,0 +1,77 @@
#ifndef ROLLINGFILEAPPENDER_H
#define ROLLINGFILEAPPENDER_H
#include <QDateTime>
#include <FileAppender.h>
/*!
* \brief The RollingFileAppender class extends FileAppender so that the underlying file is rolled over at a user chosen frequency.
*
* The class is based on Log4Qt.DailyRollingFileAppender class (http://log4qt.sourceforge.net/)
* and has the same date pattern format.
*
* For example, if the fileName is set to /foo/bar and the DatePattern set to the daily rollover ('.'yyyy-MM-dd'.log'), on 2014-02-16 at midnight,
* the logging file /foo/bar.log will be copied to /foo/bar.2014-02-16.log and logging for 2014-02-17 will continue in /foo/bar
* until it rolls over the next day.
*
* The logFilesLimit parameter is used to automatically delete the oldest log files in the directory during rollover
* (so no more than logFilesLimit recent log files exist in the directory at any moment).
* \sa setDatePattern(DatePattern), setLogFilesLimit(int)
*/
class CUTELOGGERSHARED_EXPORT RollingFileAppender : public FileAppender
{
public:
/*!
* The enum DatePattern defines constants for date patterns.
* \sa setDatePattern(DatePattern)
*/
enum DatePattern
{
/*! The minutely date pattern string is "'.'yyyy-MM-dd-hh-mm". */
MinutelyRollover = 0,
/*! The hourly date pattern string is "'.'yyyy-MM-dd-hh". */
HourlyRollover,
/*! The half-daily date pattern string is "'.'yyyy-MM-dd-a". */
HalfDailyRollover,
/*! The daily date pattern string is "'.'yyyy-MM-dd". */
DailyRollover,
/*! The weekly date pattern string is "'.'yyyy-ww". */
WeeklyRollover,
/*! The monthly date pattern string is "'.'yyyy-MM". */
MonthlyRollover
};
Q_ENUMS(DatePattern)
RollingFileAppender(const QString& fileName = QString());
DatePattern datePattern() const;
void setDatePattern(DatePattern datePattern);
void setDatePattern(const QString& datePattern);
QString datePatternString() const;
void setLogFilesLimit(int limit);
int logFilesLimit() const;
protected:
virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
const char* function, const QString& category, const QString& message);
private:
void rollOver();
void computeRollOverTime();
void computeFrequency();
void removeOldFiles();
void setDatePatternString(const QString& datePatternString);
QString m_datePatternString;
DatePattern m_frequency;
QDateTime m_rollOverTime;
QString m_rollOverSuffix;
int m_logFilesLimit;
mutable QMutex m_rollingMutex;
};
#endif // ROLLINGFILEAPPENDER_H