apache / rocketmq-client-cpp

Apache RocketMQ cpp client
https://rocketmq.apache.org/
Apache License 2.0
370 stars 160 forks source link

boost.log冲突 #458

Closed kqbi closed 1 year ago

kqbi commented 1 year ago
  1. Please describe the issue you observed:

因为要集成rocketmq,就编译了rocketmq-client-cpp的2.2.0版本的动态库librocketmq.so,这其中依赖boost-1.58.0并使用了boost.log的静态库,然后我的主程序使用了boost-1.83.0版本的boost.log的动态库,然后我的主程序一旦集成librocketmq.so就会在librocketmq库中调用boost.log的时候崩溃,具体崩溃在boost::log::v2s_mt_posix::sinks::text_file_backend::scan_for_files函数中, 初步怀疑是boost.log 中有些全局调用冲突了,这个怎么解决?

  1. Please tell us about your environment:

    • What is your OS?

centos7.9

2.2.0

ifplusor commented 1 year ago

You can modify CMakeLists.txt and link shared libraries of boost.

kqbi commented 1 year ago

嗯,解决了统一使用最新的boost-1.83,并且修改CMakeLists.txt使用boost动态库

kqbi commented 1 year ago

还是有问题,目前是能运行,但是主程序的日志先初始化然后输出日志,一旦执行rocketmq的函数主程序的日志就不输出了!!!

kqbi commented 1 year ago

解决了,其实原因就是日志级别设置要固定具体的sink上不然会影响到其他的sink, Logging.cpp修改如下:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "Logging.h"
#include <boost/date_time/gregorian/gregorian.hpp>
#include "UtilAll.h"
#define BOOST_DATE_TIME_SOURCE

namespace rocketmq {
logAdapter* logAdapter::alogInstance;
boost::mutex logAdapter::m_imtx;

logAdapter::~logAdapter() {
  boost::log::core::get()->remove_sink(m_logSink);
  m_logSink->flush();
}

logAdapter* logAdapter::getLogInstance() {
  if (alogInstance == NULL) {
    boost::mutex::scoped_lock guard(m_imtx);
    if (alogInstance == NULL) {
      alogInstance = new logAdapter();
    }
  }
  return alogInstance;
}

logAdapter::logAdapter() : m_logLevel(eLOG_LEVEL_INFO) {
  setLogDir();
  string homeDir;
  homeDir.append(m_log_dir);
  m_logFile += homeDir;
  std::string fileName = "rocketmq_client.log";
  m_logFile += fileName;

  // boost::log::expressions::attr<
  // boost::log::attributes::current_thread_id::value_type>("ThreadID");
  boost::log::register_simple_formatter_factory<boost::log::trivial::severity_level, char>("Severity");
  m_logSink = logging::add_file_log(keywords::file_name = m_logFile,
                                    keywords::target_file_name = "rocketmq_client_%Y%m%d-%N.log",
                                    keywords::rotation_size = 100 * 1024 * 1024,
                                    keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
                                    keywords::format = "[%TimeStamp%](%Severity%):%Message%",
                                    keywords::min_free_space = 300 * 1024 * 1024, keywords::target = homeDir,
                                    keywords::max_size = 200 * 1024 * 1024,  // max keep 3 log file defaultly
                                    keywords::auto_flush = true);
  // logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
  setLogLevelInner(m_logLevel);

  logging::add_common_attributes();
}

void logAdapter::setLogLevelInner(elogLevel logLevel) {
  switch (logLevel) {
    case eLOG_LEVEL_FATAL:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::fatal);
      break;
    case eLOG_LEVEL_ERROR:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::error);

      break;
    case eLOG_LEVEL_WARN:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::warning);

      break;
    case eLOG_LEVEL_INFO:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::info);

      break;
    case eLOG_LEVEL_DEBUG:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::debug);

      break;
    case eLOG_LEVEL_TRACE:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::trace);

      break;
    default:
      m_logSink->set_filter(logging::trivial::severity >= logging::trivial::info);

      break;
  }
}
void logAdapter::setLogLevel(elogLevel logLevel) {
  m_logLevel = logLevel;
  setLogLevelInner(logLevel);
}

elogLevel logAdapter::getLogLevel() {
  return m_logLevel;
}

void logAdapter::setLogDir() {
  char* p = nullptr;
  if ((p = getenv(ROCKETMQ_CLIENT_LOG_DIR.c_str()))) {
    m_log_dir = p;
  }
  if (!m_log_dir.empty()) {
    if (m_log_dir[m_log_dir.length() - 1] != '/') {
      m_log_dir += '/';
    }
  } else {
    m_log_dir = "/logs/rocketmq-client/";
  }
}

void logAdapter::setLogFileNumAndSize(int logNum, int sizeOfPerFile) {
  string homeDir;
  homeDir.append(m_log_dir);
  m_logSink->locked_backend()->set_file_collector(sinks::file::make_collector(
      keywords::target = homeDir, keywords::max_size = logNum * sizeOfPerFile * 1024 * 1024));
}
}  // namespace rocketmq
ifplusor commented 1 year ago

@kqbi Cloud you make a pull request to enhance this project?

kqbi commented 1 year ago

估计要过几天我先整理下

chons-xu commented 2 months ago

@kqbi 链接boost动态库,cmakefile.txt该怎么修改