2012年2月12日日曜日

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

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

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

2012年2月9日木曜日

Android のログレベル

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

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

01public class Log {
02 
03 private Log() {
04 }
05 
06 public static void i(final String tag, final String msg) {
07  i(tag, msg, null);
08 }
09 
10 public static void i(final String tag, final String msg, final Throwable th) {
11  // info は常に出す。
12  if (null == th) {
13   android.util.Log.i(tag, msg);
14    
15  } else {
16   android.util.Log.i(tag, msg, th);
17  }
18 }
19 
20 public static void d(final String tag, final String msg) {
21  d(tag, msg, null);
22 }
23 
24 public static void d(final String tag, final String msg, final Throwable th) {
25  if (!AppMan.DEBUG_MODE) {
26   return;
27  }
28  if (null == th) {
29   android.util.Log.d(tag, msg);
30  } else {
31   android.util.Log.d(tag, msg, th);
32  }
33 }
34 
35 public static void e(final String tag, final String msg) {
36  e(tag, msg, null);
37 }
38 
39 public static void e(final String tag, final String msg, final Throwable th) {
40  // error レベルは常に出す
41  if (null == th) {
42   android.util.Log.e(tag, msg);
43  } else {
44   android.util.Log.e(tag, msg, th);
45  }
46 }
47 
48 public static void w(final String tag, final String msg) {
49  w(tag, msg, null);
50 }
51 
52 public static void w(final String tag, final String msg, final Throwable th) {
53  // warning も常に出す事にする。
54  if (null == th) {
55   android.util.Log.w(tag, msg);
56  } else {
57   android.util.Log.w(tag, msg, th);
58  }
59 }
60 
61 public static void v(final String tag, final String msg) {
62  v(tag, msg, null);
63 }
64 
65 public static void v(final String tag, final String msg, final Throwable th) {
66  if (!AppMan.DEBUG_MODE) {
67   return;
68  }
69  if (null == th) {
70   android.util.Log.v(tag, msg);
71  } else {
72   android.util.Log.v(tag, msg, th);
73  }
74 }
75}

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

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

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

01public class Utils {
02 private Utils() {
03 }
04 
05 public static boolean  isDebugAble(Context ctx){
06  PackageManager manager = ctx.getPackageManager();
07  ApplicationInfo appInfo = null;
08  try {
09   appInfo = manager.getApplicationInfo(ctx.getPackageName(), 0);
10  } catch (NameNotFoundException e) {
11   Log.e(e.toString());
12   return false;
13  }
14  if((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) == ApplicationInfo.FLAG_DEBUGGABLE) return true;
15  return false;
16 }
17}

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

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

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