Various patches are applied
[platform/framework/web/data-provider-master.git] / src / critical_log.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <stdio.h>
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <sys/time.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <libgen.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <dlog.h>
28 #include <Eina.h>
29 #include <livebox-errno.h>
30
31 #include "conf.h"
32 #include "debug.h"
33 #include "util.h"
34 #include "critical_log.h"
35
36 static struct {
37         FILE *fp;
38         int file_id;
39         int nr_of_lines;
40         char *filename;
41 } s_info = {
42         .fp = NULL,
43         .file_id = 0,
44         .nr_of_lines = 0,
45         .filename = NULL,
46 };
47
48
49
50 static inline void rotate_log(void)
51 {
52         char *filename;
53         int namelen;
54
55         if (s_info.nr_of_lines < MAX_LOG_LINE)
56                 return;
57
58         s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
59
60         namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 20;
61         filename = malloc(namelen);
62         if (filename) {
63                 snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, s_info.filename);
64
65                 if (s_info.fp)
66                         fclose(s_info.fp);
67
68                 s_info.fp = fopen(filename, "w+");
69                 if (!s_info.fp)
70                         ErrPrint("Failed to open a file: %s\n", filename);
71
72                 DbgFree(filename);
73         }
74
75         s_info.nr_of_lines = 0;
76 }
77
78
79
80 HAPI int critical_log(const char *func, int line, const char *fmt, ...)
81 {
82         va_list ap;
83         int ret;
84         struct timeval tv;
85
86         if (!s_info.fp)
87                 return LB_STATUS_ERROR_IO;
88
89         if (gettimeofday(&tv, NULL) < 0) {
90                 ErrPrint("gettimeofday: %s\n", strerror(errno));
91                 tv.tv_sec = 0;
92                 tv.tv_usec = 0;
93         }
94
95         fprintf(s_info.fp, "%d %lu.%lu [%s:%d] ", getpid(), tv.tv_sec, tv.tv_usec, util_basename((char *)func), line);
96
97         va_start(ap, fmt);
98         ret = vfprintf(s_info.fp, fmt, ap);
99         va_end(ap);
100
101         s_info.nr_of_lines++;
102         rotate_log();
103         return ret;
104 }
105
106
107
108 HAPI int critical_log_init(const char *name)
109 {
110         int namelen;
111         char *filename;
112
113         if (s_info.fp)
114                 return LB_STATUS_SUCCESS;
115
116         s_info.filename = strdup(name);
117         if (!s_info.filename) {
118                 ErrPrint("Failed to create a log file\n");
119                 return LB_STATUS_ERROR_MEMORY;
120         }
121
122         namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 20;
123
124         filename = malloc(namelen);
125         if (!filename) {
126                 ErrPrint("Failed to create a log file\n");
127                 DbgFree(s_info.filename);
128                 s_info.filename = NULL;
129                 return LB_STATUS_ERROR_MEMORY;
130         }
131
132         snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, name);
133
134         s_info.fp = fopen(filename, "w+");
135         if (!s_info.fp) {
136                 ErrPrint("Failed to open log: %s\n", strerror(errno));
137                 DbgFree(s_info.filename);
138                 s_info.filename = NULL;
139                 DbgFree(filename);
140                 return LB_STATUS_ERROR_IO;
141         }
142
143         DbgFree(filename);
144         return LB_STATUS_SUCCESS;
145 }
146
147
148
149 HAPI void critical_log_fini(void)
150 {
151         if (s_info.filename) {
152                 DbgFree(s_info.filename);
153                 s_info.filename = NULL;
154         }
155
156         if (s_info.fp) {
157                 fclose(s_info.fp);
158                 s_info.fp = NULL;
159         }
160 }
161
162
163
164 /* End of a file */