Logo Search packages:      
Sourcecode: log4c version File versions  Download package

LOG4C_API int log4c_init ( void   ) 

constructor

Returns:
0 for success

Definition at line 93 of file init.c.

{    
    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;
}


Generated by  Doxygen 1.6.0   Back to index