2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include <sys/smack.h>
27 #include <sys/types.h>
31 #include "launchpad_logger.h"
32 #include "log_private.h"
34 #define LAUNCHPAD_LOG_APPFW_PATH "/var/log/appfw"
35 #define LAUNCHPAD_LOG_PATH "/var/log/appfw/launchpad"
36 #define LAUNCHPAD_LOG_MAX_SIZE 2500
37 #define LAUNCHPAD_LOG_MAX_BUFFER_SIZE 256
44 static int __set_smack_label(const char *path, const char *label)
48 ret = smack_setlabel(path, label, SMACK_LABEL_ACCESS);
51 _E("smack_setlabel() is failed. path(%s), label(%s), errno(%d)",
59 static int __create_directory(const char *path)
64 ret = access(path, F_OK);
68 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
69 ret = mkdir(path, mode);
72 _E("Failed to create directory(%s). errno(%d)", path, errno);
79 static int __create_launchpad_directories(void)
83 ret = __create_directory(LAUNCHPAD_LOG_APPFW_PATH);
87 ret = __set_smack_label(LAUNCHPAD_LOG_APPFW_PATH, "_");
91 ret = __create_directory(LAUNCHPAD_LOG_PATH);
95 ret = __set_smack_label(LAUNCHPAD_LOG_PATH, "User");
102 int _logger_create(const char *path, logger_h *handle)
104 struct logger_s *logger;
108 if (!path || !handle) {
109 _E("Invalid parameter");
113 ret = __create_launchpad_directories();
117 logger = calloc(1, sizeof(struct logger_s));
123 logger->fd = open(path, O_CREAT | O_WRONLY, 0640);
124 if (logger->fd < 0) {
126 _E("Failed to open path(%s), errno(%d)", path, errno);
127 _logger_destroy(logger);
131 ret = __set_smack_label(path, "User");
133 _logger_destroy(logger);
137 offset = lseek(logger->fd, 0, SEEK_END);
139 logger->index = (int)(offset / LAUNCHPAD_LOG_MAX_STRING_SIZE);
140 if (logger->index >= LAUNCHPAD_LOG_MAX_SIZE) {
142 lseek(logger->fd, 0, SEEK_SET);
151 int _logger_destroy(logger_h handle)
154 _E("Invalid parameter");
166 int _logger_print(logger_h handle, const char *tag, const char *format, ...)
168 char buf[LAUNCHPAD_LOG_MAX_BUFFER_SIZE];
169 char format_buf[128];
170 struct tm tm = { 0, };
176 if (!handle || !tag || !format) {
177 _E("Invalid parameter");
182 localtime_r(&t, &tm);
184 if (handle->index != 0)
185 offset = lseek(handle->fd, 0, SEEK_CUR);
187 offset = lseek(handle->fd, 0, SEEK_SET);
190 _E("lseek() is failed. errno(%d)", errno);
192 va_start(ap, format);
193 vsnprintf(format_buf, sizeof(format_buf), format, ap);
196 snprintf(buf, sizeof(buf),
197 "[%6d] %04d-%02d-%02d %02d:%02d:%02d %-16s %-100s\n",
199 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
200 tm.tm_hour, tm.tm_min, tm.tm_sec,
202 ret = write(handle->fd, buf, strlen(buf));
205 _E("Failed to write log message. errno(%d)", errno);
209 if (++handle->index >= LAUNCHPAD_LOG_MAX_SIZE)
215 int _logger_get_fd(logger_h handle, int *fd)
217 if (!handle || !fd) {
218 _E("Invalid parameter");