[muscle] Re: Using muscle::PutLogCallback

  • From: Raymond Dahlberg <rd@xxxxxxxxxxx>
  • To: muscle@xxxxxxxxxxxxx
  • Date: Thu, 29 Apr 2010 19:40:27 +0200

Thank you, that helps a lot :-)

I see there are potential issues with reference counting done by both Qt and
Muscle, so I have to think through the design. But now I know how to use it
either way.

/Raymond

On Wed, Apr 28, 2010 at 6:24 PM, jeremyf <jfriesne@xxxxxxxxx> wrote:

> Hi Raymond,
>
> There are two ways to do it.  The first is the quick-and-dirty way:
>
> muscle::PutLogCallback(LogCallbackRef(&_muscleLogger, false));    // Note
> the 'false' argument, it disables reference counting on _muscleLogger!
>
> That will work the same as a regular pointer, in that it won't give you the
> benefits of reference counting.  In particular, you'll need to be very
> careful to call muscle::RemoveLogCallback(LogCallbackRef(&_muscleLogger,
> false)) before your MuscleLogCallback object is destroyed, otherwise the
> logging code will potentially be left with a dangling pointer
> (&_muscleLogger) and will likely crash the next time LogTime() is called.
>
> The 'proper'/safer way to do it is to allocate your MuscleLogCallback
> object on the heap, so that reference counting can be used:
>
> muscle::PutLogCallback(LogCallbackRef(new MuscleLogCallback));
>
> In this case Muscle's reference-counting system will be used, and so the
> MuscleLogCallback object will be automatically deleted when the last
> LogCallbackRef object that references it goes away.  This is safer because
> there is no possibility of a dangling pointer situation.  (Note that if you
> do it this way, you'll need to make sure to pass in 0 as the "parent"
> argument to MuscleLogCallback, otherwise Qt might try to delete the
> MuscleLogCallback, which would be bad since Muscle might also delete it)
>
> Hope that helps,
> Jeremy
>
> On Apr 28, 2010, at 5:54 AM, Raymond Dahlberg wrote:
>
> > Hi.
> >
> > I want to use the muscle::PutLogCallback method to add a custom log
> handler that is integrated with Qt, so that I can post log messages in a Qt
> GUI.
> >
> > For this I have created a class that inherits from LogLineCallback and
> QObject so that it can emit a Qt signal when a new logLine is ready, class
> declaration like this:
> >
> > #ifndef MUSCLELOGCALLBACK_H
> > #define MUSCLELOGCALLBACK_H
> >
> > #include <syslog/LogCallback.h>
> > #include <QObject>
> >
> > using namespace muscle;
> >
> > class MuscleLogCallback : public QObject, public LogLineCallback
> > {
> > Q_OBJECT
> > public:
> >     MuscleLogCallback(QObject *parent = 0);
> >
> > signals:
> >     void logLine(const QString& message);
> >
> > protected:
> >     virtual void LogLine(const LogCallbackArgs& a);
> >
> > };
> >
> > #endif // MUSCLELOGCALLBACK_H
> >
> > I have an instance of this class in my main GUI class, where I try to add
> it to the logging system with PutLogCallback, - but this method need a const
> LogCallbackRef& as input parameter. But I have the log class as an member
> declared like this:
> >
> > private:
> >     Ui::TestServer *ui;
> >     MuscleLogCallback _muscleLogger;
> >
> > And in the constructor of the GUI class I try to do:
> >
> >     muscle::PutLogCallback(_muscleLogger);
> >
> > But this does not compile: error C2664: 'muscle::PutLogCallback' : cannot
> convert parameter 1 from 'MuscleLogCallback' to 'const
> muscle::LogCallbackRef &'
> >
> > So, how can I get a LogCallbackRef from a class that inherits from
> LogLineCallback?`
> >
> > /Raymond
>
>
>

Other related posts: