On 18 April 2017 at 00:18, Thiago Macieira <thi...@macieira.org> wrote:
> On segunda-feira, 17 de abril de 2017 14:10:28 PDT Lubomir I. Ivanov wrote:
>> i guess we could just do a:
>> void MarbleDebug::setEnabled(bool enabled)
>> {
>>     Marble::loggingCategory.setEnabled(enabled);
>>     MarbleDebug::m_enabled = enabled;
>> }
>
> I'd go a little further and drop the m_enabled variable completely. You don't
> need it, let the bit in the QLoggingCategory variable keep the state.
>
> You'd have:
>
> namespace MarbleDebug {
> Q_LOGGING_CATEGORY(category, "marble", QtWarningMsg)
>
> void setEnabled(bool enabled)
> {
>     category.setEnabled(QtDebugMsg, enabled);
> }
>
> bool isEnabled()                // do you even need this function?
> {
>         return category.isDebugEnabled();
> }
> } //namespace MarbleDebug
>
> This changes to the MarbleDebug namespace too.
>

thanks, i think i understand.

here are the updated Marble files and a patch diff for review.
i can't build Marble to test these changes ATM though.

lubomir
--
From 79005f97063620f4f7da0d087a045f4f6da02d70 Mon Sep 17 00:00:00 2001
From: "Lubomir I. Ivanov" <neolit...@gmail.com>
Date: Tue, 18 Apr 2017 00:36:17 +0300
Subject: [PATCH] MarbleDebug: use a custom QLoggingCategory

---
 src/lib/marble/MarbleDebug.cpp | 22 ++++------------------
 src/lib/marble/MarbleDebug.h   | 12 +++++-------
 2 files changed, 9 insertions(+), 25 deletions(-)

diff --git a/src/lib/marble/MarbleDebug.cpp b/src/lib/marble/MarbleDebug.cpp
index 5f53f6f..7ee9a4f 100644
--- a/src/lib/marble/MarbleDebug.cpp
+++ b/src/lib/marble/MarbleDebug.cpp
@@ -10,35 +10,21 @@
 
 #include "MarbleDebug.h"
 
-#include <QFile>
-#include <QProcess>
+#include <QLoggingCategory>
 
 namespace Marble
 {
-// bool MarbleDebug::m_enabled = true;
-bool MarbleDebug::m_enabled = false;
 
-QDebug mDebug()
-{
-    if ( MarbleDebug::isEnabled() ) {
-        return QDebug( QtDebugMsg );
-    }
-    else {
-        static QFile *nullDevice = new QFile(QProcess::nullDevice());
-        if ( !nullDevice->isOpen() )
-             nullDevice->open(QIODevice::WriteOnly);
-         return QDebug( nullDevice );
-    }
-}
+Q_LOGGING_CATEGORY(category, "marble", QtDebugMsg)
 
 bool MarbleDebug::isEnabled()
 {
-    return MarbleDebug::m_enabled;
+    return category.isDebugEnabled();
 }
 
 void MarbleDebug::setEnabled(bool enabled)
 {
-    MarbleDebug::m_enabled = enabled;
+    category.setEnabled(QtDebugMsg, enabled);
 }
 
 } // namespace Marble
diff --git a/src/lib/marble/MarbleDebug.h b/src/lib/marble/MarbleDebug.h
index 74b71ae..03b2db4 100644
--- a/src/lib/marble/MarbleDebug.h
+++ b/src/lib/marble/MarbleDebug.h
@@ -12,12 +12,15 @@
 #define MARBLE_MARBLEDEBUG_H
 
 #include <QDebug>
+#include <QLoggingCategory>
 
 #include "marble_export.h"
 
 namespace Marble
 {
 
+Q_DECLARE_LOGGING_CATEGORY(category)
+
 /**
   * a class which takes all the settings and exposes them
   */
@@ -35,15 +38,10 @@ public:
      */
     static void setEnabled(bool enabled);
 
-private:
-    static bool m_enabled;
-
 };
 
-/**
-  * a function to replace qDebug() in Marble library code
-  */
-MARBLE_EXPORT QDebug mDebug();
+// logging category
+#define mDebug qCDebug(Marble::category)
 
 } // namespace Marble
 
-- 
1.7.11.msysgit.0

//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2009      Patrick Spendrin <ps...@gmx.de>
//

#include "MarbleDebug.h"

#include <QLoggingCategory>

namespace Marble
{

Q_LOGGING_CATEGORY(category, "marble", QtDebugMsg)

bool MarbleDebug::isEnabled()
{
    return category.isDebugEnabled();
}

void MarbleDebug::setEnabled(bool enabled)
{
    category.setEnabled(QtDebugMsg, enabled);
}

} // namespace Marble
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2009      Patrick Spendrin <ps...@gmx.de>
//

#ifndef MARBLE_MARBLEDEBUG_H
#define MARBLE_MARBLEDEBUG_H

#include <QDebug>
#include <QLoggingCategory>

#include "marble_export.h"

namespace Marble
{

Q_DECLARE_LOGGING_CATEGORY(category)

/**
  * a class which takes all the settings and exposes them
  */
class MARBLE_EXPORT MarbleDebug
{
public:
    /**
     * @brief isEnabled returns whether debug information output is generated
     */
    static bool isEnabled();

    /**
     * @brief setEnabled Toggle debug information output generation
     * @param enabled Set to true to enable debug output, false to disable
     */
    static void setEnabled(bool enabled);

};

// logging category
#define mDebug qCDebug(Marble::category)

} // namespace Marble

#endif
_______________________________________________
subsurface mailing list
subsurface@subsurface-divelog.org
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to