forked from gyunaev/birdtray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
135 lines (104 loc) · 3.02 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdarg.h>
#include <QDir>
#include <QDateTime>
#include <QStandardPaths>
#include <QMessageBox>
#include <QTextStream>
#include "log.h"
#include "dialoglogoutput.h"
Log * Log::mLog;
void Log::initialize(const QString& path )
{
if (mLog != nullptr)
Log::fatal("Attempt to initialize initialized log");
Log * self = g();
if ( path.isEmpty() )
return;
QMutexLocker l( &(self->mMutex) );
bool opened = false;
if ( path == "stderr" )
{
opened = self->mOutputFile.open( stderr, QIODevice::OpenModeFlag::WriteOnly );
}
else
{
self->mOutputFile.setFileName( path );
opened = self->mOutputFile.open(QIODevice::OpenModeFlag::WriteOnly);
}
if ( !opened )
{
l.unlock();
Log::fatal(QCoreApplication::translate("Log", "Failed to open log file %1: %2")
.arg(path, self->mOutputFile.errorString()));
}
}
void Log::fatal( const QString& str )
{
Log * self = g();
// Add the log line
self->add( str );
// Write the whole log buffer into a file log.txt
QFile file( QStandardPaths::writableLocation( QStandardPaths::TempLocation ) + QDir::separator() + "birdtray-log.txt" );
// Show the dialog
QMessageBox::critical(nullptr, QCoreApplication::translate("Log", "Fatal"),
QCoreApplication::translate(
"Log", "Fatal error: %1\n\nLog file is written into file %2")
.arg(str).arg(file.fileName()));
self->mMutex.lock();
if ( file.open(QFile::WriteOnly) )
{
QByteArray writedata = self->mEntries.join("\r\n").toUtf8();
file.write( writedata );
file.close();
}
// We're done, do not unlock self->mMutex
exit( 1 );
}
void Log::debug(const char *fmt, ... )
{
va_list vl;
char buf[4096];
va_start( vl, fmt );
vsnprintf( buf, sizeof(buf) - 1, fmt, vl );
va_end( vl );
// Add the log line
g()->add( buf );
}
void Log::showLogger()
{
Log * self = g();
QMutexLocker l( &self->mMutex );
// Create a dialog if not exist
if ( self->mDialog.isNull() )
{
self->mDialog = new DialogLogOutput();
self->mDialog->add( self->mEntries );
self->mDialog->show();
}
self->mDialog->activateWindow();
}
Log *Log::g()
{
if ( mLog == nullptr )
mLog = new Log();
return mLog;
}
void Log::add(const QString &text)
{
// Prepend a timestamp
QDateTime now = QDateTime::currentDateTime();
QString logline = QString("%1 %2 ") .arg( now.toString( "yyyy-MM-dd hh:mm:ss" )) .arg(text);
QMutexLocker l( &mMutex );
// Adding it to the log buffer
if ( mEntries.count() >= MAX_LOG_LINES )
mEntries.pop_front();
mEntries.push_back( logline );
// Add it to the open log window, if any
if ( !mDialog.isNull() )
mDialog.data()->add( logline );
// if log file is open, append to log file
if (mOutputFile.isOpen()) {
mOutputFile.write( (logline + "\r\n").toUtf8() );
mOutputFile.flush();
}
}