Asynchronous log file module implements

Asynchronous log file module implements


Before the first time in the company to do a project, did not feel any problem; but later found the time to write the log, every time synchronization in writing, especially writing papers, this is time-consuming, so wanted to optimize it;

Optimization:

  • Asynchronous read and write

    Here is the use of asynchronous queues do

    Each module will log write queue, do not care about the success or failure is written; a thread is created specifically for queue read log information, write to the log file

  • Scenario Figure

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-tRTy7K6R-1582939007578) (D: \ study concluded \ Network Communications \ asynchronous log file \ 1.png)]

The benefits of using queue:

  • Decoupling, so that each module independent, complementary Effect
  • Improve performance; each module there is no loss write files, all losses by the logging module to write files to bear;


achieve

  • This implementation is that the queues used in STL queue to bottom, ensures that the global mode using a single embodiment there is only one object, to ensure use of the same queue;
  • Use open source library log4cplus as reading and writing journal library

Note: Generally a different module is a thread, this time on the need to lock, otherwise there will be access to conflict issues;

Here wrote a simple example, for reference only:

#pragma once
#include <iostream>
#include <queue>
#include <windows.h>
#include <mutex>

using namespace std;

struct MSGLOG
{
	//0 info;1 error;
	int type;
	char info[256];
};

class QueueLog
{
public:
	QueueLog();
	static QueueLog& instance();
	~QueueLog();
	bool SetLog(string logmsg,int type);
	MSGLOG GetLog();
private:
	MSGLOG msg;
	queue<MSGLOG>que;
	std::mutex _mutex;
};

#define LOG QueueLog::instance()

#include "QueueLog.h"

QueueLog::QueueLog()
{

}

QueueLog::~QueueLog()
{

}

QueueLog& QueueLog::instance()
{
	static QueueLog quelog;
	return quelog;
}

bool QueueLog::SetLog(string logmsg, int type)
{
	std::lock_guard<mutex>lock(_mutex);
	//不做判空
	memset(&msg,0,sizeof(msg));
	msg.type = type;
	memcpy(&msg.info, logmsg.c_str(), logmsg.length());
	que.push(msg);
	return true;
}

MSGLOG QueueLog::GetLog()
{
	std::lock_guard<mutex>lock(_mutex);
	MSGLOG getMsg;
	memset(&getMsg,0,sizeof(getMsg));
	if (que.empty())
	{
		goto EXIT;
	}
	//while (que.empty())
	//{
	//	Sleep(2);
	//}
	//Sleep(3);
	getMsg = que.front();
	que.pop();
EXIT:
	return getMsg;
}
#pragma once
#include <iostream>
#include <string>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h> 
#include <log4cplus/layout.h> 
#include <log4cplus/loggingmacros.h> 
#include <log4cplus/helpers/stringhelper.h> 

#define MY_LOG_FILE_PATH "../log/logconfig.properites"

using namespace std;
using namespace log4cplus;
using namespace log4cplus::helpers;

class MyLogger
{
public:
	static MyLogger & getInstance();
	Logger logger;
private:
	MyLogger();
	~MyLogger();
};

#define LOG4CPLUS MyLogger::getInstance()
#include "logger.h"

MyLogger::MyLogger()
{
	log4cplus::initialize();
	PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(MY_LOG_FILE_PATH));
	logger = Logger::getRoot();

}


MyLogger & MyLogger::getInstance()
{
	static MyLogger log;
	return log;
}

MyLogger::~MyLogger()
{
}

#include "QueueLog.h"
#include "test.h"
#include "logger.h"
#include <thread>

using namespace std;

void SetTest2()
{
	while (1)
	{
		MSGLOG msg;
		memset(&msg, 0, sizeof(msg));
		LOG.SetLog("test", 0);
		LOG.SetLog("test2", 0);
		LOG.SetLog("test3", 0);
		LOG.SetLog("test4", 0);
		Sleep(5);
	}

}

int main()
{
	std::thread t1(SetTest2);
	MSGLOG msg;
	/*memset(&msg,0,sizeof(msg));
	LOG.SetLog("test",0);*/
	SetTest();
	while (1)
	{
		memset(&msg, 0, sizeof(msg));
		msg = LOG.GetLog();
		switch(msg.type)
		{
			case 0:
				LOG4CPLUS_DEBUG(LOG4CPLUS.logger, msg.info);
				break;
			case 1:
				LOG4CPLUS_DEBUG(LOG4CPLUS.logger, msg.info);
				break;
		}
		cout << "id:" << msg.type << endl;
		cout << "msg:" << msg.info << endl;
		//Sleep(1);
	}
	t1.join();
	system("pause");
	return 0;
}


to sum up:

This is a simple asynchronous logging implementation, project process is relatively simple, this can also meet the requirements; in fact, there is now a large number of message queues open source libraries, performance is relatively high, but to design their own, troubleshooting is a process of growing;


To learn more learn C ++ back-end server, please attention:
micro-channel public number:CPP backend server development


");
return 0;
}




**************

********

**总结:**

这就是一个简单的异步日志实现,项目流程比较简单,这个也可以符合要求;其实现在存在大量的消息队列的开源库,性能比较高,不过自己去设计、排错也是一个成长的过程;

************

想了解学习更多C++后台服务器方面的知识,请关注:
微信公众号:====**CPP后台服务器开发**====

**************



发布了198 篇原创文章 · 获赞 68 · 访问量 7万+

Guess you like

Origin blog.csdn.net/Travelerwz/article/details/104569101