Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / logger / LoggerCallback.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.logger;
18
19 import java.util.Calendar;
20 import java.util.Date;
21
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.utils.Constants;
24
25 import org.oic.simulator.ILogger;
26
27 /**
28  * Class which provides a callback method to receive log from native layer.
29  */
30 public class LoggerCallback implements ILogger {
31
32     @Override
33     public void write(String time, int level, String message) {
34         if (null == time || level < 0 || null == message) {
35             return;
36         }
37         // Parse the time
38         Date date = parseTime(time);
39         if (null == date) {
40             return;
41         }
42         Activator activator = Activator.getDefault();
43         if (null == activator) {
44             return;
45         }
46         activator.getLogManager().log(level, date, message);
47     }
48
49     private Date parseTime(String time) {
50         Date date;
51         String[] token = time.split("\\.");
52         int h, m, s;
53         try {
54             if (token.length == Constants.PROPER_LOG_TIME_TOKEN_LENGTH) {
55                 h = Integer.parseInt(token[0]);
56                 m = Integer.parseInt(token[1]);
57                 s = Integer.parseInt(token[2]);
58
59                 Calendar calendar;
60                 calendar = Calendar.getInstance();
61                 calendar.set(Calendar.HOUR, h);
62                 calendar.set(Calendar.MINUTE, m);
63                 calendar.set(Calendar.SECOND, s);
64
65                 date = calendar.getTime();
66             } else {
67                 date = null;
68             }
69         } catch (NumberFormatException nfe) {
70             date = null;
71         }
72         return date;
73     }
74 }