Eclipseプラットフォームのデバッグトレース機能の使い方
アプリ開発しているとデバッグ用のオプション用意してデバッグメッセージを出力したいってことがある。 Eclipseをプラットフォームとした場合、デバッグモードというのがあり、プラグインごとにデバッグオプションを用意する仕組みがあると分かったので、その使い方をメモする。
実装
プラグインのデバッグオプションは、以下のどちらかもしくは両方を使って実装する。
org.eclipse.core.runtime.Plugin#isDebugging()
org.eclipse.core.runtime.Platform#getDebugOption(String)
それぞれ以下のように使って実装する。
Plugin#isDebugging()
デバッグオプションが一つだけでよい場合、このデフォルトで用意されたAPIを以下のように使用する。
if (MyPlugin.getDefault().isDebugging()) { // Log debug message. }
この実装は以下のコードと等価。
if ("true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.faq.examples/debug"))) { // Log debug message. }
Platform#getDebugOption(String)
デバッグオプションを複数用意して切り替えることができるようにする場合、こちらを使用する。
以下のように使って実装する。
private static final boolean DEBUG_OPTION1 = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.faq.examples/debug/option1")); private static final boolean DEBUG_OPTION2 = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.faq.examples/debug/option2")); if (DEBUG_OPTION1) { // Log debug option1 message. } if (DEBUG_OPTION2) { // Log debug option2 message. }
使い方
開発したプラグインをインストールしたEclipse(もしくはRCP)のインストールフォルダ直下にデバッグオプションを有効にする.optionsファイルを配置し、ランタイムオプション-debug
を指定して起動する。
.optionsファイルの形式はkey=value形式で以下のように記載することでオプションを有効にする。
org.eclipse.faq.examples/debug=true org.eclipse.faq.examples/debug/option1=true org.eclipse.faq.examples/debug/option2=true
-debug
のあとにURLもしくはファイルシステムパスを指定することで、インストールフォルダ直下の.optionsファイル以外も指定することができる。
ちなみに、プラグインプロジェクト直下に.optionsファイルを作成して、そこにもデバッグオプションをリスト化しておくと、デバッグ時ののtracingタブで簡単に有効にすることができるので便利。 詳細は、Tracing Tab - Eclipse Helpを参照。
プラグイン内に配置するoptionsファイルの値はfalseにしておくのが流儀っぽい。
org.eclipse.faq.examples/debug=false org.eclipse.faq.examples/debug/option1=false org.eclipse.faq.examples/debug/option2=false
参考
https://wiki.eclipse.org/FAQ_How_do_I_use_the_platform_debug_tracing_facility