)
## Link libraries ##############################################################
-PKG_CHECK_MODULES(LOGGER_DEPS REQUIRED libsystemd-journal)
+PKG_CHECK_MODULES(LOGGER_DEPS libsystemd-journal QUIET)
INCLUDE_DIRECTORIES(${LIBS_FOLDER})
INCLUDE_DIRECTORIES(SYSTEM ${LOGGER_DEPS_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} ${LOGGER_DEPS_LIBRARIES})
+## Add constants ###############################################################
+if(LOGGER_DEPS_FOUND)
+ ADD_DEFINITIONS(-DHAVE_SYSTEMD_JOURNAL=${LOGGER_DEPS_FOUND})
+endif()
+
## Generate the pc file ########################################################
CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY)
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Roman Kubiak (r.kubiak@samsung.com)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Roman Kubiak (r.kubiak@samsung.com)
+ * @brief File backend for logger
+ */
+
+#include "logger/config.hpp"
+#include "logger/formatter.hpp"
+#include "logger/backend-file.hpp"
+
+#include <fstream>
+
+namespace logger {
+
+void FileBackend::log(LogLevel logLevel,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message)
+{
+ std::ofstream out(mfilePath, std::ios::app);
+ out << LogFormatter::getHeader(logLevel, file, line, func);
+ out << message;
+ out << std::endl;
+}
+
+
+} // namespace logger
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Roman Kubiak (r.kubiak@samsung.com)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Roman Kubiak (r.kubiak@samsung.com)
+ * @brief File backend for logger
+ */
+
+#ifndef COMMON_LOGGER_BACKEND_FILE_HPP
+#define COMMON_LOGGER_BACKEND_FILE_HPP
+
+#include "logger/backend.hpp"
+
+namespace logger {
+
+class FileBackend : public LogBackend {
+public:
+ FileBackend(const std::string& filePath) : mfilePath(filePath) {}
+ void log(LogLevel logLevel,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message) override;
+private:
+ std::string mfilePath;
+};
+
+} // namespace logger
+
+#endif // COMMON_LOGGER_BACKEND_FILE_HPP
* @author Dariusz Michaluk (d.michaluk@samsung.com)
* @brief Systemd journal backend for logger
*/
-
+#ifdef HAVE_SYSTEMD_JOURNAL
#include "logger/config.hpp"
#include "logger/backend-journal.hpp"
}
} // namespace logger
+#endif // HAVE_SYSTEMD_JOURNAL
for (const auto& messageLine : tokens) {
if (!messageLine.empty()) {
fprintf(stderr,
- "%s%s%s%s\n",
- logColor.c_str(),
+ "%s%s %s%s\n",
+ mUseColours ? logColor.c_str() : "",
header.c_str(),
messageLine.c_str(),
- defaultColor.c_str());
+ mUseColours ? defaultColor.c_str() : "");
}
}
}
*/
class StderrBackend : public LogBackend {
public:
+ StderrBackend(const bool useColours = true) : mUseColours(useColours) {}
void log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
const std::string& message) override;
+private:
+ bool mUseColours;
};
} // namespace logger
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Roman Kubiak (r.kubiak@samsung.com)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Roman Kubiak (r.kubiak@samsung.com)
+ * @brief Syslog backend for logger
+ */
+
+#include "logger/config.hpp"
+#include "logger/formatter.hpp"
+#include "logger/backend-syslog.hpp"
+
+#include <syslog.h>
+#include <sstream>
+namespace logger {
+
+namespace {
+
+inline int toSyslogPriority(LogLevel logLevel)
+{
+ switch (logLevel) {
+ case LogLevel::ERROR:
+ return LOG_ERR; // 3
+ case LogLevel::WARN:
+ return LOG_WARNING; // 4
+ case LogLevel::INFO:
+ return LOG_INFO; // 6
+ case LogLevel::DEBUG:
+ return LOG_DEBUG; // 7
+ case LogLevel::TRACE:
+ return LOG_DEBUG; // 7
+ case LogLevel::HELP:
+ return LOG_DEBUG; // 7
+ default:
+ return LOG_DEBUG; // 7
+ }
+}
+
+} // namespace
+
+void SyslogBackend::log(LogLevel logLevel,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message)
+{
+ ::syslog(toSyslogPriority(logLevel), "%s %s", LogFormatter::getHeader(logLevel, file, line, func).c_str(), message.c_str());
+}
+
+} // namespace logger
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Roman Kubiak (r.kubiak@samsung.com)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Roman Kubiak (r.kubiak@samsung.com)
+ * @brief Syslog backend for logger
+ */
+
+#ifndef COMMON_LOGGER_BACKEND_SYSLOG_HPP
+#define COMMON_LOGGER_BACKEND_SYSLOG_HPP
+
+#include "logger/backend.hpp"
+
+namespace logger {
+
+class SyslogBackend : public LogBackend {
+public:
+ void log(LogLevel logLevel,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message) override;
+};
+
+} // namespace logger
+
+#endif // COMMON_LOGGER_BACKEND_SYSLOG_HPP
#define COMMON_LOGGER_LOGGER_HPP
#include "logger/level.hpp"
+#include "logger/backend-file.hpp"
+#include "logger/backend-stderr.hpp"
#include <sstream>
#include <string>