Skip to content

Commit

Permalink
REFAC(client): Switch to new logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebeatrici committed Jan 16, 2025
1 parent 44a89de commit ddfed70
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 202 deletions.
1 change: 1 addition & 0 deletions src/mumble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ set(MUMBLE_SOURCES
"Log.cpp"
"Log.h"
"Log.ui"
"Logger.cpp"
"LookConfig.cpp"
"LookConfig.h"
"LookConfig.ui"
Expand Down
39 changes: 8 additions & 31 deletions src/mumble/DeveloperConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,20 @@

#include "DeveloperConsole.h"

#include "LogEmitter.h"
#include "Global.h"

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QTextBrowser>

DeveloperConsole::DeveloperConsole(QObject *parent) : QObject(parent) {
connect(Global::get().le.data(), SIGNAL(newLogEntry(const QString &)), this, SLOT(addLogMessage(const QString &)));
DeveloperConsole::DeveloperConsole(QTextBrowser *textBox, QObject *parent)
: QObject(parent), m_window(std::make_unique< QMainWindow >()) {
m_window->resize(675, 300);
m_window->setCentralWidget(textBox);
m_window->setWindowTitle(tr("Developer Console"));
}

DeveloperConsole::~DeveloperConsole() {
QMainWindow *mw = m_window.data();
delete mw;
}

void DeveloperConsole::show() {
if (m_window.isNull()) {
QMainWindow *mw = new QMainWindow();
mw->setAttribute(Qt::WA_DeleteOnClose);
QTextBrowser *tb = new QTextBrowser();
mw->resize(675, 300);
mw->setCentralWidget(tb);
mw->setWindowTitle(tr("Developer Console"));

connect(Global::get().le.data(), SIGNAL(newLogEntry(const QString &)), tb, SLOT(append(const QString &)));

foreach (const QString &m, m_logEntries)
tb->append(m);
m_window = QPointer< QMainWindow >(mw);
}

m_window.data()->show();
m_window.data()->activateWindow();
}

void DeveloperConsole::addLogMessage(const QString &msg) {
if (m_logEntries.count() >= 1000)
m_logEntries.removeFirst();

m_logEntries.append(msg);
m_window->show();
m_window->activateWindow();
}
20 changes: 10 additions & 10 deletions src/mumble/DeveloperConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
#ifndef MUMBLE_MUMBLE_DEVELOPERCONSOLE_H_
#define MUMBLE_MUMBLE_DEVELOPERCONSOLE_H_

#include <memory>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtCore/QStringList>
#include <QtWidgets/QMainWindow>

class QMainWindow;
class QTextBrowser;

class DeveloperConsole : public QObject {
private:
Q_OBJECT
Q_DISABLE_COPY(DeveloperConsole)

protected:
QPointer< QMainWindow > m_window;
QStringList m_logEntries;
public slots:
void addLogMessage(const QString &);

public:
DeveloperConsole(QObject *parent = nullptr);
DeveloperConsole(QTextBrowser *textBox, QObject *parent = nullptr);
~DeveloperConsole();

void show();

private:
std::unique_ptr< QMainWindow > m_window;
};

#endif
2 changes: 0 additions & 2 deletions src/mumble/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class Overlay;
class LCD;
class Zeroconf;
class OverlayClient;
class LogEmitter;
class DeveloperConsole;
class TalkingUI;
class TrayIcon;
Expand Down Expand Up @@ -69,7 +68,6 @@ struct Global Q_DECL_FINAL {
LCD *lcd;
Zeroconf *zeroconf;
QNetworkAccessManager *nam;
QSharedPointer< LogEmitter > le;
DeveloperConsole *c;
TalkingUI *talkingUI;
int iPushToTalk;
Expand Down
13 changes: 9 additions & 4 deletions src/mumble/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#endif
#include "LCD.h"
#include "Log.h"
#include "LogEmitter.h"
#include "Logger.h"
#include "MainWindow.h"
#include "ServerHandler.h"
#ifdef USE_ZEROCONF
Expand Down Expand Up @@ -58,6 +58,7 @@
#include <QtCore/QProcess>
#include <QtGui/QDesktopServices>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QTextBrowser>

#include <iostream>
#include <memory>
Expand All @@ -79,6 +80,8 @@ void throw_exception(std::exception const &) {
} // namespace boost
#endif

using namespace mumble;

extern void os_init();
extern char *os_lang;

Expand Down Expand Up @@ -191,8 +194,9 @@ int main(int argc, char **argv) {
Global::g_global_struct = new Global();
}

Global::get().le = QSharedPointer< LogEmitter >(new LogEmitter());
Global::get().c = new DeveloperConsole();
auto logBox = new QTextBrowser();
log::init(logBox);
Global::get().c = new DeveloperConsole(logBox);

os_init();

Expand Down Expand Up @@ -842,6 +846,8 @@ int main(int argc, char **argv) {
}
}

log::prepareToExit();

QCoreApplication::processEvents();

// Only start deleting items once all pending events have been processed (Audio::stop deletes the audio
Expand Down Expand Up @@ -880,7 +886,6 @@ int main(int argc, char **argv) {
#endif

delete Global::get().c;
Global::get().le.clear();

DeferInit::run_destroyers();

Expand Down
64 changes: 0 additions & 64 deletions src/mumble/os_macx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "LogEmitter.h"
#include "MainWindow.h"
#ifdef USE_OVERLAY
# include "Overlay.h"
Expand All @@ -14,54 +13,13 @@
#include <CoreFoundation/CoreFoundation.h>

char *os_lang = nullptr;
static FILE *fConsole = nullptr;

static QSharedPointer<LogEmitter> le;

#define PATH_MAX 1024
static char crashhandler_fn[PATH_MAX];

static void crashhandler_signals_restore();
static void crashhandler_handle_crash();

static void mumbleMessageOutputQString(QtMsgType type, const QString &msg) {
char c;

switch (type) {
case QtDebugMsg:
c='D';
break;
case QtWarningMsg:
c='W';
break;
case QtFatalMsg:
c='F';
break;
default:
c='X';
}

QString date = QDateTime::currentDateTime().toString(QLatin1String("yyyy-MM-dd hh:mm:ss.zzz"));
QString fmsg = QString::fromLatin1("<%1>%2 %3").arg(c).arg(date).arg(msg);
fprintf(stderr, "%s\n", qPrintable(fmsg));
fprintf(fConsole, "%s\n", qPrintable(fmsg));
fflush(fConsole);

le->addLogEntry(fmsg);

if (type == QtFatalMsg) {
CFStringRef csMsg = CFStringCreateWithFormat(kCFAllocatorDefault, nullptr, CFSTR("%s\n\nThe error has been logged. Please submit your log file to the Mumble project if the problem persists."), qPrintable(msg));
CFUserNotificationDisplayAlert(0, 0, nullptr, nullptr, nullptr, CFSTR("Mumble has encountered a fatal error"), csMsg, CFSTR("OK"), nullptr, nullptr, nullptr);
CFRelease(csMsg);
exit(0);
}
}

static void mumbleMessageOutputWithContext(QtMsgType type, const QMessageLogContext &ctx, const QString &msg) {
Q_UNUSED(ctx);
mumbleMessageOutputQString(type, msg);
}

void query_language() {
CFPropertyListRef cfaLangs;
CFStringRef cfsLang;
Expand Down Expand Up @@ -129,28 +87,6 @@ static void crashhandler_handle_crash() {
void os_init() {
crashhandler_init();

const char *home = getenv("HOME");
const char *logpath = "/Library/Logs/Mumble.log";

// Make a copy of the global LogEmitter, such that
// os_macx.mm doesn't have to consider the deletion
// of the Global object and its LogEmitter object.
le = Global::get().le;

if (home) {
size_t len = strlen(home) + strlen(logpath) + 1;
static std::vector<char> buffer;
buffer.resize(len);
char *buff = buffer.data();
memset(buff, 0, len);
strcat(buff, home);
strcat(buff, logpath);
fConsole = fopen(buff, "a+");
if (fConsole) {
qInstallMessageHandler(mumbleMessageOutputWithContext);
}
}

/* Query for language setting. OS X's LANG environment variable is determined from the region selected
* in SystemPrefs -> International -> Formats -> Region instead of the system language. We override this
* by always using the system language, to get rid of all sorts of nasty language inconsistencies. */
Expand Down
43 changes: 0 additions & 43 deletions src/mumble/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,6 @@
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "LogEmitter.h"
#include "Global.h"

static QSharedPointer< LogEmitter > le;

static void mumbleMessageOutputQString(QtMsgType type, const QString &msg) {
char c;

switch (type) {
case QtDebugMsg:
c = 'D';
break;
case QtWarningMsg:
c = 'W';
break;
case QtFatalMsg:
c = 'F';
break;
default:
c = 'X';
}

QString date = QDateTime::currentDateTime().toString(QLatin1String("yyyy-MM-dd hh:mm:ss.zzz"));
QString fmsg = QString::fromLatin1("<%1>%2 %3").arg(c).arg(date).arg(msg);
fprintf(stderr, "%s\n", qPrintable(fmsg));

le->addLogEntry(fmsg);

if (type == QtFatalMsg) {
exit(1);
}
}

static void mumbleMessageOutputWithContext(QtMsgType type, const QMessageLogContext &ctx, const QString &msg) {
Q_UNUSED(ctx);
mumbleMessageOutputQString(type, msg);
}

void os_init() {
// Make a copy of the global LogEmitter, such that
// os_unix.cpp doesn't have to consider the deletion
// of the Global object and its LogEmitter object.
le = Global::get().le;

qInstallMessageHandler(mumbleMessageOutputWithContext);
}
48 changes: 0 additions & 48 deletions src/mumble/os_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "LogEmitter.h"
#include "MumbleApplication.h"

#ifdef _MSC_VER
Expand Down Expand Up @@ -45,52 +44,17 @@ void mumble_speex_init();
#define DUMP_BUFFER_SIZE 1024

static wchar_t wcCrashDumpPath[DUMP_BUFFER_SIZE];
static FILE *fConsole = nullptr;

static wchar_t wcComment[DUMP_BUFFER_SIZE] = L"";
static MINIDUMP_USER_STREAM musComment;

static QSharedPointer< LogEmitter > le;

static int cpuinfo[4];

bool bIsWin7 = false;
bool bIsVistaSP1 = false;

HWND mumble_mw_hwnd = 0;

static void mumbleMessageOutputQString(QtMsgType type, const QString &msg) {
char c;
switch (type) {
case QtDebugMsg:
c = 'D';
break;
case QtWarningMsg:
c = 'W';
break;
case QtFatalMsg:
c = 'F';
break;
default:
c = 'X';
}
QString date = QDateTime::currentDateTime().toString(QLatin1String("yyyy-MM-dd hh:mm:ss.zzz"));
QString fmsg = QString::fromLatin1("<%1>%2 %3").arg(c).arg(date).arg(msg);
fprintf(fConsole, "%s\n", qPrintable(fmsg));
fflush(fConsole);
OutputDebugStringA(qPrintable(fmsg));
le->addLogEntry(fmsg);
if (type == QtFatalMsg) {
::MessageBoxA(nullptr, qPrintable(msg), "Mumble", MB_OK | MB_ICONERROR);
exit(0);
}
}

static void mumbleMessageOutputWithContext(QtMsgType type, const QMessageLogContext &ctx, const QString &msg) {
Q_UNUSED(ctx);
mumbleMessageOutputQString(type, msg);
}

static LONG WINAPI MumbleUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
MINIDUMP_EXCEPTION_INFORMATION i;
i.ThreadId = GetCurrentThreadId();
Expand Down Expand Up @@ -255,19 +219,7 @@ void os_init() {
enableCrashOnCrashes();
mumble_speex_init();

// Make a copy of the global LogEmitter, such that
// os_win.cpp doesn't have to consider the deletion
// of the Global object and its LogEmitter object.
le = Global::get().le;

#ifdef QT_NO_DEBUG
QString console = Global::get().qdBasePath.filePath(QLatin1String("Console.txt"));
fConsole = _wfsopen(console.toStdWString().c_str(), L"a+", _SH_DENYWR);

if (fConsole) {
qInstallMessageHandler(mumbleMessageOutputWithContext);
}

QString hash;
QFile f(qApp->applicationFilePath());
if (!f.open(QIODevice::ReadOnly)) {
Expand Down

0 comments on commit ddfed70

Please sign in to comment.