BuildRequires: pkgconfig(libsystemd-daemon)
BuildRequires: pkgconfig(db-util)
BuildRequires: pkgconfig(vconf)
+BuildRequires: pkgconfig(libsystemd-journal)
BuildRequires: boost-devel
Requires: libkey-manager-common = %{version}-%{release}
%{?systemd_requires}
db-util
libsmack
libcrypto
+ libsystemd-journal
)
SET(KEY_MANAGER_COMMON_VERSION_MAJOR 1)
${COMMON_PATH}/dpl/log/src/dlog_log_provider.cpp
${COMMON_PATH}/dpl/log/src/log.cpp
${COMMON_PATH}/dpl/log/src/old_style_log_provider.cpp
+ ${COMMON_PATH}/dpl/log/src/journal_log_provider.cpp
${COMMON_PATH}/dpl/core/src/assert.cpp
${COMMON_PATH}/dpl/core/src/binary_queue.cpp
${COMMON_PATH}/dpl/core/src/colors.cpp
{ \
std::ostringstream platformLog; \
platformLog << message; \
- CKM::Log::LogSystemSingleton::Instance().Pedantic( \
+ CKM::Log::LogSystemSingleton::Instance().Log( \
+ CKM::Log::AbstractLogProvider::LogLevel::Pedantic, \
platformLog.str().c_str(), \
__FILE__, __LINE__, __FUNCTION__); \
} \
printf("%s\n", msg.str().c_str());
// Logging to dlog
- CKM::Log::LogSystemSingleton::Instance().Error(
- str.c_str(), filename, line, function);
+ CKM::Log::LogSystemSingleton::Instance().Log(CKM::Log::AbstractLogProvider::LogLevel::Error,
+ str.c_str(),
+ filename,
+ line,
+ function);
}
} // namespace CKM
class AbstractLogProvider
{
public:
+ enum class LogLevel {
+ None,
+ Error,
+ Warning,
+ Info,
+ Debug,
+ Pedantic
+ };
+
virtual ~AbstractLogProvider() {}
virtual void SetTag(const char *tag);
- virtual void Debug(const char *message,
- const char *fileName,
- int line,
- const char *function) = 0;
- virtual void Info(const char *message,
- const char *fileName,
- int line,
- const char *function) = 0;
- virtual void Warning(const char *message,
- const char *fileName,
- int line,
- const char *function) = 0;
- virtual void Error(const char *message,
- const char *fileName,
- int line,
- const char *function) = 0;
- virtual void Pedantic(const char *message,
- const char *fileName,
- int line,
- const char *function) = 0;
+ virtual void Log(LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const = 0;
+
protected:
static const char *LocateSourceFileName(const char *filename);
};
#include <dpl/log/abstract_log_provider.h>
#include <memory>
-#include <string>
namespace CKM {
namespace Log {
-class DLOGLogProvider :
- public AbstractLogProvider
+class DLOGLogProvider : public AbstractLogProvider
{
- private:
- std::unique_ptr<char[]> m_tag;
-
- static std::string FormatMessage(const char *message,
- const char *filename,
- int line,
- const char *function);
-
public:
DLOGLogProvider();
virtual ~DLOGLogProvider();
- virtual void Debug(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Info(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Warning(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Error(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Pedantic(const char *message,
- const char *fileName,
- int line,
- const char *function);
+ virtual void Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const;
+
// Set global Tag according to DLOG
void SetTag(const char *tag);
+
+ private:
+ std::unique_ptr<char[]> m_tag;
};
} // namespace Log
--- /dev/null
+/*
+ * Copyright (c) 2000 - 2014 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
+ */
+/*
+ * @file journal_log_provider.h
+ * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version 1.0
+ */
+
+#pragma once
+
+#include <dpl/log/abstract_log_provider.h>
+
+namespace CKM {
+namespace Log {
+
+class JournalLogProvider: public AbstractLogProvider
+{
+public:
+ JournalLogProvider();
+ virtual ~JournalLogProvider();
+
+ virtual void Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const;
+};
+
+} /* namespace Log */
+} /* namespace CKM */
namespace Log {
/**
* CKM log system
- *
- * To switch logs into old style, export
- * DPL_USE_OLD_STYLE_LOGS before application start
*/
-class LogSystem :
- private Noncopyable
+class LogSystem : private Noncopyable
{
- private:
- typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
- AbstractLogProviderPtrList m_providers;
-
- bool m_isLoggingEnabled;
-
public:
- bool IsLoggingEnabled() const;
LogSystem();
virtual ~LogSystem();
- /**
- * Log debug message
- */
- void Debug(const char *message,
- const char *filename,
- int line,
- const char *function);
+ AbstractLogProvider::LogLevel GetLogLevel() const { return m_level; }
- /**
- * Log info message
- */
- void Info(const char *message,
- const char *filename,
- int line,
- const char *function);
-
- /**
- * Log warning message
- */
- void Warning(const char *message,
- const char *filename,
- int line,
- const char *function);
-
- /**
- * Log error message
- */
- void Error(const char *message,
- const char *filename,
- int line,
- const char *function);
-
- /**
- * Log pedantic message
- */
- void Pedantic(const char *message,
- const char *filename,
- int line,
- const char *function);
+ void Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *filename,
+ int line,
+ const char *function);
/**
* Set default's DLOG provider Tag
* Remove abstract provider from providers list
*/
void RemoveProvider(AbstractLogProvider *provider);
+
+ private:
+ typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
+ AbstractLogProviderPtrList m_providers;
+ AbstractLogProvider::LogLevel m_level;
};
/*
//
/* avoid warnings about unused variables */
-#define DPL_MACRO_DUMMY_LOGGING(message, function) \
- do { \
- CKM::Log::NullStream ns; \
- ns << message; \
+#define DPL_MACRO_DUMMY_LOGGING(message, level) \
+ do { \
+ CKM::Log::NullStream ns; \
+ ns << message; \
} while (0)
-#define DPL_MACRO_FOR_LOGGING(message, function) \
-do \
-{ \
- if (CKM::Log::LogSystemSingleton::Instance().IsLoggingEnabled()) \
- { \
- std::ostringstream platformLog; \
- platformLog << message; \
- CKM::Log::LogSystemSingleton::Instance().function( \
- platformLog.str().c_str(), \
- __FILE__, __LINE__, __FUNCTION__); \
- } \
+#define DPL_MACRO_FOR_LOGGING(message, level) \
+do \
+{ \
+ if (level > CKM::Log::AbstractLogProvider::LogLevel::None && \
+ CKM::Log::LogSystemSingleton::Instance().GetLogLevel() >= level) \
+ { \
+ std::ostringstream platformLog; \
+ platformLog << message; \
+ CKM::Log::LogSystemSingleton::Instance().Log(level, \
+ platformLog.str().c_str(), \
+ __FILE__, \
+ __LINE__, \
+ __FUNCTION__); \
+ } \
} while (0)
/* Errors must be always logged. */
-#define LogError(message) DPL_MACRO_FOR_LOGGING(message, Error)
+#define LogError(message) \
+ DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Error)
#ifdef BUILD_TYPE_DEBUG
- #define LogDebug(message) DPL_MACRO_FOR_LOGGING(message, Debug)
- #define LogInfo(message) DPL_MACRO_FOR_LOGGING(message, Info)
- #define LogWarning(message) DPL_MACRO_FOR_LOGGING(message, Warning)
- #define LogPedantic(message) DPL_MACRO_FOR_LOGGING(message, Pedantic)
+ #define LogDebug(message) \
+ DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Debug)
+ #define LogInfo(message) \
+ DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Info)
+ #define LogWarning(message) \
+ DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Warning)
+ #define LogPedantic(message) \
+ DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Pedantic)
#else
- #define LogDebug(message) DPL_MACRO_DUMMY_LOGGING(message, Debug)
- #define LogInfo(message) DPL_MACRO_DUMMY_LOGGING(message, Info)
- #define LogWarning(message) DPL_MACRO_DUMMY_LOGGING(message, Warning)
- #define LogPedantic(message) DPL_MACRO_DUMMY_LOGGING(message, Pedantic)
+ #define LogDebug(message) \
+ DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Debug)
+ #define LogInfo(message) \
+ DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Info)
+ #define LogWarning(message) \
+ DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Warning)
+ #define LogPedantic(message) \
+ DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Pedantic)
#endif // BUILD_TYPE_DEBUG
#endif // CENT_KEY_LOG_H
#define CENT_KEY_OLD_STYLE_LOG_PROVIDER_H
#include <dpl/log/abstract_log_provider.h>
-#include <string>
namespace CKM {
namespace Log {
-class OldStyleLogProvider :
- public AbstractLogProvider
+class OldStyleLogProvider : public AbstractLogProvider
{
- private:
- bool m_showDebug;
- bool m_showInfo;
- bool m_showWarning;
- bool m_showError;
- bool m_showPedantic;
- bool m_printStdErr;
-
- static std::string FormatMessage(const char *message,
- const char *filename,
- int line,
- const char *function);
-
public:
- OldStyleLogProvider(bool showDebug,
- bool showInfo,
- bool showWarning,
- bool showError,
- bool showPedantic);
- OldStyleLogProvider(bool showDebug,
- bool showInfo,
- bool showWarning,
- bool showError,
- bool showPedantic,
- bool printStdErr);
+ OldStyleLogProvider();
virtual ~OldStyleLogProvider() {}
- virtual void Debug(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Info(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Warning(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Error(const char *message,
- const char *fileName,
- int line,
- const char *function);
- virtual void Pedantic(const char *message,
- const char *fileName,
- int line,
- const char *function);
+ virtual void Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const;
};
}
} // namespace CKM
*/
#include <stddef.h>
#include <dpl/log/abstract_log_provider.h>
+#include <dpl/availability.h>
#include <cstring>
-#define UNUSED __attribute__((unused))
-
namespace CKM {
namespace Log {
-void AbstractLogProvider::SetTag(const char *tag UNUSED) {}
+void AbstractLogProvider::SetTag(const char *tag CKM_UNUSED) {}
const char *AbstractLogProvider::LocateSourceFileName(const char *filename)
{
#include <dpl/log/dlog_log_provider.h>
#include <cstring>
#include <sstream>
+#include <stdexcept>
+#include <map>
#include <dlog.h>
-#define UNUSED __attribute__((unused))
-
namespace CKM {
namespace Log {
-std::string DLOGLogProvider::FormatMessage(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- std::ostringstream val;
- val << std::string("[") <<
- LocateSourceFileName(filename) << std::string(":") << line <<
- std::string("] ") << function << std::string("(): ") << message;
+namespace {
+typedef void (*dlogMacro)(const char*, const char*);
- return val.str();
+// I can't map LOG_ values because SLOG uses token concatenation
+void error(const char* tag, const char* msg) {
+ SLOG(LOG_ERROR, tag, "%s", msg);
+}
+void warning(const char* tag, const char* msg) {
+ SLOG(LOG_WARN, tag, "%s", msg);
+}
+void info(const char* tag, const char* msg) {
+ SLOG(LOG_INFO, tag, "%s", msg);
+}
+void debug(const char* tag, const char* msg) {
+ SLOG(LOG_DEBUG, tag, "%s", msg);
}
+void pedantic(const char* tag, const char* msg) {
+ SLOG(LOG_VERBOSE, tag, "%s", msg);
+}
+std::map<AbstractLogProvider::LogLevel, dlogMacro> dlogMacros = {
+ // [](const char* tag, const char* msg) { SLOG(LOG_ERROR, tag, "%s", msg); } won't compile
+ { AbstractLogProvider::LogLevel::Error, error },
+ { AbstractLogProvider::LogLevel::Warning, warning },
+ { AbstractLogProvider::LogLevel::Info, info },
+ { AbstractLogProvider::LogLevel::Debug, debug},
+ { AbstractLogProvider::LogLevel::Pedantic, pedantic}
+};
+
+} // namespace anonymous
+
DLOGLogProvider::DLOGLogProvider()
{}
m_tag.reset(buff);
}
-void DLOGLogProvider::Debug(const char *message,
- const char *filename,
- int line,
- const char *function)
+void DLOGLogProvider::Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const
{
- SLOG(LOG_DEBUG, m_tag.get(), "%s",
- FormatMessage(message, filename, line, function).c_str());
-}
-
-void DLOGLogProvider::Info(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- SLOG(LOG_INFO, m_tag.get(), "%s",
- FormatMessage(message, filename, line, function).c_str());
-}
-
-void DLOGLogProvider::Warning(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- SLOG(LOG_WARN, m_tag.get(), "%s",
- FormatMessage(message, filename, line, function).c_str());
-}
-
-void DLOGLogProvider::Error(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- SLOG(LOG_ERROR, m_tag.get(), "%s",
- FormatMessage(message, filename, line, function).c_str());
-}
+ std::ostringstream val;
+ val << std::string("[") << LocateSourceFileName(fileName) << std::string(":") << line <<
+ std::string("] ") << function << std::string("(): ") << message;
-void DLOGLogProvider::Pedantic(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- SLOG(LOG_DEBUG, "CKM", "%s", FormatMessage(message,
- filename,
- line,
- function).c_str());
+ try {
+ dlogMacros.at(level)(m_tag.get(), val.str().c_str());
+ } catch (const std::out_of_range&) {
+ SLOG(LOG_ERROR, m_tag.get(), "Unsupported log level: %d", level);
+ }
}
} // nemespace Log
--- /dev/null
+/*
+ * Copyright (c) 2000 - 2014 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
+ */
+/*
+ * @file journal_log_provider.cpp
+ * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version 1.0
+ */
+
+#include <dpl/log/journal_log_provider.h>
+#include <systemd/sd-journal.h>
+#include <map>
+#include <stdexcept>
+
+namespace CKM {
+namespace Log {
+
+namespace {
+std::map<AbstractLogProvider::LogLevel, int> journalLevel = {
+ { AbstractLogProvider::LogLevel::Error, LOG_ERR },
+ { AbstractLogProvider::LogLevel::Warning, LOG_WARNING },
+ { AbstractLogProvider::LogLevel::Info, LOG_INFO },
+ { AbstractLogProvider::LogLevel::Debug, LOG_DEBUG},
+ { AbstractLogProvider::LogLevel::Pedantic, LOG_DEBUG}
+};
+
+} // namespace anonymous
+
+JournalLogProvider::JournalLogProvider()
+{}
+
+JournalLogProvider::~JournalLogProvider()
+{}
+
+void JournalLogProvider::Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const
+{
+ try {
+ sd_journal_send("PRIORITY=%d", journalLevel.at(level),
+ "CODE_FILE=%s", fileName,
+ "CODE_FUNC=%s", function,
+ "CODE_LINE=%d", line,
+ "MESSAGE=%s", message,
+ NULL);
+ } catch (const std::out_of_range&) {
+ sd_journal_send("PRIORITY=%d", LOG_ERR,
+ "CODE_FILE=%s", fileName,
+ "CODE_FUNC=%s", function,
+ "CODE_LINE=%d", line,
+ "MESSAGE=Unsupported log level %d", level,
+ NULL);
+ }
+}
+
+} /* namespace Log */
+} /* namespace CKM */
#include <stddef.h>
#include <string.h>
+#include <string>
+#include <stdexcept>
+#include <unordered_map>
+#include <cassert>
+
#include <dpl/log/log.h>
#include <dpl/singleton_impl.h>
-#include <dpl/log/dlog_log_provider.h>
#include <dpl/log/old_style_log_provider.h>
+#include <dpl/log/dlog_log_provider.h>
+#include <dpl/log/journal_log_provider.h>
IMPLEMENT_SINGLETON(CKM::Log::LogSystem)
namespace Log {
namespace // anonymous
{
-#ifdef BUILD_TYPE_DEBUG
-const char *OLD_STYLE_LOGS_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS";
-const char *OLD_STYLE_PEDANTIC_LOGS_ENV_NAME =
- "DPL_USE_OLD_STYLE_PEDANTIC_LOGS";
-const char *OLD_STYLE_LOGS_MASK_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS_MASK";
-#endif // BUILD_TYPE_DEBUG
-const char *CENT_KEY_LOG_OFF = "DPL_LOG_OFF";
-} // namespace anonymous
+const char * const CKM_LOG_LEVEL = "CKM_LOG_LEVEL";
+const char * const CKM_LOG_PROVIDER = "CKM_LOG_PROVIDER";
-bool LogSystem::IsLoggingEnabled() const
-{
- return m_isLoggingEnabled;
-}
+const std::string CONSOLE = "CONSOLE";
+const std::string DLOG = "DLOG";
+const std::string JOURNALD = "JOURNALD";
-LogSystem::LogSystem() :
- m_isLoggingEnabled(!getenv(CENT_KEY_LOG_OFF))
-{
+typedef AbstractLogProvider*(*provider_fn)();
+std::unordered_map<std::string, provider_fn> new_provider = {
#ifdef BUILD_TYPE_DEBUG
- bool oldStyleLogs = false;
- bool oldStyleDebugLogs = true;
- bool oldStyleInfoLogs = true;
- bool oldStyleWarningLogs = true;
- bool oldStyleErrorLogs = true;
- bool oldStylePedanticLogs = false;
-
- // Check environment settings about pedantic logs
- const char *value = getenv(OLD_STYLE_LOGS_ENV_NAME);
-
- if (value != NULL && !strcmp(value, "1")) {
- oldStyleLogs = true;
- }
-
- value = getenv(OLD_STYLE_PEDANTIC_LOGS_ENV_NAME);
+ { CONSOLE, []{ return static_cast<AbstractLogProvider*>(new OldStyleLogProvider()); } },
+#endif // BUILD_TYPE_DEBUG
+ { DLOG, []{ return static_cast<AbstractLogProvider*>(new DLOGLogProvider()); } },
+ { JOURNALD, []{ return static_cast<AbstractLogProvider*>(new JournalLogProvider()); } }
+};
- if (value != NULL && !strcmp(value, "1")) {
- oldStylePedanticLogs = true;
- }
+} // namespace anonymous
- value = getenv(OLD_STYLE_LOGS_MASK_ENV_NAME);
-
- if (value != NULL) {
- size_t len = strlen(value);
-
- if (len >= 1) {
- if (value[0] == '0') {
- oldStyleDebugLogs = false;
- } else if (value[0] == '1') {
- oldStyleDebugLogs = true;
- }
- }
-
- if (len >= 2) {
- if (value[1] == '0') {
- oldStyleInfoLogs = false;
- } else if (value[1] == '1') {
- oldStyleInfoLogs = true;
- }
- }
-
- if (len >= 3) {
- if (value[2] == '0') {
- oldStyleWarningLogs = false;
- } else if (value[2] == '1') {
- oldStyleWarningLogs = true;
- }
- }
-
- if (len >= 4) {
- if (value[3] == '0') {
- oldStyleErrorLogs = false;
- } else if (value[3] == '1') {
- oldStyleErrorLogs = true;
- }
- }
+LogSystem::LogSystem()
+{
+ try {
+ m_level = static_cast<AbstractLogProvider::LogLevel>(std::stoi(getenv(CKM_LOG_LEVEL)));
+ } catch(const std::exception&) {
+ m_level = AbstractLogProvider::LogLevel::Debug;
}
+#ifndef BUILD_TYPE_DEBUG
+ if (m_level > AbstractLogProvider::LogLevel::Error)
+ m_level = AbstractLogProvider::LogLevel::Error;
+#endif // BUILD_TYPE_DEBUG
- // Setup default DLOG and old style logging
- if (oldStyleLogs) {
- // Old style
- AddProvider(new OldStyleLogProvider(oldStyleDebugLogs,
- oldStyleInfoLogs,
- oldStyleWarningLogs,
- oldStyleErrorLogs,
- oldStylePedanticLogs));
- } else {
- // DLOG
- AddProvider(new DLOGLogProvider());
+ AbstractLogProvider* prv = NULL;
+ try {
+ prv = new_provider.at(getenv(CKM_LOG_PROVIDER))();
+ } catch(const std::exception&) {
+ prv = new_provider[DLOG]();
}
-#else // BUILD_TYPE_DEBUG
- AddProvider(new DLOGLogProvider());
-#endif // BUILD_TYPE_DEBUG
+ AddProvider(prv);
}
LogSystem::~LogSystem()
m_providers.remove(provider);
}
-void LogSystem::Debug(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
- iterator != m_providers.end();
- ++iterator)
- {
- (*iterator)->Debug(message, filename, line, function);
- }
-}
-
-void LogSystem::Info(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
- iterator != m_providers.end();
- ++iterator)
- {
- (*iterator)->Info(message, filename, line, function);
- }
-}
-
-void LogSystem::Warning(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
- iterator != m_providers.end();
- ++iterator)
- {
- (*iterator)->Warning(message, filename, line, function);
- }
-}
-
-void LogSystem::Error(const char *message,
- const char *filename,
- int line,
- const char *function)
+void LogSystem::Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *filename,
+ int line,
+ const char *function)
{
- for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
- iterator != m_providers.end();
- ++iterator)
- {
- (*iterator)->Error(message, filename, line, function);
- }
-}
-
-void LogSystem::Pedantic(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
- iterator != m_providers.end();
- ++iterator)
- {
- (*iterator)->Pedantic(message, filename, line, function);
- }
+ for (const auto& it : m_providers )
+ it->Log(level, message, filename, line, function);
}
}
#include <cstdio>
#include <cstring>
#include <sstream>
+#include <map>
+#include <stdexcept>
#include <sys/time.h>
#include <unistd.h>
#include <dlog.h>
static_cast<int>(tv.tv_usec / 1000));
return format;
}
-} // namespace anonymous
-std::string OldStyleLogProvider::FormatMessage(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- std::ostringstream val;
+struct ColorMark {
+ const char* const begin;
+ const char* const end;
+};
- val << std::string("[") << GetFormattedTime() << std::string("] [") <<
- static_cast<unsigned long>(pthread_self()) << "/" <<
- static_cast<int>(getpid()) << std::string("] [") <<
- LocateSourceFileName(filename) << std::string(":") << line <<
- std::string("] ") << function << std::string("(): ") << message;
+std::map<AbstractLogProvider::LogLevel, ColorMark> consoleLevel = {
+ { AbstractLogProvider::LogLevel::Error, {ERROR_BEGIN, ERROR_END} },
+ { AbstractLogProvider::LogLevel::Warning, {WARNING_BEGIN, WARNING_END} },
+ { AbstractLogProvider::LogLevel::Info, {INFO_BEGIN, INFO_END} },
+ { AbstractLogProvider::LogLevel::Debug, {DEBUG_BEGIN, DEBUG_END} },
+ { AbstractLogProvider::LogLevel::Pedantic, {PEDANTIC_BEGIN, PEDANTIC_END} }
+};
- return val.str();
-}
-
-OldStyleLogProvider::OldStyleLogProvider(bool showDebug,
- bool showInfo,
- bool showWarning,
- bool showError,
- bool showPedantic) :
- m_showDebug(showDebug),
- m_showInfo(showInfo),
- m_showWarning(showWarning),
- m_showError(showError),
- m_showPedantic(showPedantic),
- m_printStdErr(false)
-{}
+} // namespace anonymous
-OldStyleLogProvider::OldStyleLogProvider(bool showDebug,
- bool showInfo,
- bool showWarning,
- bool showError,
- bool showPedantic,
- bool printStdErr) :
- m_showDebug(showDebug),
- m_showInfo(showInfo),
- m_showWarning(showWarning),
- m_showError(showError),
- m_showPedantic(showPedantic),
- m_printStdErr(printStdErr)
+OldStyleLogProvider::OldStyleLogProvider()
{}
-void OldStyleLogProvider::Debug(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- if (m_showDebug) {
- if (m_printStdErr) {
- fprintf(stderr, "%s%s%s\n", DEBUG_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), DEBUG_END);
- } else {
- fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), DEBUG_END);
- }
- }
-}
-
-void OldStyleLogProvider::Info(const char *message,
- const char *filename,
- int line,
- const char *function)
+void OldStyleLogProvider::Log(AbstractLogProvider::LogLevel level,
+ const char *message,
+ const char *fileName,
+ int line,
+ const char *function) const
{
- if (m_showInfo) {
- if (m_printStdErr) {
- fprintf(stderr, "%s%s%s\n", INFO_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), INFO_END);
- } else {
- fprintf(stdout, "%s%s%s\n", INFO_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), INFO_END);
- }
- }
-}
-
-void OldStyleLogProvider::Warning(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- if (m_showWarning) {
- if (m_printStdErr) {
- fprintf(stderr, "%s%s%s\n", WARNING_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), WARNING_END);
- } else {
- fprintf(stdout, "%s%s%s\n", WARNING_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), WARNING_END);
- }
- }
-}
+ try {
+ const struct ColorMark& mark = consoleLevel.at(level);
-void OldStyleLogProvider::Error(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- if (m_showError) {
- if (m_printStdErr) {
- fprintf(stderr, "%s%s%s\n", ERROR_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), ERROR_END);
- } else {
- fprintf(stdout, "%s%s%s\n", ERROR_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), ERROR_END);
- }
+ std::ostringstream val;
+ val << mark.begin << std::string("[") << GetFormattedTime() << std::string("] [") <<
+ static_cast<unsigned long>(pthread_self()) << "/" << static_cast<int>(getpid()) <<
+ std::string("] [") << LocateSourceFileName(fileName) << std::string(":") << line <<
+ std::string("] ") << function << std::string("(): ") << message << mark.end;
+ fprintf(stdout, "%s\n", val.str().c_str());
+ } catch (const std::out_of_range&) {
+ fprintf(stdout, "Unsupported log level: %d\n", level);
}
-}
-void OldStyleLogProvider::Pedantic(const char *message,
- const char *filename,
- int line,
- const char *function)
-{
- if (m_showPedantic) {
- if (m_printStdErr) {
- fprintf(stderr, "%s%s%s\n", PEDANTIC_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), PEDANTIC_END);
- } else {
- fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN,
- FormatMessage(message, filename, line,
- function).c_str(), PEDANTIC_END);
- }
- }
}
}
Sockets=central-key-manager-api-storage.socket
Sockets=central-key-manager-api-control.socket
Sockets=central-key-manager-api-ocsp.socket
+EnvironmentFile=-/etc/sysconfig/central-key-manager
[Install]
WantedBy=multi-user.target