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