最近工作需要,原有的 native 模块需要编译支持 OHOS(BTW, OHOS 真的很容易拼错)。因此,我为 OHOS 增加了一个 console 的 sink,与 Android 基本一致。废话不多说,直接上代码吧。直接复制 android_sink.h
即可,里面的 level 转换和 output 接口改一下就能完美运行。当然,其中的 domain 和失败返回重试还有待商榷。
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/details/fmt_helper.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/os.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/sinks/base_sink.h>
#include <chrono>
// $OHOS_HOME/sdk/default/openharmony/native/sysroot/usr/include/hilog/log.h
#include <hilog/log.h>
#include <mutex>
#include <string>
#include <thread>
#if !defined(SPDLOG_OHOS_RETRIES)
#define SPDLOG_OHOS_RETRIES 2
#endif
namespace spdlog {
namespace sinks {
/*
* OHOS sink (logging using OH_LOG_Print)
*/
template<typename Mutex