2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
24 #include <sys/types.h>
30 #if defined(HAVE_LIVEBOX)
31 #include <livebox-errno.h>
33 #include "lite-errno.h"
39 #include "critical_log.h"
46 pthread_mutex_t cri_lock;
52 .cri_lock = PTHREAD_MUTEX_INITIALIZER,
57 static inline void rotate_log(void)
62 if (s_info.nr_of_lines < MAX_LOG_LINE) {
66 s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
68 namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 30;
69 filename = malloc(namelen);
71 snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, s_info.filename, getpid());
74 if (fclose(s_info.fp) != 0) {
75 ErrPrint("fclose: %s\n", strerror(errno));
79 s_info.fp = fopen(filename, "w+");
81 ErrPrint("Failed to open a file: %s\n", filename);
87 s_info.nr_of_lines = 0;
92 HAPI int critical_log(const char *func, int line, const char *fmt, ...)
98 return LB_STATUS_ERROR_IO;
101 CRITICAL_SECTION_BEGIN(&s_info.cri_lock);
103 fprintf(s_info.fp, "%lf [%s:%d] ", util_timestamp(), util_basename((char *)func), line);
106 ret = vfprintf(s_info.fp, fmt, ap);
111 s_info.nr_of_lines++;
114 CRITICAL_SECTION_END(&s_info.cri_lock);
120 HAPI int critical_log_init(const char *name)
126 return LB_STATUS_SUCCESS;
129 s_info.filename = strdup(name);
130 if (!s_info.filename) {
131 ErrPrint("Failed to create a log file\n");
132 return LB_STATUS_ERROR_MEMORY;
135 namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 30;
137 filename = malloc(namelen);
139 ErrPrint("Failed to create a log file\n");
140 DbgFree(s_info.filename);
141 s_info.filename = NULL;
142 return LB_STATUS_ERROR_MEMORY;
145 snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, name, getpid());
147 s_info.fp = fopen(filename, "w+");
149 ErrPrint("Failed to open log: %s\n", strerror(errno));
150 DbgFree(s_info.filename);
151 s_info.filename = NULL;
153 return LB_STATUS_ERROR_IO;
157 return LB_STATUS_SUCCESS;
162 HAPI void critical_log_fini(void)
164 if (s_info.filename) {
165 DbgFree(s_info.filename);
166 s_info.filename = NULL;
170 if (fclose(s_info.fp) != 0) {
171 ErrPrint("fclose: %s\n", strerror(errno));