static void env_init (void) { static volatile int init_done = 0; char *enabled=NULL, *file=NULL, *str; char *glib=NULL; if (!init_done) { init_done = 1; if (!g_thread_supported ()) g_thread_init (NULL); /* Enables log4c logging */ if (NULL != (enabled = getenv(ENV_LOG4C_ENABLE))) { if (enabled[0] != '0') { // I mean '0', not '\0' if (log4c_init()) g_printerr("cannot load log4c\n"); else if ((file=getenv(ENV_LOG4C_LOAD))) log4c_load(file); } } if (NULL != (glib = getenv(ENV_GLIB2_ENABLE))) { g_log_set_default_handler(logger_stderr, NULL); } else if (!enabled) { g_log_set_default_handler(logger_noop, NULL); } /*configure the sleep time between two failed ADD actions*/ wait_on_add_failed = 10000UL; if ((str = getenv(ENV_WAIT_ON_FAILED_ADD))) { gint64 i64 = g_ascii_strtoll( str, NULL, 10 ); if (i64>=0LL && i64<=10000LL) wait_on_add_failed = i64; } } }
static int parse_opt(int argc, char **args) { int opt; while ((opt = getopt(argc, args, "hvpqkl:")) != -1) { switch (opt) { case 'h': flag_help = ~0; break; case 'k': PRINT_DEBUG("Keep pending file\n"); keep_pending = TRUE; break; case 'l': if (log4c_load(optarg) != 0) { PRINT_ERROR("Failed to load %s\n", optarg); } break; case 'v': flag_verbose++; break; case 'p': PRINT_DEBUG("Preserve mode activated\n"); preserve = TRUE; break; case 'q': flag_quiet = ~0; break; case '?': default: PRINT_ERROR("unexpected %c (%s)\n", optopt, strerror(opterr)); return 0; } } return 1; }
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; }