Exemplo n.º 1
0
extern int log4c_fini(void)
{
    /* Some acceptable use of goto here to avoid lots of nested ifs
     * when we need a quick exit
     */
    sd_debug("log4c_fini[");
    if (log4c_rc->config.nocleanup){
	sd_debug("not cleaning up--nocleanup specified in conf");
	goto log4c_fini_exit;
    }
  
    if (!log4c_is_init){
	sd_debug("not cleaning up--log4c not initialized");
	goto log4c_fini_exit;
    }
  
    log4c_is_init--;
    
    sd_debug("cleaning up category, appender, layout and"
	     "rollingpolicy instances");
    if (log4c_category_factory) {
	sd_factory_delete(log4c_category_factory);
	log4c_category_factory = NULL;
    }
  
    if (log4c_appender_factory) {
	sd_factory_delete(log4c_appender_factory);
	log4c_appender_factory = NULL;
    }
    log4c_appender_types_free();
  
    if (log4c_layout_factory) {
	sd_factory_delete(log4c_layout_factory);
	log4c_layout_factory = NULL;
    }
    log4c_layout_types_free();
  
#ifdef WITH_ROLLINGFILE
    if (log4c_rollingpolicy_factory) {
	sd_factory_delete(log4c_rollingpolicy_factory);
	log4c_rollingpolicy_factory = NULL;
    }
    log4c_rollingpolicy_types_free();
#endif
    
#ifdef __SD_DEBUG__
    if( getenv("SD_DEBUG")){
	sd_debug("Instance dump after cleanup:");
	log4c_dump_all_instances(stderr);
    }
#endif
#if defined(__LOG4C_DEBUG__) && defined(__GLIBC__)
    muntrace();
#endif
  
log4c_fini_exit:
    sd_debug("]");
  
    return 0;
}
Exemplo n.º 2
0
extern int log4c_init(void)
{    
    size_t i;
    int ret = 0;
  
    sd_debug("log4c_init[");
  
    /* activate GLIBC allocation debugging */
#if defined(__LOG4C_DEBUG__) && defined(__GLIBC__)
    mtrace();
#endif
  
    if (log4c_is_init){
	sd_debug("log4c already initialized ]");
	return 0;
    }
  
    log4c_is_init++;
  
    /* Initialize default types: layouts, appenders, rollingpolicies */
    sd_debug("intializing default types: appenders, layouts, rollingpolicies");
    for (i = 0; i < nlayout_types; i++) 
	log4c_layout_type_set(layout_types[i]);
  
    for (i = 0; i < nappender_types; i++) 
	log4c_appender_type_set(appender_types[i]);
#ifdef WITH_ROLLINGFILE
    for (i = 0; i < nrollingpolicy_types; i++) 
	log4c_rollingpolicy_type_set(rollingpolicy_types[i]);
#endif
  
    /* load configuration files */
    {
	int i;
	sd_debug("looking for conf files...");
	snprintf(rcfiles[0].name, sizeof(rcfiles[0].name) - 1, "%s/log4crc", 
		 getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
	snprintf(rcfiles[1].name, sizeof(rcfiles[1].name) - 1, "%s/.log4crc",
		 getenv("HOME") ? getenv("HOME") : "");
    
	for (i = 0; i < nrcfiles; i++) {
	    sd_debug("checking for conf file at '%s'", rcfiles[i].name);
	    if (SD_ACCESS_READ(rcfiles[i].name)) continue;
	    if (SD_STAT_CTIME(rcfiles[i].name,&rcfiles[i].ctime) != 0)
		sd_error("sd_stat_ctime %s failed", rcfiles[i].name);
	    rcfiles[i].exists=1;
	    if (log4c_load(rcfiles[i].name) == -1) {
		sd_error("loading %s failed", rcfiles[i].name);
		ret = -1;
	    }
	    else
		sd_debug("loading %s succeeded", rcfiles[i].name);		
	}
    }
  
    /* override configuration with environment variables */
    {
	const char* priority;
	const char* appender;
    
	sd_debug("checking environment variables...");
	if ( (priority = getenv("LOG4C_PRIORITY")) != NULL){
	    sd_debug("setting priority of root category to '%s'",
		     priority);
	    log4c_category_set_priority(log4c_category_get("root"), 
					log4c_priority_to_int(priority)); 
	}
    
	if ( (appender = getenv("LOG4C_APPENDER")) != NULL){
	    sd_debug("setting appender of root category to '%s'",
		     appender);
	    log4c_category_set_appender(log4c_category_get("root"), 
					log4c_appender_get(appender));
	}
    }
  
    /*
     *   For debug dump current types and instances:
     *   this allows the caller of log4c_init() to see:
     *   1. all types currently registered, including perhaps his own.
     *   2. all instances instantiated via the configuration file.
     *   
     *   If the caller goes on to programmatically create instaces then he
     *   can call log4c_dump_all_instances() later himself to
     *   verify that they were created as expected.
     *  
     */    
#ifdef __SD_DEBUG__
    if( getenv("SD_DEBUG")){
	log4c_dump_all_types(stderr);
	log4c_dump_all_instances(stderr);
    }
#endif
  
  
    sd_debug("]");
    return ret;
}