f99996d52abcadeb50222e5a36b071cbf3401c06
[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.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://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         struct timeval tv;
54
55         if (!s_info.fp)
56                 return LB_STATUS_ERROR_IO;
57
58         gettimeofday(&tv, NULL);
59         fprintf(s_info.fp, "%d %lu.%lu [%s:%d] ", getpid(), tv.tv_sec, tv.tv_usec, util_basename((char *)func), line);
60
61         va_start(ap, fmt);
62         ret = vfprintf(s_info.fp, fmt, ap);
63         va_end(ap);
64
65         s_info.nr_of_lines++;
66         if (s_info.nr_of_lines == MAX_LOG_LINE) {
67                 char *filename;
68                 int namelen;
69
70                 s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
71
72                 namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 20;
73                 filename = malloc(namelen);
74                 if (filename) {
75                         snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, s_info.filename);
76
77                         if (s_info.fp)
78                                 fclose(s_info.fp);
79
80                         s_info.fp = fopen(filename, "w+");
81                         if (!s_info.fp)
82                                 ErrPrint("Failed to open a file: %s\n", filename);
83
84                         free(filename);
85                 }
86
87                 s_info.nr_of_lines = 0;
88         }
89         return ret;
90 }
91
92
93
94 int critical_log_init(const char *name)
95 {
96         int namelen;
97         char *filename;
98
99         if (s_info.fp)
100                 return 0;
101
102         s_info.filename = strdup(name);
103         if (!s_info.filename) {
104                 ErrPrint("Failed to create a log file\n");
105                 return LB_STATUS_ERROR_MEMORY;
106         }
107
108         namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 20;
109
110         filename = malloc(namelen);
111         if (!filename) {
112                 ErrPrint("Failed to create a log file\n");
113                 free(s_info.filename);
114                 s_info.filename = NULL;
115                 return LB_STATUS_ERROR_MEMORY;
116         }
117
118         snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, name);
119
120         s_info.fp = fopen(filename, "w+");
121         if (!s_info.fp) {
122                 ErrPrint("Failed to open log: %s\n", strerror(errno));
123                 free(s_info.filename);
124                 s_info.filename = NULL;
125                 free(filename);
126                 return LB_STATUS_ERROR_IO;
127         }
128
129         free(filename);
130         return 0;
131 }
132
133
134
135 int critical_log_fini(void)
136 {
137         if (s_info.filename) {
138                 free(s_info.filename);
139                 s_info.filename = NULL;
140         }
141
142         if (s_info.fp) {
143                 fclose(s_info.fp);
144                 s_info.fp = NULL;
145         }
146
147         return 0;
148 }
149
150
151
152 /* End of a file */