summaryrefslogtreecommitdiff
path: root/src/log.c
blob: b25e8a71f26d3599fe496688dea83da53211d652 (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
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <app_common.h>
#include "log.h"

#define MAX_LOG_SIZE 4096
#define PATH_MAX 4096

static FILE *log_fp = NULL;
static log_type ltype = LOG_TYPE_DLOG;

static const char log_prio_name[][DLOG_PRIO_MAX-1] = {
	"UNKNOWN",
	"DEFAULT", /**< Default */
	"VERBOSE", /**< Verbose */
	"DEBUG", /**< Debug */
	"INFO", /**< Info */
	"WARN", /**< Warning */
	"ERROR", /**< Error */
	"FATAL", /**< Fatal */
	"SILENT" /**< Silent */
};

static inline char* getFormattedTime(void)
{
	struct timeval val;
	struct tm *ptm;
	static char res_time[40] = {0, };

	gettimeofday(&val, NULL);
	ptm = localtime(&val.tv_sec);

	// format : YYMMDDhhmmssuuuuuu
	snprintf(res_time, sizeof(res_time), "%04d-%02d-%02d %02d:%02d:%02d:%06ld"
		, ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday
		, ptm->tm_hour, ptm->tm_min, ptm->tm_sec
		, val.tv_usec);

	return res_time;
}

static int __open_log_file(void)
{
	char buf[PATH_MAX] = {0,};
	char *prefix = NULL;

	prefix = app_get_data_path();

	if (!prefix)
		goto error;

	snprintf(buf, sizeof(buf)-1, "%s%s", prefix, "log.txt");
	free(prefix);

	log_fp = fopen(buf, "a"); /* append or create new ??? */
	if (log_fp == NULL) {
		dlog_print(DLOG_WARN, LOG_TAG, "%s\n", strerror(errno));
		goto error;
	}

	return 0;

error:
	dlog_print(DLOG_WARN, LOG_TAG, "error to use log file, so use dlog as log system\n");
	ltype = LOG_TYPE_DLOG;
	return -1;
}

int log_type_set(log_type type)
{
	ltype = type;
	dlog_print(DLOG_DEBUG, LOG_TAG, "setting log type : [%d]\n", type);
	switch (type) {
	case LOG_TYPE_FILE:
	case LOG_TYPE_ALL:
		__open_log_file();
		break;
	case LOG_TYPE_DLOG: /* nothing to do */
	default:
		ltype = LOG_TYPE_DLOG;
		break;
	}
	return 0;
}

void log_file_close(void)
{
	if (log_fp) {
		fclose(log_fp);
		log_fp = NULL;
		dlog_print(DLOG_DEBUG, LOG_TAG, "close log file\n");
	}

	log_type_set(LOG_TYPE_DLOG);

	return;
}

int log_print(log_priority prio, const char *tag, const char *fmt, ...)
{
	va_list ap;

	switch (ltype) {
	case LOG_TYPE_FILE:
		if (log_fp) {
			va_start(ap, fmt);
			fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
			vfprintf(log_fp, fmt, ap);
			va_end(ap);
			fflush(log_fp);
		}
		break;
	case LOG_TYPE_ALL:
		va_start(ap, fmt);
		if (log_fp) {
			fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]);
			vfprintf(log_fp, fmt, ap);
		}
		dlog_vprint(prio, tag, fmt, ap);
		va_end(ap);

		if (log_fp)
			fflush(log_fp);
		break;
	case LOG_TYPE_DLOG:
	default:
		va_start(ap, fmt);
		dlog_vprint(prio, tag, fmt, ap);
		va_end(ap);
		break;
	}
	return 0;
}