[muscle] Re: Using muscle::PutLogCallback

  • From: jeremyf <jfriesne@xxxxxxxxx>
  • To: muscle@xxxxxxxxxxxxx
  • Date: Wed, 28 Apr 2010 09:24:05 -0700

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,

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:
> #include <syslog/LogCallback.h>
> #include <QObject>
> using namespace muscle;
> class MuscleLogCallback : public QObject, public LogLineCallback
> {
> public:
>     MuscleLogCallback(QObject *parent = 0);
> signals:
>     void logLine(const QString& message);
> protected:
>     virtual void LogLine(const LogCallbackArgs& a);
> };
> 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: