package com.qiyou.mb.android.service;

  import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.utils.log.QLog;

import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;

public class LogService extends IntentService {
public static String TAG="com.qiyou.mb.android.service.LOGSERVICE";
	public LogService(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	public LogService() {
		super(TAG);
		// TODO Auto-generated constructor stub
	}
	String shell = "logcat | grep com.qiyou";	
	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		if(intent.getExtras()!=null)
		shell = intent.getExtras().get(QiYouConstant.EXTRA_KEY_LOG_SHELL).toString();
	}
	public void exportLog()
	{
//	String shell = "logcat | grep ^E.com.qiyou.mb.android";	
	
	
    try
    {
        Process process = Runtime.getRuntime().exec(shell);
        InputStream inputStream = process.getInputStream();
        
        
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
        File dir = null;
        if (sdCardExist)
        {
//            dir = new File(Environment.getExternalStorageDirectory().toString()
//                    + File.separator + "logcat-qiyou"+System.currentTimeMillis()+".txt");
              dir = new File("/sdcard/"+"logcat-qiyou"+System.currentTimeMillis()+".log");// 创建目录  	    	 
            if (!dir.exists())
            {
                dir.createNewFile();
                QLog.getLogger().d(TAG, String.format("LOGCAT file %s created",dir) );
            }

        }
        byte[] buffer = new byte[1024];
        int bytesLeft = 5 * 1024 * 1024; // Or whatever
        try
        {
            FileOutputStream fos = new FileOutputStream(dir);
            try
            {
                while (bytesLeft > 0)
                {
                    int read = inputStream.read(buffer, 0, Math.min(bytesLeft,
                            buffer.length));
                    if (read == -1)
                    {
                        throw new EOFException("Unexpected end of data");
                    }
                    fos.write(buffer, 0, read);
                    bytesLeft -= read;
//                    QLog.getLogger().d(TAG, String.format("LOGCAT file %s content write",read) );
                }
            } finally
            {
                fos.close(); // Or use Guava's
                             // Closeables.closeQuietly,
                // or try-with-resources in Java 7
            }
        } finally
        {
            inputStream.close();
        }
//        String logcat = convertStreamToString(inputStream);
//        outputFile2SdTest(logcat, "logwyx.txt");
        QLog.getLogger().d(TAG, "LOGCAT = ok" );
    } catch (IOException e)
    {
        e.printStackTrace();
    }
	}
}
