Add debug message control by environment variable 43/11443/5
authorzheng_wenlong <wenlong_zheng@nexty-ele.com>
Mon, 23 Oct 2017 05:35:58 +0000 (14:35 +0900)
committerZheng Wenlong <wenlong_zheng@nexty-ele.com>
Thu, 26 Oct 2017 08:58:09 +0000 (08:58 +0000)
    Add HMI_DEBUG to print debug message,
    It is controlled by the USE_HMI_DEBUG environment variable.

BUG-AGL: SPEC-998
Change-Id: I4b948f3af0d3a1b5e8707518e5b83a1029df3667
Signed-off-by: zheng_wenlong <wenlong_zheng@nexty-ele.com>
include/hmi-debug.h [new file with mode: 0644]
src/libhomescreen.cpp

diff --git a/include/hmi-debug.h b/include/hmi-debug.h
new file mode 100644 (file)
index 0000000..3240171
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
+ *
+ * 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 __HMI_DEBUG_H__
+#define __HMI_DEBUG_H__
+
+#include <time.h>
+#include <stdio.h>
+#include <string.h>
+#include <afb/afb-binding.h>
+
+enum LOG_LEVEL{
+    LOG_LEVEL_NONE = 0,
+    LOG_LEVEL_ERROR,
+    LOG_LEVEL_WARNING,
+    LOG_LEVEL_NOTICE,
+    LOG_LEVEL_INFO,
+    LOG_LEVEL_DEBUG,
+    LOG_LEVEL_MAX = LOG_LEVEL_ERROR
+};
+
+#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
+
+#define HMI_ERROR(prefix, args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, prefix, args, ##__VA_ARGS__)
+#define HMI_WARNING(prefix, args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
+#define HMI_NOTICE(prefix, args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
+#define HMI_INFO(prefix, args,...)  _HMI_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
+#define HMI_DEBUG(prefix, args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
+
+static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
+
+static void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
+{
+    const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?0:atoi(getenv("USE_HMI_DEBUG"));
+    if(log_level < level)
+    {
+        return;
+    }
+
+    char *message;
+    struct timespec tp;
+    unsigned int time;
+
+    clock_gettime(CLOCK_REALTIME, &tp);
+       time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
+
+       va_list args;
+       va_start(args, log);
+       if (log == NULL || vasprintf(&message, log, args) < 0)
+        message = NULL;
+    fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
+    va_end(args);
+       free(message);
+}
+
+#endif  //__HMI_DEBUG_H__
\ No newline at end of file
index ef73cbc..90ecec2 100644 (file)
 #include <cstring>
 
 #include <libhomescreen.hpp>
-
-#define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
-#define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__)
+#include "hmi-debug.h"
 
 using namespace std;
 
-static void _DLOG(const char* func, const int line, const char* log, ...);
-static void _ELOG(const char* func, const int line, const char* log, ...);
 static bool has_verb(const string& verb);
 static const char API[] = "homescreen";
 
@@ -126,16 +122,16 @@ int LibHomeScreen::init(const int port, const string& token)
        }
        else
        {
-               ELOG("port and token should be > 0, Initial port and token uses.");
+               HMI_ERROR("libhomescreen","port and token should be > 0, Initial port and token uses.");
        }
 
        ret = initialize_websocket();
        if(ret != 0 )
        {
-               ELOG("Failed to initialize websocket");
+               HMI_ERROR("libhomescreen","Failed to initialize websocket");
        }
        else{
-               DLOG("Initialized");
+               HMI_DEBUG("libhomescreen","Initialized");
        }
 
        this->runEventloop();
@@ -175,7 +171,7 @@ int LibHomeScreen::initialize_websocket()
        int ret = sd_event_default(&mploop);
        if(ret < 0)
        {
-               ELOG("Failed to create event loop");
+               HMI_ERROR("libhomescreen","Failed to create event loop");
                goto END;
        }
 
@@ -187,7 +183,7 @@ int LibHomeScreen::initialize_websocket()
        sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
        if(sp_websock == NULL)
        {
-               ELOG("Failed to create websocket connection");
+               HMI_ERROR("libhomescreen","Failed to create websocket connection");
                goto END;
        }
 
@@ -206,7 +202,7 @@ END:
 static void *event_loop_run(void *args)
 {
        struct sd_event* loop = (struct sd_event*)(args);
-       DLOG("start eventloop");
+       HMI_DEBUG("libhomescreen","start eventloop");
        for(;;)
                sd_event_run(loop, 30000000);
 }
@@ -231,7 +227,7 @@ int LibHomeScreen::runEventloop()
            int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop);
                if(ret != 0)
                {
-                       ELOG("Cannot run eventloop due to error:%d", errno);
+                       HMI_ERROR("libhomescreen","Cannot run eventloop due to error:%d", errno);
                        return -1;
                }
                else
@@ -239,18 +235,18 @@ int LibHomeScreen::runEventloop()
        }
        else
        {
-               ELOG("Connecting is not established yet");
+               HMI_ERROR("libhomescreen","Connecting is not established yet");
                return -1;
        }
 }
 
 /**
- * ショートカットアイコンがタップされたイベントの発行
+ * Sending ShortCut Icon tapped event
  *
- * HomeScreenアプリケーションにて各アプリアイコンがタップされたときにイベントを発行する
+ * When HomeScreen shortcut area is tapped, sending a event
  *
  * #### Parameters
- * - application_name [in] : タップされたアプリケーションの名前(label)
+ * - application_name [in] : Tapped application name (label)
  *
  * #### Return
  * - Returns 0 on success or -1 in case of error.
@@ -343,12 +339,12 @@ int LibHomeScreen::call(const string& verb, struct json_object* arg)
        }
        if (!has_verb(verb))
        {
-               ELOG("verb doesn't exit");
+               HMI_ERROR("libhomescreen","verb doesn't exit");
                return -1;
        }
        ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this);
        if (ret < 0) {
-               ELOG("Failed to call verb:%s",verb.c_str());
+               HMI_ERROR("libhomescreen","Failed to call verb:%s",verb.c_str());
        }
        return ret;
 }
@@ -377,12 +373,12 @@ int LibHomeScreen::call(const char* verb, struct json_object* arg)
        }
        if (!has_verb(string(verb)))
        {
-               ELOG("verb doesn't exit");
+               HMI_ERROR("libhomescreen","verb doesn't exit");
                return -1;
        }
        ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this);
        if (ret < 0) {
-               ELOG("Failed to call verb:%s",verb);
+               HMI_ERROR("libhomescreen","Failed to call verb:%s",verb);
        }
        return ret;
 }
@@ -411,7 +407,7 @@ int LibHomeScreen::subscribe(const string& event_name)
 
        int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
        if (ret < 0) {
-               ELOG("Failed to call verb:%s",__FUNCTION__);
+               HMI_ERROR("libhomescreen","Failed to call verb");
        }
        return ret;
 }
@@ -440,7 +436,7 @@ int LibHomeScreen::unsubscribe(const string& event_name)
 
        int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this);
        if (ret < 0) {
-               ELOG("Failed to call verb:%s",__FUNCTION__);
+               HMI_ERROR("libhomescreen","Failed to call verb");
        }
        return ret;
 }
@@ -449,7 +445,7 @@ int LibHomeScreen::unsubscribe(const string& event_name)
 
 void LibHomeScreen::on_hangup(void *closure, struct afb_wsj1 *wsj)
 {
-       DLOG("%s called", __FUNCTION__);
+       HMI_DEBUG("libhomescreen","called");
        if(onHangup != nullptr)
        {
                onHangup();
@@ -468,9 +464,9 @@ void LibHomeScreen::on_call(void *closure, const char *api, const char *verb, st
 */
 void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
 {
-       cout << "[libhomescreen on_event]: " << event << " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
+       HMI_DEBUG("libhomescreen","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
 
-  if (strstr(event, API) == NULL) {
+       if (strstr(event, API) == NULL) {
                return;
        }
 
@@ -520,7 +516,7 @@ void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_m
  */
 void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
 {
-       cout << "[libhomescreen on_reply]: " <<  " (" << afb_wsj1_msg_object_s(msg) << ")" << endl;
+       HMI_DEBUG("libhomescreen","msg: (%s)", afb_wsj1_msg_object_s(msg));
        if(onReply != nullptr)
        {
                struct json_object* reply = afb_wsj1_msg_object_j(msg);
@@ -530,35 +526,9 @@ void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
        }
 }
 
-/* Internal Function in libhomescreen */
-
-static void _ELOG(const char* func, const int line, const char* log, ...)
-{
-       char *message;
-       va_list args;
-       va_start(args, log);
-       if (log == NULL || vasprintf(&message, log, args) < 0)
-               message = NULL;
-       cout << "[libhomescreen ERROR]" << func << "(" << line << "):" << message << endl;
-       va_end(args);
-       free(message);
-}
-
-static void _DLOG(const char* func, const int line, const char* log, ...)
-{
-       char *message;
-       va_list args;
-       va_start(args, log);
-       if (log == NULL || vasprintf(&message, log, args) < 0)
-               message = NULL;
-       cout << "[libhomescreen DEBUG]" << func << "(" << line << "):" << message << endl;
-       va_end(args);
-       free(message);
-}
-
 static bool has_verb(const string& verb)
 {
-       DLOG("verb is %s", verb.c_str());
+       HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());
        if(find(LibHomeScreen::api_list.begin(), LibHomeScreen::api_list.end(), verb) != LibHomeScreen::api_list.end())
                return true;
        else