glog多線程下按照指定條件創建新的日誌文件

#define NOGDI 
#include <iostream>  
#include <vector>  
#include <windows.h>
#include "glog/logging.h"
#pragma comment(lib,"libglog.lib")  

using namespace std;  
using namespace google;

DWORD WINAPI TH(PVOID lp);
void init()
{
	google::InitGoogleLogging("a");
	google::SetLogDestination(google::INFO,"E://Log//Log_");  
}

CRITICAL_SECTION cs;
int main(int argc, char* argv[])
{
	InitializeCriticalSection(&cs);
	init();
	CreateThread(NULL, 0, TH, NULL, 0, NULL);
	int i=0;
	while(true)
	{
		EnterCriticalSection(&cs);
			LOG(INFO) << "main" << i++ <<endl;
		LeaveCriticalSection(&cs);
		Sleep(1000);
	}

	system("pause");
	DeleteCriticalSection(&cs);
	return 0;
}
DWORD WINAPI TH(PVOID lp)
{
	int i = 0;
	while(true)
	{
		EnterCriticalSection(&cs);
		LOG(INFO) << "Thread" << i++ <<endl;
		cout <<i<<endl;
		if (i % 2 == 0) //Test only, you can change the condition to what you want
		{
			google::ShutdownGoogleLogging();
			init();
		}
		LeaveCriticalSection(&cs);
		Sleep(1000);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章