隐藏G3Log的实现

时间:2015-02-17 01:04:35

标签: c++11 mutex glog g2log

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

1 个答案:

答案 0 :(得分: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++