G3Log的隐藏实现



我正在尝试使用G3Log(Google logger-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");
};

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

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

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

最新更新