Add logging helper.
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Thu, 22 May 2014 10:51:00 +0000 (12:51 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:08 +0000 (14:19 +0200)
Change-Id: Ib4006dd6a813e9de795333458eb5fa3620aa46d9

src/CMakeLists.txt
src/common/CMakeLists.txt
src/common/log/log.cpp [new file with mode: 0644]
src/common/log/log.h [new file with mode: 0644]

index c113c5f..9c4a23d 100644 (file)
@@ -18,6 +18,7 @@
 
 PKG_CHECK_MODULES(CYNARA_DEP
     libsystemd-daemon
+    libsystemd-journal
     REQUIRED
     )
 
index 8ea3a3e..f609e17 100644 (file)
@@ -23,6 +23,7 @@ SET(COMMON_PATH ${CYNARA_PATH}/common)
 
 SET(COMMON_SOURCES
     ${COMMON_PATH}/common.cpp
+    ${COMMON_PATH}/log/log.cpp
     )
 
 ADD_LIBRARY(${TARGET_CYNARA_COMMON} SHARED ${COMMON_SOURCES})
diff --git a/src/common/log/log.cpp b/src/common/log/log.cpp
new file mode 100644 (file)
index 0000000..5a84f4f
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.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 log.cpp
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Simple file containing defintion of logging level variable.
+ */
+
+#include "log.h"
+
+#include <stdlib.h>
+
+#ifdef BUILD_TYPE_DEBUG
+int __log_level = LOG_DEBUG;
+#else
+int __log_level = LOG_ERR;
+#endif
+
+static int strlog2intlog(const char *strlog) {
+    if(!strncmp("LOG_EMERG", strlog, strlen("LOG_EMERG")))
+        return LOG_EMERG;
+    if(!strncmp("LOG_ALERT", strlog, strlen("LOG_ALERT")))
+        return LOG_ALERT;
+    if(!strncmp("LOG_CRIT", strlog, strlen("LOG_CRIT")))
+        return LOG_CRIT;
+    if(!strncmp("LOG_ERR", strlog, strlen("LOG_ERR")))
+        return LOG_ERR;
+    if(!strncmp("LOG_WARNING", strlog, strlen("LOG_WARNING")))
+        return LOG_WARNING;
+    if(!strncmp("LOG_NOTICE", strlog, strlen("LOG_NOTICE")))
+        return LOG_NOTICE;
+    if(!strncmp("LOG_INFO", strlog, strlen("LOG_INFO")))
+        return LOG_INFO;
+    if(!strncmp("LOG_DEBUG", strlog, strlen("LOG_DEBUG")))
+        return LOG_DEBUG;
+
+    return LOG_ERR;
+}
+
+void init_log(void) {
+    char *env_val = getenv("CYNARA_LOG_LEVEL");
+    if (env_val) {
+        __log_level = strlog2intlog(env_val);
+    }
+}
diff --git a/src/common/log/log.h b/src/common/log/log.h
new file mode 100644 (file)
index 0000000..6f1eca4
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Lukasz Wojciechowski <l.wojciechow@partner.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        log.h
+ * @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file defines logging utilities.
+ */
+
+#ifndef CYNARA_COMMON_LOG_H
+#define CYNARA_COMMON_LOG_H
+
+#include <systemd/sd-journal.h>
+
+extern int __log_level;
+
+#define __LOG(LEVEL, ...) \
+    do { \
+        if(LEVEL <= __log_level) \
+            sd_journal_print(LEVEL, __VA_ARGS__); \
+    } while (0)
+
+#define LEGM(...)  __LOG(LOG_EMERG, __VA_ARGS__)   /* system is unusable */
+#define LOGA(...)  __LOG(LOG_ALERT, __VA_ARGS__)   /* action must be taken immediately */
+#define LOGC(...)  __LOG(LOG_CRIT, __VA_ARGS__)    /* critical conditions */
+#define LOGE(...)  __LOG(LOG_ERR, __VA_ARGS__)     /* error conditions */
+#define LOGW(...)  __LOG(LOG_WARNING, __VA_ARGS__) /* warning conditions */
+#define LOGN(...)  __LOG(LOG_NOTICE, __VA_ARGS__)  /* normal but significant condition */
+#define LOGI(...)  __LOG(LOG_INFO, __VA_ARGS__)    /* informational */
+#define LOGD(...)  __LOG(LOG_DEBUG, __VA_ARGS__)   /* debug-level messages */
+
+void init_log(void);
+
+#endif /* CYNARA_COMMON_LOG_H */