Advertisement
fslasht

adb logcatwo

Mar 14th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Threading;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11.  
  12. namespace LogcatViewer
  13. {
  14. public partial class MainView : Form
  15. {
  16. enum LoggingMode
  17. {
  18. START,
  19. PAUSE,
  20. STOP
  21. };
  22. LoggingMode loggingmode_ = LoggingMode.STOP;
  23.  
  24.  
  25. Thread threadLogging_=null;
  26. System.Diagnostics.Process process_ = null;
  27.  
  28. public MainView()
  29. {
  30. InitializeComponent();
  31. }
  32.  
  33. private void btnStart_Click(object sender, EventArgs e)
  34. {
  35. btnStart.Enabled = false;
  36. btnStop.Enabled = true;
  37. checkBoxPause.Checked = false;
  38.  
  39. Console.Out.WriteLine("logcat start");
  40. threadLogging_ = new Thread(new ThreadStart(CaptureLogcat));
  41. threadLogging_.Start();
  42.  
  43.  
  44.  
  45. }
  46.  
  47.  
  48.  
  49. delegate void LoggingDelegate(String logstr);
  50.  
  51. void Logging(String logstr)
  52. {
  53. textBoxLog.AppendText(logstr + "\r\n");
  54.  
  55. textBoxLog.SelectionStart = textBoxLog.Text.Length;
  56. //テキストボックスにフォーカスを移動
  57. textBoxLog.Focus();
  58. //カレット位置までスクロール
  59. textBoxLog.ScrollToCaret();
  60. }
  61.  
  62.  
  63. delegate void LoggingCSVDelegate(String logstr);
  64.  
  65. void LoggingCSV(String logstr)
  66. {
  67. textBoxCSV.AppendText(logstr + "\r\n");
  68.  
  69. textBoxCSV.SelectionStart = textBoxCSV.Text.Length;
  70. //テキストボックスにフォーカスを移動
  71. textBoxCSV.Focus();
  72. //カレット位置までスクロール
  73. textBoxCSV.ScrollToCaret();
  74. }
  75.  
  76. void CaptureLogcat()
  77. {
  78. loggingmode_ = LoggingMode.START;
  79.  
  80. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
  81.  
  82. //ComSpecのパスを取得する
  83. psi.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
  84.  
  85. //出力を読み取れるようにする
  86. psi.RedirectStandardInput = false;
  87. psi.RedirectStandardOutput = true;
  88. psi.UseShellExecute = false;
  89.  
  90. //ウィンドウを表示しないようにする
  91. psi.CreateNoWindow = true;
  92.  
  93. System.Diagnostics.Process p;
  94.  
  95. //
  96. if (textBoxCmdLogStart.Text != "")
  97. {
  98. //コマンドラインを指定("/c"は実行後閉じるために必要)
  99. psi.Arguments = textBoxCmdLogStart.Text;
  100. //起動
  101. p = System.Diagnostics.Process.Start(psi);
  102. p.WaitForExit();
  103. p.Close();
  104. }
  105.  
  106.  
  107. //コマンドラインを指定("/c"は実行後閉じるために必要)
  108. psi.Arguments = textBoxCmdLog.Text;
  109. //起動
  110. p = System.Diagnostics.Process.Start(psi);
  111.  
  112. process_ = p;
  113.  
  114.  
  115. // StreamReader sr = new StreamReader(p.StandardOutput.BaseStream,System.Text.Encoding.GetEncoding("UTF-8"));
  116.  
  117.  
  118. byte[] readbuf = new byte[65536];
  119. int readpos = 0;
  120.  
  121. string logstr;
  122. //出力を読み取る
  123. while (loggingmode_ != LoggingMode.STOP)
  124. {
  125. while (checkBoxPause.Checked)
  126. {
  127. Thread.Sleep(500);
  128. }
  129.  
  130.  
  131. int readbyte = -1;
  132. // p.StandardOutput.BaseStream.ReadTimeout = 100;
  133. if (p.StandardOutput.BaseStream.CanRead ) {
  134. readbyte = p.StandardOutput.BaseStream.ReadByte();
  135. // p.StandardOutput.ReadLine();
  136. }
  137. if (readbyte >= 0)
  138. {
  139. if (readpos < 65525) readbuf[readpos++] = (byte)readbyte;
  140.  
  141. if ((readbyte == (byte)0x0a) || (readbyte == (byte)0x0d))
  142. {
  143. Encoding sjisEnc = Encoding.GetEncoding("UTF-8");
  144. logstr = sjisEnc.GetString(readbuf, 0, readpos - 1);
  145. readpos = 0;
  146.  
  147. if (logstr != "")
  148. {
  149.  
  150.  
  151.  
  152.  
  153. if (textBoxLogFilter.Text == "" || logstr.IndexOf(textBoxLogFilter.Text) >= 0)
  154. {
  155. Invoke(new LoggingDelegate(Logging), logstr);
  156. }
  157. if (textBoxCSVTAG.Text != "")
  158. {
  159. int i = logstr.IndexOf(textBoxCSVTAG.Text);
  160. if (i >= 0)
  161. {
  162. Invoke(new LoggingCSVDelegate(LoggingCSV), logstr.Substring(i + textBoxCSVTAG.Text.Length));
  163.  
  164. }
  165. }
  166. }
  167.  
  168. }
  169.  
  170. }
  171. else {
  172. Thread.Sleep(10);
  173. }
  174. // logstr = p.StandardOutput.ReadLine();
  175.  
  176. if (loggingmode_ == LoggingMode.STOP) break;
  177.  
  178. }
  179.  
  180. //WaitForExitはReadToEndの後である必要がある
  181. //(親プロセス、子プロセスでブロック防止のため)
  182. // p.WaitForExit();
  183.  
  184. // sr.Close();
  185. p.WaitForExit();
  186. p.Close();
  187.  
  188. //出力された結果を表示
  189. Console.WriteLine("STOP");
  190. }
  191.  
  192. private void btnStop_Click(object sender, EventArgs e)
  193. {
  194. loggingmode_ = LoggingMode.STOP;
  195.  
  196.  
  197. btnStart.Enabled = true;
  198. btnStop.Enabled = false;
  199.  
  200. // いろいろやって終了しようとしている
  201. process_.StandardOutput.BaseStream.Close();
  202.  
  203. process_.Kill();
  204. process_ = null;
  205. threadLogging_.Join(5000);
  206.  
  207. }
  208.  
  209. private void btnPause_Click(object sender, EventArgs e)
  210. {
  211.  
  212.  
  213. }
  214.  
  215. private void checkBoxShotSetting_CheckedChanged(object sender, EventArgs e)
  216. {
  217. groupBoxSetting.Visible = checkBoxShotSetting.Checked;
  218. }
  219.  
  220. private void btnClear_Click(object sender, EventArgs e)
  221. {
  222. textBoxLog.Text = "";
  223.  
  224. }
  225.  
  226. private void btnCopy_Click(object sender, EventArgs e)
  227. {
  228. Clipboard.SetData(DataFormats.Text, textBoxLog.Text);
  229.  
  230. }
  231.  
  232. private void button1_Click(object sender, EventArgs e)
  233. {
  234. checkBoxShotSetting.Checked = false;
  235. }
  236.  
  237. private void MainView_Load(object sender, EventArgs e)
  238. {
  239.  
  240. }
  241.  
  242. private void groupBoxSetting_Enter(object sender, EventArgs e)
  243. {
  244.  
  245. }
  246.  
  247. private void btnClearCSV_Click(object sender, EventArgs e)
  248. {
  249. textBoxCSV.Text = "";
  250. }
  251.  
  252. private void btnSaveCSV_Click(object sender, EventArgs e)
  253. {
  254.  
  255. try
  256. {
  257.  
  258. StreamWriter writer = new StreamWriter(textBoxCSVFilename.Text,
  259. false, // 上書き ( true = 追加 )
  260. Encoding.GetEncoding("Shift_JIS"));
  261. writer.WriteLine(textBoxCSV.Text);
  262. writer.Close();
  263.  
  264.  
  265. if (checkBoxOpenAtCSVSave.Checked)
  266. {
  267. System.Diagnostics.Process.Start(textBoxCSVFilename.Text);
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. MessageBox.Show(ex.Message, "CSVファイル保存・実行失敗");
  273. }
  274.  
  275.  
  276. }
  277.  
  278. private void MainView_FormClosing(object sender, FormClosingEventArgs e)
  279. {
  280. if (loggingmode_ != LoggingMode.STOP)
  281. {
  282.  
  283. checkBoxPause.Checked = false;
  284. loggingmode_ = LoggingMode.STOP;
  285.  
  286. // いろいろやって終了しようとしている
  287. Thread.Sleep(2000);
  288. process_.Kill();
  289. process_ = null;
  290.  
  291. threadLogging_.Abort();
  292.  
  293. }
  294. }
  295.  
  296.  
  297.  
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement