2

我正在尝试使用 G3Log(Google 记录器的一个版本 - glog)在静态库中进行一些记录。在我尝试将该静态库引入 C++/CLI 托管包装器之前,一切都运行良好。当我这样做时,我遇到了可怕的问题:

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

问题是接收器的回调函数需要 g2::LogMessageMover,为此,我必须带回头文件。那么,我怎样才能封装 glog.hpp 标头,以免它对 C++/CLI 应用程序可见?

这是我尝试过的,但我被困在接收器的回调中。

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();
};

这是CPP文件:

#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");
};
4

1 回答 1

2

似乎是 g3log/src/shared_queue.hpp 包含互斥体的违规包含

想到几个不同的选项 1) 将队列从仅标头更改为 .hpp + .cpp 实现,其中互斥部分隐藏在 pimpl 中

2) 将队列替换为无锁队列。您可能需要一个包装器才能提供所需的 API。我还没有测试过这个,但它看起来很有希望 http://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++

于 2015-02-26T03:57:15.303 回答