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