2012年2月12日日曜日

ORA-00907:右カッコがありません。

職場でちょっとハマったのでメモ。

副問い合わせにorder byを入れると右カッコがありませんになる。

2012年2月9日木曜日

Android のログレベル

ログレベルについて軽く調べたところによると、特にマニュフェストファイルの
<application android:debuggable>
属性に連動しているわけではないという事がわかった。

知らないうちにLog.d()などを沢山埋めている場合、
<application android:debuggable="false">
にしたところで、勝手に抑止されるわけではないので、以下のようなクラスを用意すると良いだろう。

public class Log {

 private Log() {
 }

 public static void i(final String tag, final String msg) {
  i(tag, msg, null);
 }

 public static void i(final String tag, final String msg, final Throwable th) {
  // info は常に出す。
  if (null == th) {
   android.util.Log.i(tag, msg);
   
  } else {
   android.util.Log.i(tag, msg, th);
  }
 }

 public static void d(final String tag, final String msg) {
  d(tag, msg, null);
 }

 public static void d(final String tag, final String msg, final Throwable th) {
  if (!AppMan.DEBUG_MODE) {
   return;
  }
  if (null == th) {
   android.util.Log.d(tag, msg);
  } else {
   android.util.Log.d(tag, msg, th);
  }
 }

 public static void e(final String tag, final String msg) {
  e(tag, msg, null);
 }

 public static void e(final String tag, final String msg, final Throwable th) {
  // error レベルは常に出す
  if (null == th) {
   android.util.Log.e(tag, msg);
  } else {
   android.util.Log.e(tag, msg, th);
  }
 }

 public static void w(final String tag, final String msg) {
  w(tag, msg, null);
 }

 public static void w(final String tag, final String msg, final Throwable th) {
  // warning も常に出す事にする。
  if (null == th) {
   android.util.Log.w(tag, msg);
  } else {
   android.util.Log.w(tag, msg, th);
  }
 }

 public static void v(final String tag, final String msg) {
  v(tag, msg, null);
 }

 public static void v(final String tag, final String msg, final Throwable th) {
  if (!AppMan.DEBUG_MODE) {
   return;
  }
  if (null == th) {
   android.util.Log.v(tag, msg);
  } else {
   android.util.Log.v(tag, msg, th);
  }
 }
}


後は、各クラスでeclipseの ctrl + shift + o でimport定義を更新して、上記クラスを利用するようにすれば、簡単にマニュフェストファイルに連動して抑止する事ができる。

ただし、上記ソースを見て頂ければわかると思いますが、AppMan.DEBUG_MODE がマニュフェストファイルと連動していなくてはならない。
アプリケーション起動の際、この定数をマニュフェストファイルを参照して初期化する必要がある。

マニュフェストファイルの android:debuggable 属性を参照するコードは以下。

public class Utils {
 private Utils() {
 }

 public static boolean  isDebugAble(Context ctx){
  PackageManager manager = ctx.getPackageManager();
  ApplicationInfo appInfo = null;
  try {
   appInfo = manager.getApplicationInfo(ctx.getPackageName(), 0);
  } catch (NameNotFoundException e) {
   Log.e(e.toString());
   return false;
  }
  if((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) == ApplicationInfo.FLAG_DEBUGGABLE) return true;
  return false;
 }
}

このソースは以下から貰いました。ありがとうございます。

http://www.taosoftware.co.jp/blog/2009/03/android_apireleasedebug.html

後は、アプリケーション起動時、一番最初に表示されるActivityなどで、独自のAppManクラスのDEBUG_MODE定数に上記ユーティリティの戻り値をセットすれば良い。