Skip to content

Commit

Permalink
Rewrite the category logging
Browse files Browse the repository at this point in the history
- two categories 1. canvas painter 2. file manager
- can turn the log of one category on/off by changing the log rules in log.cpp
  • Loading branch information
chchwy committed Jul 3, 2020
1 parent 5991a54 commit b11aa67
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 47 deletions.
3 changes: 3 additions & 0 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GNU General Public License for more details.
#include "layermanager.h"
#include "layercamera.h"
#include "platformhandler.h"
#include "log.h"

void installTranslator(PencilApplication& app)
{
Expand Down Expand Up @@ -332,6 +333,8 @@ int main(int argc, char* argv[])
PencilApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
}

initCategoryLogging();

PencilApplication app(argc, argv);

installTranslator(app);
Expand Down
1 change: 1 addition & 0 deletions core_lib/core_lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ SOURCES += src/graphics/bitmap/bitmapimage.cpp \
src/util/fileformat.cpp \
src/util/pencilerror.cpp \
src/util/pencilsettings.cpp \
src/util/log.cpp \
src/util/util.cpp \
src/canvaspainter.cpp \
src/soundplayer.cpp \
Expand Down
39 changes: 20 additions & 19 deletions core_lib/src/canvaspainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ GNU General Public License for more details.
#include "util.h"



CanvasPainter::CanvasPainter()
: mLog("CanvasPainter")
{
ENABLE_DEBUG_LOG(mLog, false);
}

CanvasPainter::~CanvasPainter()
Expand Down Expand Up @@ -186,6 +183,7 @@ void CanvasPainter::setPaintSettings(const Object* object, int currentLayer, int
Q_ASSERT(object);
mObject = object;

CANVASPAINTER_LOG("Set CurrentLayerIndex = %d", currentLayer);
mCurrentLayerIndex = currentLayer;
mFrameNumber = frame;
mBuffer = buffer;
Expand Down Expand Up @@ -289,20 +287,17 @@ void CanvasPainter::paintBitmapFrame(QPainter& painter,
{
#ifdef _DEBUG
LayerBitmap* bitmapLayer = dynamic_cast<LayerBitmap*>(layer);
if (bitmapLayer == nullptr)
{
Q_ASSERT(bitmapLayer);
return;
}
Q_ASSERT(bitmapLayer);
#else
LayerBitmap* bitmapLayer = static_cast<LayerBitmap*>(layer);
#endif

//qCDebug(mLog) << "Paint Onion skin bitmap, Frame = " << nFrame;
CANVASPAINTER_LOG(" Paint Bitmap Frame = %d, UseLastKeyFrame = %d", nFrame, useLastKeyFrame);
BitmapImage* paintedImage = nullptr;
if (useLastKeyFrame)
{
paintedImage = bitmapLayer->getLastBitmapImageAtFrame(nFrame, 0);
CANVASPAINTER_LOG(" Actual frame = %d", paintedImage->pos());
}
else
{
Expand All @@ -317,11 +312,11 @@ void CanvasPainter::paintBitmapFrame(QPainter& painter,

if (paintedImage == nullptr)
{
paintedImage = new BitmapImage();
paintedImage = new BitmapImage;
}

paintedImage->loadFile(); // Critical! force the BitmapImage to load the image
//qCDebug(mLog) << "Paint Image Size:" << paintedImage->image()->size();
CANVASPAINTER_LOG(" Paint Image Size: %dx%d", paintedImage->image()->width(), paintedImage->image()->height());

BitmapImage paintToImage;
paintToImage.paste(paintedImage);
Expand Down Expand Up @@ -362,6 +357,13 @@ void CanvasPainter::paintBitmapFrame(QPainter& painter,

prescale(&paintToImage);
paintToImage.paintImage(painter, mScaledBitmap, mScaledBitmap.rect(), paintToImage.bounds());

// static int cc = 0;
// QString path = QString("C:/Temp/pencil2d/canvas-%1-%2-%3.png")
// .arg(cc++, 3, 10, QChar('0'))
// .arg(layer->name())
// .arg(mFrameNumber);
// Q_ASSERT(mCanvas->save(path));

This comment has been minimized.

Copy link
@J5lx

J5lx Jul 19, 2020

Member

Is keeping this dead code intentional?

}

void CanvasPainter::prescale(BitmapImage* bitmapImage)
Expand Down Expand Up @@ -395,16 +397,12 @@ void CanvasPainter::paintVectorFrame(QPainter& painter,
{
#ifdef _DEBUG
LayerVector* vectorLayer = dynamic_cast<LayerVector*>(layer);
if (vectorLayer == nullptr)
{
Q_ASSERT(vectorLayer);
return;
}
Q_ASSERT(vectorLayer);
#else
LayerVector* vectorLayer = static_cast<LayerVector*>(layer);
#endif

qCDebug(mLog) << "Paint Onion skin vector, Frame = " << nFrame;
CANVASPAINTER_LOG("Paint Onion skin vector, Frame = %d", nFrame);
VectorImage* vectorImage = nullptr;
if (useLastKeyFrame)
{
Expand Down Expand Up @@ -494,10 +492,12 @@ void CanvasPainter::paintCurrentFrame(QPainter& painter, int startLayer, int end
if (layer->visible() == false)
continue;

if (mOptions.eLayerVisibility == LayerVisibility::RELATED && !isCameraLayer) {
if (mOptions.eLayerVisibility == LayerVisibility::RELATED && !isCameraLayer)
{
painter.setOpacity(calculateRelativeOpacityForLayer(i));
}

CANVASPAINTER_LOG(" Render Layer[%d] %s", i, layer->name());
switch (layer->type())
{
case Layer::BITMAP: { paintBitmapFrame(painter, layer, mFrameNumber, false, true, i == mCurrentLayerIndex); break; }
Expand All @@ -512,7 +512,8 @@ qreal CanvasPainter::calculateRelativeOpacityForLayer(int layerIndex) const
int layerOffset = mCurrentLayerIndex - layerIndex;
int absoluteOffset = qAbs(layerOffset);
qreal newOpacity = 1.0;
if (absoluteOffset != 0) {
if (absoluteOffset != 0)
{
newOpacity = qPow(static_cast<qreal>(mOptions.fLayerVisibilityThreshold), absoluteOffset);
}
return newOpacity;
Expand Down
4 changes: 2 additions & 2 deletions core_lib/src/canvaspainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class CanvasPainter

/** Calculate layer opacity based on current layer offset */
qreal calculateRelativeOpacityForLayer(int layerIndex) const;

void savePaintResult(QString layerName, int frameNum);
private:
CanvasPainterOptions mOptions;

Expand All @@ -146,8 +148,6 @@ class CanvasPainter
QRect mSelection;
QTransform mSelectionTransform;

QLoggingCategory mLog;

// Caches specifically for when drawing on the canvas
std::unique_ptr<QPixmap> mPreLayersCache, mPostLayersCache;

Expand Down
24 changes: 11 additions & 13 deletions core_lib/src/structure/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ namespace
"</ul>";
}

FileManager::FileManager(QObject* parent) : QObject(parent),
mLog("FileManager")
FileManager::FileManager(QObject* parent) : QObject(parent)
{
ENABLE_DEBUG_LOG(mLog, false);
srand(static_cast<uint>(time(nullptr)));
}

Expand All @@ -53,7 +51,7 @@ Object* FileManager::load(QString sFileName)
dd << QString("File name: ").append(sFileName);
if (!QFile::exists(sFileName))
{
qCDebug(mLog) << "ERROR - File doesn't exist.";
DEBUG_LOG_FILEMANAGER("ERROR - File doesn't exist");
return cleanUpWithErrorCode(Status(Status::FILE_NOT_FOUND, dd, tr("Could not open file"),
tr("The file does not exist, so we are unable to open it. Please check "
"to make sure the path is correct and that the file is accessible and try again.")));
Expand Down Expand Up @@ -116,14 +114,15 @@ Object* FileManager::load(QString sFileName)
QDomDocument xmlDoc;
if (!xmlDoc.setContent(&file))
{
qCDebug(mLog) << "Couldn't open the main XML file.";
DEBUG_LOG_FILEMANAGER("Couldn't open the main XML file");
dd << "Error parsing or opening the main XML file";
return cleanUpWithErrorCode(Status(Status::ERROR_INVALID_XML_FILE, dd, openErrorTitle, openErrorDesc + contactLinks));
}

QDomDocumentType type = xmlDoc.doctype();
if (!(type.name() == "PencilDocument" || type.name() == "MyObject"))
{
DEBUG_LOG_FILEMANAGER("Invalid main XML doctype");
dd << QString("Invalid main XML doctype: ").append(type.name());
return cleanUpWithErrorCode(Status(Status::ERROR_INVALID_PENCIL_FILE, dd, openErrorTitle, openErrorDesc + contactLinks));
}
Expand Down Expand Up @@ -178,8 +177,7 @@ bool FileManager::loadObject(Object* object, const QDomElement& root)
if (element.tagName() == "object")
{
ok = object->loadXML(element, [this]{ progressForward(); });

if (!ok) qCDebug(mLog) << "Failed to Load object";
if (!ok) FILEMANAGER_LOG("Failed to Load object");

}
else if (element.tagName() == "editor" || element.tagName() == "projectdata")
Expand Down Expand Up @@ -311,7 +309,7 @@ Status FileManager::save(Object* object, QString sFileName)
Layer* layer = object->getLayer(i);

dd << QString("Layer[%1] = [id=%2, name=%3, type=%4]").arg(i).arg(layer->id()).arg(layer->name()).arg(layer->type());

Status st = layer->save(sDataFolder, zippedFiles, [this] { progressForward(); });
if (!st.ok())
{
Expand All @@ -328,7 +326,7 @@ Status FileManager::save(Object* object, QString sFileName)
zippedFiles.append(sPaletteFile);
else
dd << "Failed to save the palette xml";

progressForward();

// -------- save main XML file -----------
Expand Down Expand Up @@ -379,7 +377,7 @@ Status FileManager::save(Object* object, QString sFileName)
if (!s.ok())
{
dd.collect(s.details());
return Status(Status::ERROR_MINIZ_FAIL, dd,
return Status(Status::ERROR_MINIZ_FAIL, dd,
tr("Miniz Error"),
tr("An internal error occurred. Your file may not be saved successfully."));
}
Expand Down Expand Up @@ -559,7 +557,7 @@ QString FileManager::backupPreviousFile(const QString& fileName)
bool ok = QFile::rename(info.absoluteFilePath(), sBackupFileFullPath);
if (!ok)
{
qDebug() << "Cannot backup the previous file.";
FILEMANAGER_LOG("Cannot backup the previous file");
return "";
}
return sBackupFileFullPath;
Expand All @@ -581,7 +579,7 @@ void FileManager::progressForward()

bool FileManager::loadPalette(Object* obj)
{
qCDebug(mLog) << "Load Palette..";
DEBUG_LOG_FILEMANAGER("Load Palette..");

QString paletteFilePath = QDir(obj->dataDir()).filePath(PFF_PALETTE_FILE);
if (!obj->importPalette(paletteFilePath))
Expand Down Expand Up @@ -623,7 +621,7 @@ Status FileManager::verifyObject(Object* obj)
{
obj->data()->setCurrentLayer(maxLayer - 1);
}

// Must have at least 1 camera layer
std::vector<LayerCamera*> camLayers = obj->getLayersByType<LayerCamera>();
if (camLayers.empty())
Expand Down
2 changes: 0 additions & 2 deletions core_lib/src/structure/filemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class FileManager : public QObject

int mCurrentProgress = 0;
int mMaxProgressValue = 100;

QLoggingCategory mLog;
};

#endif // OBJECTSAVELOADER_H
16 changes: 16 additions & 0 deletions core_lib/src/util/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "log.h"
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(logCanvasPainter, "core.canvasPainter");
Q_LOGGING_CATEGORY(logFileManager, "core.FileManager");

void initCategoryLogging()
{
const QString logRules =
"*.debug=false\n"
"core.canvasPainter.debug=false"
"core.fileManager.debug=false";

QLoggingCategory::setFilterRules(logRules);
}
27 changes: 16 additions & 11 deletions core_lib/src/util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ GNU General Public License for more details.
*/

#ifndef LOG_H
#define LOG_H
#pragma once
#include<QLoggingCategory>

#if QT_VERSION >= QT_VERSION_CHECK( 5, 2, 0 )
#include<QLoggingCategory>
#define ENABLE_DEBUG_LOG( Logger, OnOff ) Logger.setEnabled( QtDebugMsg, OnOff )
//#define DEBUG_LOG_CANVASPAINTER
#define DEBUG_LOG_FILEMANAGER

#ifdef DEBUG_LOG_CANVASPAINTER
Q_DECLARE_LOGGING_CATEGORY(logCanvasPainter);
#define CANVASPAINTER_LOG(...) qCDebug(logCanvasPainter, __VA_ARGS__)
#else
#define CANVASPAINTER_LOG(...) (0)
#endif

#if QT_VERSION < QT_VERSION_CHECK( 5, 2, 0 )
#define QLoggingCategory QString
#define qCDebug( C ) qDebug()
#define qCWarning( C ) qWarning()
#define ENABLE_DEBUG_LOG( Logger, OnOff )
#ifdef DEBUG_LOG_FILEMANAGER
Q_DECLARE_LOGGING_CATEGORY(logFileManager);
#define FILEMANAGER_LOG(...) qCDebug(logFileManager, __VA_ARGS__)
#else
#define FILEMANAGER_LOG(...) (0)
#endif

#endif // LOG_H
void initCategoryLogging();

0 comments on commit b11aa67

Please sign in to comment.