123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- /*
- * init.c
- * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
- * See the COPYING file for the terms of usage and distribution.
- */
- #ifdef HAVE_CONFIG_H
- #endif
- #include <tchar.h>
- #include <stdlib.h>
- #include "log4c/config.h"
- #include "log4c/init.h"
- #include "log4c/category.h"
- #include "log4c/appender.h"
- #include "log4c/layout.h"
- #include "log4c/rollingpolicy.h"
- #include "log4c/rc.h"
- #include "sd/error.h"
- #include "sd/sprintf.h"
- #include "sd/factory.h"
- #include "sd/sd_xplatform.h"
- #include "log4c/appender_type_stream.h"
- #include "log4c/appender_type_stream2.h"
- #include "log4c/appender_type_syslog.h"
- #include "log4c/appender_type_mmap.h"
- #include "log4c/appender_type_rollingfile.h"
- #include "log4c/rollingpolicy_type_sizewin.h"
- #include "log4c/layout_type_basic.h"
- #include "log4c/layout_type_dated.h"
- #include "log4c/layout_type_basic_r.h"
- #include "log4c/layout_type_dated_r.h"
- static const TCHAR version[] = _T("$Id$");
- #if defined(__LOG4C_DEBUG__) && defined(__GLIBC__)
- #include <mcheck.h>
- #endif
- // 布局类型数组;
- static const log4c_layout_type_t * const layout_types[] = {
- &log4c_layout_type_basic,
- &log4c_layout_type_dated,
- /* These appenders are not yet ported with msvc */
- #ifndef _WIN32
- &log4c_layout_type_basic_r,
- &log4c_layout_type_dated_r
- #endif
- };
- // 布局数组大小;
- static size_t nlayout_types = sizeof(layout_types) / sizeof(layout_types[0]);
- // 日志目的地数组;
- static const log4c_appender_type_t * const appender_types[] = {
- &log4c_appender_type_stream,
- &log4c_appender_type_stream2,
- #ifdef HAVE_MMAP
- &log4c_appender_type_mmap,
- #endif
- #ifdef HAVE_SYSLOG_H
- &log4c_appender_type_syslog,
- #endif
- #ifdef WITH_ROLLINGFILE
- &log4c_appender_type_rollingfile
- #endif
- };
- // 目的地数组大小;
- static size_t nappender_types = sizeof(appender_types) / sizeof(appender_types[0]);
- #ifdef WITH_ROLLINGFILE
- static const log4c_rollingpolicy_type_t * const rollingpolicy_types[] = {
- &log4c_rollingpolicy_type_sizewin
- };
- static size_t nrollingpolicy_types =
- sizeof(rollingpolicy_types) / sizeof(rollingpolicy_types[0]);
- #endif
- // 静态变量标志log4c是否初始化;
- static int log4c_is_init = 0;
- // 资源文件结构体;
- typedef struct rcfile
- {
- TCHAR name[256];
- time_t ctime;
- int exists;
- } rcfile_t;
- // 资源结构体;
- static rcfile_t rcfiles[] = {
- {_T("$LOG4C_RCPATH/log4crc.txt") },
- {_T("$HOME/.log4crc.txt") },
- {_T("./log4crc.txt") }
- };
- // 资源文件数组大小;
- static const int nrcfiles = sizeof(rcfiles) / sizeof(rcfiles[0]);
- /******************************************************************************/
- extern int log4c_init(void)
- {// C语言,需要把所有变量声明在函数开始位置;
- size_t i;
- int j = 0;
- int ret = 0;
- const TCHAR* priority = NULL;
- const TCHAR* appender = NULL;
- sd_debug(_T("log4c_init["));
- /* activate GLIBC allocation debugging */
- #if defined(__LOG4C_DEBUG__) && defined(__GLIBC__)
- mtrace();
- #endif
- if (log4c_is_init)
- {// log4c已初始化;
- sd_debug(_T("log4c already initialized ]"));
- return 0;
- }
- // 自增,标志log4c已初始化;
- log4c_is_init++;
- /* Initialize default types: layouts, appenders, rollingpolicies */
- sd_debug(_T("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 */
- sd_debug(_T("looking for conf files..."));
- _sntprintf(
- rcfiles[0].name,
- sizeof(rcfiles[0].name) - 1,
- _T("%s/log4crc.txt"),
- _tgetenv(_T("LOG4C_RCPATH")) ? _tgetenv(_T("LOG4C_RCPATH")) : LOG4C_RCPATH);
- _sntprintf(
- rcfiles[1].name, sizeof(rcfiles[1].name) - 1, _T("%s/.log4crc.txt"),
- _tgetenv(_T("HOME")) ? _tgetenv(_T("HOME")) : _T(""));
- for (j = 0; j < nrcfiles; j++)
- {
- sd_debug(_T("checking for conf file at '%s'"), rcfiles[j].name);
- if (SD_ACCESS_READ(rcfiles[j].name)) //文件是否只读,只读继续;
- continue;
- if (SD_STAT_CTIME(rcfiles[j].name,&rcfiles[j].ctime) != 0)
- sd_error(_T("sd_stat_ctime %s failed"), rcfiles[j].name);
- rcfiles[j].exists=1;
- if (log4c_load(rcfiles[j].name) == -1)
- {
- sd_error(_T("loading %s failed"), rcfiles[j].name);
- ret = -1;
- }
- else
- sd_debug(_T("loading %s succeeded"), rcfiles[j].name);
- }
- /* override configuration with environment variables */
- sd_debug(_T("checking environment variables..."));
- if ( (priority = _tgetenv(_T("LOG4C_PRIORITY"))) != NULL)
- {
- sd_debug(_T("setting priority of root category to '%s'"), priority);
- log4c_category_set_priority(log4c_category_get(_T("root")), log4c_priority_to_int(priority));
- }
- if ( (appender = _tgetenv(_T("LOG4C_APPENDER"))) != NULL)
- {
- sd_debug(_T("setting appender of root category to '%s'"),appender);
- log4c_category_set_appender(log4c_category_get(_T("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( _tgetenv("SD_DEBUG")){
- log4c_dump_all_types(stderr);
- log4c_dump_all_instances(stderr);
- }
- #endif
- sd_debug(_T("]"));
- return ret;
- }
- /******************************************************************************/
- void __log4c_reread(void)
- {
- time_t file_ctime;
- int i;
- for (i = 0; i < nrcfiles; i++){
- /* only reread files that existed when we first initialized */
- if (rcfiles[i].exists && SD_STAT_CTIME(rcfiles[i].name,&file_ctime) == 0){
- /* time_t is number of second since epoch, just compare for == */
- if (file_ctime != rcfiles[i].ctime){
- sd_debug(_T("Need reread on file %s\n"),rcfiles[i].name);
- SD_STAT_CTIME(rcfiles[i].name,&rcfiles[i].ctime);
- if (log4c_rc_load(log4c_rc, rcfiles[i].name) == -1){
- sd_error(_T("re-loading config file %s failed"), rcfiles[i].name);
- }
- }
- }
- }
- }
- /******************************************************************************/
- /*
- * Rereads any log4crc files that have changed
- */
- void log4c_reread(void)
- {
- #ifdef __ENABLE_REREAD__
- if (0 != log4c_rc->config.reread){
- __log4c_reread();
- }
- #endif
- }
- /******************************************************************************/
- extern int log4c_fini(void)
- {
- int rc = 0;
- /* Some acceptable use of goto here to avoid lots of nested ifs
- * when we need a quick exit
- */
- sd_debug(_T("log4c_fini["));
- if (log4c_rc->config.nocleanup){
- sd_debug(_T("not cleaning up--nocleanup specified in conf"));
- rc = -1;
- goto log4c_fini_exit;
- }
- if (!log4c_is_init){
- rc = 0;
- sd_debug(_T("not cleaning up--log4c not initialized"));
- goto log4c_fini_exit;
- }
- log4c_is_init--;
- sd_debug(_T("cleaning up category, appender, layout androllingpolicy 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;
- }
- if (log4c_layout_factory) {
- sd_factory_delete(log4c_layout_factory);
- log4c_layout_factory = NULL;
- }
- if (log4c_rollingpolicy_factory) {
- sd_factory_delete(log4c_rollingpolicy_factory);
- log4c_rollingpolicy_factory = NULL;
- }
- ///////////////////////////////////////////////////////////////////////////
- // 检测出来需要额外释放的内存 [7/23/2010 jesse]
- log4c_layout_delete_global();
- log4c_appender_delete_global();
- log4c_rollingpolicy_delete_global();
- #ifdef __SD_DEBUG__
- if( _tgetenv("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(_T("]"));
- return 0;
- }
- /******************************************************************************/
- #ifdef __GNUC__
- extern void __attribute__ ((constructor)) __log4c_init(void)
- {
- #ifdef WITH_CONSTRUCTORS
- log4c_init();
- #endif
- }
- extern void __attribute__ ((destructor)) __log4c_fini(void)
- {
- #ifdef WITH_CONSTRUCTORS
- log4c_fini();
- #endif
- }
- #endif
- /******************************************************************************/
- extern void log4c_dump_all_types(FILE *fp){
- /*
- *
- * For debug, dump all the types that have been registered during init. We just
- * display the name of the the type for the moment--the rest of the type info
- * right now consists of functions to call, so not really printable.
- */
- log4c_appender_types_print(fp);
- log4c_layout_types_print(fp);
- #ifdef WITH_ROLLINGFILE
- log4c_rollingpolicy_types_print(fp);
- #endif
- }
- extern void log4c_dump_all_instances(FILE *fp){
- /*
- * Also dump any instances that were created during init by
- * reading the conf file.
- *
- * An instances of a type consists of the base
- * type information (name plus function table) and an instance name and
- * configuration. For example one can have an instance of the rollingfile
- * appender which logs to /var/tmp and another instance which logs to
- * /usr/tmp. They are both of type rollingfile, but are distinct instances of
- * it
- */
- _ftprintf(fp, _T("instance dump follows (may be empty):\n"));
- sd_factory_print(log4c_category_factory, fp);
- sd_factory_print(log4c_appender_factory, fp);
- sd_factory_print(log4c_layout_factory, fp);
- #ifdef WITH_ROLLINGFILE
- sd_factory_print(log4c_rollingpolicy_factory, fp);
- #endif
- }
- //////////////////////////////////////////////////////////////////////////
- ///下面函数为自己写的一些函数,为使用log4c起来方便而设置的
- /** 带有配置文件名的初始化函数
- 完成功能和log4c_init完全一致,只是这里的配置文件名以参数形式传入的
- @returns 0 for success .
- 作者:jesse 日期:2008-9-6
- */
- extern int log4c_init_with_cfg_file(const TCHAR *strCfgFileName)
- {
- size_t i;
- int ret = 0;
- sd_debug(_T("log4c_init["));
- /* activate GLIBC allocation debugging */
- #if defined(__LOG4C_DEBUG__) && defined(__GLIBC__)
- mtrace();
- #endif
- if (log4c_is_init)
- {
- sd_debug(_T("log4c already initialized ]"));
- return 0;
- }
- log4c_is_init++;
- /* Initialize default types: layouts, appenders, rollingpolicies */
- sd_debug(_T("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(_T("looking for conf files..."));
- if(NULL == strCfgFileName)
- {
- sd_debug(_T("log4c_init[ERROR:strCfgFileName==NULL"));
- ret = -1;
- }
- else
- {
- time_t timeFile;
- sd_debug(_T("checking for conf file at '%s'"), strCfgFileName);
- if (SD_ACCESS_READ(strCfgFileName))
- {
- sd_error(_T("loading %s failed"), strCfgFileName);
- ret = -1;
- }
- else
- {
- if (SD_STAT_CTIME(strCfgFileName, &timeFile) != 0)
- {
- sd_error(_T("sd_stat_ctime %s failed"), strCfgFileName);
- }
- ///rcfiles[i].exists=1;
- if (log4c_load(strCfgFileName) == -1)
- {
- sd_error(_T("loading %s failed"), strCfgFileName);
- ret = -1;
- }
- else
- {
- sd_debug(_T("loading %s succeeded"), strCfgFileName);
- }
- }
- }
- //}
- /* override configuration with environment variables */
- {
- const TCHAR* priority;
- const TCHAR* appender;
- sd_debug(_T("checking environment variables..."));
- if ( (priority = _tgetenv(_T("LOG4C_PRIORITY"))) != NULL){
- sd_debug(_T("setting priority of root category to '%s'"),priority);
- log4c_category_set_priority(log4c_category_get(_T("root")),
- log4c_priority_to_int(priority));
- }
- if ( (appender = _tgetenv(_T("LOG4C_APPENDER"))) != NULL){
- sd_debug(_T("setting appender of root category to '%s'"),appender);
- log4c_category_set_appender(log4c_category_get(_T("root")), log4c_appender_get(appender));
- }
- }
- /*
- * For debug dump current types and instances:
- * ptrThis 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( _tgetenv("SD_DEBUG")){
- log4c_dump_all_types(stderr);
- log4c_dump_all_instances(stderr);
- }
- #endif
- sd_debug(_T("]"));
- return ret;
- }
|