Wrap all TRACE() calls in data recovery functions with RECO_TRACE()

In this way we can completely disable the log of data recovery events.
In a future we might offer a way to log all these events in a log file
(probably included in crash reports).
This commit is contained in:
David Capello 2023-05-01 19:22:53 -03:00
parent b7f06c1366
commit 25cb258f76
6 changed files with 59 additions and 40 deletions

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -20,6 +20,7 @@
#include "app/app.h" #include "app/app.h"
#include "app/context.h" #include "app/context.h"
#include "app/crash/log.h"
#include "app/crash/recovery_config.h" #include "app/crash/recovery_config.h"
#include "app/crash/session.h" #include "app/crash/session.h"
#include "app/doc.h" #include "app/doc.h"
@ -83,7 +84,7 @@ void BackupObserver::stop()
void BackupObserver::onAddDocument(Doc* document) void BackupObserver::onAddDocument(Doc* document)
{ {
TRACE("RECO: Observe document %p\n", document); RECO_TRACE("RECO: Observe document %p\n", document);
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
m_documents.push_back(document); m_documents.push_back(document);
@ -91,7 +92,7 @@ void BackupObserver::onAddDocument(Doc* document)
void BackupObserver::onRemoveDocument(Doc* doc) void BackupObserver::onRemoveDocument(Doc* doc)
{ {
TRACE("RECO: Remove document %p\n", doc); RECO_TRACE("RECO: Remove document %p\n", doc);
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
base::remove_from_container(m_documents, doc); base::remove_from_container(m_documents, doc);
@ -108,13 +109,13 @@ void BackupObserver::onRemoveDocument(Doc* doc)
// in m_closedDocs list anyway so we call markAsBackedUp(), and // in m_closedDocs list anyway so we call markAsBackedUp(), and
// then it's deleted from ClosedDocs::backgroundThread() // then it's deleted from ClosedDocs::backgroundThread()
TRACE("RECO: Adding to CLOSEDOC %p\n", doc); RECO_TRACE("RECO: Adding to CLOSEDOC %p\n", doc);
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
m_closedDocs.push_back(doc); m_closedDocs.push_back(doc);
} }
else { else {
TRACE("RECO: Removing doc %p from session\n", doc); RECO_TRACE("RECO: Removing doc %p from session\n", doc);
m_session->removeDocument(doc); m_session->removeDocument(doc);
} }
} }
@ -135,8 +136,8 @@ void BackupObserver::backgroundThread()
while (!m_done) { while (!m_done) {
m_wakeup.wait_for(lock, std::chrono::seconds(waitFor)); m_wakeup.wait_for(lock, std::chrono::seconds(waitFor));
TRACE("RECO: Start backup process for %d documents\n", RECO_TRACE("RECO: Start backup process for %d documents\n",
m_documents.size() + m_closedDocs.size()); m_documents.size() + m_closedDocs.size());
SwitchBackupIcon icon; SwitchBackupIcon icon;
base::Chrono chrono; base::Chrono chrono;
@ -151,10 +152,10 @@ void BackupObserver::backgroundThread()
for (auto it=m_closedDocs.begin(); it != m_closedDocs.end(); ) { for (auto it=m_closedDocs.begin(); it != m_closedDocs.end(); ) {
Doc* doc = *it; Doc* doc = *it;
TRACE("RECO: Save backup data for %p...\n", doc); RECO_TRACE("RECO: Save backup data for %p...\n", doc);
if (saveDocData(doc)) { if (saveDocData(doc)) {
TRACE("RECO: Doc %p is fully backed up\n", doc); RECO_TRACE("RECO: Doc %p is fully backed up\n", doc);
it = m_closedDocs.erase(it); it = m_closedDocs.erase(it);
doc->markAsBackedUp(); doc->markAsBackedUp();
@ -168,7 +169,7 @@ void BackupObserver::backgroundThread()
waitFor = (somethingLocked ? lockedPeriod: normalPeriod); waitFor = (somethingLocked ? lockedPeriod: normalPeriod);
TRACE("RECO: Backup process done (%.16g)\n", chrono.elapsed()); RECO_TRACE("RECO: Backup process done (%.16g)\n", chrono.elapsed());
} }
} }
@ -180,10 +181,10 @@ bool BackupObserver::saveDocData(Doc* doc)
return true; return true;
if (doc->inhibitBackup()) { if (doc->inhibitBackup()) {
TRACE("RECO: Document '%d' backup is temporarily inhibited\n", doc->id()); RECO_TRACE("RECO: Document '%d' backup is temporarily inhibited\n", doc->id());
} }
else if (!m_session->saveDocumentChanges(doc)) { else if (!m_session->saveDocumentChanges(doc)) {
TRACE("RECO: Document '%d' backup was canceled by UI\n", doc->id()); RECO_TRACE("RECO: Document '%d' backup was canceled by UI\n", doc->id());
} }
else { else {
#ifdef TEST_BACKUP_INTEGRITY #ifdef TEST_BACKUP_INTEGRITY
@ -192,18 +193,18 @@ bool BackupObserver::saveDocData(Doc* doc)
m_session->restoreBackupDocById(doc->id(), nullptr)); m_session->restoreBackupDocById(doc->id(), nullptr));
DocDiff diff = compare_docs(doc, copy.get()); DocDiff diff = compare_docs(doc, copy.get());
if (diff.anything) { if (diff.anything) {
TRACEARGS("RECO: Differences:", RECO_TRACE("RECO: Differences: %s %s %s %s %s %s %s %s %s %s %s\n",
diff.canvas ? "canvas": "", diff.canvas ? "canvas": "",
diff.totalFrames ? "totalFrames": "", diff.totalFrames ? "totalFrames": "",
diff.frameDuration ? "frameDuration": "", diff.frameDuration ? "frameDuration": "",
diff.tags ? "tags": "", diff.tags ? "tags": "",
diff.palettes ? "palettes": "", diff.palettes ? "palettes": "",
diff.tilesets ? "tilesets": "", diff.tilesets ? "tilesets": "",
diff.layers ? "layers": "", diff.layers ? "layers": "",
diff.cels ? "cels": "", diff.cels ? "cels": "",
diff.images ? "images": "", diff.images ? "images": "",
diff.colorProfiles ? "colorProfiles": "", diff.colorProfiles ? "colorProfiles": "",
diff.gridBounds ? "gridBounds": ""); diff.gridBounds ? "gridBounds": "");
Doc* copyDoc = copy.release(); Doc* copyDoc = copy.release();
ui::execute_from_ui_thread( ui::execute_from_ui_thread(
@ -212,14 +213,14 @@ bool BackupObserver::saveDocData(Doc* doc)
}); });
} }
else { else {
TRACE("RECO: No differences\n"); RECO_TRACE("RECO: No differences\n");
} }
#endif #endif
return true; return true;
} }
} }
catch (const std::exception&) { catch (const std::exception&) {
TRACE("RECO: Document '%d' is locked\n", doc->id()); RECO_TRACE("RECO: Document '%d' is locked\n", doc->id());
} }
return false; return false;
} }

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -12,6 +12,7 @@
#include "app/crash/data_recovery.h" #include "app/crash/data_recovery.h"
#include "app/crash/backup_observer.h" #include "app/crash/backup_observer.h"
#include "app/crash/log.h"
#include "app/crash/session.h" #include "app/crash/session.h"
#include "app/pref/preferences.h" #include "app/pref/preferences.h"
#include "app/resource_finder.h" #include "app/resource_finder.h"
@ -23,8 +24,6 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#define RECO_TRACE(...) // TRACE
namespace app { namespace app {
namespace crash { namespace crash {

16
src/app/crash/log.h Normal file
View File

@ -0,0 +1,16 @@
// Aseprite
// Copyright (c) 2023 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_CRASH_LOG_H_INCLUDED
#define APP_CRASH_LOG_H_INCLUDED
#pragma once
#include "base/debug.h"
// TODO should we log some events in release?
#define RECO_TRACE(...) // TRACE(__VA_ARGS__)
#endif

View File

@ -14,6 +14,7 @@
#include "app/console.h" #include "app/console.h"
#include "app/crash/doc_format.h" #include "app/crash/doc_format.h"
#include "app/crash/internals.h" #include "app/crash/internals.h"
#include "app/crash/log.h"
#include "app/doc.h" #include "app/doc.h"
#include "base/convert_to.h" #include "base/convert_to.h"
#include "base/exception.h" #include "base/exception.h"
@ -152,7 +153,7 @@ private:
if (!ver) if (!ver)
continue; continue;
TRACE("RECO: Restoring %s #%d v%d\n", prefix, id, ver); RECO_TRACE("RECO: Restoring %s #%d v%d\n", prefix, id, ver);
std::string fn = prefix; std::string fn = prefix;
fn.push_back('-'); fn.push_back('-');
@ -166,11 +167,11 @@ private:
obj = (this->*readMember)(s); obj = (this->*readMember)(s);
if (obj) { if (obj) {
TRACE("RECO: %s #%d v%d restored successfully\n", prefix, id, ver); RECO_TRACE("RECO: %s #%d v%d restored successfully\n", prefix, id, ver);
return obj; return obj;
} }
else { else {
TRACE("RECO: %s #%d v%d was not restored\n", prefix, id, ver); RECO_TRACE("RECO: %s #%d v%d was not restored\n", prefix, id, ver);
} }
} }
@ -187,7 +188,7 @@ private:
m_docFormatVer = read16(s); m_docFormatVer = read16(s);
if (s.eof()) m_docFormatVer = DOC_FORMAT_VERSION_0; if (s.eof()) m_docFormatVer = DOC_FORMAT_VERSION_0;
TRACE("RECO: internal format version=%d\n", m_docFormatVer); RECO_TRACE("RECO: internal format version=%d\n", m_docFormatVer);
// Load DocumentInfo only // Load DocumentInfo only
if (m_loadInfo) { if (m_loadInfo) {

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019-2022 Igara Studio S.A. // Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -13,6 +13,7 @@
#include "app/console.h" #include "app/console.h"
#include "app/context.h" #include "app/context.h"
#include "app/crash/log.h"
#include "app/crash/read_document.h" #include "app/crash/read_document.h"
#include "app/crash/recovery_config.h" #include "app/crash/recovery_config.h"
#include "app/crash/write_document.h" #include "app/crash/write_document.h"
@ -236,7 +237,7 @@ bool Session::saveDocumentChanges(Doc* doc)
app::Context ctx; app::Context ctx;
std::string dir = base::join_path(m_path, std::string dir = base::join_path(m_path,
base::convert_to<std::string>(doc->id())); base::convert_to<std::string>(doc->id()));
TRACE("RECO: Saving document '%s'...\n", dir.c_str()); RECO_TRACE("RECO: Saving document '%s'...\n", dir.c_str());
// Create directory for document // Create directory for document
if (!base::is_directory(dir)) if (!base::is_directory(dir))
@ -375,7 +376,7 @@ void Session::markDocumentAsCorrectlyClosed(app::Doc* doc)
std::string openFn = base::join_path(dir, kOpenFilename); std::string openFn = base::join_path(dir, kOpenFilename);
if (base::is_file(openFn)) { if (base::is_file(openFn)) {
TRACE("RECO: Document was closed correctly, deleting file '%s'\n", openFn.c_str()); RECO_TRACE("RECO: Document was closed correctly, deleting file '%s'\n", openFn.c_str());
base::delete_file(openFn); base::delete_file(openFn);
} }
} }
@ -389,7 +390,7 @@ void Session::deleteDirectory(const std::string& dir)
for (auto& item : base::list_files(dir)) { for (auto& item : base::list_files(dir)) {
std::string objfn = base::join_path(dir, item); std::string objfn = base::join_path(dir, item);
if (base::is_file(objfn)) { if (base::is_file(objfn)) {
TRACE("RECO: Deleting file '%s'\n", objfn.c_str()); RECO_TRACE("RECO: Deleting file '%s'\n", objfn.c_str());
base::delete_file(objfn); base::delete_file(objfn);
} }
} }

View File

@ -13,6 +13,7 @@
#include "app/crash/doc_format.h" #include "app/crash/doc_format.h"
#include "app/crash/internals.h" #include "app/crash/internals.h"
#include "app/crash/log.h"
#include "app/doc.h" #include "app/doc.h"
#include "base/convert_to.h" #include "base/convert_to.h"
#include "base/fs.h" #include "base/fs.h"
@ -355,7 +356,7 @@ private:
// Rotate versions and add the latest one // Rotate versions and add the latest one
versions.rotateRevisions(obj->version()); versions.rotateRevisions(obj->version());
TRACE(" - Saved %s #%d v%d\n", prefix, obj->id(), obj->version()); RECO_TRACE(" - Saved %s #%d v%d\n", prefix, obj->id(), obj->version());
return true; return true;
} }
@ -365,11 +366,11 @@ private:
m_deleteFiles.erase(m_deleteFiles.end()-1); m_deleteFiles.erase(m_deleteFiles.end()-1);
try { try {
TRACE(" - Deleting <%s>\n", file.c_str()); RECO_TRACE(" - Deleting <%s>\n", file.c_str());
base::delete_file(file); base::delete_file(file);
} }
catch (const std::exception&) { catch (const std::exception&) {
TRACE(" - Cannot delete <%s>\n", file.c_str()); RECO_TRACE(" - Cannot delete <%s>\n", file.c_str());
} }
} }
} }