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 <dynamicbox_errno.h>
32 #include <dynamicbox_conf.h>
34 #include "lite-errno.h"
40 #include "critical_log.h"
47 pthread_mutex_t cri_lock;
53 .cri_lock = PTHREAD_MUTEX_INITIALIZER,
58 static inline void rotate_log(void)
63 if (s_info.nr_of_lines < DYNAMICBOX_CONF_MAX_LOG_LINE) {
67 s_info.file_id = (s_info.file_id + 1) % DYNAMICBOX_CONF_MAX_LOG_FILE;
69 namelen = strlen(s_info.filename) + strlen(DYNAMICBOX_CONF_LOG_PATH) + 30;
70 filename = malloc(namelen);
72 snprintf(filename, namelen, "%s/%d_%s.%d", DYNAMICBOX_CONF_LOG_PATH, s_info.file_id, s_info.filename, getpid());
75 if (fclose(s_info.fp) != 0) {
76 ErrPrint("fclose: %s\n", strerror(errno));
80 s_info.fp = fopen(filename, "w+");
82 ErrPrint("Failed to open a file: %s\n", filename);
88 s_info.nr_of_lines = 0;
93 HAPI int critical_log(const char *func, int line, const char *fmt, ...)
99 return DBOX_STATUS_ERROR_IO_ERROR;
102 CRITICAL_SECTION_BEGIN(&s_info.cri_lock);
104 fprintf(s_info.fp, "%lf [%s:%d] ", util_timestamp(), util_basename((char *)func), line);
107 ret = vfprintf(s_info.fp, fmt, ap);
112 s_info.nr_of_lines++;
115 CRITICAL_SECTION_END(&s_info.cri_lock);
121 HAPI int critical_log_init(const char *name)
127 return DBOX_STATUS_ERROR_NONE;
130 s_info.filename = strdup(name);
131 if (!s_info.filename) {
132 ErrPrint("Failed to create a log file\n");
133 return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
136 namelen = strlen(name) + strlen(DYNAMICBOX_CONF_LOG_PATH) + 30;
138 filename = malloc(namelen);
140 ErrPrint("Failed to create a log file\n");
141 DbgFree(s_info.filename);
142 s_info.filename = NULL;
143 return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
146 snprintf(filename, namelen, "%s/%d_%s.%d", DYNAMICBOX_CONF_LOG_PATH, s_info.file_id, name, getpid());
148 s_info.fp = fopen(filename, "w+");
150 ErrPrint("Failed to open log: %s\n", strerror(errno));
151 DbgFree(s_info.filename);
152 s_info.filename = NULL;
154 return DBOX_STATUS_ERROR_IO_ERROR;
158 return DBOX_STATUS_ERROR_NONE;
163 HAPI void critical_log_fini(void)
165 if (s_info.filename) {
166 DbgFree(s_info.filename);
167 s_info.filename = NULL;
171 if (fclose(s_info.fp) != 0) {
172 ErrPrint("fclose: %s\n", strerror(errno));