Update package version to 0.1.56
[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     snprintf(log_path, sizeof(log_path), "%s/log", data_path);
46
47     if (data_path)
48         free(data_path);
49
50     int idx = 0;
51     if (preference_get_int(LAST_LOG_FILE_INDEX, &idx) != PREFERENCE_ERROR_NONE) {
52         idx = 1;
53         snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx);
54
55         if (access(strLogFile, F_OK) == 0) {
56             if (unlink(strLogFile) == -1)
57                 LOGE("Failed to remove log file");
58         }
59
60         if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
61             LOGW("Failed to set last file index");
62     } else {
63         char tmpLogFile[PATH_MAX];
64         snprintf(tmpLogFile, sizeof(tmpLogFile), "%s/sticker_%d.log", log_path, idx);
65
66         ifstream log_file(tmpLogFile, ifstream::binary);
67         if (!log_file || !log_file.is_open()) {
68             snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
69         } else {
70             log_file.seekg(0, log_file.end);
71             int size = log_file.tellg();
72
73             if (size >= MAX_LOG_SIZE) {
74                 if (idx + 1 > MAX_LOG_COUNT)
75                     idx = 1;
76                 else
77                     idx += 1;
78
79                 if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE)
80                     LOGW("Failed to set last file index");
81
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");
86                 }
87             } else
88                 snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile);
89         }
90     }
91
92     std::ofstream sticker_log_file (strLogFile, std::ios::app);
93     sticker_log_file << full_buf << std::endl;
94     sticker_log_file.flush ();
95 }