Browse Source

add for sys log module

zhengwei 8 years ago
parent
commit
c50c9eee98
10 changed files with 214 additions and 10 deletions
  1. 77 1
      ProcessRemodule.cpp
  2. 17 2
      ProcessRemodule.h
  3. 2 0
      YAServer.cpp
  4. 1 0
      YAServer.h
  5. 0 3
      YAServer.sln
  6. 8 3
      YAServer.vcxproj
  7. 2 0
      YAServer.vcxproj.filters
  8. 5 1
      YAServerDlg.cpp
  9. 70 0
      log_process_module.cpp
  10. 32 0
      log_process_module.h

+ 77 - 1
ProcessRemodule.cpp

@@ -5,6 +5,8 @@
 #include <iostream>
 #include <exception> 
 
+#include "log_process_module.h"
+
 using namespace std;
 //#include <thread>
 //#include <mutex>
@@ -25,6 +27,10 @@ unsigned nSqlThreadID[SQL_PROCESS_THREAD_NUM];
 HANDLE hErrLogThread[ERR_LOG_PROCESS_THREAD_NUM];
 unsigned nErrLogThreadID[ERR_LOG_PROCESS_THREAD_NUM];
 
+HANDLE hSysLogThread[SYS_LOG_PROCESS_THREAD_NUM];
+unsigned nSysLogThreadID[SYS_LOG_PROCESS_THREAD_NUM];
+
+
 
 
 
@@ -285,7 +291,6 @@ int send_err_log_message(char* pchr)
 	unsigned thread_id = rand() % ERR_LOG_PROCESS_THREAD_NUM;
 	if(!PostThreadMessage(nErrLogThreadID[thread_id], ERR_LOG_MSG, (WPARAM)pchr, 0))//post thread msg
     {
-        PostThreadMessage(nErrLogThreadID[thread_id], ERR_LOG_MSG, (WPARAM)pchr, 0);
         printf("post message failed,errno:%d\n",::GetLastError());
         delete[] pchr;
 		return 1;
@@ -293,6 +298,51 @@ int send_err_log_message(char* pchr)
 	return 0;
 }
 
+int sys_log_process_thread_init()
+{
+	int i = 0;
+	log_module_init();
+	for(i=0; i<SYS_LOG_PROCESS_THREAD_NUM; i++)
+	{
+	    hSysLogThread[i] = (HANDLE)_beginthreadex( NULL, 0, &sys_log_process_entry, NULL, 0, &(nSysLogThreadID[i]) );
+        if(hSysLogThread[i] == 0)
+        {
+            printf("start thread failed,errno:%d\n",::GetLastError());
+            return 1;
+        }
+	}
+	return 0;
+}
+
+// thread function
+unsigned __stdcall sys_log_process_entry(void *param)
+{
+     //printf("thread sys log_process_entry start\n");
+
+     MSG msg;
+	 //check for msg quene isExist or not
+     PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
+     while(true)
+     {
+        if(GetMessage(&msg,0,0,0)) //get msg from message queue
+         {
+            switch(msg.message)
+             {
+                 case SYS_LOG_MSG:
+                     char * pInfo = (char *)msg.wParam;
+				 
+				     #ifndef  UT_TEST
+                     debug_print(0, pInfo);
+                     #else
+                     delete[] pInfo;
+                     #endif
+                     break;
+             }
+         }
+     };
+    return 0;
+}
+
 int service_task_init()
 {
 	BufferUtility* bufPtr = BufferUtility::getInstance();
@@ -300,10 +350,36 @@ int service_task_init()
 	message_process_thread_init();
 	sql_process_thread_init();
 	err_log_process_thread_init();
+	sys_log_process_thread_init();
 	::Sleep(1000);
 	return 0;
 }
 
+int send_sys_log_message(char* pchr)
+{
+    //send thread message
+	unsigned thread_id = rand() % SYS_LOG_PROCESS_THREAD_NUM;
+	if(!PostThreadMessage(nSysLogThreadID[thread_id], SYS_LOG_MSG, (WPARAM)pchr, 0))//post thread msg
+    {
+        printf("post message failed,errno:%d\n",::GetLastError());
+        delete[] pchr;
+		return 1;
+    }
+	return 0;
+}
+
+int debug_print_syslog(UINT debugLevel, const char* format, ...)
+{
+    char *buffer = new char[LOG_BUF_SIZE];
+	memset(buffer, 0, LOG_BUF_SIZE);
+    va_list args;
+    va_start (args, format);
+    vsnprintf_s(buffer, LOG_BUF_SIZE, LOG_BUF_SIZE-1, format, args);
+    va_end (args);
+	
+	send_sys_log_message(buffer);
+	return 0;
+}
 
 
 

+ 17 - 2
ProcessRemodule.h

@@ -1,5 +1,7 @@
+#ifndef PROCESS_REMODULE_H
+#define PROCESS_REMODULE_H
+
 #include <stdio.h>
-#include <windows.h>
 #include <cstdio>
 #include <process.h>
 #ifndef  UT_TEST
@@ -36,6 +38,17 @@ extern int err_log_process_thread_init();
 extern unsigned __stdcall err_log_process_entry(void *param);
 extern int send_err_log_message(char* pchr);
 
+
+//sys log thread function
+#define SYS_LOG_MSG WM_USER+400
+#define    SYS_LOG_PROCESS_THREAD_NUM    1
+extern HANDLE hSysLogThread[SYS_LOG_PROCESS_THREAD_NUM];
+extern unsigned nSysLogThreadID[SYS_LOG_PROCESS_THREAD_NUM];
+extern int sys_log_process_thread_init();
+extern unsigned __stdcall sys_log_process_entry(void *param);
+extern int send_sys_log_message(char* pchr);
+extern int debug_print_syslog(UINT debugLevel, const char* format, ...);
+
 int service_task_init();
 
 class BufferUtility
@@ -49,4 +62,6 @@ public:
 	ParseData* getItem();
 	int releaseItem(int index);
 	int releaseItem(ParseData* itemPtr);
-};
+};
+
+#endif

+ 2 - 0
YAServer.cpp

@@ -12,6 +12,7 @@
 #include "tlhelp32.h"
 
 #include "ProcessRemodule.h"
+#include "log_process_module.h"
 
 
 #ifdef _DEBUG
@@ -275,6 +276,7 @@ void CYAServerApp::load_sys_conf()
 	}
 
 	service_task_init();
+	debug_print_syslog(0, "Sys init Finish.");
 	
 }
 

+ 1 - 0
YAServer.h

@@ -10,6 +10,7 @@
 
 #include "resource.h"		// Ö÷·ûºÅ
 #include <string>
+
 using namespace std;
 
 // CYAServerApp:

+ 0 - 3
YAServer.sln

@@ -17,7 +17,4 @@ Global
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 	EndGlobalSection
-	GlobalSection(Performance) = preSolution
-		HasPerformanceSessions = true
-	EndGlobalSection
 EndGlobal

+ 8 - 3
YAServer.vcxproj

@@ -72,7 +72,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ProgramDatabaseFile>$(OutDir)\..\Symbol\$(TargetName).pdb</ProgramDatabaseFile>
       <AdditionalLibraryDirectories>..\lib\$(Configuration)\;..\thirdparty\lib\mysql\</AdditionalLibraryDirectories>
-      <AdditionalDependencies>jsoncpp_110.lib;libmysql.lib;libboost_system-vc110-mt-gd-1_63.lib;libboost_date_time-vc110-mt-gd-1_63.lib;libboost_random-vc110-mt-gd-1_63.lib</AdditionalDependencies>
+      <AdditionalDependencies>jsoncpp_110.lib;libmysql.lib;libboost_system-vc110-mt-gd-1_63.lib;libboost_date_time-vc110-mt-gd-1_63.lib;libboost_random-vc110-mt-gd-1_63.lib;log4cppD.lib</AdditionalDependencies>
     </Link>
     <Midl>
       <MkTypLibCompatible>false</MkTypLibCompatible>
@@ -95,7 +95,7 @@
       <Optimization>MaxSpeed</Optimization>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>..\common\json;..\thirdparty\eigen;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>
@@ -105,7 +105,7 @@
       <OptimizeReferences>true</OptimizeReferences>
       <ProgramDatabaseFile>$(OutDir)\..\Symbol\$(TargetName).pdb</ProgramDatabaseFile>
       <AdditionalLibraryDirectories>..\lib\$(Configuration)\;..\thirdparty\lib\mysql\</AdditionalLibraryDirectories>
-      <AdditionalDependencies>jsoncpp_110.lib;libmysql.lib;libboost_system-vc110-mt-1_63.lib;libboost_date_time-vc110-mt-1_63.lib;libboost_random-vc110-mt-1_63.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>jsoncpp_110.lib;libmysql.lib;libboost_system-vc110-mt-1_63.lib;libboost_date_time-vc110-mt-1_63.lib;libboost_random-vc110-mt-1_63.lib;log4cppLIB.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <Midl>
       <MkTypLibCompatible>false</MkTypLibCompatible>
@@ -171,6 +171,7 @@ xcopy "$(SolutionDir)..\config.ini" "D:\0a-share\$(Configuration)\" /Y /C /D /S<
     <ClInclude Include="Filter\KalmanFilter.h" />
     <ClInclude Include="LogSetting.h" />
     <ClInclude Include="log_def.h" />
+    <ClInclude Include="log_process_module.h" />
     <ClInclude Include="minidupm.h" />
     <ClInclude Include="MysqlConnPool.h" />
     <ClInclude Include="ProcessRemodule.h" />
@@ -246,6 +247,10 @@ xcopy "$(SolutionDir)..\config.ini" "D:\0a-share\$(Configuration)\" /Y /C /D /S<
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
     </ClCompile>
     <ClCompile Include="LogSetting.cpp" />
+    <ClCompile Include="log_process_module.cpp">
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
+    </ClCompile>
     <ClCompile Include="minidump.cpp" />
     <ClCompile Include="MysqlConnPool.cpp" />
     <ClCompile Include="ProcessRemodule.cpp" />

+ 2 - 0
YAServer.vcxproj.filters

@@ -98,6 +98,7 @@
       <Filter>hp-socket\socket</Filter>
     </ClCompile>
     <ClCompile Include="ProcessRemodule.cpp" />
+    <ClCompile Include="log_process_module.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\common\matrix\BaseMatrix.h">
@@ -266,6 +267,7 @@
       <Filter>hp-socket\socket</Filter>
     </ClInclude>
     <ClInclude Include="ProcessRemodule.h" />
+    <ClInclude Include="log_process_module.h" />
   </ItemGroup>
   <ItemGroup>
     <None Include="..\config.ini" />

+ 5 - 1
YAServerDlg.cpp

@@ -28,6 +28,9 @@
 #include "LogSetting.h"
 #include "SysSetting.h"
 
+#include "ProcessRemodule.h"
+#include "log_process_module.h"
+
 
 // 用来跟踪内存使用情况
 #define TRACE_MEMORY_WRITELOG
@@ -2121,7 +2124,8 @@ void CYAServerDlg::parse_package_data(BYTE* DataBuffer, int nLen, DWORD dwConnID
 	int reader_id = 0;
 	int nTick = 0;
 	CString strtime = _T("");
-
+    INT codeType = wChr;
+    debug_print_syslog(0, "receive msg code: %x", codeType);
 	switch (wChr){
 	case CHAR_LOCATEDATA: // 定位数据
 		{

+ 70 - 0
log_process_module.cpp

@@ -0,0 +1,70 @@
+#include "log_process_module.h"
+#include <iostream>
+#include <time.h>
+#include <string.h>
+using namespace std;
+
+log4cpp::Appender *appender[LOG_FILE_NUM];
+log4cpp::Layout *layout[LOG_FILE_NUM];
+log4cpp::Category* category[LOG_FILE_NUM];
+char* log_file_name[LOG_FILE_NUM]     = {"./sys/syslog.txt"};
+char* log_appender_name[LOG_FILE_NUM] = {"syslog"};
+char* log_category_name[LOG_FILE_NUM] = {"syslog"};
+
+int log_module_init(void)
+{
+	UINT32 i = 0;
+	system("mkdir sys");
+	for(i=0; i<LOG_FILE_NUM; i++)
+	{
+		appender[i] = new log4cpp::FileAppender(log_appender_name[i], log_file_name[i]);
+		layout[i] = new log4cpp::SimpleLayout();
+		category[i] = &(log4cpp::Category::getInstance(log_category_name[i]));
+		appender[i]->setLayout(layout[i]);
+        category[i]->setAppender(appender[i]);
+        category[i]->setPriority(log4cpp::Priority::INFO);
+	}
+	return 0;
+}
+
+int sys_log_print(UINT debugLevel, UINT logFileIndex, char* logStr)
+{
+    if(NULL == logStr)
+    {
+        return 0;
+    }
+	char buffer[LOG_BUF_SIZE + 50] = {0};
+	
+    const time_t t = time(NULL);	
+	struct tm* current_time = localtime(&t);
+
+    sprintf(buffer, "[%d:%d:%d]", 
+		current_time->tm_hour,
+        current_time->tm_min,
+        current_time->tm_sec);
+	strcat (buffer, logStr);
+
+	switch (debugLevel)
+	{
+		case SYS_LOG_INFO:
+			category[logFileIndex]->info(buffer);
+			break;
+		case SYS_LOG_WARNING:
+			category[logFileIndex]->warn(buffer);
+			break;
+		case SYS_LOG_ERROR:
+			category[logFileIndex]->error(buffer);
+			break;
+		default:		
+	    category[logFileIndex]->info(buffer);
+	}
+	return 0;
+}
+
+int debug_print(UINT debugLevel, char* logStr)
+{
+    sys_log_print(debugLevel, 0, logStr);
+    return 0;
+}
+
+

+ 32 - 0
log_process_module.h

@@ -0,0 +1,32 @@
+#ifndef LOG_PROCESS_MODULE
+#define LOG_PROCESS_MODULE
+
+#include "log4cpp/Category.hh"
+#include "log4cpp/FileAppender.hh"
+#include "log4cpp/BasicLayout.hh"
+#include "log4cpp/SimpleLayout.hh"
+
+
+
+#pragma comment(lib, "log4cppLIB.lib")
+
+
+#define SYS_LOG_INFO        0
+#define SYS_LOG_WARNING     1
+#define SYS_LOG_ERROR       2
+
+#define LOG_BUF_SIZE        200
+
+#define LOG_FILE_NUM        1
+extern log4cpp::Appender *appender[LOG_FILE_NUM];
+extern log4cpp::Layout *layout[LOG_FILE_NUM];
+extern log4cpp::Category* category[LOG_FILE_NUM];
+extern char* log_file_name[LOG_FILE_NUM];
+extern char* log_appender_name[LOG_FILE_NUM];
+extern char* log_category_name[LOG_FILE_NUM];
+
+extern int log_module_init(void);
+extern int debug_print(UINT debugLevel, char* logStr);
+
+
+#endif