void Plat_SetSimpleLogName( const char * szLogName ) { Plat_GetExePath( g_LogName, 256 ); //DeleteFileByTime( g_LogName ,1); strcat( g_LogName, "/Log/" ); // strcat( g_LogName, szLogName ); /* if (!Plat_CreatePath( g_LogName )){ printf("failed to create log file :%s\n", g_LogName); return; } */ struct tm *newtime; time_t aclock; time( &aclock ); // Get time in seconds newtime = localtime( &aclock ); // Convert time to struct tm form char strSubLogName[256] = { }; sprintf( strSubLogName ,"[LOG_%d_%d_%d]/" , newtime->tm_hour,newtime->tm_min ,newtime->tm_sec ); strcat( g_LogName , strSubLogName ); strcat( g_LogName, szLogName ); if (!Plat_CreatePath( g_LogName )){ //return; } char fileTime[64] = {}; sprintf( fileTime ,"[%d_%d_%d].log",newtime->tm_hour,newtime->tm_min ,newtime->tm_sec); strcat( g_LogName , fileTime ); g_LogFile = fopen(g_LogName, "a+"); if (!g_LogFile){ printf("failed to fdopen this log file:%s\n", szLogName); exit(0); return; } #ifdef THREAD_LOG g_pLogUnitHead = new CHAINHEADER(); g_bLogRun = TRUE;// by sun 2009.2.7 pthread_mutex_init( &g_LogInfoMtx, NULL ); pthread_mutex_init( &g_LogMtx, NULL ); pthread_cond_init( &g_LogCondVar, NULL ); pthread_create( &g_hLogThread, NULL, &LogThreadFunc, NULL); #endif }
void ReCordLogCount() { //SYSTEMTIME SystemTime; //GetSystemTime(&SystemTime); // fixed文件夹创建日期错误 time_t aclock; time( &aclock ); struct tm *newtime = localtime( &aclock ); newtime->tm_year+=1900; newtime->tm_mon+=1; //如果变化日期则重新创建目录 if( newtime->tm_mday != g_InitDay ) { memset(g_LogName, 0 ,sizeof(g_LogName)); memset(g_LogFileName,0 ,sizeof(g_LogFileName)); Plat_GetExePath( g_LogName, 256 ); #ifdef WIN32 strcat( g_LogName, "Log\\" ); #else strcat( g_LogName, "Log/" ); #endif char strSubLogName[256] = { }; #ifdef WIN32 sprintf( strSubLogName ,"[LOG_%d_%d_%d]\\" ,newtime->tm_year,newtime->tm_mon, newtime->tm_mday); #else sprintf( strSubLogName ,"[LOG_%d_%d_%d]/" , newtime->tm_year,newtime->tm_mon, newtime->tm_mday ); #endif strcat( g_LogName , strSubLogName ); strcat( g_LogName, g_FileName ); strcpy(g_LogFileName,g_LogName); g_InitDay = newtime->tm_mday; g_LogCount = 0 ;//重置计数 Plat_CreatePath( g_LogName ); char fileTime[512] = {}; sprintf( fileTime ,"[%d_%d_%d].log" , newtime->tm_hour,newtime->tm_min ,newtime->tm_sec ); strcat( g_LogName , fileTime ); } g_LogCount++; #ifdef THREAD_LOG if(g_LogCount >= LOG_MAXCOUNT )//如果超过最大记录条数将更换文件名 { char fileTime[512] = {}; char LogFileName[LOG_ITEM_SIZE] = {0}; sprintf( fileTime ,"[%d_%d_%d].log" , newtime->tm_hour,newtime->tm_min ,newtime->tm_sec); strcpy(LogFileName, g_LogFileName); strcat( LogFileName , fileTime ); strcpy(g_LogName,LogFileName); g_LogCount = 0 ; } #endif }
void Plat_SetSimpleLogName( const char * szLogName ) { Plat_GetExePath( g_LogName, 256 ); //DeleteFileByTime( g_LogName ,1); #ifdef WIN32 strcat( g_LogName, "Log\\" ); #else strcat( g_LogName, "Log/" ); #endif struct tm *newtime; time_t aclock; time( &aclock ); // Get time in seconds newtime = localtime( &aclock ); // Convert time to struct tm form char strSubLogName[256] = { }; #ifdef WIN32 FILETIME tmCreateTime ; FILETIME tmExitTime ; FILETIME tmKernalTime ; FILETIME tmUserTime ; SYSTEMTIME tmSystemTime; GetProcessTimes( GetCurrentProcess( ),&tmCreateTime , &tmExitTime , &tmKernalTime , &tmUserTime ); FileTimeToSystemTime( &tmCreateTime ,&tmSystemTime ); TIME_ZONE_INFORMATION zinfo; GetTimeZoneInformation(&zinfo);//得到时区信息 SystemTimeToTzSpecificLocalTime(&zinfo,&tmSystemTime,&tmSystemTime);//将格林威治时间转换为对应的当前时区的时间 //SetLocalTime(&stime);//设置时间 //tReturning = CTime(stime); //sprintf( strSubLogName ,"[LOG_%d_%d_%d_%d_%d_%d]\\" ,tmSystemTime.wYear ,tmSystemTime.wMonth, tmSystemTime.wDay,tmSystemTime.wHour ,tmSystemTime.wMinute , tmSystemTime.wSecond ); sprintf( strSubLogName ,"[LOG_%d_%d_%d]\\" ,tmSystemTime.wYear ,tmSystemTime.wMonth, tmSystemTime.wDay ); #else //sprintf( strSubLogName ,"[LOG_%d_%d_%d]\\" , newtime->tm_hour,newtime->tm_min ,newtime->tm_sec ); sprintf( strSubLogName ,"[LOG_%d_%d_%d]/" , newtime->tm_hour,newtime->tm_min ,newtime->tm_sec ); #endif strcat( g_LogName , strSubLogName ); strcat( g_LogName, szLogName ); strcpy(g_LogFileName,g_LogName); strcpy(g_FileName,szLogName); g_InitDay = tmSystemTime.wDay; if (!Plat_CreatePath( g_LogName )) { //return; } char fileTime[512] = {}; sprintf( fileTime ,"[%d_%d_%d].log" , newtime->tm_hour,newtime->tm_min ,newtime->tm_sec ); strcat( g_LogName , fileTime ); //if( g_LogName[0] != 0 ) // g_hFile = fopen(g_LogName,"at+"); //unlink( g_LogName ); #ifdef WIN32 //by jinsheng 2009.1.7 //_CrtSetReportHook( NewCrtMemReport ); #endif #ifdef THREAD_LOG g_pLogUnitHead = new CHAINHEADER; // DWORD dwThreadID = 0; g_bLogRun = TRUE;// by sun 2009.2.7 g_LogCount = 0 ; #ifdef WIN32 g_hLogEvent = CreateEvent( NULL, TRUE, FALSE, NULL );// by sun 2009.2.7 // [4/10/2010 sun ] // g_hLogThread = (HANDLE)CreateThread( NULL, 0, &LogThreadFunc, NULL, 0, &dwThreadID ); #else sem_init( &g_hLogEvent, 1, 0 ); // li9chuan 2010-12-1 #endif g_hLogThread = ThreadLib::Create( LogThreadFunc, NULL ); #endif }