123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- /*
- * rollingpolicy.c
- *
- *
- * See the COPYING file for the terms of usage and distribution.
- */
- #include <log4c/appender.h>
- #include <log4c/appender_type_rollingfile.h>
- #include <log4c/rollingpolicy.h>
- #include <log4c/rollingpolicy_type_sizewin.h>
- #include <string.h>
- #include <sd/malloc.h>
- #include <sd/factory.h>
- #include <sd/hash.h>
- #include <sd/factory.h>
- #include <sd/error.h>
- /* Internal struct that defines the conf and the state info
- * for an instance of the appender_type_fileroller type.
- */
- struct __log4c_rollingpolicy
- {
- char* policy_name;
- const log4c_rollingpolicy_type_t* policy_type;
- void * policy_udata;
- rollingfile_udata_t* policy_rfudatap;
- #define PFLAGS_IS_INITIALIZED 0x0001
- int policy_flags;
- };
- sd_factory_t* log4c_rollingpolicy_factory = NULL;
- //////////////////////////////////////////////////////////////////////////
- ///以下为jesse修改的代码,将原有代码用#if 0...#endif注释掉
- // 为解决log4c_layout_types中内存泄漏而将静态局部变量提升为全局局部变量 [9/8/2008 jesse]
- static sd_hash_t* rollingpolicy_c_gs_types = NULL;
- static sd_hash_t* log4c_rollingpolicy_types(void)
- {
- ///static sd_hash_t* types = NULL;
- if (!rollingpolicy_c_gs_types)
- rollingpolicy_c_gs_types = sd_hash_new(20, NULL);
- return rollingpolicy_c_gs_types;
- }
- /** 删除本rollingpolicy.c文件中定义的全局和静态的指针指向的内存,防止内存泄漏
- 如:
- 1.rollingpolicy_c_gs_types.
- 2.
- @return void.
- 作者:jesse 日期:2008.09.08
- */
- extern void log4c_rollingpolicy_delete_global()
- {
- if (NULL != rollingpolicy_c_gs_types)
- {
- sd_hash_delete(rollingpolicy_c_gs_types);
- rollingpolicy_c_gs_types = NULL;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- #if 0
- /*******************************************************************************/
- static sd_hash_t* log4c_rollingpolicy_types(void){
- static sd_hash_t* types = NULL;
- if (!types)
- types = sd_hash_new(20, NULL);
- return types;
- }
- #endif
- extern void log4c_rollingpolicy_types_print(FILE *fp)
- {
- sd_hash_iter_t* i;
-
- fprintf(fp, "rollingpolicy types:");
- for (i = sd_hash_begin(log4c_rollingpolicy_types());
- i != sd_hash_end(log4c_rollingpolicy_types());
- i = sd_hash_iter_next(i) )
- {
- fprintf(fp, "'%s' ",((log4c_rollingpolicy_type_t *)(i->data))->name );
- }
- fprintf(fp, "\n");
- }
- /*******************************************************************************/
- LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_get(const char* a_name)
- {
- static const sd_factory_ops_t log4c_rollingpolicy_factory_ops = {
- /*(void*) log4c_rollingpolicy_new,
- (void*) log4c_rollingpolicy_delete,
- (void*) log4c_rollingpolicy_print,*/
- (void *( *)(const char *))log4c_rollingpolicy_new,
- (void ( *)(void *))log4c_rollingpolicy_delete,
- (void ( *)(void *,FILE *))log4c_rollingpolicy_print
- };
-
- if (!log4c_rollingpolicy_factory) {
- log4c_rollingpolicy_factory =
- sd_factory_new("log4c_rollingpolicy_factory",
- &log4c_rollingpolicy_factory_ops);
-
- /* build default rollingpolicy
- log4c_rollingpolicy_set_udata(log4c_appender_get("stderr"), stderr);
- log4c_appender_set_udata(log4c_appender_get("stdout"), stdout);*/
- }
-
- return (log4c_rollingpolicy_t*)sd_factory_get(log4c_rollingpolicy_factory, a_name);
- }
- /****************************************************************************/
- LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_new(const char* a_name){
- log4c_rollingpolicy_t* ptrThis;
-
- sd_debug("log4c_rollingpolicy_new[ ");
- if (!a_name)
- return NULL;
- sd_debug("new policy name='%s'", a_name);
- ptrThis = (log4c_rollingpolicy_t*)sd_calloc(1, sizeof(log4c_rollingpolicy_t));
- ptrThis->policy_name = sd_strdup(a_name);
- ptrThis->policy_type = &log4c_rollingpolicy_type_sizewin;
- ptrThis->policy_udata = NULL;
- ptrThis->policy_rfudatap = NULL;
- ptrThis->policy_flags = 0;
-
- sd_debug("]");
-
- return ptrThis;
- }
- /*******************************************************************************/
- LOG4C_API void log4c_rollingpolicy_delete(log4c_rollingpolicy_t* ptrThis)
- {
-
- sd_debug("log4c_rollingpolicy_delete['%s'",
- (ptrThis && ptrThis->policy_name ? ptrThis->policy_name: "(no name)"));
- if (!ptrThis){
- goto log4c_rollingpolicy_delete_exit;
- }
-
- if (log4c_rollingpolicy_fini(ptrThis)){
- sd_error("failed to fini rollingpolicy");
- goto log4c_rollingpolicy_delete_exit;
- }
-
- if (ptrThis->policy_name){
- sd_debug("freeing policy name");
- free(ptrThis->policy_name);
- ptrThis->policy_name = NULL;
- };
- sd_debug("freeing ptrThis rolling policy instance");
- free(ptrThis);
- log4c_rollingpolicy_delete_exit:
- sd_debug("]");
- }
- /*******************************************************************************/
- LOG4C_API int log4c_rollingpolicy_init(log4c_rollingpolicy_t *ptrThis, rollingfile_udata_t* rfup){
-
- int rc = 0;
-
- if (!ptrThis)
- return -1;
-
- ptrThis->policy_rfudatap = rfup;
-
- if (!ptrThis->policy_type)
- return 0;
-
- if (!ptrThis->policy_type->init)
- return 0;
-
- rc = ptrThis->policy_type->init(ptrThis, rfup);
-
- ptrThis->policy_flags |= PFLAGS_IS_INITIALIZED;
-
- return rc;
-
- }
- LOG4C_API int log4c_rollingpolicy_fini(log4c_rollingpolicy_t *ptrThis){
-
- int rc = 0;
-
- sd_debug("log4c_rollingpolicy_fini['%s'",
- (ptrThis && ptrThis->policy_name ? ptrThis->policy_name: "(no name")) ;
-
- if (!ptrThis){
- rc = 0;
- } else {
- if (ptrThis->policy_flags & PFLAGS_IS_INITIALIZED){
- if (ptrThis->policy_type){
- rc = ptrThis->policy_type->fini(ptrThis);
- }
- }
-
- if (!rc){
- ptrThis->policy_flags &= ~PFLAGS_IS_INITIALIZED;
- }else{
- sd_debug("Call to rollingpolicy fini failed");
- }
- }
-
- sd_debug("]");
- return rc;
- }
- /*******************************************************************************/
- LOG4C_API int log4c_rollingpolicy_is_triggering_event(log4c_rollingpolicy_t* ptrThis, const log4c_logging_event_t* a_event, long current_fs){
- if (!ptrThis)
- return -1;
-
- if (!ptrThis->policy_type)
- return 0;
-
- if (!ptrThis->policy_type->is_triggering_event)
- return 0;
-
- return ptrThis->policy_type->is_triggering_event(ptrThis, a_event, current_fs);
- }
- /*******************************************************************************/
- LOG4C_API int log4c_rollingpolicy_rollover(log4c_rollingpolicy_t* ptrThis, FILE **fpp){
-
- if (!ptrThis)
- return -1;
-
- if (!ptrThis->policy_type)
- return 0;
-
- if (!ptrThis->policy_type->rollover)
- return 0;
-
- return ptrThis->policy_type->rollover(ptrThis, fpp);
- }
- /*******************************************************************************/
- LOG4C_API void* log4c_rollingpolicy_get_udata(const log4c_rollingpolicy_t* ptrThis){
- return (ptrThis ? ptrThis->policy_udata : NULL);
- }
- /*******************************************************************************/
- LOG4C_API void* log4c_rollingpolicy_get_name(const log4c_rollingpolicy_t* ptrThis){
- return (ptrThis ? ptrThis->policy_name : NULL);
- }
- /*******************************************************************************/
- LOG4C_API rollingfile_udata_t* log4c_rollingpolicy_get_rfudata(const log4c_rollingpolicy_t* ptrThis){
- return (ptrThis ? ptrThis->policy_rfudatap : NULL);
- }
- /*******************************************************************************/
- LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_set( const log4c_rollingpolicy_type_t* a_type){
-
- sd_hash_iter_t* i = NULL;
- void* previous = NULL;
-
- if (!a_type)
- return NULL;
-
- if ( (i = sd_hash_lookadd(log4c_rollingpolicy_types(), a_type->name)) == NULL)
- return NULL;
-
- previous = i->data;
- i->data = (void*) a_type;
-
- return (log4c_rollingpolicy_type_t*)previous;
- }
- /*****************************************************************************/
- LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_get( const char* a_name){
- sd_hash_iter_t* i;
-
- if (!a_name)
- return NULL;
-
- if ( (i = sd_hash_lookup(log4c_rollingpolicy_types(), a_name)) != NULL)
- return (log4c_rollingpolicy_type_t*)i->data;
-
- return NULL;
- }
- /*******************************************************************************/
- LOG4C_API void log4c_rollingpolicy_set_udata(log4c_rollingpolicy_t* ptrThis, void *udatap){
- if ( ptrThis) {
- ptrThis->policy_udata = udatap;
- }
- }
- /*******************************************************************************/
- LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_set_type( log4c_rollingpolicy_t* a_rollingpolicy, const log4c_rollingpolicy_type_t* a_type){
-
- const log4c_rollingpolicy_type_t* previous;
-
- if (!a_rollingpolicy)
- return NULL;
-
- previous = a_rollingpolicy->policy_type;
- a_rollingpolicy->policy_type = a_type;
- return previous;
- }
- /*******************************************************************************/
- LOG4C_API void log4c_rollingpolicy_print(const log4c_rollingpolicy_t* ptrThis, FILE* a_stream)
- {
- if (!ptrThis)
- return;
-
- fprintf(a_stream, "{ name:'%s' udata:%p}",
- ptrThis->policy_name,
- ptrThis->policy_udata);
- }
- LOG4C_API int log4c_rollingpolicy_is_initialized(log4c_rollingpolicy_t* ptrThis){
-
- if (!ptrThis)
- return(0);
-
- return( ptrThis->policy_flags & PFLAGS_IS_INITIALIZED);
-
- }
|