*/
#include "log/backend-journal.hpp"
+#include "log/formatter.hpp"
+
#include <systemd/sd-journal.h>
namespace security_containers {
namespace log {
-void SystemdJournalBackend::log(const std::string& severity,
+void SystemdJournalBackend::log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
const std::string& message)
{
#define SD_JOURNAL_SUPPRESS_LOCATION
- sd_journal_send("PRIORITY=%s", severity.c_str(),
+ sd_journal_send("PRIORITY=%s", LogFormatter::toString(logLevel).c_str(),
"CODE_FILE=%s", file.c_str(),
"CODE_LINE=%d", line,
"CODE_FUNC=%s", func.c_str(),
#undef SD_JOURNAL_SUPPRESS_LOCATION
}
-
} // namespace log
} // namespace security_containers
-
*/
class SystemdJournalBackend : public LogBackend {
public:
- void log(const std::string& severity,
+ void log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
*/
class NullLogger : public LogBackend {
public:
- void log(const std::string& /*severity*/,
+ void log(LogLevel /*logLevel*/,
const std::string& /*file*/,
const unsigned int& /*line*/,
const std::string& /*func*/,
*/
#include "log/backend-stderr.hpp"
+#include "log/formatter.hpp"
#include <sstream>
#include <iomanip>
-#include <sys/time.h>
namespace security_containers {
namespace log {
-namespace {
-
-inline std::string getCurrentTime(void)
-{
- char time[13];
- struct timeval tv;
- gettimeofday(&tv, NULL);
- struct tm* tm = localtime(&tv.tv_sec);
- sprintf(time, "%02d:%02d:%02d.%03d", tm->tm_hour, tm->tm_min, tm->tm_sec, int(tv.tv_usec / 1000));
-
- return std::string(time);
-}
-
-} // namespace
-
-void StderrBackend::log(const std::string& severity,
+void StderrBackend::log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
// example log string
// 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing
- logLine << getCurrentTime() << ' '
- << std::left << std::setw(8) << '[' + severity + ']'
+ logLine << LogFormatter::setConsoleColor(logLevel)
+ << LogFormatter::getCurrentTime() << ' '
+ << std::left << std::setw(8) << '[' + LogFormatter::toString(logLevel) + ']'
<< std::left << std::setw(52) << file + ':' + std::to_string(line) + ' ' + func + ':'
- << message << std::endl;
+ << message
+ << LogFormatter::setDefaultConsoleColor() << std::endl;
+
fprintf(stderr, "%s", logLine.str().c_str());
}
*/
class StderrBackend : public LogBackend {
public:
- void log(const std::string& severity,
+ void log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
#ifndef COMMON_LOG_BACKEND_HPP
#define COMMON_LOG_BACKEND_HPP
-#include <string>
+#include "log/level.hpp"
+#include <string>
namespace security_containers {
namespace log {
*/
class LogBackend {
public:
- virtual void log(const std::string& severity,
+ virtual void log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief Console color for StderrBackend logger
+ */
+
+#include "log/ccolor.hpp"
+
+#include <stdio.h>
+
+namespace security_containers {
+namespace log {
+
+std::string getConsoleEscapeSequence(Attributes attr, Color color)
+{
+ char command[10];
+
+ // Command is the control command to the terminal
+ snprintf(command, sizeof(command), "%c[%d;%dm", 0x1B, attr, color);
+ return std::string(command);
+}
+
+} // namespace log
+} // namespace security_containers
+
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief Console color for StderrBackend logger
+ */
+
+#ifndef COMMON_LOG_CCOLOR_HPP
+#define COMMON_LOG_CCOLOR_HPP
+
+#include <string>
+
+namespace security_containers {
+namespace log {
+
+enum class Color : unsigned int {
+ DEFAULT = 0,
+ BLACK = 90,
+ RED = 91,
+ GREEN = 92,
+ YELLOW = 93,
+ BLUE = 94,
+ MAGENTA = 95,
+ CYAN = 96,
+ WHITE = 97,
+};
+
+enum class Attributes : unsigned int {
+ DEFAULT = 0,
+ BOLD = 1,
+};
+
+std::string getConsoleEscapeSequence(Attributes attr, Color color);
+
+} // namespace log
+} // namespace security_containers
+
+#endif // COMMON_LOG_CCOLOR_HPP
+
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief Helper formatter for logger
+ */
+
+#include "log/formatter.hpp"
+#include "log/ccolor.hpp"
+
+#include <sys/time.h>
+#include <cassert>
+
+namespace security_containers {
+namespace log {
+
+std::string LogFormatter::getCurrentTime(void)
+{
+ char time[13];
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ struct tm* tm = localtime(&tv.tv_sec);
+ snprintf(time,
+ sizeof(time),
+ "%02d:%02d:%02d.%03d",
+ tm->tm_hour,
+ tm->tm_min,
+ tm->tm_sec,
+ int(tv.tv_usec / 1000));
+
+ return std::string(time);
+}
+
+std::string LogFormatter::setConsoleColor(LogLevel logLevel)
+{
+ switch (logLevel) {
+ case LogLevel::ERROR:
+ return getConsoleEscapeSequence(Attributes::BOLD, Color::RED);
+ case LogLevel::WARN:
+ return getConsoleEscapeSequence(Attributes::BOLD, Color::YELLOW);
+ case LogLevel::INFO:
+ return getConsoleEscapeSequence(Attributes::BOLD, Color::BLUE);
+ case LogLevel::DEBUG:
+ return getConsoleEscapeSequence(Attributes::DEFAULT, Color::GREEN);
+ case LogLevel::TRACE:
+ return getConsoleEscapeSequence(Attributes::DEFAULT, Color::BLACK);
+ default:
+ return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
+ }
+}
+
+std::string LogFormatter::setDefaultConsoleColor(void)
+{
+ return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
+}
+
+std::string LogFormatter::toString(LogLevel logLevel)
+{
+ switch (logLevel) {
+ case LogLevel::ERROR:
+ return "ERROR";
+ case LogLevel::WARN:
+ return "WARN";
+ case LogLevel::INFO:
+ return "INFO";
+ case LogLevel::DEBUG:
+ return "DEBUG";
+ case LogLevel::TRACE:
+ return "TRACE";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+std::string LogFormatter::stripProjectDir(const std::string& file)
+{
+ const std::string SOURCE_DIR = PROJECT_SOURCE_DIR "/";
+ // it will work until someone use in cmake FILE(GLOB ... RELATIVE ...)
+ assert(0 == file.compare(0, SOURCE_DIR.size(), SOURCE_DIR));
+ return file.substr(SOURCE_DIR.size());
+}
+
+} // namespace log
+} // namespace security_containers
+
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk <d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief Helper formatter for logger
+ */
+
+#ifndef COMMON_LOG_FORMATTER_HPP
+#define COMMON_LOG_FORMATTER_HPP
+
+#include "log/level.hpp"
+
+#include <string>
+
+namespace security_containers {
+namespace log {
+
+class LogFormatter {
+public:
+ static std::string getCurrentTime(void);
+ static std::string setConsoleColor(LogLevel logLevel);
+ static std::string setDefaultConsoleColor(void);
+ static std::string toString(LogLevel logLevel);
+ static std::string stripProjectDir(const std::string& file);
+};
+
+} // namespace log
+} // namespace security_containers
+
+#endif // COMMON_LOG_FORMATTER_HPP
+
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk (d.michaluk@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 Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief LogLevel
+ */
+
+#ifndef COMMON_LOG_LEVEL_HPP
+#define COMMON_LOG_LEVEL_HPP
+
+namespace security_containers {
+namespace log {
+
+enum class LogLevel {
+ TRACE,
+ DEBUG,
+ INFO,
+ WARN,
+ ERROR
+};
+
+} // namespace log
+} // namespace security_containers
+
+#endif // COMMON_LOG_LEVEL_HPP
+
*/
#include "log/logger.hpp"
+#include "log/formatter.hpp"
#include "log/backend-null.hpp"
#include <memory>
#include <mutex>
-#include <cassert>
-
namespace security_containers {
namespace log {
std::unique_ptr<LogBackend> gLogBackendPtr(new NullLogger());
std::mutex gLogMutex;
-const std::string SOURCE_DIR = PROJECT_SOURCE_DIR "/";
-
-inline std::string stripProjectDir(const std::string& file)
-{
- // it will work until someone use in cmake FILE(GLOB ... RELATIVE ...)
- assert(0 == file.compare(0, SOURCE_DIR.size(), SOURCE_DIR));
- return file.substr(SOURCE_DIR.size());
-}
-
} // namespace
-Logger::Logger(const std::string& severity,
+Logger::Logger(LogLevel logLevel,
const std::string& file,
const unsigned int line,
const std::string& func)
- : mSeverity(severity),
- mFile(stripProjectDir(file)),
+ : mLogLevel(logLevel),
+ mFile(LogFormatter::stripProjectDir(file)),
mLine(line),
mFunc(func)
{
void Logger::logMessage(const std::string& message)
{
std::unique_lock<std::mutex> lock(gLogMutex);
- gLogBackendPtr->log(mSeverity, mFile, mLine, mFunc, message);
+ gLogBackendPtr->log(mLogLevel, mFile, mLine, mFunc, message);
}
void Logger::setLogLevel(LogLevel level)
#ifndef COMMON_LOG_LOGGER_HPP
#define COMMON_LOG_LOGGER_HPP
+#include "log/level.hpp"
+
#include <sstream>
#include <string>
namespace security_containers {
namespace log {
-
-enum class LogLevel {
- TRACE, DEBUG, INFO, WARN, ERROR
-};
-
class LogBackend;
class Logger {
public:
- Logger(const std::string& severity,
+ Logger(LogLevel logLevel,
const std::string& file,
const unsigned int line,
const std::string& func);
static void setLogBackend(LogBackend* pBackend);
private:
- std::string mSeverity;
+ LogLevel mLogLevel;
std::string mFile;
unsigned int mLine;
std::string mFunc;
security_containers::log::LogLevel::SEVERITY) { \
std::ostringstream messageStream__; \
messageStream__ << MESSAGE; \
- security_containers::log::Logger logger(#SEVERITY, __FILE__, __LINE__, __func__); \
+ security_containers::log::Logger logger(security_containers::log::LogLevel::SEVERITY, \
+ __FILE__, \
+ __LINE__, \
+ __func__); \
logger.logMessage(messageStream__.str()); \
} \
} while(0)
#endif // COMMON_LOG_LOGGER_HPP
+
#include "ut.hpp"
#include "log/logger.hpp"
+#include "log/formatter.hpp"
#include "log/backend.hpp"
#include "log/backend-stderr.hpp"
StubbedBackend(std::ostringstream& s) : mLogStream(s) {}
// stubbed function
- void log(const std::string& severity,
+ void log(LogLevel logLevel,
const std::string& file,
const unsigned int& line,
const std::string& func,
const std::string& message) override
{
- mLogStream << '[' + severity + ']' + ' '
+ mLogStream << '[' + LogFormatter::toString(logLevel) + ']' + ' '
<< file + ':' + std::to_string(line) + ' ' + func + ':'
<< message << std::endl;
}