tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject cvs commit: jakarta-tomcat-connectors/jk/native2/common jk_logger_file.c
Date Sat, 01 Nov 2003 14:45:26 GMT
mturk       2003/11/01 06:45:26

  Modified:    jk/native2/common jk_logger_file.c
  Log:
  Use the apr file_io for logging.
  
  Revision  Changes    Path
  1.42      +88 -100   jakarta-tomcat-connectors/jk/native2/common/jk_logger_file.c
  
  Index: jk_logger_file.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native2/common/jk_logger_file.c,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- jk_logger_file.c	30 Oct 2003 20:08:36 -0000	1.41
  +++ jk_logger_file.c	1 Nov 2003 14:45:26 -0000	1.42
  @@ -89,40 +89,34 @@
   
   static const char * jk2_logger_file_logFmt = JK_TIME_FORMAT;
   
  -static void jk2_logger_file_setTimeStr(jk_env_t *env,char * str, int len)
  +static void jk2_logger_file_setTimeStr(jk_env_t *env, char *str, int len)
   {
  -    time_t      t = time(NULL);
  -    struct tm   *tms;
  +    apr_time_exp_t gmt;
  +    apr_size_t     l;
   
  -    tms = gmtime(&t);
  -
  -    if( tms==NULL ) {
  -        return;
  -    }
  -
  -    strftime(str, len, jk2_logger_file_logFmt, tms);
  +    apr_time_exp_gmt(&gmt, apr_time_now());
  +    apr_strftime(str, &l, len, jk2_logger_file_logFmt, &gmt);
   }
   
  -static int JK_METHOD jk2_logger_file_log(jk_env_t *env,jk_logger_t *l,                
                
  +static int JK_METHOD jk2_logger_file_log(jk_env_t *env, jk_logger_t *l,               
                 
                                  int level,
                                  const char *what)
   {
  -    FILE *f = (FILE *)l->logger_private;
  +    apr_status_t rv = APR_SUCCESS;
  +    apr_file_t *f = (apr_file_t *)l->logger_private;
   
  -    if( f==NULL ) {
  +    if (f == NULL) {
           /* This is usefull to debug what happens before logger is set.
              On apache you need -X option ( no detach, single process ) */
  -        if( what != NULL ) fprintf(stderr, what );
  +        if (what != NULL)
  +            fprintf(stderr, what);
           return JK_OK;
       }
       if(l && l->level <= level && l->logger_private &&
what) {       
  -        unsigned sz = strlen(what);
  -        if(sz) {
  -            fwrite(what, 1, sz, f);
  -            /* [V] Flush the dam' thing! */
  -            fflush(f);
  -        }
  -
  +        rv = apr_file_puts(what, f);
  +        /* flush the log for each entry only for debug level */
  +        if (rv == APR_SUCCESS && l->level == JK_LOG_DEBUG_LEVEL)
  +            rv = apr_file_flush(f);
           return JK_OK;
       }
   
  @@ -131,73 +125,72 @@
   
   int jk2_logger_file_parseLogLevel(jk_env_t *env, const char *level)
   {
  -    if( level == NULL ) return JK_LOG_INFO_LEVEL;
  +    if (!level)
  +        return JK_LOG_INFO_LEVEL;
       
  -    if(0 == strcasecmp(level, JK_LOG_INFO_VERB)) {
  +    if (!strcasecmp(level, JK_LOG_INFO_VERB))
           return JK_LOG_INFO_LEVEL;
  -    }
   
  -    if(0 == strcasecmp(level, JK_LOG_ERROR_VERB)) {
  +    if (!strcasecmp(level, JK_LOG_ERROR_VERB))
           return JK_LOG_ERROR_LEVEL;
  -    }
   
  -    if(0 == strcasecmp(level, JK_LOG_EMERG_VERB)) {
  +    if (!strcasecmp(level, JK_LOG_EMERG_VERB))
           return JK_LOG_EMERG_LEVEL;
  -    }
   
       return JK_LOG_DEBUG_LEVEL;
   }
   
  -static int JK_METHOD jk2_logger_file_init(jk_env_t *env,jk_logger_t *_this )
  +static int JK_METHOD jk2_logger_file_init(jk_env_t *env, jk_logger_t *_this )
   {
  -    FILE *oldF=(FILE *)_this->logger_private;
  -    FILE *f=NULL;
  -    jk_workerEnv_t *workerEnv=env->getByName( env, "workerEnv" );
  -    if( _this->name==NULL ) {
  +    apr_status_t rv;
  +    apr_file_t *oldF = (apr_file_t *)_this->logger_private;
  +
  +    apr_file_t *f = NULL;
  +    jk_workerEnv_t *workerEnv = env->getByName(env, "workerEnv");
  +    if (!_this->name) {
           _this->name="${serverRoot}/logs/jk2.log";
       }
       _this->name = jk2_config_replaceProperties(env, workerEnv->initData,
                                                  _this->mbean->pool, _this->name);
  -    if( !_this->name || strcmp( "stderr", _this->name )==0 ) {
  -        _this->logger_private = stderr;
  -    } else {
  -    
  -#ifdef AS400
  -        f = fopen(_this->name, "a+, o_ccsid=0");
  -#else
  -        f = fopen(_this->name, "a+");
  -#endif        
  -        
  -        if(f==NULL) {
  +    if (!_this->name || !strcmp("stderr", _this->name)) {
  +         if ((rv = apr_file_open_stderr(&f, env->globalPool->_private)) !=
  +                                        APR_SUCCESS) {
  +            _this->jkLog(env, _this,JK_LOG_ERROR,
  +                         "Can't open stderr file\n");
  +            return JK_ERR;
  +        }
  +        _this->logger_private = f;
  +    } 
  +    else {
  +        if ((rv = apr_file_open(&f, _this->name,  
  +                                APR_APPEND | APR_READ | APR_WRITE | APR_CREATE,
  +                                APR_OS_DEFAULT,
  +                                env->globalPool->_private)) != APR_SUCCESS) {
               _this->jkLog(env, _this,JK_LOG_ERROR,
                            "Can't open log file %s\n", _this->name );
               return JK_ERR;
           }
  -#if defined(F_SETFD) && defined(FD_CLOEXEC)
  -        /* Protect the log file
  -         * so that it will not be inherited by child processes
  -         */
  -        fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
  -#endif
           _this->logger_private = f;
       } 
       _this->jkLog(env, _this,JK_LOG_INFO,
                    "Initializing log file %s\n", _this->name );
  -    if( oldF!=NULL && oldF != stderr) {
  -        fclose( oldF );
  +    if (oldF) {
  +        apr_file_close(oldF);
       }
       return JK_OK;
   }
   
   static int jk2_logger_file_close(jk_env_t *env,jk_logger_t *_this)
   {
  -    FILE *f = _this->logger_private;
  -    if (f == NULL || f != stderr)
  +    apr_status_t rv;
  +    apr_file_t *f = _this->logger_private;
  +    
  +    if (!f)
           return JK_OK;
       
  -    fflush(f);
  -    fclose(f);
  -    _this->logger_private=NULL;
  +    apr_file_flush(f);
  +    rv = apr_file_close(f);
  +    _this->logger_private = NULL;
   
       /*     free(_this); */
       return JK_OK;
  @@ -207,22 +200,22 @@
   jk2_logger_file_setProperty(jk_env_t *env, jk_bean_t *mbean, 
                               char *name,  void *valueP )
   {
  -    jk_logger_t *_this=mbean->object;
  -    char *value=valueP;
  -    if( strcmp( name, "name" )==0 ) {
  -        _this->name=(char *)value;
  -    } else if( strcmp( name, "file" )==0 ) {
  -        _this->name=(char *)value;
  +    jk_logger_t *_this = mbean->object;
  +    char *value = valueP;
  +    if (!strcmp(name, "name"))
  +        _this->name = (char *)value;
  +    else if (!strcmp(name, "file")) {
  +        _this->name = (char *)value;
           /* Set the file imediately */
  -        jk2_logger_file_init(env, (jk_logger_t *)mbean->object );
  -
  -    } else if( strcmp( name, "timeFormat" )==0 ) {
  +        jk2_logger_file_init(env, (jk_logger_t *)mbean->object);
  +    } 
  +    else if (!strcmp(name, "timeFormat"))
           jk2_logger_file_logFmt = value;
  -    } else if( strcmp( name, "level" )==0 ) {
  +    else if (!strcmp(name, "level")) {
           _this->level = jk2_logger_file_parseLogLevel(env, value);
  -        if( _this->level == 0 ) {
  -            _this->jkLog( env, _this, JK_LOG_INFO,
  -                          "Level %s %d \n", value, _this->level );
  +        if( _this->level == 0) {
  +            _this->jkLog(env, _this, JK_LOG_INFO,
  +                         "Level %s %d \n", value, _this->level);
           }
       }
       return JK_OK;
  @@ -238,48 +231,44 @@
       int rc = 0;
       char *buf;
       char *fmt1;
  -    apr_pool_t *aprPool=env->tmpPool->_private;
  +    apr_pool_t *aprPool = env->tmpPool->_private;
       char rfctime[APR_RFC822_DATE_LEN];
       apr_time_t time = apr_time_now();
       
  -    if( !file || !args) {
  +    if (!file || !args)
           return -1;
  -    }
   
  -    if(l->logger_private==NULL ||
  -       l->level <= level) {
  +
  +    if (l->logger_private == NULL ||
  +        l->level <= level) {
           char *f = (char *)(file + strlen(file) - 1);
           char *slevel;
           switch (level){
  -            case JK_LOG_INFO_LEVEL : 
  -                slevel=JK_LOG_INFO_VERB;
  +            case JK_LOG_INFO_LEVEL:
  +                slevel = JK_LOG_INFO_VERB;
                   break;
  -            case JK_LOG_ERROR_LEVEL : 
  -                slevel=JK_LOG_ERROR_VERB;
  +            case JK_LOG_ERROR_LEVEL:
  +                slevel = JK_LOG_ERROR_VERB;
                   break;
  -            case JK_LOG_EMERG_LEVEL : 
  -                slevel=JK_LOG_EMERG_VERB;
  +            case JK_LOG_EMERG_LEVEL:
  +                slevel = JK_LOG_EMERG_VERB;
                   break;
  -            case JK_LOG_DEBUG_LEVEL : 
  +            case JK_LOG_DEBUG_LEVEL:
               default:
  -                slevel=JK_LOG_DEBUG_VERB;
  +                slevel = JK_LOG_DEBUG_VERB;
                   break;
  -
           }
  -        while(f != file && '\\' != *f && '/' != *f) {
  +        while (f != file && *f != '\\' && *f != '/')
               f--;
  -        }
  -        if(f != file) {
  -            f++;
  -        }
  +        if (f != file)
  +            ++f;
           
           /* XXX rfc822_date or apr_ctime ? */
  -        apr_ctime( rfctime, time );
  -        fmt1=apr_psprintf( aprPool, "[%s] (%5s ) [%s (%d)]  %s", rfctime, slevel, f, line,
fmt );
  -        buf=apr_pvsprintf( aprPool, fmt1, args );
  +        apr_ctime(rfctime, time);
  +        fmt1 = apr_psprintf(aprPool, "[%s] (%5s ) [%s (%d)]  %s", rfctime, slevel, f, line,
fmt);
  +        buf = apr_pvsprintf(aprPool, fmt1, args);
   
  -        l->log(env, l, level, buf);
  -        
  +        l->log(env, l, level, buf);        
       }
       
       return rc;
  @@ -296,7 +285,7 @@
       int rc;
       
       va_start(args, fmt);
  -    rc=jk2_logger_file_jkVLog( env, l, file, line, level, fmt, args );
  +    rc = jk2_logger_file_jkVLog(env, l, file, line, level, fmt, args);
       va_end(args);
   
       return rc;
  @@ -308,7 +297,7 @@
   {
       jk_logger_t *log = (jk_logger_t *)pool->calloc(env, pool, sizeof(jk_logger_t));
   
  -    if(log==NULL ) {
  +    if (log == NULL) {
           fprintf(stderr, "loggerFile.factory(): OutOfMemoryException\n"); 
           return JK_ERR;
       }
  @@ -318,14 +307,13 @@
       log->init =jk2_logger_file_init;
       log->jkLog = jk2_logger_file_jkLog;
       log->jkVLog = jk2_logger_file_jkVLog;
  -    log->level=JK_LOG_ERROR_LEVEL;
  +    log->level = JK_LOG_ERROR_LEVEL;
       jk2_logger_file_logFmt = JK_TIME_FORMAT;
       
  -    result->object=log;
  -    log->mbean=result;
  +    result->object = log;
  +    log->mbean = result;
       
       result->setAttribute = jk2_logger_file_setProperty;
   
       return JK_OK;
   }
  -
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message