2

I am trying to use G3Log (a version of the Google logger - glog) to do some logging within a static library. All works very well until I try to bring that static library into a C++/CLI managed wrapper. When I do this, I get the dreaded issue:

error C1189: #error : <mutex> is not supported when compiling with /clr or /clr:pure.

The problem, is that the callback functions for the sink require g2::LogMessageMover, and to do that, I have to bring back in the header file. So, how can I encapsulate the glog.hpp header so as not to make it visible to the C++/CLI application?

Here is what I tried, but I am stuck on the callback for the sink.

class Log {
private:
    void *log_;
    void *trace_;
    void *logworker;

public:

    std::string LogPath = "";
    std::string LogFile = "Log.txt";
    std::string TraceFile = "Trace.txt";

    Log();

    void Initialize(std::string log_path, std::string log_file, std::string trace_file);

    // How to define this and hide the implementation??
    void LogMessage( g2::LogMessageMover message );

    // How to define this and hide the implementation??
    void TraceMessage( g2::LogMessageMover message);

    virtual ~Log();
};

Here is the CPP file:

#include "include/g2logworker.hpp"
#include "include/g2log.hpp"

Log::Log() {
    logworker = (void *)(g2::LogWorker::createWithNoSink().get());
};

void Log::Initialize(std::string log_path, std::string log_file, std::string trace_file) {
    auto worker = static_cast<g2::LogWorker *>(logworker);
    auto loghandle = worker->addSink(std::make_unique<Log>(), &Log::LogMessage);
    log_ = (void *) loghandle.get();

    auto tracehandle = worker->addSink(std::make_unique<Log>(), &Log::TraceMessage);
    trace_ = (void *) tracehandle.get();

    g2::initializeLogging(worker);
};

void Log::LogMessage( g2::LogMessageMover message) {
    fprintf(stderr, "Got the log message");
};

void Log::TraceMessage( g2::LogMessageMover message) {
    fprintf(stderr, "Got the trace message");
};
user3072517
  • 513
  • 1
  • 7
  • 21

1 Answers1

2

It seems that it's the g3log/src/shared_queue.hpp that contains the offending include for a mutex

A couple of different options come to mind 1) change the queue from a header only to a .hpp + .cpp implementation where the mutex part is hidden in a pimpl

2) replace the queue for a lock free queue. You probably need to have a wrapper to be able to provide the needed API. I haven't tested this one but it looks promising http://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++

Kjell Hedström
  • 470
  • 5
  • 11
  • 1
    The moody camel lock free queue works great with g3log. In addition you also get a significant speed increase for the average case. – Kjell Hedström Jun 13 '15 at 21:21