Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common / src / org / tizen / common / util / log / Logger.java
1 /*
2 *  Common
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: 
7 * Kangho Kim <kh5325.kim@samsung.com>
8
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * Contributors:
22 * - S-Core Co., Ltd
23 *
24 */
25
26 package org.tizen.common.util.log;
27
28 import java.text.MessageFormat;
29
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Status;
33 import org.tizen.common.CommonPlugin;
34 import org.tizen.common.ui.view.console.ConsoleManager;
35
36
37 /**
38  * Logger.
39  * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
40  */
41 public class Logger {
42     private static String loggerName = Logger.class.getName();
43
44     public static void log(IStatus status) {
45         CommonPlugin plugin = CommonPlugin.getDefault();
46         if (plugin != null) {
47             plugin.getLog().log(status);
48         } else {
49             ConsoleManager conManager = new ConsoleManager("Error Log", false);
50             conManager.println(status.getPlugin() + " : " + status.getMessage());
51         }
52     }
53
54     private static String getCallerName() {
55         // Get the stack trace.
56         StackTraceElement stack[] = (new Throwable()).getStackTrace();
57         int ix = 0;
58         // Now search for the first frame before the "Logger" class.
59         while (ix < stack.length) {
60             StackTraceElement frame = stack[ix];
61             String cname = frame.getClassName();
62             if (!cname.equals(loggerName)) {
63                 return cname;
64             }
65             ix++;
66         }
67         return "noname";
68     }
69
70     public static void log(Throwable e) {
71         if (e instanceof CoreException) {
72             log(new Status(IStatus.ERROR, getCallerName(), ((CoreException) e).getStatus().getSeverity(), e.getMessage(), e.getCause()));
73         } else {
74             log(new Status(IStatus.ERROR, getCallerName(), e.toString(), e));
75         }
76     }
77     
78     public static void debug( String message, Object... arguments )
79     {
80         
81     }
82
83     public static void info(String message, Object... arguments) {
84         log(new Status(Status.INFO, getCallerName(), getPossiblyFormattedString(message, arguments)));
85     }
86
87     public static void error(Object message, Throwable t) {
88         log(new Status(Status.ERROR, getCallerName(), message.toString(), t));
89     }
90
91     public static void error(String message, Object... arguments) {
92         log(new Status(Status.ERROR, getCallerName(), getPossiblyFormattedString(message, arguments)));
93     }
94
95     public static void warning(String message, Object... arguments) {
96         log(new Status(Status.WARNING, getCallerName(), getPossiblyFormattedString(message, arguments)));
97     }
98
99     private static String getPossiblyFormattedString(String message, Object... arguments) {
100         return arguments.length > 0 ? MessageFormat.format(message, arguments)
101                                                   : message;
102     }
103
104 }