8b626c5effe19ccb76060c89ad3c636b04148825
[platform/framework/web/livebox-viewer.git] / src / critical_log.c
1 /*
2  * Copyright 2012  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://www.tizenopensource.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
34 static struct {
35         FILE *fp;
36         int file_id;
37         int nr_of_lines;
38         char *filename;
39 } s_info = {
40         .fp = NULL,
41         .file_id = 0,
42         .nr_of_lines = 0,
43         .filename = NULL,
44 };
45
46
47
48 int critical_log(const char *func, int line, const char *fmt, ...)
49 {
50         va_list ap;
51         int ret;
52         struct timeval tv;
53
54         if (!s_info.fp)
55                 return -EIO;
56
57         gettimeofday(&tv, NULL);
58         fprintf(s_info.fp, "%d %lu.%lu [%s:%d] ", getpid(), tv.tv_sec, tv.tv_usec, util_basename((char *)func), line);
59
60         va_start(ap, fmt);
61         ret = vfprintf(s_info.fp, fmt, ap);
62         va_end(ap);
63
64         s_info.nr_of_lines++;
65         if (s_info.nr_of_lines == MAX_LOG_LINE) {
66                 char *filename;
67                 int namelen;
68
69                 s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
70
71                 namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 20;
72                 filename = malloc(namelen);
73                 if (filename) {
74                         snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, s_info.filename);
75
76                         if (s_info.fp)
77                                 fclose(s_info.fp);
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                         free(filename);
84                 }
85
86                 s_info.nr_of_lines = 0;
87         }
88         return ret;
89 }
90
91
92
93 int critical_log_init(const char *name)
94 {
95         int namelen;
96         char *filename;
97
98         if (s_info.fp)
99                 return 0;
100
101         s_info.filename = strdup(name);
102         if (!s_info.filename) {
103                 ErrPrint("Failed to create a log file\n");
104                 return -ENOMEM;
105         }
106
107         namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 20;
108
109         filename = malloc(namelen);
110         if (!filename) {
111                 ErrPrint("Failed to create a log file\n");
112                 free(s_info.filename);
113                 s_info.filename = NULL;
114                 return -ENOMEM;
115         }
116
117         snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, name);
118
119         s_info.fp = fopen(filename, "w+");
120         if (!s_info.fp) {
121                 ErrPrint("Failed to open log: %s\n", strerror(errno));
122                 free(s_info.filename);
123                 s_info.filename = NULL;
124                 free(filename);
125                 return -EIO;
126         }
127
128         free(filename);
129         return 0;
130 }
131
132
133
134 int critical_log_fini(void)
135 {
136         if (s_info.filename) {
137                 free(s_info.filename);
138                 s_info.filename = NULL;
139         }
140
141         if (s_info.fp) {
142                 fclose(s_info.fp);
143                 s_info.fp = NULL;
144         }
145
146         return 0;
147 }
148
149
150
151 /* End of a file */