6bf10d677ad43dbd9679466d0587c4790a08646d
[apps/livebox/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 #include <pthread.h>
27
28 #include <dlog.h>
29 #include <Eina.h>
30 #if defined(HAVE_LIVEBOX)
31 #include <livebox-errno.h>
32 #else
33 #include "lite-errno.h"
34 #endif
35
36 #include "conf.h"
37 #include "debug.h"
38 #include "util.h"
39 #include "critical_log.h"
40
41 static struct {
42         FILE *fp;
43         int file_id;
44         int nr_of_lines;
45         char *filename;
46         pthread_mutex_t cri_lock;
47 } s_info = {
48         .fp = NULL,
49         .file_id = 0,
50         .nr_of_lines = 0,
51         .filename = NULL,
52         .cri_lock = PTHREAD_MUTEX_INITIALIZER,
53 };
54
55
56
57 static inline void rotate_log(void)
58 {
59         char *filename;
60         int namelen;
61
62         if (s_info.nr_of_lines < MAX_LOG_LINE) {
63                 return;
64         }
65
66         s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
67
68         namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 30;
69         filename = malloc(namelen);
70         if (filename) {
71                 snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, s_info.filename, getpid());
72
73                 if (s_info.fp) {
74                         if (fclose(s_info.fp) != 0) {
75                                 ErrPrint("fclose: %s\n", strerror(errno));
76                         }
77                 }
78
79                 s_info.fp = fopen(filename, "w+");
80                 if (!s_info.fp) {
81                         ErrPrint("Failed to open a file: %s\n", filename);
82                 }
83
84                 DbgFree(filename);
85         }
86
87         s_info.nr_of_lines = 0;
88 }
89
90
91
92 HAPI int critical_log(const char *func, int line, const char *fmt, ...)
93 {
94         va_list ap;
95         int ret;
96
97         if (!s_info.fp) {
98                 return LB_STATUS_ERROR_IO;
99         }
100
101         CRITICAL_SECTION_BEGIN(&s_info.cri_lock);
102
103         fprintf(s_info.fp, "%lf [%s:%d] ", util_timestamp(), util_basename((char *)func), line);
104
105         va_start(ap, fmt);
106         ret = vfprintf(s_info.fp, fmt, ap);
107         va_end(ap);
108
109         fflush(s_info.fp);
110
111         s_info.nr_of_lines++;
112         rotate_log();
113
114         CRITICAL_SECTION_END(&s_info.cri_lock);
115         return ret;
116 }
117
118
119
120 HAPI int critical_log_init(const char *name)
121 {
122         int namelen;
123         char *filename;
124
125         if (s_info.fp) {
126                 return LB_STATUS_SUCCESS;
127         }
128
129         s_info.filename = strdup(name);
130         if (!s_info.filename) {
131                 ErrPrint("Failed to create a log file\n");
132                 return LB_STATUS_ERROR_MEMORY;
133         }
134
135         namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 30;
136
137         filename = malloc(namelen);
138         if (!filename) {
139                 ErrPrint("Failed to create a log file\n");
140                 DbgFree(s_info.filename);
141                 s_info.filename = NULL;
142                 return LB_STATUS_ERROR_MEMORY;
143         }
144
145         snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, name, getpid());
146
147         s_info.fp = fopen(filename, "w+");
148         if (!s_info.fp) {
149                 ErrPrint("Failed to open log: %s\n", strerror(errno));
150                 DbgFree(s_info.filename);
151                 s_info.filename = NULL;
152                 DbgFree(filename);
153                 return LB_STATUS_ERROR_IO;
154         }
155
156         DbgFree(filename);
157         return LB_STATUS_SUCCESS;
158 }
159
160
161
162 HAPI void critical_log_fini(void)
163 {
164         if (s_info.filename) {
165                 DbgFree(s_info.filename);
166                 s_info.filename = NULL;
167         }
168
169         if (s_info.fp) {
170                 if (fclose(s_info.fp) != 0) {
171                         ErrPrint("fclose: %s\n", strerror(errno));
172                 }
173                 s_info.fp = NULL;
174         }
175 }
176
177
178
179 /* End of a file */