Example #1
0
  protected int encodeLine(
      MessageTree tree,
      Message message,
      ChannelBuffer buf,
      char type,
      Policy policy,
      int level,
      LineCounter counter) {
    BufferHelper helper = m_bufferHelper;
    int count = 0;

    if (counter != null) {
      counter.inc();

      count += helper.tr1(buf, counter.getCount() % 2 != 0 ? "odd" : "even");
    } else {
      count += helper.tr1(buf, null);
    }

    count += helper.td1(buf);

    count += helper.nbsp(buf, level * 2); // 2 spaces per level
    count += helper.write(buf, (byte) type);

    if (type == 'T' && message instanceof Transaction) {
      long duration = ((Transaction) message).getDurationInMillis();

      count += helper.write(buf, m_dateHelper.format(message.getTimestamp() + duration));
    } else {
      count += helper.write(buf, m_dateHelper.format(message.getTimestamp()));
    }

    count += helper.td2(buf);

    count += helper.td(buf, message.getType());
    count += helper.td(buf, message.getName());

    if (policy != Policy.WITHOUT_STATUS) {
      if (Message.SUCCESS.equals(message.getStatus())) {
        count += helper.td(buf, " "); // do not output "0"
      } else {
        count += helper.td(buf, message.getStatus(), "class=\"error\"");
      }

      Object data = message.getData();

      count += helper.td1(buf);

      if (policy == Policy.WITH_DURATION && message instanceof Transaction) {
        long durationInMicro = ((Transaction) message).getDurationInMicros();
        long durationInMillis = durationInMicro / 1000L;

        if (durationInMicro < 100L) {
          count += helper.write(buf, String.format("%.2f", durationInMicro / 1000.0));
        } else if (durationInMicro < 10000L) { // less than 10 ms
          count += helper.write(buf, String.format("%.2f", durationInMicro / 1000.0));
        } else { // no fraction
          count += helper.write(buf, Long.toString(durationInMillis));
        }

        count += helper.write(buf, "ms ");
      }

      count += helper.writeRaw(buf, String.valueOf(data));
      count += helper.td2(buf);
    } else {
      count += helper.td(buf, "");
      count += helper.td(buf, "");
    }

    count += helper.tr2(buf);
    count += helper.crlf(buf);

    return count;
  }