Пример #1
0
  private Packet parseAttrs(String log) {
    Packet pkt = new Packet();
    log = Util.strip(log);

    // Extract prefix
    int idx = log.indexOf("IN=");
    if (idx > 0) {
      pkt.prefix = log.substring(0, idx);
      log = log.substring(idx);
    }
    pkt.hash = log.hashCode();

    // Extract referenced packet
    idx = log.indexOf('[');
    if (idx > 0) {
      int idx2 = log.lastIndexOf(']');
      if (idx2 > idx) {
        pkt.ref = parseAttrs(log.substring(idx + 1, idx2 - 1));
      }
      log = log.substring(0, idx - 1);
    }

    // Split log line and extract attributes and flags
    String fields[] = log.split(" ");
    for (String f : fields) {
      if (f.contains("=")) {
        String kv[] = f.split("=", 2);
        pkt.put(kv[0], kv[1]);
      } else {
        pkt.addFlag(f);
      }
    }

    // Check parsed packet
    pkt.check();
    return pkt;
  }
Пример #2
0
  /** Parse a log line from logs generated by crash */
  private boolean parseFmtCrash(BugReportModule br, String line, LogLine prev) {
    if (line.length() <= 33) return false; // just some sane value... maybe it should be 23?

    // Do some initial verification
    boolean newLine = true;
    char c = line.charAt(8);
    if (c != '[') newLine = false;
    c = line.charAt(16);
    if (c != '.') newLine = false;
    c = line.charAt(23);
    if (c != ']') newLine = false;

    if (!newLine) {
      // Continuation of the previous one
      if (prev == null) {
        // No previous line, we cannot continue, ignore
        return false;
      }
      // Copy the important fields from the previous line
      level = prev.level;
      ts = prev.ts;
      pidS = prev.pidS;
      pidE = prev.pidE;
      pid = prev.pid;
      tagS = prev.tagS;
      tagE = prev.tagE;
      tag = prev.tag;
      msgS = prev.msgS;
      msgE = msgS + line.length();
      msg = line;
      line = prev.line.substring(0, prev.msgS) + msg;
      this.line = line;
    } else {
      // This will hold the rebuilt line
      StringBuffer sb = new StringBuffer();

      // Parse the level
      level = line.charAt(0);
      if (level == ' ') {
        level = line.charAt(2);
      }
      if (level == ' ') {
        level = line.charAt(3);
      }
      if (level == ' ') {
        level = 'I';
      }

      // Parse the timestamp
      try {
        int sec = Integer.parseInt(Util.strip(line.substring(9, 16)));
        int usec = Integer.parseInt(Util.strip(line.substring(17, 23)));
        ts = sec * 1000 + usec / 1000;
        sb.append(
            String.format(
                "%02d:%02d:%02d.%03d ", (sec / 3600) % 24, (sec / 60) % 60, sec % 60, usec / 1000));
      } catch (NumberFormatException e) {
        return false; // wrong ts? or wrong format?
      }

      // Parse pid
      pidS = line.indexOf('(');
      pidE = line.indexOf(':', pidS);
      if (pidS < 0 || pidE < 0) {
        return false; // wrong format?
      }
      pidS++;
      try {
        pid = Integer.parseInt(line.substring(pidS, pidE));
      } catch (NumberFormatException t) {
        return false; // strange pid
      }

      // Parse tag
      tagS = line.indexOf(')');
      if (tagS < 0) return false;
      tagS += 2;
      tagE =
          line.indexOf(
              " ",
              tagS); // This might not work for tags which actually contain a space, but there is no
                     // more reliable way
      if (tagE < 0) return false;
      tag = line.substring(tagS, tagE);

      // This could be an event tag, extract the tag id
      int idx0 = tag.indexOf('(');
      int idx1 = tag.indexOf(')');
      try {
        if (idx0 == -1 && idx1 == -1) {
          tagId = Integer.parseInt(tag);
        } else if (idx0 != -1 && idx1 != -1 && idx0 < idx1) {
          tagId = Integer.parseInt(tag.substring(idx0 + 1, idx1));
        }
      } catch (NumberFormatException nfe) {
        /* ignore */
      }

      // Read message
      int idx = tagE + 1;
      while (idx < line.length() && line.charAt(idx) == ' ') {
        idx++;
      }
      if (idx < line.length()) {
        msg = line.substring(idx);
        msgS = idx;
        msgE = line.length();
      } else {
        msg = "";
        msgS = msgE = line.length();
      }

      // Finish reconstruction
      sb.append(level);
      sb.append('/');
      tagS = sb.length();
      sb.append(tag);
      tagE = sb.length();
      sb.append('(');
      pidS = sb.length();
      sb.append(pid);
      pidE = sb.length();
      sb.append("): ");
      msgS = sb.length();
      sb.append(msg);
      msgE = sb.length();
      line = sb.toString();
      this.line = line;
    }

    finishParse(br);

    // Override field parsing (Do some basic field parsing... very basic)
    if (msg != null) {
      fields = msg.split(",");
      for (int i = 0; i < fields.length; i++) {
        fields[i] = Util.strip(fields[i]);
      }
    }

    fmt = FMT_CRASH;
    return true;
  }