muduo icon indicating copy to clipboard operation
muduo copied to clipboard

Fix bug about AsyncLogging

Open ligewei opened this issue 8 years ago • 4 comments

Use the following codes, write nothing to log file, in fact it should write one log message to log file. Because AsyncLogging::stop() set running_ as false before while (running_) in AsyncLogging::threadFunc().

#include <muduo/base/AsyncLogging.h>
#include <muduo/base/Logging.h>
#include <muduo/base/Timestamp.h>

#include <stdio.h>
#include <sys/resource.h>
#include <unistd.h>

int kRollSize = 500*1000*1000;

muduo::AsyncLogging* g_asyncLog = NULL;

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

void bench(bool longLog)
{
  muduo::Logger::setOutput(asyncOutput);

  int cnt = 0;
  const int kBatch = 1;
  muduo::string empty = " ";
  muduo::string longStr(3000, 'X');
  longStr += " ";

  for (int t = 0; t < 1; ++t)
  {
    //muduo::Timestamp start = muduo::Timestamp::now();
    for (int i = 0; i < kBatch; ++i)
    {
      LOG_INFO << "Hello 0123456789" << " abcdefghijklmnopqrstuvwxyz "
               << (longLog ? longStr : empty)
               << cnt;
      ++cnt;
    }
    //muduo::Timestamp end = muduo::Timestamp::now();
    //printf("%f\n", timeDifference(end, start)*1000000/kBatch);
    //struct timespec ts = { 0, 500*1000*1000 };
    //nanosleep(&ts, NULL);
  }
}

int main(int argc, char* argv[])
{
  {
    // set max virtual memory to 2GB.
    size_t kOneGB = 1000*1024*1024;
    rlimit rl = { 2*kOneGB, 2*kOneGB };
    setrlimit(RLIMIT_AS, &rl);
  }

  printf("pid = %d\n", getpid());

  char name[256];
  strncpy(name, argv[0], 256);
  muduo::AsyncLogging log(::basename(name), kRollSize);
  log.start();
  g_asyncLog = &log;

  bool longLog = argc > 1;
  bench(longLog);
}

ligewei avatar Dec 21 '17 03:12 ligewei

Please review this patch.

ligewei avatar Dec 24 '17 06:12 ligewei

  1. accessing shared members like currentBuffer_ and buffers_ should be guarded by mutex_.
  2. according to AsyncLogging::append(), you should output buffers_ before currentBuffer_.
  3. check #149 to see use do {} while(running_) is a simpler fix?

chenshuo avatar Dec 26 '17 19:12 chenshuo

  1. accessing shared members like currentBuffer_ and buffers_ should be guarded by mutex_. Agreed
  2. according to AsyncLogging::append(), you should output buffers_ before currentBuffer_. Agreed
  3. check #149 to see use do {} while(running_) is a simpler fix? Code is simpler,I also think it is OK in most cases, already create a new commit(0de71d146b3e55b46e1fadf277ec46b63de70edc), but the process using AsyncLogging will be delayed flushInterval_ seconds to exit.

ligewei avatar Dec 27 '17 03:12 ligewei

One way to address this:

the process using AsyncLogging will be delayed flushInterval_ seconds to exit.

In AsyncLogging::stop(), push currentBuffer_ to buffers_ before calling notify().

chenshuo avatar Jan 18 '18 22:01 chenshuo