forked from quickshell/quickshell
		
	core/log: add timestamps to log files
This commit is contained in:
		
							parent
							
								
									38ba3fff24
								
							
						
					
					
						commit
						7c7326ec52
					
				
					 3 changed files with 25 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -55,6 +55,6 @@ void FileLogger::init() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
void FileLogger::onMessage(const LogMessage& msg) {
 | 
			
		||||
	LogManager::formatMessage(this->fileStream, msg, false);
 | 
			
		||||
	LogManager::formatMessage(this->fileStream, msg, false, true);
 | 
			
		||||
	this->fileStream << Qt::endl;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ void LogManager::messageHandler(
 | 
			
		|||
 | 
			
		||||
	auto* self = LogManager::instance();
 | 
			
		||||
 | 
			
		||||
	LogManager::formatMessage(self->stdoutStream, message, self->colorLogs);
 | 
			
		||||
	LogManager::formatMessage(self->stdoutStream, message, self->colorLogs, false);
 | 
			
		||||
	self->stdoutStream << Qt::endl;
 | 
			
		||||
 | 
			
		||||
	emit self->logMessage(message);
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +32,17 @@ LogManager* LogManager::instance() {
 | 
			
		|||
	return instance;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LogManager::formatMessage(QTextStream& stream, const LogMessage& msg, bool color) {
 | 
			
		||||
void LogManager::formatMessage(
 | 
			
		||||
    QTextStream& stream,
 | 
			
		||||
    const LogMessage& msg,
 | 
			
		||||
    bool color,
 | 
			
		||||
    bool timestamp
 | 
			
		||||
) {
 | 
			
		||||
	if (timestamp) {
 | 
			
		||||
		if (color) stream << "\033[90m";
 | 
			
		||||
		stream << msg.time.toString("yyyy-MM-dd hh:mm:ss.zzz");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (color) {
 | 
			
		||||
		switch (msg.type) {
 | 
			
		||||
		case QtDebugMsg: stream << "\033[34m DEBUG"; break;
 | 
			
		||||
| 
						 | 
				
			
			@ -62,4 +72,6 @@ void LogManager::formatMessage(QTextStream& stream, const LogMessage& msg, bool
 | 
			
		|||
	if (color && msg.type != QtFatalMsg) stream << "\033[0m";
 | 
			
		||||
 | 
			
		||||
	stream << ": " << msg.body;
 | 
			
		||||
 | 
			
		||||
	if (color && msg.type == QtFatalMsg) stream << "\033[0m";
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,18 +2,26 @@
 | 
			
		|||
 | 
			
		||||
#include <utility>
 | 
			
		||||
 | 
			
		||||
#include <qdatetime.h>
 | 
			
		||||
#include <qlogging.h>
 | 
			
		||||
#include <qobject.h>
 | 
			
		||||
#include <qtextstream.h>
 | 
			
		||||
#include <qtmetamacros.h>
 | 
			
		||||
 | 
			
		||||
struct LogMessage {
 | 
			
		||||
	explicit LogMessage(QtMsgType type, const char* category, QByteArray body)
 | 
			
		||||
	explicit LogMessage(
 | 
			
		||||
	    QtMsgType type,
 | 
			
		||||
	    const char* category,
 | 
			
		||||
	    QByteArray body,
 | 
			
		||||
	    QDateTime time = QDateTime::currentDateTime()
 | 
			
		||||
	)
 | 
			
		||||
	    : type(type)
 | 
			
		||||
	    , time(std::move(time))
 | 
			
		||||
	    , category(category)
 | 
			
		||||
	    , body(std::move(body)) {}
 | 
			
		||||
 | 
			
		||||
	QtMsgType type;
 | 
			
		||||
	QDateTime time;
 | 
			
		||||
	const char* category;
 | 
			
		||||
	QByteArray body;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -24,7 +32,7 @@ class LogManager: public QObject {
 | 
			
		|||
public:
 | 
			
		||||
	static LogManager* instance();
 | 
			
		||||
 | 
			
		||||
	static void formatMessage(QTextStream& stream, const LogMessage& msg, bool color);
 | 
			
		||||
	static void formatMessage(QTextStream& stream, const LogMessage& msg, bool color, bool timestamp);
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
	void logMessage(LogMessage msg);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue