บทนำ (Overview)
“LogCat” ถูกใช้ในงาน “Debugging” เพื่อช่วยให้การเขียนโปรแกรมนั้นง่ายและสะดวกยิ่งขึ้น โดยเฉพาะโปรแกรมที่มีความซับซ้อนอย่างมาก แต่อย่างไรก็ตามเมื่อเสร็จสิ้นกระบวนการ “Debugging” แล้วนั้น เมื่อโปรแกรมจะถูกนำเสนอแกผู้ใช้งาน (end-users) จำเป็นต้องปิดการ Debugging ให้หมด เพื่อป้องการข้อมูลสำคัญ (Sensitive information) จำพวกเช่น รหัสผ่าน เบอร์โทรศัพท์ผู้ใช้งาน ข้อมูลเชิงลึกของผู้พัฒนา หรือแม้แต่ข้อมูลลูกค้าที่อาจถูกเปิดเผยได้ ในบทความนี้จะนำเสนอวิธีการตรวจสอบ “Application” ว่ามีการเปิด “Debug” หรือไม่
ขั้นตอน (Steps)
- สร้างและเปิด “Android Emulator” ตามบทความ (How to start Android Emulator (AVD))
- ติดตั้ง “.apk” ลงบน “Android Emulator” ตามบทความ (How to install .apk on emulator)
- เปิด “Android Device Monitoring” โดยไปที่ “Tools > Android > Android Device Monitoring” หรือ
- จะได้หน้าจอของ “LogCat” ให้หา “Sensitive information” ตามคอลัมภ์ “Text”
จากรูปข้างต้นจะเห็นว่าหน้าจอของ “LogCat” ในส่วนคอลัมภ์แรกนั้นจะมีชื่อว่า “Level” และ “Tag” ซึ่งเป็นชื่ออ้างอิงของการ “Debug” โดยแต่ละ “Level” จะมีตัวย่อของการ “Warning” ต่าง ๆ กันไป ดังนี้ นอกจากตรวจสอบค่า “Error” ใน “LogCat” แล้วสิ่งสำคัญที่ต้องหาคือค่าที่ถูกกำหนดในขั้น “Debugging” ของ “Programmer” ที่ได้ทำการเขียนไว้ ซึ่งสามารถเขียนลง “Log” ได้โดยใช้ Library ของ “android.util.Log” ดังนี้
- รูปแบบของการเขียน “Log” ดังนี้
Log.d("TAG","TEXT");
- ทดสอบการเขียนลง Log โดยเมื่อกดปุ่มแล้วให้เขียนลง LogCat ดังนี้
ที่ไฟล์ MainActivity.javapackage com.example.wsunpachit.myapplication; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.widget.Button; import android.widget.TextView; import android.util.Log; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView txtView1 = (TextView) findViewById(R.id.textView1); final Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.d("Password","p@ssw0rd"); txtView1.setText("test"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
ที่ไฟล์ activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="18dp" android:textAppearance="?android:attr/textAppearanceLarge" android:text="p@ssw0rd" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" android:text="Login" /> </RelativeLayout>
- “Log” จะถูกเขียนโดยที่คอลัมภ์ “TAG” จะปรากฏคำว่า “Password” และ คอลัมภ์ “Text” จะปรากฏคำว่า “p@ssword”
Log.d("Password","p@ssw0rd");
- ทดสอบการคลิกปุ่ม
- กลับมาดูที่หน้าจอ “Android Device Monitor” จะพบ “Log” ตาม “Tag” ที่อ้างอิง
Log.i(“LOG TAG”, “Info Log”);
Log.d(“LOG TAG”, “Debug Log”);
Log.e(“LOG TAG”, “Error Log”);
Log.v(“LOG TAG”, “Verbose Log);
Log.w(“LOG TAG”, “Warn Log”);