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>
32 #include "critical_log.h"
33 #include "livebox-errno.h" /* For error code */
49 int critical_log(const char *func, int line, const char *fmt, ...)
56 return LB_STATUS_ERROR_IO;
59 gettimeofday(&tv, NULL);
60 fprintf(s_info.fp, "%d %lu.%lu [%s:%d] ", getpid(), tv.tv_sec, tv.tv_usec, util_basename((char *)func), line);
63 ret = vfprintf(s_info.fp, fmt, ap);
67 if (s_info.nr_of_lines == MAX_LOG_LINE) {
71 s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;
73 namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 20;
74 filename = malloc(namelen);
76 snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, s_info.filename);
79 if (fclose(s_info.fp) != 0) {
80 ErrPrint("fclose: %s\n", strerror(errno));
84 s_info.fp = fopen(filename, "w+");
86 ErrPrint("Failed to open a file: %s\n", filename);
92 s_info.nr_of_lines = 0;
99 int critical_log_init(const char *name)
108 s_info.filename = strdup(name);
109 if (!s_info.filename) {
110 ErrPrint("Failed to create a log file\n");
111 return LB_STATUS_ERROR_MEMORY;
114 namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 20;
116 filename = malloc(namelen);
118 ErrPrint("Failed to create a log file\n");
119 free(s_info.filename);
120 s_info.filename = NULL;
121 return LB_STATUS_ERROR_MEMORY;
124 snprintf(filename, namelen, "%s/%d_%s", SLAVE_LOG_PATH, s_info.file_id, name);
126 s_info.fp = fopen(filename, "w+");
128 ErrPrint("Failed to open log: %s\n", strerror(errno));
129 free(s_info.filename);
130 s_info.filename = NULL;
132 return LB_STATUS_ERROR_IO;
141 int critical_log_fini(void)
143 if (s_info.filename) {
144 free(s_info.filename);
145 s_info.filename = NULL;
149 if (fclose(s_info.fp) != 0) {
150 ErrPrint("fclose: %s\n", strerror(errno));