Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.IO;
- namespace LogcatViewer
- {
- public partial class MainView : Form
- {
- enum LoggingMode
- {
- START,
- PAUSE,
- STOP
- };
- LoggingMode loggingmode_ = LoggingMode.STOP;
- Thread threadLogging_=null;
- System.Diagnostics.Process process_ = null;
- public MainView()
- {
- InitializeComponent();
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- btnStart.Enabled = false;
- btnStop.Enabled = true;
- checkBoxPause.Checked = false;
- Console.Out.WriteLine("logcat start");
- threadLogging_ = new Thread(new ThreadStart(CaptureLogcat));
- threadLogging_.Start();
- }
- delegate void LoggingDelegate(String logstr);
- void Logging(String logstr)
- {
- textBoxLog.AppendText(logstr + "\r\n");
- textBoxLog.SelectionStart = textBoxLog.Text.Length;
- //テキストボックスにフォーカスを移動
- textBoxLog.Focus();
- //カレット位置までスクロール
- textBoxLog.ScrollToCaret();
- }
- delegate void LoggingCSVDelegate(String logstr);
- void LoggingCSV(String logstr)
- {
- textBoxCSV.AppendText(logstr + "\r\n");
- textBoxCSV.SelectionStart = textBoxCSV.Text.Length;
- //テキストボックスにフォーカスを移動
- textBoxCSV.Focus();
- //カレット位置までスクロール
- textBoxCSV.ScrollToCaret();
- }
- void CaptureLogcat()
- {
- loggingmode_ = LoggingMode.START;
- System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
- //ComSpecのパスを取得する
- psi.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
- //出力を読み取れるようにする
- psi.RedirectStandardInput = false;
- psi.RedirectStandardOutput = true;
- psi.UseShellExecute = false;
- //ウィンドウを表示しないようにする
- psi.CreateNoWindow = true;
- System.Diagnostics.Process p;
- //
- if (textBoxCmdLogStart.Text != "")
- {
- //コマンドラインを指定("/c"は実行後閉じるために必要)
- psi.Arguments = textBoxCmdLogStart.Text;
- //起動
- p = System.Diagnostics.Process.Start(psi);
- p.WaitForExit();
- p.Close();
- }
- //コマンドラインを指定("/c"は実行後閉じるために必要)
- psi.Arguments = textBoxCmdLog.Text;
- //起動
- p = System.Diagnostics.Process.Start(psi);
- process_ = p;
- // StreamReader sr = new StreamReader(p.StandardOutput.BaseStream,System.Text.Encoding.GetEncoding("UTF-8"));
- byte[] readbuf = new byte[65536];
- int readpos = 0;
- string logstr;
- //出力を読み取る
- while (loggingmode_ != LoggingMode.STOP)
- {
- while (checkBoxPause.Checked)
- {
- Thread.Sleep(500);
- }
- int readbyte = -1;
- // p.StandardOutput.BaseStream.ReadTimeout = 100;
- if (p.StandardOutput.BaseStream.CanRead ) {
- readbyte = p.StandardOutput.BaseStream.ReadByte();
- // p.StandardOutput.ReadLine();
- }
- if (readbyte >= 0)
- {
- if (readpos < 65525) readbuf[readpos++] = (byte)readbyte;
- if ((readbyte == (byte)0x0a) || (readbyte == (byte)0x0d))
- {
- Encoding sjisEnc = Encoding.GetEncoding("UTF-8");
- logstr = sjisEnc.GetString(readbuf, 0, readpos - 1);
- readpos = 0;
- if (logstr != "")
- {
- if (textBoxLogFilter.Text == "" || logstr.IndexOf(textBoxLogFilter.Text) >= 0)
- {
- Invoke(new LoggingDelegate(Logging), logstr);
- }
- if (textBoxCSVTAG.Text != "")
- {
- int i = logstr.IndexOf(textBoxCSVTAG.Text);
- if (i >= 0)
- {
- Invoke(new LoggingCSVDelegate(LoggingCSV), logstr.Substring(i + textBoxCSVTAG.Text.Length));
- }
- }
- }
- }
- }
- else {
- Thread.Sleep(10);
- }
- // logstr = p.StandardOutput.ReadLine();
- if (loggingmode_ == LoggingMode.STOP) break;
- }
- //WaitForExitはReadToEndの後である必要がある
- //(親プロセス、子プロセスでブロック防止のため)
- // p.WaitForExit();
- // sr.Close();
- p.WaitForExit();
- p.Close();
- //出力された結果を表示
- Console.WriteLine("STOP");
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- loggingmode_ = LoggingMode.STOP;
- btnStart.Enabled = true;
- btnStop.Enabled = false;
- // いろいろやって終了しようとしている
- process_.StandardOutput.BaseStream.Close();
- process_.Kill();
- process_ = null;
- threadLogging_.Join(5000);
- }
- private void btnPause_Click(object sender, EventArgs e)
- {
- }
- private void checkBoxShotSetting_CheckedChanged(object sender, EventArgs e)
- {
- groupBoxSetting.Visible = checkBoxShotSetting.Checked;
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- textBoxLog.Text = "";
- }
- private void btnCopy_Click(object sender, EventArgs e)
- {
- Clipboard.SetData(DataFormats.Text, textBoxLog.Text);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- checkBoxShotSetting.Checked = false;
- }
- private void MainView_Load(object sender, EventArgs e)
- {
- }
- private void groupBoxSetting_Enter(object sender, EventArgs e)
- {
- }
- private void btnClearCSV_Click(object sender, EventArgs e)
- {
- textBoxCSV.Text = "";
- }
- private void btnSaveCSV_Click(object sender, EventArgs e)
- {
- try
- {
- StreamWriter writer = new StreamWriter(textBoxCSVFilename.Text,
- false, // 上書き ( true = 追加 )
- Encoding.GetEncoding("Shift_JIS"));
- writer.WriteLine(textBoxCSV.Text);
- writer.Close();
- if (checkBoxOpenAtCSVSave.Checked)
- {
- System.Diagnostics.Process.Start(textBoxCSVFilename.Text);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "CSVファイル保存・実行失敗");
- }
- }
- private void MainView_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (loggingmode_ != LoggingMode.STOP)
- {
- checkBoxPause.Checked = false;
- loggingmode_ = LoggingMode.STOP;
- // いろいろやって終了しようとしている
- Thread.Sleep(2000);
- process_.Kill();
- process_ = null;
- threadLogging_.Abort();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement