int main(int argc, char *argv[]) { sd_test_t *t; int ret; FILE *f; t = sd_test_new(argc, argv); log4c_init(); appender = log4c_appender_get("stream"); f = fopen("/dev/null", "w+"); log4c_appender_set_udata(appender, f); root = log4c_category_get("root"); log4c_category_set_appender(root, appender); log4c_category_set_priority(root, LOG4C_PRIORITY_TRACE); sd_test_add(t, test_big); ret = sd_test_run(t, argc, argv); log4c_appender_set_udata(appender, NULL); fclose(f); sd_test_delete(t); log4c_fini(); return ! ret; }
extern log4c_appender_t* log4c_appender_get(const char* a_name) { static const sd_factory_ops_t log4c_appender_factory_ops = { (void*) log4c_appender_new, (void*) log4c_appender_delete, (void*) log4c_appender_print, }; if (!log4c_appender_factory) { log4c_appender_factory = sd_factory_new("log4c_appender_factory", &log4c_appender_factory_ops); /* build default appenders */ log4c_appender_set_udata(log4c_appender_get("stderr"), stderr); log4c_appender_set_udata(log4c_appender_get("stdout"), stdout); } return sd_factory_get(log4c_appender_factory, a_name); }
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; }
int main(int argc, char *argv[]) { int flags = 0; char *buffer = NULL; int i; usec_t start; usec_t stop; char *test_name; /* declare and get a reference to some unconfigured appenders */ log4c_appender_t *mmap_appender = log4c_appender_get("bench.mmap"); log4c_appender_t *stream_appender = log4c_appender_get("stdout"); log4c_appender_t *stream2_appender = log4c_appender_get("bench.stream2"); log4c_appender_t *streamfile_appender = log4c_appender_get("bench.stream"); /* * Obligatory log4c init call */ log4c_init(); /* * Get some categories */ catmmap = log4c_category_get("mmap"); catstream = log4c_category_get("stream"); catstream2 = log4c_category_get("stream2"); catstream_file = log4c_category_get("stream_file"); /* * Read command line options */ getopts(argc, argv); /* * Configure the categories and appenders */ log4c_appender_set_type(mmap_appender, log4c_appender_type_get("mmap")); log4c_category_set_appender(catmmap, mmap_appender); log4c_category_set_appender(catstream, stream_appender); log4c_appender_set_type(stream2_appender, log4c_appender_type_get("stream2")); log4c_category_set_appender(catstream2, stream2_appender); log4c_category_set_appender(catstream_file, streamfile_appender); log4c_category_set_priority(log4c_category_get("root"), LOG4C_PRIORITY_ERROR); /* * Get a buffer for the message */ buffer = (char *) malloc(g_msgsize * sizeof(char)); memset(buffer, 'X', g_msgsize); buffer[g_msgsize - 1] = '\0'; /* * Calibration: do a couple of 1 second sleeps to make sure * the timing routines are in working order. */ start = my_utime(); for (i = 0; i < 2; i++) { sleep(1); } stop = my_utime(); display_time("calibration 2 x 1 second sleep calls", start, stop, (stop - start), (stop - start) / 2); /* logs that write to the screen, if required */ if ( !g_noscreen_appenders) { /* * fprintf writing to the screen */ start = my_utime(); for (i = 0; i < g_num_msgs; i++) { fprintf(stdout, "%s\n", buffer); } stop = my_utime(); display_time("fprintf", start, stop, (stop - start), (stop - start) / g_num_msgs); /* * log4c writing to the screen */ start = my_utime(); for (i = 0; i < g_num_msgs; i++) { log4c_category_error(catstream, "%s", buffer); } stop = my_utime(); display_time("fprintf", start, stop, (stop - start), (stop - start) / g_num_msgs); } /* Logs that write to files */ /* * mmap appender writing to bench.mmap * * On windows as this type is not implemented it makes pretty much * null calls to log4c--so it does nothing but is safe. */ start = my_utime(); for (i = 0; i < g_num_msgs; i++) { log4c_category_error(catmmap, "%s", buffer); } stop = my_utime(); display_time("mmap", start, stop, (stop - start), (stop - start) / g_num_msgs); /* * stream appender writing to bench.stream */ start = my_utime(); for (i = 0; i < g_num_msgs; i++) { log4c_category_error(catstream_file, "%s", buffer); } stop = my_utime(); display_time("stream_file", start, stop, (stop - start), (stop - start) / g_num_msgs); /* * stream2 appender writing to bench.stream2 * in buffered mode */ log4c_appender_open(stream2_appender); flags = log4c_stream2_get_flags(stream2_appender); start = my_utime(); for (i = 0; i < g_num_msgs; i++) { log4c_category_error(catstream2, "%s", buffer); } stop = my_utime(); test_name = (flags & LOG4C_STREAM2_UNBUFFERED ? "stream2 unbuffered -" : "stream2 buffered -"); display_time(test_name, start, stop, (stop - start), (stop - start) / g_num_msgs); log4c_appender_close(stream2_appender); /* * stream2 appender writing to bench.stream2 * in unbuffered mode -- for comparison with buffered mode * test above */ log4c_appender_open(stream2_appender); log4c_stream2_set_flags(stream2_appender, LOG4C_STREAM2_UNBUFFERED); flags = log4c_stream2_get_flags(stream2_appender); start = my_utime(); for (i = 0; i < g_num_msgs; i++) { log4c_category_error(catstream2, "%s", buffer); } stop = my_utime(); display_time((flags & LOG4C_STREAM2_UNBUFFERED ? "stream2 unbuffered -" : "stream2 buffered -"), start, stop, (stop - start), (stop - start) / g_num_msgs); log4c_appender_close(stream2_appender); /* * Obligatory log4c cleanup call */ log4c_fini(); free(buffer); return 0; }