Эх сурвалжийг харах

T20M5021 #comment libtoolkit 日志库的改造

gifur 5 жил өмнө
parent
commit
d8ab0a90f3

+ 2 - 2
CMakeSettings.json

@@ -30,11 +30,11 @@
       "configurationType": "Debug",
       "cmakeExecutable": "/usr/bin/cmake",
       "remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
-      "cmakeCommandArgs": "",
+      "cmakeCommandArgs": "-D BUILD_TESTING=ON",
       "buildCommandArgs": "",
       "ctestCommandArgs": "",
       "inheritEnvironments": [ "linux_x64" ],
-      "remoteMachineName": "-48757982;10.42.0.1 (username=gifur, port=1022, authentication=Password)",
+      "remoteMachineName": "-530210665;10.42.0.1 (username=root, port=1022, authentication=Password)",
       "remoteCMakeListsRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/src",
       "remoteBuildRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/build/${name}",
       "remoteInstallRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/install/${name}",

+ 6 - 6
Common/precompile.h

@@ -16,18 +16,18 @@
 #define WIN32_LEAN_AND_MEAN
 #endif //WIN32_LEAN_AND_MEAN
 #include <Mmsystem.h>
+#include <process.h>
+#include <crtdbg.h>
+#include <locale.h>
+#include <time.h>
+#endif // _WIN32
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
-#include <process.h>
 #include <errno.h>
 #include <time.h>
 #include <string.h>
-#include <crtdbg.h>
-#include <locale.h>
-#include <time.h>
-
-#endif // _WIN32
 
 #ifdef SPABASE_LINKED_AS_STATIC_LIBRARY
 #define SPBASE_API

+ 11 - 1
libtoolkit/CMakeLists.txt

@@ -2,7 +2,7 @@ set(MODULE_NAME "libtoolkit")
 set(MODULE_PREFIX "LIBTOOLKIT")
 
 if(MSVC)
-	add_definitions(-DLIBTOOLKIT_EXPORTS)
+	# add_definitions(-DLIBTOOLKIT_EXPORTS)
 endif(MSVC)
 
 include(CheckLibraryExists)
@@ -22,12 +22,20 @@ list(REMOVE_ITEM ${MODULE_PREFIX}_SRCS
     cputime.c
     crc32.h
     crc32.c
+    getopt.h
+    getopt.c
+    getparam.h
+    getparam.c
     iatpatch.h
     iatpatch.c
     iconv.c
     iconv.h
+    intalgo.h
+    intalgo.c
     ioreactor.c
     ioreactor.h
+    kvpconfig.h
+    kvpconfig.c
     ntservice.h
     ntservice.c
     rbtree.h
@@ -73,6 +81,7 @@ endif()
 add_library(${MODULE_NAME} SHARED ${${MODULE_PREFIX}_SRCS})
 
 if(MSVC)
+  target_compile_definitions(${MODULE_NAME} PRIVATE  "-DLIBTOOLKIT_EXPORTS")
   list(APPEND ${MODULE_PREFIX}_LIBS 
     dbghelp 
     ws2_32
@@ -86,6 +95,7 @@ target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS})
 if (BUILD_TESTING)
 	add_library(${MODULE_NAME}_a STATIC ${${MODULE_PREFIX}_SRCS})
 	target_link_libraries(${MODULE_NAME}_a ${${MODULE_PREFIX}_LIBS})
+    target_compile_definitions(${MODULE_NAME}_a PRIVATE "-DTOOLKIT_LINKED_AS_STATIC_LIBRARY")
 	add_subdirectory(test)
 endif()
 

+ 8 - 0
libtoolkit/ListEntry.h

@@ -1,3 +1,11 @@
+/*
+ * category: [data structure]
+ * apply status: 
+ * edit status:
+ * build status:
+ * description:
+ */
+
 #ifndef LISTENTRY_H
 #define LISTENTRY_H
 

+ 9 - 5
libtoolkit/config.h

@@ -1,21 +1,25 @@
 #ifndef TOOLKIT_CONFIG_H
 #define TOOLKIT_CONFIG_H
 
+#ifndef TOOLKIT_LINKED_AS_STATIC_LIBRARY
 #ifdef _WIN32
 #ifdef LIBTOOLKIT_EXPORTS
 #define TOOLKIT_API _declspec(dllexport)
-#else
+#else //LIBTOOLKIT_EXPORTS
 #define TOOLKIT_API _declspec(dllimport)
-#endif
+#endif //LIBTOOLKIT_EXPORTS
 #elif defined(__GUNC__)
 #if __GUNC__ >= 4
 #define TOOLKIT_API __attribute__((visibility("default")))
 #else
 #define TOOLKIT_API
-#endif
-#else
+#endif // __GUNC__ >= 4
+#else //_WIN32
 #define TOOLKIT_API
-#endif
+#endif //_WIN32
+#else //TOOLKIT_LINKED_AS_STATIC_LIBRARY
+#define TOOLKIT_API
+#endif //TOOLKIT_LINKED_AS_STATIC_LIBRARY
 
 #ifdef _WIN32
 #define TOOLKIT_CC __cdecl

+ 8 - 0
libtoolkit/getparam.h

@@ -1,3 +1,11 @@
+/*
+ * category: [tool]
+ * apply status: no used
+ * edit status: not
+ * build status:
+ * description:
+ */
+
 #ifndef GETPARAM_H
 #define GETPARAM_H
 

+ 9 - 0
libtoolkit/gettimeofday.h

@@ -1,3 +1,12 @@
+/*
+ * category: [tool for time]
+ * apply status: framework & linux
+ * edit status: 
+ * build status: yes
+ * description:
+ */
+
+
 #ifndef __GETTIMEOFDAY_H__
 #define __GETTIMEOFDAY_H__
 

+ 8 - 0
libtoolkit/hash.h

@@ -1,3 +1,11 @@
+/*
+ * category: [algorithm]
+ * apply status: hashset, shm_table, timerqueue, sp_tbs and mod_sipphone
+ * edit status: not
+ * build status:
+ * description:
+ */
+
 #ifndef __HASH_H__
 #define __HASH_H__
 

+ 1 - 0
libtoolkit/hashset.c

@@ -3,6 +3,7 @@
 #include "hashset.h"
 #include "list.h"
 #include "hash.h"
+#include <winpr/string.h>
 
 /* double hash , same with .netframework hashtable */
 

+ 8 - 0
libtoolkit/hashset.h

@@ -1,3 +1,11 @@
+/*
+ * category: [data structure]
+ * apply status: log_mgr, sp_log, SpEntity, mod_evtconverter
+ * edit status: not
+ * build status:
+ * description:
+ */
+
 #ifndef HASHSET_H
 #define HASHSET_H
 

+ 8 - 0
libtoolkit/heaputil.h

@@ -1,3 +1,11 @@
+/*
+ * category: [data structure]
+ * apply status: timerqueue
+ * edit status: not
+ * build status:
+ * description:
+ */
+
 #ifndef HEAPUTIL_H
 #define HEAPUTIL_H
 

+ 8 - 0
libtoolkit/iatpatch.h

@@ -1,3 +1,11 @@
+/*
+ * category: [debug]
+ * apply status: no
+ * edit status: no
+ * build status:
+ * description: Import address table patcher.
+ */
+
 #ifndef __IATPACH_H__
 #define __IATPACH_H__
 

+ 8 - 0
libtoolkit/iconv.h

@@ -1,3 +1,11 @@
+/*
+ * category: [tool] for character
+ * apply status: no
+ * edit status: no
+ * build status:
+ * description: 
+ */
+
 #ifndef ICONV_H
 #define ICONV_H
 

+ 1 - 0
libtoolkit/iniutil.c

@@ -3,6 +3,7 @@
 #include "strutil.h"
 #include "iniutil.h"
 #include "vsscanf.h"
+#include <winpr/string.h>
 
 TOOLKIT_API char *inifile_read_str(const char *file, 
 					   const char *section, 

+ 9 - 0
libtoolkit/iniutil.h

@@ -1,3 +1,12 @@
+/*
+ * category: [config]
+ * apply status: 
+ * edit status: 
+ * build status:
+ * description: to read .ini format file
+ * TODO: multi thread and multi process test.
+ */
+
 #ifndef __LOGINI_H__
 #define __LOGINI_H__
 

+ 9 - 0
libtoolkit/intalgo.h

@@ -1,3 +1,11 @@
+/*
+ * category: [math]
+ * apply status: cputime
+ * edit status:
+ * build status:
+ * description: 
+ */
+
 #ifndef __INTALGO_H__
 #define __INTALGO_H__
 
@@ -14,6 +22,7 @@ extern "C" {
 #define rounddiv(a,b) (((a) + ((b) >> 1)) / (b))
 #define sqr(x) ((x) * (x))
 
+/*greatest common divisor*/
 TOOLKIT_API int gcd(int x1, int x2);
 
 static __inline int lcm(int x1, int x2)

+ 1 - 1
libtoolkit/iobuffer.c

@@ -17,7 +17,7 @@ struct iobuffer_t
 	DECLARE_REF_COUNT_MEMBER(sp_ref_cnt);
 };
 
-#define DEFAULT_CAPACITY			256
+#define DEFAULT_CAPACITY			            256
 #define DEFAULT_HEADER_CAPACITY		128
 
 static void expand(iobuffer_t *iobuf, int head_capacity, int newcapacity)

+ 15 - 7
libtoolkit/iobuffer.h

@@ -1,3 +1,11 @@
+/*
+ * category: [data structure, core]
+ * apply status: 
+ * edit status:
+ * build status:
+ * description:
+ */
+
 #ifndef IOBUFFER_H
 #define IOBUFFER_H
 
@@ -13,14 +21,14 @@ extern "C" {
 
 #include <stdarg.h>
 
-#define IOBUF_T_I8	0
-#define IOBUF_T_I4	1
-#define IOBUF_T_I2	2
-#define IOBUF_T_I1	3
-#define IOBUF_T_STR	4
-#define IOBUF_T_BUF	5
+#define IOBUF_T_I8	       0
+#define IOBUF_T_I4	       1
+#define IOBUF_T_I2	       2
+#define IOBUF_T_I1	       3
+#define IOBUF_T_STR	   4
+#define IOBUF_T_BUF	   5
 #define IOBUF_T_WSTR 6 /* wchar_t */
-#define IOBUF_T_7BIT	7
+#define IOBUF_T_7BIT	   7
 
 typedef struct iobuffer_t iobuffer_t;
 

+ 2 - 0
libtoolkit/ioqueue.c

@@ -13,6 +13,8 @@
 #include <winpr/handle.h>
 #include <winpr/synch.h>
 #include <winpr/pipe.h>
+#include <winpr/string.h>
+
 
 #ifndef SO_UPDATE_CONNECT_CONTEXT
 #define SO_UPDATE_CONNECT_CONTEXT 0x7010

+ 11 - 3
libtoolkit/ioreactor.h

@@ -1,3 +1,11 @@
+/*
+ * category: [network]
+ * apply status: no use
+ * edit status: no
+ * build status: exclude
+ * description:
+ */
+
 #ifndef __IOREACTOR_H
 #define __IOREACTOR_H
 
@@ -16,10 +24,10 @@ extern "C" {
 typedef struct ioreactor_s ioreactor_t;
 
 #define IOR_OP_READ			1
-#define IOR_OP_WRITE		2
+#define IOR_OP_WRITE		    2
 #define IOR_OP_CONNECT		4
-#define IOR_OP_ACCEPT		8
-#define IOR_OP_CLOSE		16
+#define IOR_OP_ACCEPT		    8
+#define IOR_OP_CLOSE		   16
 
 #define IOR_FD_PIPE		1
 #define IOR_FD_SOCKET	2

+ 8 - 0
libtoolkit/jhash.h

@@ -1,3 +1,11 @@
+/*
+ * category: [algorithm]
+ * apply status: sp_log, sp_ses, mod_evtconverter
+ * edit status: no
+ * build status:
+ * description:
+ */
+
 #ifndef _LINUX_JHASH_H
 #define _LINUX_JHASH_H
 

+ 8 - 0
libtoolkit/kvpconfig.h

@@ -1,3 +1,11 @@
+/*
+ * category: [config]
+ * apply status: no
+ * edit status: no
+ * build status:
+ * description: key value pair configure.
+ */
+
 #ifndef __KVPCONFIG_H__
 #define __KVPCONFIG_H__
 

+ 8 - 0
libtoolkit/list.h

@@ -1,3 +1,11 @@
+/*
+ * category: [data structure]
+ * apply status: 
+ * edit status: 
+ * build status:
+ * description: 
+ */
+
 #ifndef LIST_H
 #define LIST_H
 

+ 10 - 8
libtoolkit/log.c

@@ -10,6 +10,7 @@
 #include "fileutil.h"
 #include "sockutil.h"
 #include "modCheck.h"
+#include <winpr/string.h>
 
 #define XLOG_VERSION	0x0101
 
@@ -19,7 +20,6 @@ TOOLKIT_API int xlog_version();
 TOOLKIT_API int xlog_init2(const char *inifile, const char *sections);
 TOOLKIT_API int xlog_set_level(const char* inst, int level);
 TOOLKIT_API int xlog_get_level(const char* inst, int *level);
-TOOLKIT_API int xlog_log_f(const char *inst, int level, const char *fmt, ...);
 // #define XLOG_TRACE(inst, fmt, ...)	\
 // 	xlog_log_f(inst, XLOG_LEVEL_TRACE, fmt, __VA_ARGS__)
 // #define XLOG_DEBUG(inst, fmt, ...)	\
@@ -33,6 +33,7 @@ TOOLKIT_API int xlog_log_f(const char *inst, int level, const char *fmt, ...);
 // #define XLOG_FATAL(inst, fmt, ...)	\
 // 	xlog_log_f(inst, XLOG_LEVEL_FATAL, fmt, __VA_ARGS__)
 
+/*get ini file from ini|etc|conf folder under current execute directory*/
 static char *find_best_config()
 {
 	char path[MAX_PATH];
@@ -97,7 +98,8 @@ TOOLKIT_API int xlog_init2(const char *file, const char *sections)
 	char *copysections = NULL;
 	char *best_config_file = NULL;
 	int rc = 0;
-
+	logmgr_t* log_mgr = NULL;
+	assert(toolkit_getResource() != NULL);
 	if (toolkit_getResource()->g_initialized++) {
 		return 0;
 	}
@@ -115,8 +117,11 @@ TOOLKIT_API int xlog_init2(const char *file, const char *sections)
 			p = strtok(NULL, delimers);
 		}
 	}
-	logmgr_t* log_mgr = (logmgr_t*)(toolkit_getResource()->g_mgr);
+	assert(toolkit_getResource()->g_mgr == NULL);
 	logmgr_create(&log_mgr);
+	toolkit_getResource()->g_mgr = log_mgr;
+	/*gcc error: lvalue required as unary ¡®&¡¯ operand*/
+	//logmgr_create(&((logmgr_t*)(toolkit_getResource()->g_mgr)));
 	singlefilefactory_create(&fac);
 	logmgr_register_factory((logmgr_t *)(toolkit_getResource()->g_mgr), fac);
 	periodicfilefactory_create(&fac);
@@ -188,11 +193,8 @@ TOOLKIT_API int xlog_init2(const char *file, const char *sections)
 	if (arr_section)
 		array_free(arr_section);
 	if (rc < 0) {
-		//TODO: to confirm the correctness
-		logmgr_t* log_mgr = (logmgr_t*)(toolkit_getResource()->g_mgr);
-		logmgr_t** p_log_mgr = &log_mgr;
-		logmgr_destroy((logmgr_t *)(toolkit_getResource()->g_mgr));
-		*p_log_mgr = NULL;
+		logmgr_destroy((logmgr_t*)(toolkit_getResource()->g_mgr));
+		toolkit_getResource()->g_mgr = NULL;
 	}
 
 	return rc;

+ 13 - 4
libtoolkit/log.h

@@ -1,3 +1,11 @@
+/*
+ * category: [core]
+ * apply status:
+ * edit status:
+ * build status:
+ * description:
+ */
+
 #ifndef __XLOG_H__
 #define __XLOG_H__
 
@@ -13,10 +21,10 @@ extern "C" {
 
 #define XLOG_LEVEL_ALL			0
 #define XLOG_LEVEL_TRACE		1
-#define XLOG_LEVEL_DEBUG	2
-#define XLOG_LEVEL_INFO		3
-#define XLOG_LEVEL_WARN	4
-#define XLOG_LEVEL_ERROR	5
+#define XLOG_LEVEL_DEBUG   	2
+#define XLOG_LEVEL_INFO		    3
+#define XLOG_LEVEL_WARN	    4
+#define XLOG_LEVEL_ERROR	    5
 #define XLOG_LEVEL_FATAL		6
 #define XLOG_LEVEL_NONE		7
 
@@ -26,6 +34,7 @@ TOOLKIT_API int xlog_remove_logger(const char *name);
 TOOLKIT_API int xlog_init(const char *inifile);
 TOOLKIT_API int xlog_term();
 TOOLKIT_API int xlog_log(const char *inst, int level, const char *str);
+TOOLKIT_API int xlog_log_f(const char* inst, int level, const char* fmt, ...);
 TOOLKIT_API int xlog_log_v(const char *inst, int level, const char *fmt, va_list arg);
 
 

+ 56 - 48
libtoolkit/log_factory.h

@@ -5,61 +5,69 @@
 
 #include "list.h"
 
-typedef struct logfactory_t
-{
-	struct list_head entry;
-	char *name;
-	void (*destroy)(void *self);
-	void* (*create_log)(void *self, const char* inst);
-	int (*set_log_param)(void *self, void *log, const char *key, const char *value);
-	int (*init_log)(void *self, void *log);
-	int (*term_log)(void *self, void *log);
-	void (*destroy_log)(void *self, void *log);
-	int (*record_log)(void *self, void *log, int level, unsigned long ts_low, unsigned long ts_high, const char *s, int n);
-}logfactory_t;
+#ifdef __cplusplus
+extern "C" {
+#endif
 
-static __inline void logfactory_destroy(void *self)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	(*fac->destroy)(self);
-}
+	typedef struct logfactory_t
+	{
+		struct list_head entry;
+		char* name;
+		void (*destroy)(void* self);
+		void* (*create_log)(void* self, const char* inst);
+		int (*set_log_param)(void* self, void* log, const char* key, const char* value);
+		int (*init_log)(void* self, void* log);
+		int (*term_log)(void* self, void* log);
+		void (*destroy_log)(void* self, void* log);
+		int (*record_log)(void* self, void* log, int level, unsigned long ts_low, unsigned long ts_high, const char* s, int n);
+	}logfactory_t;
 
-static __inline void* logfactory_create_log(void *self, const char *inst)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	return (*fac->create_log)(self, inst);
-}
+	static __inline void logfactory_destroy(void* self)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		(*fac->destroy)(self);
+	}
 
-static __inline int logfactory_set_log_param(void *self, void *log, const char *key, const char *value)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	return (*fac->set_log_param)(self, log, key, value);
-}
+	static __inline void* logfactory_create_log(void* self, const char* inst)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		return (*fac->create_log)(self, inst);
+	}
 
-static __inline int logfactory_init_log(void *self, void *log)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	return (*fac->init_log)(self, log);
-}
+	static __inline int logfactory_set_log_param(void* self, void* log, const char* key, const char* value)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		return (*fac->set_log_param)(self, log, key, value);
+	}
 
-static __inline int logfactory_term_log(void *self, void *log)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	return (*fac->term_log)(self, log);
-}
+	static __inline int logfactory_init_log(void* self, void* log)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		return (*fac->init_log)(self, log);
+	}
 
-static __inline void logfactory_destroy_log(void *self, void *log)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	(*fac->destroy_log)(self, log);
-}
+	static __inline int logfactory_term_log(void* self, void* log)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		return (*fac->term_log)(self, log);
+	}
 
-static __inline int logfactory_log_record(void *self, void *log, int level, unsigned long ts_low, unsigned long ts_high, const char *s, int n)
-{
-	logfactory_t *fac = (logfactory_t *)self;
-	return (*fac->record_log)(self, log, level, ts_low, ts_high, s, n);
-}
+	static __inline void logfactory_destroy_log(void* self, void* log)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		(*fac->destroy_log)(self, log);
+	}
+
+	static __inline int logfactory_log_record(void* self, void* log, int level, unsigned long ts_low, unsigned long ts_high, const char* s, int n)
+	{
+		logfactory_t* fac = (logfactory_t*)self;
+		return (*fac->record_log)(self, log, level, ts_low, ts_high, s, n);
+	}
 
-const char *log_level2str(int level);
+	const char* log_level2str(int level);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif //__LOGFACTORY_H__

+ 3 - 0
libtoolkit/log_mgr.c

@@ -6,6 +6,9 @@ int logmgr_create(logmgr_t **p_mgr)
 {
 	logmgr_t *mgr;
 	mgr = MALLOC_T(logmgr_t);
+	if (mgr == NULL) {
+		return -1;
+	}
 	INIT_LIST_HEAD(&mgr->factory_list);
 	mgr->log_table = stringmap_create(-1);
 	*p_mgr = mgr;

+ 27 - 19
libtoolkit/log_mgr.h

@@ -7,24 +7,32 @@
 #include "log_factory.h"
 #include "hashset.h"
 
-typedef struct logmgr_t logmgr_t;
-
-struct logmgr_t
-{
-	struct list_head factory_list;
-	stringmap_t *log_table;
-};
-
-int logmgr_create(logmgr_t **p_mgr);
-void logmgr_destroy(logmgr_t *mgr);
-int logmgr_register_factory(logmgr_t *mgr, logfactory_t *fac);
-int logmgr_unregister_factory(logmgr_t *mgr, logfactory_t *fac);
-logfactory_t* logmgr_get_factory(logmgr_t *mgr, const char *name);
-int logmgr_create_log(logmgr_t *mgr, const char *factory, const char *inst, logbase_t **p_log);
-int logmgr_set_param(logmgr_t *mgr, logbase_t *log, const char *key, const char *value);
-int logmgr_init_log(logmgr_t *mgr, logbase_t *log);
-int logmgr_term_log(logmgr_t *mgr, logbase_t *log);
-void logmgr_destroy_log(logmgr_t *mgr, logbase_t *log);
-logbase_t *logmgr_find_log(logmgr_t *mgr, const char *inst);
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	typedef struct logmgr_t logmgr_t;
+
+	struct logmgr_t
+	{
+		struct list_head factory_list;
+		stringmap_t* log_table;
+	};
+
+	int logmgr_create(logmgr_t** p_mgr);
+	void logmgr_destroy(logmgr_t* mgr);
+	int logmgr_register_factory(logmgr_t* mgr, logfactory_t* fac);
+	int logmgr_unregister_factory(logmgr_t* mgr, logfactory_t* fac);
+	logfactory_t* logmgr_get_factory(logmgr_t* mgr, const char* name);
+	int logmgr_create_log(logmgr_t* mgr, const char* factory, const char* inst, logbase_t** p_log);
+	int logmgr_set_param(logmgr_t* mgr, logbase_t* log, const char* key, const char* value);
+	int logmgr_init_log(logmgr_t* mgr, logbase_t* log);
+	int logmgr_term_log(logmgr_t* mgr, logbase_t* log);
+	void logmgr_destroy_log(logmgr_t* mgr, logbase_t* log);
+	logbase_t* logmgr_find_log(logmgr_t* mgr, const char* inst);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif //__LOGMGR_H__

+ 45 - 14
libtoolkit/log_periodic.c

@@ -1,11 +1,12 @@
 #include "precompile.h"
-
+#include <stdio.h>
 #include "log_periodic.h"
 #include "log_base.h"
 #include "log_factory.h"
 #include "log.h"
 #include "spinlock.h"
 #include "fileutil.h"
+#include <winpr/string.h>
 
 typedef struct periodiclogfactory_t periodiclogfactory_t;
 
@@ -103,14 +104,26 @@ static int periodicfilefactory_set_log_param(void *self, void *log, const char *
 	periodiclogfactory_t *fac = (periodiclogfactory_t *)self;
 	periodicfile_t *plog = (periodicfile_t *)log;
 	if (_stricmp(key, "file") == 0) {
-		if (strchr(value, ':') == NULL) {
-			char *p;
-			GetModuleFileNameA(NULL, plog->path_template, MAX_PATH);
-			p = strrchr(plog->path_template, '\\');
-			*(p+1) = 0;
-			strcpy(p+1, value);;
-		} else {
-			strcpy(plog->path_template, value);
+		if (value != NULL) {
+#ifdef _WIN32
+			if (strchr(value, ':') == NULL) {
+#else
+			if (value[0] != '/') {
+#endif // _WIN32
+				char* p;
+				GetModuleFileNameA(NULL, plog->path_template, MAX_PATH);
+				p = strrchr(plog->path_template, '/');
+				if (!p)
+					p = strrchr(plog->path_template, '\\');
+				*(p + 1) = 0;
+				strcpy(p + 1, value);
+			}
+			else {
+				strcpy(plog->path_template, value);
+			}
+		}
+		else {
+			return -1;
 		}
 	} else if (_stricmp(key, "use_lock") == 0) {
 		if (_stricmp(value, "true") == 0 || _stricmp(value, "1") == 0) {
@@ -208,16 +221,22 @@ static int periodicfilefactory_record_log(void *self,
 		else
 			fastlock_enter(plog->lock);
 	}
-
+	//TODO: test the calculate performation.
+#ifdef _WIN32
 	utc_ft.dwLowDateTime = (DWORD)ts_low;
 	utc_ft.dwHighDateTime = (DWORD)ts_high;
 	FileTimeToLocalFileTime(&utc_ft, &local_ft);
 	FileTimeToSystemTime(&local_ft, &st);
+#else
+	GetLocalTime(&st);
+#endif // _WIN32
 	ExpandTemplatePath(&st, plog->path_template, tmp);
-	ptmp = strrchr(tmp, '\\');
+	ptmp = strrchr(tmp, '/');
+	if(!ptmp)
+		ptmp = strrchr(tmp, '\\');
 	*ptmp = 0;
 	CreateDirRecursiveA(tmp);
-	*ptmp = '\\';
+	*ptmp = SPLIT_SLASH;
 	if (strcmp(tmp, plog->curr_file) != 0) {
 		if (plog->fp) {
 			fclose(plog->fp);
@@ -225,8 +244,12 @@ static int periodicfilefactory_record_log(void *self,
 		}
 		plog->fp = fopen(tmp, "ab");
 		if (plog->fp) {
+			/*fix bug*/
+			int flen = 0;
 			strcpy(plog->curr_file, tmp);
-			plog->first_record = !!ftell(plog->fp);
+			fseek(plog->fp, 0, SEEK_END);
+			flen = ftell(plog->fp);
+			plog->first_record = !flen;
 		} else {
 			plog->curr_file[0] = 0;
 		}
@@ -240,8 +263,13 @@ static int periodicfilefactory_record_log(void *self,
 				st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, log_level2str(level));
 			plog->first_record = 0;
 		} else {
-			n = sprintf(tmp, "\r\n[%02d:%02d:%02d.%03d][%s] ", 
+#ifdef _WIN32
+			n = sprintf(tmp, "\r\n[%02d:%02d:%02d.%03d][%s] ",
+				st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, log_level2str(level));
+#else
+			n = sprintf(tmp, "\n[%02d:%02d:%02d.%03d][%s] ",
 				st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, log_level2str(level));
+#endif // _WIN32
 		}
 		_fwrite_nolock(tmp, n, 1, plog->fp);
 		_fwrite_nolock(s, sn, 1, plog->fp);
@@ -266,6 +294,9 @@ int periodicfilefactory_create(logfactory_t **p_fac)
 		return -1;
 
 	fac = ZALLOC_T(periodiclogfactory_t);
+	if (!fac) {
+		return -1;
+	}
 	fac->base.name = _strdup("periodic");
 	fac->base.record_log = &periodicfilefactory_record_log;
 	fac->base.init_log = &periodicfilefactory_init_log;

+ 9 - 1
libtoolkit/log_periodic.h

@@ -5,6 +5,14 @@
 
 #include "log_factory.h"
 
-int periodicfilefactory_create(logfactory_t **p_fac);
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	int periodicfilefactory_create(logfactory_t** p_fac);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif //__PERIODICFILELOGFACTORY_H__

+ 1 - 0
libtoolkit/log_redirect.c

@@ -5,6 +5,7 @@
 #include "log.h"
 #include "spinlock.h"
 #include "array.h"
+#include <winpr/string.h>
 
 typedef struct redirectlogfactory_t redirectlogfactory_t;
 

+ 8 - 0
libtoolkit/log_single.c

@@ -6,6 +6,7 @@
 #include "spinlock.h"
 #include "fileutil.h"
 #include "strutil.h"
+#include <winpr/string.h>
 
 typedef struct singlelogfactory_t singlelogfactory_t;
 
@@ -165,10 +166,14 @@ static int singlefilefactory_record_log(void *self,
 			fastlock_enter(slog->lock);
 		}
 
+#ifdef _WIN32
 		utc_ft.dwLowDateTime = (DWORD)ts_low;
 		utc_ft.dwHighDateTime = (DWORD)ts_high;
 		FileTimeToLocalFileTime(&utc_ft, &local_ft);
 		FileTimeToSystemTime(&local_ft, &st);
+#else
+		GetLocalTime(&st);
+#endif // _WIN32
 		if (slog->first_record) {
 			n = sprintf(tmp, "[%02d:%02d:%02d.%03d][%s] ",
 				st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, log_level2str(level));
@@ -198,6 +203,9 @@ int singlefilefactory_create(logfactory_t **p_fac)
 		return -1;
 
 	fac = MALLOC_T(singlelogfactory_t);
+	if (!fac) {
+		return -1;
+	}
 	fac->base.name = _strdup("single");
 	fac->base.record_log = &singlefilefactory_record_log;
 	fac->base.init_log = &singlefilefactory_init_log;

+ 1 - 0
libtoolkit/log_udpclient.c

@@ -6,6 +6,7 @@
 #include "log.h"
 #include "spinlock.h"
 #include <winpr/winsock.h>
+#include <winpr/string.h>
 
 typedef struct udplogfactory_t udplogfactory_t;
 

+ 2 - 0
libtoolkit/log_udpdaemon.c

@@ -6,6 +6,8 @@
 #include "log.h"
 #include <winpr/winsock.h>
 #include <winpr/synch.h>
+#include <winpr/thread.h>
+#include <winpr/string.h>
 
 typedef struct udplogdaemonfactory_t udplogdaemonfactory_t;
 

+ 1 - 0
libtoolkit/memutil.c

@@ -2,6 +2,7 @@
 #include "memutil.h"
 #include "shm_mem.h"
 #include <winpr/locale.h>
+#include <winpr/string.h>
 
 TOOLKIT_API void *buffer_resize(void* old, int elemsize, int oldcnt, int newcnt)
 {

+ 2 - 0
libtoolkit/rtp.c

@@ -4,6 +4,8 @@
 #include <time.h>
 #include "modCheck.h"
 
+#include <winpr/string.h>
+
 #ifndef  H264_VIDEO_PT
 #define H264_VIDEO_PT     97 // H264
 #endif 

+ 2 - 1
libtoolkit/test/CMakeLists.txt

@@ -62,11 +62,12 @@ foreach(test ${${MODULE_PREFIX}_TESTS})
 	get_filename_component(test_name ${test} NAME_WE)
 	add_executable(${test_name} ${test})
 	target_link_libraries(${test_name} ${comm_test_libraries})
+	target_compile_definitions(${test_name} PRIVATE "-DTOOLKIT_LINKED_AS_STATIC_LIBRARY")
 	set_property(TARGET ${test_name} PROPERTY FOLDER "test/libtoolkit")
 	# 会将 测试的执行程序生成在指定的文件夹,并且带上编译的配置条件比如 Debug
 	set_target_properties(${test_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
 	message(STATUS "add test case: ${test_name}...")
-	if(${test_name} STREQUAL "test_file")
+	if(${test_name} STREQUAL "test_file" OR ${test_name} STREQUAL "test_log")
 	add_test(NAME ${test_name} COMMAND ${TESTING_OUTPUT_DIRECTORY}/${test_name}  ${TEST_AREA})
 	else()
 	add_test(NAME ${test_name} COMMAND ${TESTING_OUTPUT_DIRECTORY}/${test_name})

+ 118 - 0
libtoolkit/test/test_log.cpp

@@ -0,0 +1,118 @@
+#include <gtest/gtest.h>
+#include "log.h"
+#include "log_mgr.h"
+#include "log_factory.h"
+#include "log_single.h"
+#include "log_periodic.h"
+#include "log_udpclient.h"
+#include "log_udpdaemon.h"
+
+TEST(LogTest, LogPeriodicFactory)
+{
+	logfactory_t* factory;
+	logbase_t* log_inst = NULL;
+	ASSERT_TRUE(periodicfilefactory_create(&factory) == 0);
+	log_inst = (logbase_t*)factory->create_log(factory, "inst");
+	ASSERT_TRUE(log_inst != NULL);
+ 	ASSERT_TRUE(factory->init_log(factory, log_inst) == 0);
+	ASSERT_TRUE(factory->set_log_param(factory, log_inst, "invalid", NULL) != 0);
+	ASSERT_TRUE(factory->set_log_param(factory, log_inst, "file", NULL) != 0);
+	ASSERT_TRUE(factory->set_log_param(factory, log_inst, "file", "test_inst.log") == 0);
+	//TODO:
+
+	factory->term_log(factory, log_inst);
+	factory->destroy_log(factory, log_inst);
+	factory->destroy(factory);
+}
+
+
+TEST(LogTest, LogMgrRoutine)
+{
+	const auto args = ::testing::internal::GetArgvs();
+	EXPECT_FALSE(args.empty());
+	std::string test_dir("");
+	if (args.size() == 1) {
+		auto dir = args[0].find_last_of('/');
+		if (dir == std::string::npos)
+			dir = args[0].find_last_of('\\');
+		if (dir != std::string::npos) {
+			test_dir = args[0].substr(0, dir + 1);
+			test_dir += "test_libtoolkit_area";
+		}
+		EXPECT_TRUE(!test_dir.empty());
+	}
+	else {
+		test_dir = args[1];
+	}
+	std::cout << "test dir: " << test_dir << std::endl;
+	logmgr_t* mgr;
+	auto rc = logmgr_create(&mgr);
+	ASSERT_TRUE(rc == 0);
+	logfactory_t* t;
+	ASSERT_TRUE(0 == periodicfilefactory_create(&t));
+	ASSERT_TRUE(0 == logmgr_register_factory(mgr, t));
+	ASSERT_FALSE(0 == logmgr_create_log(mgr, "no_exist", NULL, NULL));
+	logbase_t *log_base;
+	/* invoke 'logfactory_create_log' and log_base become 'periodicfile_t' type*/
+	ASSERT_TRUE(0 == logmgr_create_log(mgr, "periodic", "log_instance", &log_base));
+	ASSERT_FALSE(stringmap_find(mgr->log_table, "log_instance") == NULL);
+	ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "level", "All"));
+	ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "use_lock", "1"));
+	std::string log_pattern = test_dir;
+	log_pattern += "/{yyyy}{MM}{dd}.log";
+	ASSERT_TRUE(0 == logfactory_set_log_param(t, log_base, "file", log_pattern.c_str()));
+	/*the c trap: 
+		you must know what it would be deal with before you put the param in and force to convert to void* type*/
+	logfactory_init_log((void*)t, (void*)log_base);
+	/*to log*/
+
+	FILETIME ft;
+	SYSTEMTIME st;
+#ifdef _WIN32
+	GetSystemTime(&st);
+	SystemTimeToFileTime(&st, &ft);
+#else
+	ft.dwHighDateTime = ft.dwLowDateTime = 0;
+#endif // _WIN32
+	ASSERT_TRUE(0 == logfactory_log_record(
+		t, log_base, XLOG_LEVEL_INFO, ft.dwLowDateTime, ft.dwHighDateTime, "I am log test", strlen("I am log test")));
+
+	/*let logmgr_destroy to do it!*/
+	//logmgr_destroy_log(mgr, log_base);
+	logmgr_destroy(mgr);
+}
+
+TEST(LogTest, LogInitTest)
+{
+	auto rc = xlog_init(NULL);
+	ASSERT_TRUE(rc == 0);
+	
+	char log_file_dir_format[260];
+#ifdef _WIN32
+	strcpy(log_file_dir_format, "C:\\rvc\\dbg\\logTest\\{yyyy}{MM}{dd}.log");
+#else
+	strcpy(log_file_dir_format, "/opt/rvc/dbg/logTest/{yyyy}{MM}{dd}.log");
+#endif // _WIN32
+
+	ASSERT_TRUE(0 != xlog_add_logger(NULL, NULL));
+	ASSERT_TRUE(0 != xlog_add_logger("", ""));
+	rc = xlog_add_logger("logTest",
+		"periodic",
+		"level", "All",
+		"use_lock", "1",
+		"file", log_file_dir_format,
+		NULL);
+	ASSERT_TRUE(rc == 0);
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_ERROR, "error test %s, %d", "hello world", 12345));
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_FATAL, "fatal test %s", "hello world"));
+	ASSERT_FALSE(0 == xlog_log_f("NoneTest", XLOG_LEVEL_FATAL, "fatal test %s", "hello world"));
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_WARN, "warn test %s", "hello world"));
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_INFO, "info test %s", "hello world"));
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_DEBUG, "debug test %s", "hello world"));
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_TRACE, "trace test %s", "hello world"));
+	ASSERT_TRUE(0 == xlog_log_f("logTest", XLOG_LEVEL_NONE, "none test %s", "hello world"));
+	ASSERT_TRUE(0 == xlog_log("logTest", XLOG_LEVEL_TRACE, "log tace test."));
+	ASSERT_FALSE(0 == xlog_log("NoneTeset", XLOG_LEVEL_TRACE, "log tace test."));
+	rc = xlog_term();
+	ASSERT_TRUE(rc == 0);
+}

+ 44 - 0
libtoolkit/test/test_misc.cpp

@@ -0,0 +1,44 @@
+#include <gtest/gtest.h>
+#include "gettimeofday.h"
+#include "list.h"
+
+TEST(TestUtilMisc, TestGetTimeOfDay)
+{
+	struct timeval tv1;
+	struct timeval tv2;
+	struct timezone tz;
+	ASSERT_TRUE(gettimeofday(&tv1, NULL) == 0);
+	ASSERT_TRUE(gettimeofday(&tv2, &tz) == 0);
+}
+
+TEST(TestUtilMisc, TestList)
+{
+	LIST_HEAD(struct_list);
+
+	typedef struct _test_struct{
+		struct list_head entry;
+		void* unused;
+	} test_struct;
+
+	ASSERT_TRUE(list_empty(&struct_list));
+
+	test_struct* pos;
+	list_for_each_entry(pos, &struct_list, test_struct, entry) {
+		ASSERT_TRUE(FALSE);
+	}
+
+	test_struct one;
+	INIT_LIST_HEAD(&one.entry);
+	one.unused = (void*)1;
+	list_add_tail(&one.entry, &struct_list);
+	ASSERT_FALSE(list_empty(&struct_list));
+
+	list_for_each_entry(pos, &struct_list, test_struct, entry) {
+		ASSERT_TRUE(pos->unused  == (void*)1);
+	}
+
+	list_del(&one.entry);
+	ASSERT_TRUE(list_empty(&struct_list));
+
+}
+

+ 2 - 0
libtoolkit/url.c

@@ -5,6 +5,8 @@
 #include <string.h>
 #include <ctype.h>
 
+#include <winpr/string.h>
+
 /* from libasynchttp */
 
 TOOLKIT_API char* urlencode(unsigned char *string) {

+ 2 - 0
winpr/include/winpr/interlocked.h

@@ -156,6 +156,8 @@ extern "C"
 	WINPR_API PVOID InterlockedCompareExchangePointer(PVOID volatile* Destination, PVOID Exchange,
 	                                                  PVOID Comperand);
 
+	WINPR_API VOID _ReadWriteBarrier(VOID);
+
 #else /* _WIN32 */
 #define WINPR_LIST_ENTRY LIST_ENTRY
 #define _WINPR_LIST_ENTRY _LIST_ENTRY

+ 1 - 0
winpr/include/winpr/thread.h

@@ -246,6 +246,7 @@ extern "C"
 	WINPR_API HANDLE OpenThread(DWORD dwDesiredAccess, BOOL  bInheritHandle, DWORD dwThreadId);
 	WINPR_API BOOL SetThreadPriority(HANDLE hThread, int nPriority);
 #define GetCurrentThread _GetCurrentThread
+#define _beginthreadex CreateThread
 	/* Processor */
 
 	WINPR_API DWORD GetCurrentProcessorNumber(void);

+ 5 - 0
winpr/libwinpr/interlocked/interlocked.c

@@ -275,6 +275,11 @@ PVOID InterlockedCompareExchangePointer(PVOID volatile* Destination, PVOID Excha
 #endif
 }
 
+VOID _ReadWriteBarrier(VOID)
+{
+
+}
+
 #endif /* _WIN32 */
 
 #if defined(_WIN32) && !defined(WINPR_INTERLOCKED_COMPARE_EXCHANGE64)