save files to Documents by default

This commit is contained in:
Andrew Meadows 2017-12-12 14:11:25 -08:00
parent 74a1f5de96
commit 87fa919ac8
4 changed files with 66 additions and 41 deletions

View file

@ -11,11 +11,12 @@
#include <QtCore/QLoggingCategory>
#include <QtCore/QThread>
#include <shared/FileUtils.h>
#include <shared/QtHelpers.h>
#include <DependencyManager.h>
#include <Trace.h>
#include <StatTracker.h>
#include <OffscreenUi.h>
#include <StatTracker.h>
#include <Trace.h>
#include "Application.h"
@ -141,8 +142,13 @@ void TestScriptingInterface::endTraceEvent(QString name) {
tracing::traceEvent(trace_test(), name, tracing::DurationEnd);
}
void TestScriptingInterface::savePhysicsSimulationStats(QString filename) {
qApp->saveNextPhysicsStats(filename);
void TestScriptingInterface::savePhysicsSimulationStats(QString originalPath) {
QString path = FileUtils::replaceDateTimeTokens(originalPath);
path = FileUtils::computeDocumentPath(path);
if (!FileUtils::canCreateFile(path)) {
return;
}
qApp->saveNextPhysicsStats(path);
}
void TestScriptingInterface::profileRange(const QString& name, QScriptValue fn) {

View file

@ -16,7 +16,6 @@
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QStandardPaths>
#include <QtCore/QDateTime>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
@ -31,6 +30,8 @@
#include "Gzip.h"
#include "PortableHighResolutionClock.h"
#include "SharedLogging.h"
#include "shared/FileUtils.h"
#include "shared/GlobalAppProperties.h"
using namespace tracing;
@ -104,30 +105,13 @@ void TraceEvent::writeJson(QTextStream& out) const {
#endif
}
void Tracer::serialize(const QString& originalPath) {
QString path = originalPath;
// Filter for specific tokens potentially present in the path:
auto now = QDateTime::currentDateTime();
path = path.replace("{DATE}", now.date().toString("yyyyMMdd"));
path = path.replace("{TIME}", now.time().toString("HHmm"));
// If the filename is relative, turn it into an absolute path relative to the document directory.
QFileInfo originalFileInfo(path);
if (originalFileInfo.isRelative()) {
QString docsLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
path = docsLocation + "/" + path;
QFileInfo info(path);
if (!info.absoluteDir().exists()) {
QString originalRelativePath = originalFileInfo.path();
QDir(docsLocation).mkpath(originalRelativePath);
}
void Tracer::serialize(const QString& filename) {
QString fullPath = FileUtils::replaceDateTimeTokens(filename);
fullPath = FileUtils::computeDocumentPath(fullPath);
if (!FileUtils::canCreateFile(fullPath)) {
return;
}
std::list<TraceEvent> currentEvents;
{
std::lock_guard<std::mutex> guard(_eventsMutex);
@ -137,11 +121,6 @@ void Tracer::serialize(const QString& originalPath) {
}
}
// If the file exists and we can't remove it, fail early
if (QFileInfo(path).exists() && !QFile::remove(path)) {
return;
}
// If we can't open a temp file for writing, fail early
QByteArray data;
{
@ -159,15 +138,16 @@ void Tracer::serialize(const QString& originalPath) {
out << "\n]";
}
if (path.endsWith(".gz")) {
if (fullPath.endsWith(".gz")) {
QByteArray compressed;
gzip(data, compressed);
data = compressed;
}
}
{
QFile file(path);
QFile file(fullPath);
if (!file.open(QIODevice::WriteOnly)) {
qDebug(shared) << "failed to open file '" << fullPath << "'";
return;
}
file.write(data);
@ -191,7 +171,6 @@ void Tracer::serialize(const QString& originalPath) {
} }
}
};
data = document.toJson(QJsonDocument::Compact);
}
#endif

View file

@ -12,6 +12,7 @@
#include "FileUtils.h"
#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QProcess>
@ -20,6 +21,8 @@
#include <QtCore/QRegularExpression>
#include <QtGui/QDesktopServices>
#include "../SharedLogging.h"
QString FileUtils::readFile(const QString& filename) {
QFile file(filename);
@ -82,20 +85,54 @@ QString FileUtils::standardPath(QString subfolder) {
// standard path
// Mac: ~/Library/Application Support/Interface
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
if (!subfolder.startsWith("/")) {
subfolder.prepend("/");
}
if (!subfolder.endsWith("/")) {
subfolder.append("/");
}
path.append(subfolder);
QDir logDir(path);
if (!logDir.exists(path)) {
logDir.mkpath(path);
}
return path;
}
QString FileUtils::replaceDateTimeTokens(const QString& originalPath) {
// Filter for specific tokens potentially present in the path:
auto now = QDateTime::currentDateTime();
QString path = originalPath;
path.replace("{DATE}", now.date().toString("yyyyMMdd"));
path.replace("{TIME}", now.time().toString("HHmm"));
return path;
}
QString FileUtils::computeDocumentPath(const QString& originalPath) {
// If the filename is relative, turn it into an absolute path relative to the document directory.
QString path = originalPath;
QFileInfo originalFileInfo(originalPath);
if (originalFileInfo.isRelative()) {
QString docsLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
path = docsLocation + "/" + originalPath;
}
return path;
}
bool FileUtils::canCreateFile(const QString& fullPath) {
// If the file exists and we can't remove it, fail early
QFileInfo fileInfo(fullPath);
if (fileInfo.exists() && !QFile::remove(fullPath)) {
qDebug(shared) << "unable to overwrite file '" << fullPath << "'";
return false;
}
QDir dir(fileInfo.absolutePath());
if (!dir.exists()) {
if (!dir.mkpath(fullPath)) {
qDebug(shared) << "unable to create dir '" << dir.absolutePath() << "'";
return false;
}
}
return true;
}

View file

@ -21,6 +21,9 @@ public:
static QString standardPath(QString subfolder);
static QString readFile(const QString& filename);
static QStringList readLines(const QString& filename, QString::SplitBehavior splitBehavior = QString::KeepEmptyParts);
static QString replaceDateTimeTokens(const QString& path);
static QString computeDocumentPath(const QString& path);
static bool canCreateFile(const QString& fullPath);
};
#endif // hifi_FileUtils_h