/*
* 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);
  
}
