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


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

void thd2()
{
  while (1) {
    auto tr = SLog::get("log2");
    tr->error("th2 1");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    tr->error("th2 2");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    tr->info("th2 3");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    tr->info("th2 4");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    tr->info("th2 5");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    tr->info("th2 6");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    tr->info("th2 7");
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
  }
}

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

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

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

  // 此函数是为了方便替换原有代码中的printf
  cslog("I'm global log too %d %s", 1, "string");

  // 专属日志
  SLog::get("subject")->error("I'm {}", "subject");
  auto sublog = SLog::get("subject");
  sublog->error("I'm {}", "subject");


  SLog::get("./2222/subject")->error("I'm {}", "2222");

  // 带行号文件名
  SPDLOG_LOGGER_INFO(sublog, "with line filename");

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


  unsigned char buffer[1024];
  //memset(buffer, 1024, 0);

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

  SLog::cleanup();

  SLog::get("subject")->error("after drop 1");
  SLog::global()->info("after drop global");

  //return 0;

  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日志打印到文件中");
  while (1) {
    std::string input;
    std::cin >> input;
    if (input == "dump") {
        SLog::dump_trace();
    }
  }
}