spdlog_sample.cpp 4.19 KB
#include "spdlog/spdlogwarp.hh"
#include <thread>
#include <iostream>
#include "spdlog/fmt/fmt.h"
#include "spdlog/fmt/bin_to_hex.h"

#define SLP_MS 1000
void thd()
{
  // trace日志会缓存在内存
  while (1) {
    auto tr = SLog::get_trace("trace1");
    tr->trace("1");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->trace("2");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->trace("3");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->trace("4");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->trace("5");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->trace("6");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->trace("7");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
  }
}

void thd2()
{
  while (1) {
    auto tr = SLog::get("error_info");
    tr->error("th2 1");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->error("th2 2");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->info("th2 1");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    tr->info("th2 2");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
  }
}

void thd3()
{
  while (1) {
    SLog::get("debuglog")->debug("th3 1");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    SLog::get("debuglog")->debug("th3 2");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    SLog::get("debuglog")->debug("th3 3");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
  }
}

void thd4()
{
  while (1) {
    SLog::global()->info("th4 1");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    SLog::global()->debug("th4 2");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
    SLog::global()->error("th4 3");
    std::this_thread::sleep_for(std::chrono::milliseconds(SLP_MS));
  }
}

int main()
{
  //初始化前确认是否已经初始化过了。避免主程序已经初始化过了。子模块建议使用专属日志
  if (!SLog::has_inited()) {
    SLog::init("./", "sample");
  }
  // 公共日志
  SLog::global()->info("I'm global {} {}", 1, "string");

  // 此函数是为了方便替换原有代码中的printf。会输出日志到[appname].log中
  cslog("I'm global log too %d %s", 1, "string");

  // 专属日志,输出到subject.log
  SLog::get("subject")->error("I'm {}", "subject");
  auto sublog = SLog::get("subject");
  sublog->error("I'm {}", "subject");
  // 与sublog->info相比,写出的日志带行号文件名
  SPDLOG_LOGGER_INFO(sublog, "with line filename");
  SPDLOG_LOGGER_DEBUG(sublog, "with line filename");


  // 专属日志,输出到./2222/subject.log
  SLog::get("./2222/subject")->error("I'm {}", "2222");


  // 额外赠送功能,string的format,fmtlib库
  std::string fmtstr;
  fmt::format(fmtstr, "format {} {} {}", 2, 2.9, "string");
  std::cout << fmtstr;


  // 打印二进制数据方法
  unsigned char buffer[1024];
  sublog->info("binary: {}", spdlog::to_hex(std::begin(buffer), std::begin(buffer)+1024));

  //反初始化
  SLog::cleanup();

  //创建多个线程同时写日志,SLP_MS改小增加日志量,测试性能
  auto th = std::thread(thd);
  th.detach();
  auto th2 = std::thread(thd2);
  th2.detach();
  auto th3 = std::thread(thd3);
  th3.detach();
  auto th4 = std::thread(thd4);
  th4.detach();

  SLog::global()->info("输入dump回车,将trace日志打印到文件中,输入debug、info、error动态更改日志等级");
  while (1) {
    std::string input;
    std::cin >> input;
    if (input == "dump") {
      SLog::dump_trace();
    } else if(input == "info") {
      //动态变更日志等级至info
      SLog::change_level(spdlog::level::info);
      SLog::global()->critical("input:info");
    } else if(input == "debug") {
      //动态变更日志等级至debug
      SLog::change_level(spdlog::level::debug);
      SLog::global()->critical("input:debug");
    } else if(input == "error") {
      //动态变更日志等级至error
      SLog::change_level(spdlog::level::err);
      SLog::global()->critical("input:error");
    }
  }
}