--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <iostream>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <chrono>
+#include <errno.h>
+
+#include "file-sink.h"
+
+namespace ode {
+
+#define MAX_LOG_LEN 16
+
+namespace {
+std::string getFileName(const std::string &path)
+{
+ std::string name;
+ auto pos = path.rfind('/');
+ pos = (pos == std::string::npos) ? 0 : pos + 1;
+ name = path.substr(pos, path.size());
+ return name;
+}
+} //namespace
+
+FileLogSink::FileLogSink(const std::string& name)
+ : path("/opt/var/log/"), fd(-1)
+{
+ path.append(getFileName(name));
+ fd = ::open(path.c_str(), O_CREAT | O_RDWR | O_APPEND | O_SYNC, 0664);
+}
+
+FileLogSink::~FileLogSink()
+{
+ ::close(fd);
+}
+
+void FileLogSink::resize()
+{
+ std::lock_guard<std::recursive_mutex> lock(mtx);
+ struct stat st;
+ int ret = 0;
+ int blkcnt = MAX_LOG_LEN;
+ size_t blksize = 0;
+
+ if (::lstat(path.c_str(), &st) < 0) {
+ std::cerr << "Invalid log file" << std::endl;
+ return;
+ }
+
+ blksize = st.st_blksize;
+ if (blksize > 0)
+ blkcnt = (st.st_size / blksize) + 1;
+
+ if (blkcnt <= MAX_LOG_LEN)
+ return;
+
+ ret = ::fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, blksize*(blkcnt-MAX_LOG_LEN));
+ if (ret < 0)
+ std::cerr << "Failed to collapse the log file : " << errno << std::endl;
+}
+
+void FileLogSink::write(const std::string &message)
+{
+ std::lock_guard<std::recursive_mutex> lock(mtx);
+ resize();
+
+ auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
+ std::tm tm = *std::localtime(&now);
+ std::string log("[" + std::to_string(tm.tm_hour)
+ + ":"
+ + std::to_string(tm.tm_min)
+ + ":"
+ + std::to_string(tm.tm_sec) + "] ");
+
+ log.append(message);
+ ::write(fd, log.c_str(), log.size());
+ ::sync();
+}
+
+void FileLogSink::sink(const std::string &message)
+{
+ auto level = message.substr(0, message.find('<'));
+ auto subMsg = message.substr(message.find(':') + 1);
+ std::string log;
+
+ switch (audit::StringToLogLevel(level)) {
+ case audit::LogLevel::Error:
+ log.append("[" + this->tag + " ERROR] : " + subMsg);
+ break;
+ case audit::LogLevel::Warning:
+ log.append("[" + this->tag + " WARN] : " + subMsg);
+ break;
+ case audit::LogLevel::Debug:
+ log.append("[" + this->tag + " DEBUG] : " + subMsg);
+ break;
+ case audit::LogLevel::Info:
+ log.append("[" + this->tag + " INFO] : " + subMsg);
+ break;
+ case audit::LogLevel::Trace:
+ log.append("[" + this->tag + " TRACE] : " + subMsg);
+ break;
+ default:
+ log.append("[" + this->tag + " SILENT] : " + subMsg);
+ break;
+ }
+
+ write(log);
+}
+
+} //namespace ode
--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#ifndef __ODE_FILE_LOGSINK_H__
+#define __ODE_FILE_LOGSINK_H__
+
+#include <string>
+#include <memory>
+#include <mutex>
+
+#include <klay/filesystem.h>
+#include <klay/audit/logger.h>
+
+namespace ode {
+
+class FileLogSink : public audit::LogSink {
+public:
+ explicit FileLogSink(const std::string& tag);
+ ~FileLogSink();
+ void sink(const std::string& message) override;
+
+private:
+ void resize();
+ void write(const std::string &message);
+
+private:
+ std::string path;
+ int fd;
+ std::string tag;
+ std::recursive_mutex mtx;
+};
+
+} //namespace ode
+
+#endif //__ODE_FILE_LOGSINK_H__
#include <cynara-client.h>
#include <sys/smack.h>
-#include <klay/audit/dlog-sink.h>
-
#include "logger.h"
+#include "file-sink.h"
#include "secure-erase.h"
#include "internal-encryption.h"
#include "external-encryption.h"
namespace {
const std::string ODE_MANAGER_ADDRESS = "/tmp/.ode.sock";
-std::unique_ptr<audit::DlogLogSink> _sink = nullptr;
+std::unique_ptr<FileLogSink> _sink = nullptr;
} // namespace
ServerContext::ServerContext() : rmi::Service(ODE_MANAGER_ADDRESS)
{
- _sink.reset(new audit::DlogLogSink("ODE"));
+ _sink.reset(new FileLogSink("ode.log"));
SINK = _sink.get();
INFO(SINK, "ODE server starting.");