removed wrong contact points & added two authors
[platform/core/location/lbs-server.git] / lbs-server / src / dump_log.c
1 /*
2  * lbs-server
3  *
4  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <glib.h>
26
27 #include "debug_util.h"
28
29 #define GPG_DUMP_LOG    "/tmp/dump_gps.log"
30
31 int fd = -1;
32
33 struct tm * __get_current_time(struct tm *cur_time)
34 {
35         time_t now;
36         time(&now);
37         return localtime_r(&now, cur_time);
38 }
39
40 void gps_init_log()
41 {
42         LOG_GPS(DBG_ERR, "gps_init_log");
43         int ret = -1;
44         struct tm cur_time;
45         char buf[256] = {0, };
46         fd = open(GPG_DUMP_LOG, O_RDWR | O_APPEND | O_CREAT, 0644);
47         if (fd < 0) {
48                 LOG_GPS(DBG_ERR, "Fail to open file[%s]", GPG_DUMP_LOG);
49                 return;
50         }
51
52         if (!__get_current_time(&cur_time)) {
53                 LOG_GPS(DBG_ERR, "Can't get current time[%s]", GPG_DUMP_LOG);
54                 return;
55         }
56
57         g_snprintf(buf, 256, "[%02d:%02d:%02d] -- START GPS -- \n", cur_time.tm_hour, cur_time.tm_min, cur_time.tm_sec);
58         ret = write(fd, buf, strlen(buf));
59
60         if (ret == -1)
61                 LOG_GPS(DBG_ERR, "Fail to write file[%s]", GPG_DUMP_LOG);
62 }
63
64 void gps_deinit_log()
65 {
66         LOG_GPS(DBG_ERR, "gps_deinit_log");
67         if (fd < 0) return;
68         int ret = -1;
69         struct tm cur_time;
70         char buf[256] = {0, };
71
72         if (!__get_current_time(&cur_time)) {
73                 LOG_GPS(DBG_ERR, "Can't get current time[%s]", GPG_DUMP_LOG);
74         } else {
75                 g_snprintf(buf, 256, "[%02d:%02d:%02d] -- END GPS -- \n", cur_time.tm_hour, cur_time.tm_min, cur_time.tm_sec);
76                 ret = write(fd, buf, strlen(buf));
77
78                 if (ret == -1)
79                         LOG_GPS(DBG_ERR, "Fail to write file[%s]", GPG_DUMP_LOG);
80         }
81         close(fd);
82         fd = -1;
83 }
84
85 void gps_dump_log(const char *str, const char *app_id)
86 {
87         if (fd < 0) {
88                 LOG_GPS(DBG_ERR, "Not available fd[%d]", fd);
89                 return;
90         }
91         int ret = -1;
92         char buf[256] = {0, };
93         struct tm cur_time;
94
95         if (!__get_current_time(&cur_time)) {
96                 LOG_GPS(DBG_ERR, "Can't get current time[%s]", GPG_DUMP_LOG);
97                 return;
98         }
99
100         if (app_id == NULL)
101                 g_snprintf(buf, 256, "[%02d:%02d:%02d] %s\n", cur_time.tm_hour, cur_time.tm_min, cur_time.tm_sec, str);
102         else
103                 g_snprintf(buf, 256, "[%02d:%02d:%02d] %s from [%s]\n", cur_time.tm_hour, cur_time.tm_min, cur_time.tm_sec, str, app_id);
104
105         LOG_GPS(DBG_ERR, "Add dump log [%s", buf);
106         ret = write(fd, buf, strlen(buf));
107         if (ret == -1)
108                 LOG_GPS(DBG_ERR, "Fail to write file[%s]", GPG_DUMP_LOG);
109 }