Update package version to 0.1.63
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / sticker_log.cpp
1 #include <dlog.h>
2 #include <stdio.h>
3 #include <string>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <iostream>
8 #include <fstream>
9 #include <linux/limits.h>
10 #include <time.h>
11 #include <app_common.h>
12 #include <app_preference.h>
13
14 #include "log.h"
15 #include "config.h"
16 #include "receiver_preference.h"
17
18 using namespace std;
19
20 void sticker_save_log(const char *fmt, ...)
21 {
22     char buf[4000] = {0};
23     char time_buf[96] = {0};
24     char full_buf[4096] = {0};
25     char log_path[PATH_MAX];
26     char strLogFile[PATH_MAX];
27     va_list ap;
28
29     struct timespec ts;
30     clock_gettime(CLOCK_REALTIME, &ts);
31     const time_t tt = ts.tv_sec;
32     const long int real_millisec = ts.tv_nsec / 1000000;
33
34     struct tm *const ptm = localtime(&tt);
35     strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", ptm);
36
37     va_start(ap, fmt);
38     vsnprintf(buf, sizeof (buf), fmt, ap);
39     va_end(ap);
40
41     snprintf(full_buf, sizeof(full_buf), "%s.%03ld %s", time_buf, real_millisec, buf);
42
43     char *data_path = NULL;
44     data_path = app_get_shared_data_path();
45     if (!data_path)
46         return;
47
48     snprintf(log_path, sizeof(log_path), "%s/log", data_path);
49
50     if (access(data_path, R_OK) != 0) {
51         free(data_path);
52         return;
53     }
54
55     free(data_path);
56
57     int idx = 0;
58     if (preference_get_int(LAST_LOG_FILE_INDEX, &idx) != PREFERENCE_ERROR_NONE) {
59         idx = 1;
60         snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx);
61
62         if (access(strLogFile, F_OK) == 0) {
63             if (unlink(strLogFile) == -1)
64                 LOGE("Failed to remove log file");
65         }
66
67         if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
68             LOGW("Failed to set last file index");
69     } else {
70         char tmpLogFile[PATH_MAX];
71         snprintf(tmpLogFile, sizeof(tmpLogFile), "%s/sticker_%d.log", log_path, idx);
72
73         ifstream log_file(tmpLogFile, ifstream::binary);
74         if (!log_file || !log_file.is_open()) {
75             snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
76         } else {
77             log_file.seekg(0, log_file.end);
78             int size = log_file.tellg();
79
80             if (size >= MAX_LOG_SIZE) {
81                 if (idx + 1 > MAX_LOG_COUNT)
82                     idx = 1;
83                 else
84                     idx += 1;
85
86                 if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
87                     LOGW("Failed to set last file index");
88
89                 snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx);
90                 if (access(strLogFile, F_OK) == 0) {
91                     if (unlink(strLogFile) == -1)
92                         LOGE("Failed to remove log file");
93                 }
94             } else
95                 snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
96         }
97     }
98
99     std::ofstream sticker_log_file (strLogFile, std::ios::app);
100     sticker_log_file << full_buf << std::endl;
101     sticker_log_file.flush ();
102 }