You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by ca...@apache.org on 2005/03/10 20:47:42 UTC

cvs commit: logging-log4cxx/tests/src/util transformer.cpp transformer.h

carnold     2005/03/10 11:47:42

  Modified:    tests/src/util transformer.cpp transformer.h
  Log:
  LOGCXX-54: avoid sed -i not supported on Mac
  
  Revision  Changes    Path
  1.16      +70 -48    logging-log4cxx/tests/src/util/transformer.cpp
  
  Index: transformer.cpp
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/tests/src/util/transformer.cpp,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- transformer.cpp	1 Mar 2005 23:30:45 -0000	1.15
  +++ transformer.cpp	10 Mar 2005 19:47:41 -0000	1.16
  @@ -58,10 +58,7 @@
   }
   
   
  -void Transformer::transform(const File& in, const File& out,
  -        const log4cxx::Filter::PatternList& patterns)
  -{
  -    {
  +void Transformer::copyFile(const File& in, const File& out) {
          apr_pool_t* pool;
          apr_status_t stat = apr_pool_create(&pool, NULL);
   
  @@ -74,12 +71,12 @@
           apr_int32_t flags = APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE;
           stat = apr_file_open(&child_out, out.getOSName().c_str(),
             flags, APR_OS_DEFAULT, pool);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
   
           apr_file_t* in_file;
           stat = apr_file_open(&in_file, in.getOSName().c_str(),
              APR_FOPEN_READ, APR_OS_DEFAULT, pool);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
           apr_size_t bufsize = 32000;
           void* buf = apr_palloc(pool, bufsize);
           apr_size_t bytesRead = bufsize;
  @@ -88,30 +85,75 @@
               stat = apr_file_read(in_file, buf, &bytesRead);
               if (stat == 0 && bytesRead > 0) {
                   stat = apr_file_write(child_out, buf, &bytesRead);
  -                assert(stat == 0);
  +                assert(stat == APR_SUCCESS);
               }
           }
           stat = apr_file_close(child_out);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
           apr_pool_destroy(pool);
  -     }
  +}
  +
  +void Transformer::createSedCommandFile(const std::string& regexName, 
  +        const log4cxx::Filter::PatternList& patterns,
  +        apr_pool_t* pool) {
  +        apr_file_t* regexFile;
  +        apr_status_t stat = apr_file_open(&regexFile,
  +               regexName.c_str(),
  +               APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE, APR_OS_DEFAULT,
  +               pool);
   
  -     if (patterns.size() > 0) {
  -        apr_pool_t* pool;
  +        std::string tmp;
  +        for (log4cxx::Filter::PatternList::const_iterator iter = patterns.begin();
  +          iter != patterns.end();
  +          iter++) {
  +          tmp = "sQ";
  +          tmp.append(iter->first);
  +          tmp.append(1, 'Q');
  +          tmp.append(iter->second);
  +          tmp.append("Qg\n");
  +          apr_file_puts(tmp.c_str(), regexFile);
  +        }
  +        apr_file_close(regexFile);        
  +}
  +
  +void Transformer::transform(const File& in, const File& out,
  +        const log4cxx::Filter::PatternList& patterns)
  +{
  +    //
  +    //   if no patterns just copy the file
  +    //
  +    if (patterns.size() == 0) {
  +        copyFile(in, out);
  +    } else {
  +       apr_pool_t* pool;
           apr_status_t stat = apr_pool_create(&pool, NULL);
  +        
  +        //
  +        //   write the regex's to a temporary file since they
  +        //      may get mangled if passed as parameters
  +        //
  +        std::string regexName(in.getOSName());
  +        regexName.append(".sed");
  +        createSedCommandFile(regexName, patterns, pool);
  +        
  +        
           //
  -        //   if there are patterns, invoke sed to execute the replacements
  +        //  prepare to launch sed
           //
           //
           apr_procattr_t* attr = NULL;
           stat = apr_procattr_create(&attr, pool);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
  +
  +        stat = apr_procattr_io_set(attr, APR_NO_PIPE, APR_FULL_BLOCK,
  +                             APR_FULL_BLOCK);
  +        assert(stat == APR_SUCCESS);
   
           //
           //   find the program on the path
           //
           stat = apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
   
           //
           //   build the argument list
  @@ -127,70 +169,50 @@
           //
           args[i++] = "sed";
   
  -        //
  -        //   write the regex's to a temporary file since they
  -        //      may get mangled if passed as parameters
  -        //
  -        std::string regexName(in.getOSName());
  -        regexName.append(".sed");
   
           std::string regexArg("-f");
           regexArg.append(regexName);
           args[i++] = regexArg.c_str();
   
  -        args[i++] = "-i";
           //
           //    specify the input file
  -        args[i++] = out.getOSName().c_str();
  +        args[i++] = in.getOSName().c_str();
           args[i] = NULL;
   
   
  -        apr_file_t* regexFile;
  -        stat = apr_file_open(&regexFile,
  -               regexName.c_str(),
  -               APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE, APR_OS_DEFAULT,
  -               pool);
  -
  -        std::string tmp;
  -        for (log4cxx::Filter::PatternList::const_iterator iter = patterns.begin();
  -          iter != patterns.end();
  -          iter++) {
  -          tmp = "sQ";
  -          tmp.append(iter->first);
  -          tmp.append(1, 'Q');
  -          tmp.append(iter->second);
  -          tmp.append("Qg\n");
  -          apr_file_puts(tmp.c_str(), regexFile);
  -        }
  -        apr_file_close(regexFile);
  -
   
           //
  -        //    set the output stream to go to the console
  +        //    set the output stream to the filtered file
           //
           apr_file_t* child_out;
  -        stat = apr_file_open_stdout(&child_out, pool);
  -        assert(stat == 0);
  +        apr_int32_t flags = APR_FOPEN_READ | APR_FOPEN_WRITE |
  +            APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE;
  +        stat = apr_file_open(&child_out, out.getOSName().c_str(),
  +          flags, APR_OS_DEFAULT, pool);
  +        assert(stat == APR_SUCCESS);
   
           stat =  apr_procattr_child_out_set(attr, child_out, NULL);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
   
           //
  -        //   redirect the child's error stream this processes
  +        //   redirect the child's error stream to this processes' error stream
           //
           apr_file_t* child_err;
           stat = apr_file_open_stderr(&child_err, pool);
           assert(stat == 0);
           stat =  apr_procattr_child_err_set(attr, child_err, NULL);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
   
   
   
           apr_proc_t pid;
           stat = apr_proc_create(&pid,"sed", args, NULL, attr, pool);
  -        assert(stat == 0);
  +        assert(stat == APR_SUCCESS);
   
           apr_proc_wait(&pid, NULL, NULL, APR_WAIT);
  +        stat = apr_file_close(child_out);
  +        assert(stat == APR_SUCCESS);
  +
           apr_pool_destroy(pool);
   
        }
  
  
  
  1.7       +11 -0     logging-log4cxx/tests/src/util/transformer.h
  
  Index: transformer.h
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/tests/src/util/transformer.h,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- transformer.h	28 Jan 2005 17:03:37 -0000	1.6
  +++ transformer.h	10 Mar 2005 19:47:41 -0000	1.7
  @@ -20,6 +20,10 @@
   #include "filter.h"
   #include <vector>
   
  +extern "C" {
  +struct apr_pool_t;
  +}
  +
   namespace log4cxx
   {
          class File;
  @@ -38,6 +42,13 @@
                   static void transform(const File& in,
                                         const File& out,
                                         const std::vector< log4cxx::Filter::PatternReplacement >& patterns);
  +        private:
  +                static void copyFile(const File& in,
  +                                      const File& out);
  +                static void createSedCommandFile(const std::string& regexName, 
  +                    const log4cxx::Filter::PatternList& patterns,
  +                    apr_pool_t* pool);
  +        
           };
   }