fce3f516376975ec7670e5e7d4f58c4eda68b9a2
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / util / Logger.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.util;
23
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26
27 public class Logger {
28
29     public static final int VERBOSE = 0;
30     public static final int DEBUG   = 1;
31     public static final int INFO    = 2;
32     public static final int WARNING = 3;
33     public static final int ERROR   = 4;
34
35     private static int logLevel = DEBUG;
36
37     public static void setLogLevel(int level) {
38         logLevel = level;
39     }
40
41     public static void v(String log) {
42         printLog(VERBOSE, log);
43     }
44
45     public static void d(String log) {
46         printLog(DEBUG, log);
47     }
48
49     public static void i(String log) {
50         printLog(INFO, log);
51     }
52
53     public static void w(String log) {
54         printLog(WARNING, log);
55     }
56
57     public static void e(String log) {
58         printLog(ERROR, log);
59     }
60
61     public static void w(String log, Exception ex) {
62         printLog(WARNING, log);
63         ex.printStackTrace();
64     }
65
66     public static void w(String log, Throwable th) {
67         printLog(WARNING, log);
68         th.printStackTrace();
69     }
70
71     public static void e(String log, Exception ex) {
72         printLog(ERROR, log);
73         ex.printStackTrace();
74     }
75
76     public static void e(String log, Throwable th) {
77         printLog(ERROR, log);
78         th.printStackTrace();
79     }
80
81     private static void printLog(int level, String log) {
82
83         if (logLevel > level)
84             return;
85
86         String format = "";
87         format += getTime();
88         format += " [" + getLogLevelString(level) + "]";
89
90         if (level >= ERROR) {
91             format += " [" + getDetailInfo() + "]";
92         }
93
94         format += " " + log;
95
96         System.out.println(format);
97     }
98
99     private static String getDetailInfo() {
100
101         String res = "";
102
103         StackTraceElement ste = Thread.currentThread().getStackTrace()[4];
104         String className = ste.getClassName()
105                 .substring(ste.getClassName().lastIndexOf(".") + 1);
106
107         res += ste.getFileName() + ", " + className + "." + ste.getMethodName()
108                 + "(), line:" + ste.getLineNumber();
109
110         return res;
111     }
112
113     private static String getTime() {
114         Calendar calendar = Calendar.getInstance();
115         SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
116
117         return dateFormat.format(calendar.getTime());
118     }
119
120     private static String getLogLevelString(int level) {
121
122         String res = "";
123
124         if (level == VERBOSE) {
125             res = "V";
126         } else if (level == DEBUG) {
127             res = "D";
128         } else if (level == INFO) {
129             res = "I";
130         } else if (level == WARNING) {
131             res = "W";
132         } else if (level == ERROR) {
133             res = "E";
134         }
135
136         return res;
137     }
138 }