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>
29 #include <livebox-errno.h>
34 #include "critical_log.h"
50 static inline void rotate_log(void)
55 if (s_info.nr_of_lines < MAX_LOG_LINE)
58 s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
60 namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 20;
61 filename = malloc(namelen);
63 snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, s_info.filename);
68 s_info.fp = fopen(filename, "w+");
70 ErrPrint("Failed to open a file: %s\n", filename);
75 s_info.nr_of_lines = 0;
80 HAPI int critical_log(const char *func, int line, const char *fmt, ...)
87 return LB_STATUS_ERROR_IO;
89 if (gettimeofday(&tv, NULL) < 0) {
90 ErrPrint("gettimeofday: %s\n", strerror(errno));
95 fprintf(s_info.fp, "%d %lu.%lu [%s:%d] ", getpid(), tv.tv_sec, tv.tv_usec, util_basename((char *)func), line);
98 ret = vfprintf(s_info.fp, fmt, ap);
101 s_info.nr_of_lines++;
108 HAPI int critical_log_init(const char *name)
114 return LB_STATUS_SUCCESS;
116 s_info.filename = strdup(name);
117 if (!s_info.filename) {
118 ErrPrint("Failed to create a log file\n");
119 return LB_STATUS_ERROR_MEMORY;
122 namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 20;
124 filename = malloc(namelen);
126 ErrPrint("Failed to create a log file\n");
127 DbgFree(s_info.filename);
128 s_info.filename = NULL;
129 return LB_STATUS_ERROR_MEMORY;
132 snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, name);
134 s_info.fp = fopen(filename, "w+");
136 ErrPrint("Failed to open log: %s\n", strerror(errno));
137 DbgFree(s_info.filename);
138 s_info.filename = NULL;
140 return LB_STATUS_ERROR_IO;
144 return LB_STATUS_SUCCESS;
149 HAPI void critical_log_fini(void)
151 if (s_info.filename) {
152 DbgFree(s_info.filename);
153 s_info.filename = NULL;