日志(20)

日志流程



TRACE
指出比DEBUG粒度更细的一些信息事件(开发过程中使用)
DEBUG
指出细粒度信息事件对调试应用程序是非常有帮助的。(开发过程中使用)
INFO
表明消息在粗粒度级别上突出强调应用程序的运行过程。
WARN
系统能正常运行,但可能会出现潜在错误的情形。
ERROR
指出虽然发生错误事件,但仍然不影响系统的继续运行。
FATAL
指出每个严重的错误事件将会导致应用程序的退出。



测试程序1

#include <muduo/base/Logging.h>
#include <errno.h>

using namespace muduo;

int main()
{

 // Logger.LogLevel = LogLevel::INFO ;
//  muduo::Logger.setLogLevel( LogLevel::INFO ) ;
    LOG_TRACE<<"trace ...";
    LOG_DEBUG<<"debug ...";
    LOG_INFO<<"info ...";
    LOG_WARN<<"warn ...";
    LOG_ERROR<<"error ...";
    //LOG_FATAL<<"fatal ...";
    errno = 13;
    LOG_SYSERR<<"syserr ...";
    LOG_SYSFATAL<<"sysfatal ...";
    return 0;
}

程序输出

[root@localhost bin]# ./log_test1
20131016 15:34:26.231014Z  3904 TRACE main trace ... - Log_test1.cc:11
20131016 15:34:26.231251Z  3904 DEBUG main debug ... - Log_test1.cc:12
20131016 15:34:26.231264Z  3904 INFO  info ... - Log_test1.cc:13
20131016 15:34:26.231274Z  3904 WARN  warn ... - Log_test1.cc:14
20131016 15:34:26.231285Z  3904 ERROR error ... - Log_test1.cc:15
20131016 15:34:26.231293Z  3904 ERROR Permission denied (errno=13) syserr ... - Log_test1.cc:18
20131016 15:34:26.231317Z  3904 FATAL Permission denied (errno=13) sysfatal ... - Log_test1.cc:19
Aborted
[root@localhost bin]#


测试程序2

#include <muduo/base/LogFile.h>
#include <muduo/base/Logging.h>

boost::scoped_ptr<muduo::LogFile> g_logFile;

void outputFunc(const char* msg, int len)
{
  g_logFile->append(msg, len);
}

void flushFunc()
{
  g_logFile->flush();
}

int main(int argc, char* argv[])
{
  char name[256];
  strncpy(name, argv[0], 256);
  g_logFile.reset(new muduo::LogFile(::basename(name), 200*1000));
  muduo::Logger::setOutput(outputFunc);
  muduo::Logger::setFlush(flushFunc);

  muduo::string line = "1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ";

  for (int i = 0; i < 10000; ++i)
  {
    LOG_INFO << line << i;

    usleep(1000);
  }
}

程序输出

[root@localhost bin]# cat /tmp/muduo_log 
20131016 15:50:46.099054Z  4358 TRACE main trace ... - Log_test2.cc:28
20131016 15:50:46.099182Z  4358 DEBUG main debug ... - Log_test2.cc:29
20131016 15:50:46.099191Z  4358 INFO  info ... - Log_test2.cc:30
20131016 15:50:46.099197Z  4358 WARN  warn ... - Log_test2.cc:31
20131016 15:50:46.099201Z  4358 ERROR error ... - Log_test2.cc:32
20131016 15:50:46.099206Z  4358 ERROR Permission denied (errno=13) syserr ... - Log_test2.cc:35
[root@localhost bin]#


测试程序2

#include <muduo/base/Logging.h>
#include <muduo/base/LogFile.h>
#include <muduo/base/ThreadPool.h>

#include <stdio.h>

int g_total;
FILE* g_file;
boost::scoped_ptr<muduo::LogFile> g_logFile;

void dummyOutput(const char* msg, int len)
{
  g_total += len;
  if (g_file)
  {
    fwrite(msg, 1, len, g_file);
  }
  else if (g_logFile)
  {
    g_logFile->append(msg, len);
  }
}

void bench(const char* type)
{
  muduo::Logger::setOutput(dummyOutput);
  muduo::Timestamp start(muduo::Timestamp::now());
  g_total = 0;

  int n = 1000*1000;
  const bool kLongLog = false;
  muduo::string empty = " ";
  muduo::string longStr(3000, 'X');
  longStr += " ";
  for (int i = 0; i < n; ++i)
  {
    LOG_INFO << "Hello 0123456789" << " abcdefghijklmnopqrstuvwxyz"
             << (kLongLog ? longStr : empty)
             << i;
  }
  muduo::Timestamp end(muduo::Timestamp::now());
  double seconds = timeDifference(end, start);
  printf("%12s: %f seconds, %d bytes, %10.2f msg/s, %.2f MiB/s\n",
         type, seconds, g_total, n / seconds, g_total / seconds / (1024 * 1024));
}

void logInThread()
{
  LOG_INFO << "logInThread";
  usleep(1000);
}

int main()
{
  getppid(); // for ltrace and strace
    //线程池 , 名字"pool"
  muduo::ThreadPool pool("pool");
    //五个线程
  pool.start(5);
  //五个任务
  pool.run(logInThread);
  pool.run(logInThread);
  pool.run(logInThread);
  pool.run(logInThread);
  pool.run(logInThread);

  LOG_TRACE << "trace";
  LOG_DEBUG << "debug";
  LOG_INFO << "Hello";
  LOG_WARN << "World";
  LOG_ERROR << "Error";
  LOG_INFO << sizeof(muduo::Logger);
  LOG_INFO << sizeof(muduo::LogStream);
  LOG_INFO << sizeof(muduo::Fmt);
  LOG_INFO << sizeof(muduo::LogStream::Buffer);

  sleep(1);
  //
  bench("nop");

  char buffer[64*1024];

  g_file = fopen("/dev/null", "w");
  setbuffer(g_file, buffer, sizeof buffer);
  bench("/dev/null");
  fclose(g_file);

  g_file = fopen("/tmp/log", "w");
  setbuffer(g_file, buffer, sizeof buffer);
  bench("/tmp/log");
  fclose(g_file);

  g_file = NULL;
  //线程安全的方式
  g_logFile.reset(new muduo::LogFile("test_log_st", 500*1000*1000, false));
  bench("test_log_st");
    //线程不安全的方式
  g_logFile.reset(new muduo::LogFile("test_log_mt", 500*1000*1000, true));
  bench("test_log_mt");
  g_logFile.reset();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值