Ejemplo n.º 1
0
 bool get(LogString& in, logchar& c)
 {
         if (in.empty()) {
             c = 0;
             return false;
         }
         c = in[0];
         in.erase(in.begin());
         return true;
 }
Ejemplo n.º 2
0
    /**
     * Abbreviate element of name.
     * @param buf buffer to receive element.
     * @param startPos starting index of name element.
     * @return starting index of next element.
     */
    LogString::size_type abbreviate(LogString& buf, LogString::size_type startPos) const {
      LogString::size_type nextDot = buf.find(0x2E /* '.' */, startPos);

      if (nextDot != LogString::npos) {
        if ((nextDot - startPos) > charCount) {
          buf.erase(buf.begin() + (startPos + charCount), buf.begin() + nextDot);
          nextDot = startPos + charCount;

          if (ellipsis != 0x00) {
            buf.insert(nextDot, 1, ellipsis);
            nextDot++;
          }
        }

        nextDot++;
      }

      return nextDot;
    }
Ejemplo n.º 3
0
/**
 * Purge and rename old log files in preparation for rollover
 * @param lowIndex low index
 * @param highIndex high index.  Log file associated with high
 * index will be deleted if needed.
 * @return true if purge was successful and rollover should be attempted.
 */
bool FixedWindowRollingPolicy::purge(int lowIndex, int highIndex, Pool& p) const {
  int suffixLength = 0;

  std::vector<FileRenameActionPtr> renames;
  LogString buf;
  ObjectPtr obj = new Integer(lowIndex);
  formatFileName(obj, buf, p);

  LogString lowFilename(buf);

  if (lowFilename.compare(lowFilename.length() - 3, 3, LOG4CXX_STR(".gz")) == 0) {
    suffixLength = 3;
  } else if (lowFilename.compare(lowFilename.length() - 4, 4, LOG4CXX_STR(".zip")) == 0) {
    suffixLength = 4;
  }

  for (int i = lowIndex; i <= highIndex; i++) {
    File toRenameCompressed;
    toRenameCompressed.setPath(lowFilename);
    File toRenameBase;
    toRenameBase.setPath(lowFilename.substr(0, lowFilename.length() - suffixLength));
    File* toRename = &toRenameCompressed;
    bool isBase = false;
    bool exists = toRenameCompressed.exists(p);

    if (suffixLength > 0) {
      if (exists) {
        if (toRenameBase.exists(p)) {
          toRenameBase.deleteFile(p);
        }
      } else {
        toRename = &toRenameBase;
        exists = toRenameBase.exists(p);
        isBase = true;
      }
    }

    if (exists) {
      //
      //    if at upper index then
      //        attempt to delete last file
      //        if that fails then abandon purge
      if (i == highIndex) {
        if (!toRename->deleteFile(p)) {
          return false;
        }

        break;
      }

      //
      //   if intermediate index
      //     add a rename action to the list
      buf.erase(buf.begin(), buf.end());
      obj = new Integer(i + 1);
      formatFileName(obj, buf, p);

      LogString highFilename(buf);
      LogString renameTo(highFilename);

      if (isBase) {
        renameTo =
          highFilename.substr(0, highFilename.length() - suffixLength);
      }

      renames.push_back(new FileRenameAction(*toRename, File().setPath(renameTo), true));
      lowFilename = highFilename;
    } else {
      break;
    }
  }

  //
  //   work renames backwards
  //
  for(std::vector<FileRenameActionPtr>::reverse_iterator iter = renames.rbegin();
      iter != renames.rend();
      iter++) {

     try {
         if (!(*iter)->execute(p)) {
            return false;
         }
     } catch (std::exception& ) {
        LogLog::warn(LOG4CXX_STR("Exception during purge in RollingFileAppender"));

        return false;
     }
  }
  return true;
}