#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <string.h>
#include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <glib.h>
#include <bundle.h>
#include <bundle_internal.h>
void *user_data;
};
+#define WIDGET_LOG_BUFFER_SIZE 10000
+#define WIDGET_LOG_BUFFER_STRING_SIZE 128
+
+static int __log_index;
+static int __log_fd;
+static bool __log_init = false;
+
+static int __init_log(void)
+{
+ int offset;
+ char buffer[256] = {0, };
+ char caller[255] = {0, };
+
+ aul_app_get_appid_bypid(getpid(), caller, sizeof(caller));
+ snprintf(buffer, sizeof(buffer),
+ "/run/aul/log/widget/%d/widget_%s.log", getuid(), caller);
+ __log_fd = open(buffer, O_CREAT | O_WRONLY, 0644);
+ if (__log_fd < 0) {
+ _E("Failed to open %s - %d", buffer, errno);
+ return -1;
+ }
+
+ offset = lseek(__log_fd, 0, SEEK_END);
+ if (offset != 0) {
+ __log_index = (int)(offset / WIDGET_LOG_BUFFER_STRING_SIZE);
+ if (__log_index >= WIDGET_LOG_BUFFER_SIZE) {
+ __log_index = 0;
+ lseek(__log_fd, 0, SEEK_SET);
+ }
+ }
+ __log_init = true;
+
+ return 0;
+}
+
+API int aul_widget_write_log(const char *tag, const char *format, ...)
+{
+ int ret;
+ int offset;
+ time_t now;
+ char time_buf[32] = {0,};
+ char format_buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
+ char buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
+ va_list ap;
+
+ if (!__log_init)
+ __init_log();
+
+ if (__log_fd < 0) {
+ _E("Invalid file descriptor");
+ return -1;
+ }
+
+ time(&now);
+ ctime_r(&now, time_buf);
+ if (__log_index != 0)
+ offset = lseek(__log_fd, 0, SEEK_CUR);
+ else
+ offset = lseek(__log_fd, 0, SEEK_SET);
+
+ if (offset == -1)
+ _E("error in lseek: %d", errno);
+
+
+ va_start(ap, format);
+ vsnprintf(format_buffer, sizeof(format_buffer), format, ap);
+ va_end(ap);
+
+ snprintf(buffer, sizeof(buffer), "[%-6d][%-5d] %-15s %-50s %s",
+ getpid(), __log_index, tag, format_buffer, time_buf);
+
+ ret = write(__log_fd, buffer, strlen(buffer));
+ if (ret < 0) {
+ _E("Cannot write the amd log: %d", ret);
+ return -1;
+ }
+
+ if (++__log_index >= WIDGET_LOG_BUFFER_SIZE)
+ __log_index = 0;
+
+ return 0;
+}
+
static const char *__to_appid(const char *widget_id)
{
const char *appid;