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();
45 snprintf(log_path, sizeof(log_path), "%s/log", data_path);
51 if (preference_get_int(LAST_LOG_FILE_INDEX, &idx) != PREFERENCE_ERROR_NONE) {
53 snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx);
55 if (access(strLogFile, F_OK) == 0) {
56 if (unlink(strLogFile) == -1)
57 LOGE("Failed to remove log file");
60 if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
61 LOGW("Failed to set last file index");
63 char tmpLogFile[PATH_MAX];
64 snprintf(tmpLogFile, sizeof(tmpLogFile), "%s/sticker_%d.log", log_path, idx);
66 ifstream log_file(tmpLogFile, ifstream::binary);
67 if (!log_file || !log_file.is_open()) {
68 snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
70 log_file.seekg(0, log_file.end);
71 int size = log_file.tellg();
73 if (size >= MAX_LOG_SIZE) {
74 if (idx + 1 > MAX_LOG_COUNT)
79 if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
80 LOGW("Failed to set last file index");
82 snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx);
83 if (access(strLogFile, F_OK) == 0) {
84 if (unlink(strLogFile) == -1)
85 LOGE("Failed to remove log file");
88 snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
92 std::ofstream sticker_log_file (strLogFile, std::ios::app);
93 sticker_log_file << full_buf << std::endl;
94 sticker_log_file.flush ();