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