summaryrefslogtreecommitdiff
path: root/home/src/critical_log.c
blob: fe84dd2e79ac9040b492f63b0d052b6c10db56ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>

#include <dlog.h>
#include <Eina.h>
#include <dynamicbox_errno.h>

#include "util.h"
#include "critical_log.h"

#if !defined(SECURE_LOGD)
#define SECURE_LOGD LOGD
#endif

#if !defined(SECURE_LOGW)
#define SECURE_LOGW LOGW
#endif

#if !defined(SECURE_LOGE)
#define SECURE_LOGE LOGE
#endif

#if !defined(DbgPrint)
#define DbgPrint(format, arg...)	SECURE_LOGD(format, ##arg)
#endif

#if !defined(ErrPrint)
#define ErrPrint(format, arg...)	SECURE_LOGE(format, ##arg)
#endif

#if !defined(WarnPrint)
#define WarnPrint(format, arg...)	SECURE_LOGW(format, ##arg)
#endif

#define DbgFree(a) free(a)

#define SLAVE_LOG_PATH "/tmp/.w-home_log/"
#define MAX_LOG_LINE 80
#define MAX_LOG_FILE 3

#define CRITICAL_SECTION_BEGIN(handle) \
do { \
	int ret; \
	ret = pthread_mutex_lock(handle); \
	if (ret != 0) \
		ErrPrint("Failed to lock: %s\n", strerror(ret)); \
} while (0)

#define CRITICAL_SECTION_END(handle) \
do { \
	int ret; \
	ret = pthread_mutex_unlock(handle); \
	if (ret != 0) \
		ErrPrint("Failed to unlock: %s\n", strerror(ret)); \
} while (0)

static struct {
	FILE *fp;
	int file_id;
	int nr_of_lines;
	char *filename;
	pthread_mutex_t cri_lock;
} s_info = {
	.fp = NULL,
	.file_id = 0,
	.nr_of_lines = 0,
	.filename = NULL,
	.cri_lock = PTHREAD_MUTEX_INITIALIZER,
};



static inline void rotate_log(void)
{
	char *filename;
	int namelen;

	if (s_info.nr_of_lines < MAX_LOG_LINE) {
		return;
	}

	if (!s_info.filename) {
		return;
	}

	s_info.file_id = (s_info.file_id + 1) % MAX_LOG_FILE;

	namelen = strlen(s_info.filename) + strlen(SLAVE_LOG_PATH) + 30;
	filename = malloc(namelen);
	if (filename) {
		snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, s_info.filename, getpid());

		if (s_info.fp) {
			if (fclose(s_info.fp) != 0) {
				ErrPrint("fclose: %s\n", strerror(errno));
			}
		}

		s_info.fp = fopen(filename, "w+");
		if (!s_info.fp) {
			ErrPrint("Failed to open a file: %s\n", filename);
		}

		DbgFree(filename);
	}

	s_info.nr_of_lines = 0;
}



HAPI int critical_log(const char *func, int line, const char *fmt, ...)
{
	va_list ap;
	int ret;

	if (!s_info.fp) {
		return DBOX_STATUS_ERROR_IO_ERROR;
	}

	CRITICAL_SECTION_BEGIN(&s_info.cri_lock);

	fprintf(s_info.fp, "%lf [%s:%d] ", util_timestamp(), util_basename((char *)func), line);

	va_start(ap, fmt);
	ret = vfprintf(s_info.fp, fmt, ap);
	va_end(ap);

	fflush(s_info.fp);

	s_info.nr_of_lines++;
	rotate_log();

	CRITICAL_SECTION_END(&s_info.cri_lock);
	return ret;
}



HAPI int critical_log_init(const char *name)
{
	int namelen;
	char *filename;

	if (!name) {
		ErrPrint("name is NULL");
		return DBOX_STATUS_ERROR_INVALID_PARAMETER;
	}

	if (mkdir(SLAVE_LOG_PATH, 0755) < 0) {
		ErrPrint("mkdir(%s) returns: %s\n", SLAVE_LOG_PATH, strerror(errno));
	}

	if (s_info.fp) {
		return DBOX_STATUS_ERROR_NONE;
	}

	s_info.filename = strdup(name);
	if (!s_info.filename) {
		ErrPrint("Failed to create a log file\n");
		return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
	}

	namelen = strlen(name) + strlen(SLAVE_LOG_PATH) + 30;

	filename = malloc(namelen);
	if (!filename) {
		ErrPrint("Failed to create a log file\n");
		DbgFree(s_info.filename);
		s_info.filename = NULL;
		return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
	}

	snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, name, getpid());

	s_info.fp = fopen(filename, "w+");
	if (!s_info.fp) {
		ErrPrint("Failed to open log: %s\n", strerror(errno));
		DbgFree(s_info.filename);
		s_info.filename = NULL;
		DbgFree(filename);
		return DBOX_STATUS_ERROR_IO_ERROR;
	}

	DbgFree(filename);
	return DBOX_STATUS_ERROR_NONE;
}



HAPI void critical_log_fini(void)
{
	if (s_info.filename) {
		DbgFree(s_info.filename);
		s_info.filename = NULL;
	}

	if (s_info.fp) {
		if (fclose(s_info.fp) != 0) {
			ErrPrint("fclose: %s\n", strerror(errno));
		}
		s_info.fp = NULL;
	}
}



/* End of a file */