From 840d1a33ef67510fcca6d385c81883b233d039f2 Mon Sep 17 00:00:00 2001 From: Stojce Slavkovski Date: Mon, 23 Dec 2013 21:23:47 +0100 Subject: [PATCH] Reveal log file - change log location lo application data folder - reveal file in Finder (Explorer) implementation --- interface/src/AbstractLoggerInterface.h | 3 +- interface/src/Application.cpp | 8 ++-- interface/src/FileLogger.cpp | 19 +++++++-- interface/src/FileLogger.h | 1 + interface/src/ui/LogDialog.cpp | 2 +- libraries/shared/src/FileUtils.cpp | 56 +++++++++++++++++++++++++ libraries/shared/src/FileUtils.h | 21 ++++++++++ 7 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 libraries/shared/src/FileUtils.cpp create mode 100644 libraries/shared/src/FileUtils.h diff --git a/interface/src/AbstractLoggerInterface.h b/interface/src/AbstractLoggerInterface.h index 13e3aa0ffb..d53ada6cca 100644 --- a/interface/src/AbstractLoggerInterface.h +++ b/interface/src/AbstractLoggerInterface.h @@ -22,6 +22,7 @@ public: virtual void addMessage(QString) = 0; virtual QStringList getLogData(QString) = 0; + virtual void locateLog() = 0; signals: void logReceived(QString message); @@ -30,4 +31,4 @@ private: bool _extraDebugging = false; }; -#endif /* defined(__hifi__AbstractAudioInterface__) */ +#endif /* defined(__interface__AbstractLoggerInterface__) */ diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a90aaab223..442b5f27a1 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -141,7 +141,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _recentMaxPackets(0), _resetRecentMaxPacketsSoon(true), _swatch(NULL), - _pasteMode(false) + _pasteMode(false), + _logger(new FileLogger()) { _applicationStartupTime = startup_time; @@ -149,8 +150,6 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : QFontDatabase::addApplicationFont("resources/styles/Inconsolata.otf"); _window->setWindowTitle("Interface"); - _logger = new FileLogger(); - qInstallMessageHandler(messageHandler); // call Menu getInstance static method to set up the menu @@ -269,7 +268,8 @@ Application::~Application() { VoxelTreeElement::removeDeleteHook(&_voxels); // we don't need to do this processing on shutdown Menu::getInstance()->deleteLater(); - + + delete _logger; delete _settings; delete _followMode; delete _glWidget; diff --git a/interface/src/FileLogger.cpp b/interface/src/FileLogger.cpp index b9dadb4a8c..5f652ec0f2 100644 --- a/interface/src/FileLogger.cpp +++ b/interface/src/FileLogger.cpp @@ -8,13 +8,23 @@ #include "FileLogger.h" #include "HifiSockAddr.h" +#include #include #include +#include +#include FileLogger::FileLogger() : _lines(NULL) { QHostAddress clientAddress = QHostAddress(getHostOrderLocalAddress()); QDateTime now = QDateTime::currentDateTime(); - _fileName = QString("hifi-log_%1_%2.txt").arg(clientAddress.toString(), now.toString("yyyy-MM-dd_hh.mm.ss")); + + _fileName = QStandardPaths::writableLocation(QStandardPaths::DataLocation); + QDir logDir(_fileName); + if (!logDir.exists(_fileName)) { + logDir.mkdir(_fileName); + } + + _fileName.append(QString("/hifi-log_%1_%2.txt").arg(clientAddress.toString(), now.toString("yyyy-MM-dd_hh.mm.ss"))); setExtraDebugging(false); } @@ -41,10 +51,13 @@ void FileLogger::addMessage(QString message) { _lines.append(message); QFile file(_fileName); - if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { QTextStream out(&file); out << message; } _mutex.unlock(); -} \ No newline at end of file +} + +void FileLogger::locateLog() { + FileUtils::LocateFile(_fileName); +} diff --git a/interface/src/FileLogger.h b/interface/src/FileLogger.h index 848bcd44d3..fffbc5fa91 100644 --- a/interface/src/FileLogger.h +++ b/interface/src/FileLogger.h @@ -19,6 +19,7 @@ public: virtual void addMessage(QString); virtual QStringList getLogData(QString); + virtual void locateLog(); private: QStringList _lines; diff --git a/interface/src/ui/LogDialog.cpp b/interface/src/ui/LogDialog.cpp index ed1584aad6..5d6a404c76 100644 --- a/interface/src/ui/LogDialog.cpp +++ b/interface/src/ui/LogDialog.cpp @@ -121,7 +121,7 @@ void LogDialog::handleSearchButton() { } void LogDialog::handleRevealButton() { - + _logger->locateLog(); } void LogDialog::handleExtraDebuggingCheckbox(const int state) { diff --git a/libraries/shared/src/FileUtils.cpp b/libraries/shared/src/FileUtils.cpp new file mode 100644 index 0000000000..b58799104f --- /dev/null +++ b/libraries/shared/src/FileUtils.cpp @@ -0,0 +1,56 @@ +// +// FileUtils.cpp +// hifi +// +// Created by Stojce Slavkovski on 12/23/13. +// +// + +#include "FileUtils.h" +#include +#include + +void FileUtils::LocateFile(QString filePath) { + + // adopted from + // http://stackoverflow.com/questions/3490336/how-to-reveal-in-finder-or-show-in-explorer-with-qt + // and + // http://lynxline.com/show-in-finder-show-in-explorer/ + + QFileInfo fileInfo(filePath); + if (!fileInfo.exists()) { + return; + } + + bool success = false; +#ifdef Q_OS_MAC + QStringList args; + args << "-e"; + args << "tell application \"Finder\""; + args << "-e"; + args << "activate"; + args << "-e"; + args << "select POSIX file \"" + fileInfo.absoluteFilePath().toUtf8() + "\""; + args << "-e"; + args << "end tell"; + success = QProcess::startDetached("osascript", args); +#endif + +#ifdef Q_OS_WIN + + QStringList args; + // don't send `select` command switch if `filePath` is folder + if (!fileInfo.isDir()) { + args << "/select,"; + } + args += QDir::toNativeSeparators(fileInfo.absoluteFilePath().toUtf8()); + success = QProcess::startDetached("explorer", args); + +#endif + + // fallback, open enclosing folder + if (!success) { + const QString folder = fileInfo.path(); + QDesktopServices::openUrl(QUrl::fromLocalFile(folder)); + } +} diff --git a/libraries/shared/src/FileUtils.h b/libraries/shared/src/FileUtils.h new file mode 100644 index 0000000000..a725e72ca1 --- /dev/null +++ b/libraries/shared/src/FileUtils.h @@ -0,0 +1,21 @@ +// +// FileUtils.h +// hifi +// +// Created by Stojce Slavkovski on 12/23/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#ifndef hifi_FileUtils_h +#define hifi_FileUtils_h + +#include + +class FileUtils { + +public: + static void LocateFile(QString); + +}; + +#endif