/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jtsan.writers;

import org.jtsan.EventType;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * An event writer to dump string output to a file along with the binary output.
 * Useful for debugging.
 *
 * @author: Sergey Vorobyev
 */
public class BinAndStrEventWriter implements EventWriter {

  BinaryEventWriter binWriter;
  StringEventWriter strWriter;

  // StrWriter write events to this file.
  public static final String STR_OUT_FILE = "jtsan.events.debug";

  public BinAndStrEventWriter() {
    binWriter = new BinaryEventWriter();
    strWriter = new StringEventWriter();
    try {
      strWriter.setOutputStream(new FileOutputStream(STR_OUT_FILE));
    } catch (FileNotFoundException e) {
      throw new RuntimeException("Failed to open file for writing: " + STR_OUT_FILE, e);
    }
  }

  public void setOutputStream(OutputStream outputStream) {
    binWriter.setOutputStream(outputStream);
  }

  public void writeEvent(EventType type, long tid, long pc, long address, long extra) {
    synchronized (this) {
      binWriter.writeEvent(type, tid, pc, address, extra);
      strWriter.writeEvent(type, tid, pc, address, extra);
    }
  }

  public void writeCodePosition(long pc, String descr) {
    synchronized (this) {
      binWriter.writeCodePosition(pc, descr);
      strWriter.writeCodePosition(pc, descr);
    }
  }

  public void writeComment(String str, long pc) {
    synchronized (this) {
      binWriter.writeComment(str, pc);
      strWriter.writeComment(str, pc);
    }
  }

}

@@ -1570,32 +1570,45 @@
 ;%0A  %7D%0A%0A  public 
+synchronized 
 void writeEvent(
@@ -1674,36 +1674,8 @@
 ) %7B%0A
-    synchronized (this) %7B%0A  
     
@@ -1727,26 +1727,24 @@
 extra);%0A    
- 
- 
 strWriter.wr
@@ -1784,22 +1784,16 @@
 extra);%0A
-    %7D%0A
   %7D%0A%0A  p
@@ -1790,32 +1790,45 @@
 ;%0A  %7D%0A%0A  public 
+synchronized 
 void writeCodePo
@@ -1863,36 +1863,8 @@
 ) %7B%0A
-    synchronized (this) %7B%0A  
     
@@ -1903,18 +1903,16 @@
 descr);%0A
-  
     strW
@@ -1947,22 +1947,16 @@
 descr);%0A
-    %7D%0A
   %7D%0A%0A  p
@@ -1957,24 +1957,37 @@
 %7D%0A%0A  public 
+synchronized 
 void writeCo
@@ -2019,36 +2019,8 @@
 ) %7B%0A
-    synchronized (this) %7B%0A  
     
@@ -2052,26 +2052,24 @@
 r, pc);%0A    
-  
 strWriter.wr
@@ -2089,21 +2089,15 @@
 r, pc);%0A
-    %7D%0A
   %7D%0A%0A%7D%0A

Remove lame synchronization