9 #include <linux/limits.h>
11 #include <app_common.h>
12 #include <app_preference.h>
16 #include "receiver_preference.h"
20 void sticker_save_log(const char *fmt, ...)
23 char time_buf[96] = {0};
24 char full_buf[4096] = {0};
25 char log_path[PATH_MAX];
26 char strLogFile[PATH_MAX];
30 clock_gettime(CLOCK_REALTIME, &ts);
31 const time_t tt = ts.tv_sec;
32 const long int real_millisec = ts.tv_nsec / 1000000;
34 struct tm *const ptm = localtime(&tt);
35 strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", ptm);
38 vsnprintf(buf, sizeof (buf), fmt, ap);
41 snprintf(full_buf, sizeof(full_buf), "%s.%03ld %s", time_buf, real_millisec, buf);
43 char *data_path = NULL;
44 data_path = app_get_shared_data_path();
48 snprintf(log_path, sizeof(log_path), "%s/log", data_path);
50 if (access(data_path, R_OK) != 0) {
58 if (preference_get_int(LAST_LOG_FILE_INDEX, &idx) != PREFERENCE_ERROR_NONE) {
60 snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx);
62 if (access(strLogFile, F_OK) == 0) {
63 if (unlink(strLogFile) == -1)
64 LOGE("Failed to remove log file");
67 if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
68 LOGW("Failed to set last file index");
70 char tmpLogFile[PATH_MAX];
71 snprintf(tmpLogFile, sizeof(tmpLogFile), "%s/sticker_%d.log", log_path, idx);
73 ifstream log_file(tmpLogFile, ifstream::binary);
74 if (!log_file || !log_file.is_open()) {
75 snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
77 log_file.seekg(0, log_file.end);
78 int size = log_file.tellg();
80 if (size >= MAX_LOG_SIZE) {
81 if (idx + 1 > MAX_LOG_COUNT)
86 if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
87 LOGW("Failed to set last file index");
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");
95 snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
99 std::ofstream sticker_log_file (strLogFile, std::ios::app);
100 sticker_log_file << full_buf << std::endl;
101 sticker_log_file.flush ();