improve the nlp service code 79/185379/5
authorzhangzg <zg84.zhang@samsung.com>
Mon, 30 Jul 2018 05:40:44 +0000 (13:40 +0800)
committerzhangzg <zg84.zhang@samsung.com>
Tue, 31 Jul 2018 03:18:16 +0000 (11:18 +0800)
1.improve log
2.add null check for input parameter

Change-Id: Id6dc656bac4f222f3c6498679ce95ef3e32ac737

service/inc/nlp_log.h [new file with mode: 0644]
service/inc/service.h
service/src/service.c

diff --git a/service/inc/nlp_log.h b/service/inc/nlp_log.h
new file mode 100644 (file)
index 0000000..c9cbd57
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef __NLP_LOG_H__
+#define __NLP_LOG_H__
+
+#include <dlog.h>
+#include <string.h>
+
+#ifdef  LOG_TAG
+#undef  LOG_TAG
+#endif
+#define LOG_TAG "NLP_SERVICE"
+
+#define COLOR_RED       "\033[0;31m"
+#define COLOR_GREEN     "\033[0;32m"
+#define COLOR_BROWN     "\033[0;33m"
+#define COLOR_BLUE      "\033[0;34m"
+#define COLOR_PURPLE    "\033[0;35m"
+#define COLOR_CYAN      "\033[0;36m"
+#define COLOR_LIGHTBLUE "\033[0;37m"
+#define COLOR_END       "\033[0;m"
+
+#define PLOG(fmt, args...) LOGI(fmt, ##args)
+#define PINFO(fmt, args...) LOGI(COLOR_PURPLE"* Info * " fmt COLOR_END, ##args)
+#define PWARNING(fmt, args...) LOGI(COLOR_BLUE"* Warning * " fmt COLOR_END, ##args)
+#define PERR(fmt, args...) LOGE(COLOR_RED"* Critical * " fmt COLOR_END, ##args)
+#define PENTER() LOGI(COLOR_GREEN"BEGIN >>>>"COLOR_END)
+#define PLEAVE() LOGI(COLOR_GREEN"END <<<<"COLOR_END)
+
+#define PRET_VM(expr, val, fmt, arg...) do { \
+    if (expr) { \
+        PERR(fmt, ##arg); \
+        return (val); \
+    } \
+} while (0)
+
+#define PRET_M(expr, fmt, arg...) do { \
+    if (expr) { \
+        PERR(fmt, ##arg); \
+        return; \
+    } \
+} while (0)
+
+#define P_M(expr, fmt, arg...) do { \
+    if (expr) { \
+        PERR(fmt, ##arg); \
+    } \
+} while (0)
+
+#endif /* __NLP_LOG_H__ */
index 1930dc0..3af6cb7 100644 (file)
@@ -1,14 +1,8 @@
 #ifndef __service_H__
 #define __service_H__
 
-#include <dlog.h>
 #include <Python.h>
 
-#ifdef  LOG_TAG
-#undef  LOG_TAG
-#endif
-#define LOG_TAG "nltk_service"
-
 typedef enum {
     NLTK_CMD_NONE = -1,
     NLTK_CMD_TOKENIZE,
index 7c7955a..11fb89f 100644 (file)
@@ -1,9 +1,9 @@
 #include <tizen.h>
 #include <dlfcn.h>
 #include <service_app.h>
-#include "service.h"
-#include <dlog.h>
 #include <glib.h>
+#include "service.h"
+#include "nlp_log.h"
 #include "message.h"
 
 struct client_s {
@@ -14,24 +14,25 @@ struct client_s {
 static struct client_s *__create_client(const char *id,
         rpc_port_message_notify_cb_h cb)
 {
+    PENTER();
     struct client_s *handle;
 
     handle = calloc(1, sizeof(struct client_s));
     if (!handle) {
-        LOGE("Out of memory");
+        PERR("Out of memory");
         return NULL;
     }
 
     handle->id = strdup(id);
     if (!handle->id) {
-        LOGE("Out of memory");
+        PERR("Out of memory");
         free(handle);
         return NULL;
     }
 
     rpc_port_message_notify_cb_clone(cb, &handle->cb);
     if (!handle->cb) {
-        LOGE("Out of memory");
+        PERR("Out of memory");
         free(handle->id);
         free(handle);
         return NULL;
@@ -42,6 +43,7 @@ static struct client_s *__create_client(const char *id,
 
 static void __destroy_client(gpointer data)
 {
+    PENTER();
     struct client_s *handle = data;
 
     if (!handle)
@@ -58,26 +60,28 @@ static void __destroy_client(gpointer data)
 static void __messeage_create(rpc_port_stub_message_context_h context,
         void *user_data)
 {
+    PENTER();
     char *sender = NULL;
 
     rpc_port_stub_message_context_get_sender(context, &sender);
     if (!sender)
         return;
 
-    LOGI("[__RPC_PORT__] sender(%s)", sender);
+    PLOG("[__RPC_PORT__] sender(%s)", sender);
     free(sender);
 }
 
 static void __message_terminate(rpc_port_stub_message_context_h context,
         void *user_data)
 {
+    PENTER();
     char *sender = NULL;
 
     rpc_port_stub_message_context_get_sender(context, &sender);
     if (!sender)
         return;
 
-    LOGI("[__RPC_PORT__] sender(%s)", sender);
+    PLOG("[__RPC_PORT__] sender(%s)", sender);
     free(sender);
 }
 
@@ -87,7 +91,7 @@ static int __message_register(rpc_port_stub_message_context_h context,
 {
     struct client_s *client;
 
-    LOGI("[__RPC_PORT__] name(%s)", name);
+    PLOG("[__RPC_PORT__] name(%s)", name);
     client = __create_client(name, cb);
     if (!client)
         return -1;
@@ -103,7 +107,7 @@ static int __message_send(rpc_port_stub_message_context_h context,
     char *message = NULL;
     bundle_get_str(msg, "command", &message);
     rpc_port_stub_message_context_get_tag(context, (void *)&sender_client);
-    LOGI("[__RPC_PORT__] name(%s), msg(%s)", sender_client->id, message);
+    PLOG("[__RPC_PORT__] name(%s), msg(%s)", sender_client->id, message);
 
     bundle *reply = bundle_create();
     char *info = NULL;
@@ -161,7 +165,7 @@ static int __message_send(rpc_port_stub_message_context_h context,
             }
             bundle_add_str(reply, "command", "word_tokenize");
             bundle_add_str_array(reply, "return_token", tokens, len);
-            LOGD("word_tokenize process done");
+            PINFO("word_tokenize process done");
             break;
         case NLTK_CMD_POSTAG:
             args = nltk_make_args_from_string(info);
@@ -183,7 +187,7 @@ static int __message_send(rpc_port_stub_message_context_h context,
             bundle_add_str(reply, "command", "pos_tag");
             bundle_add_str_array(reply, "return_tag", tag, len);
             bundle_add_str_array(reply, "return_token", token, len);
-            LOGD("pos_tag process done");
+            PINFO("pos_tag process done");
             break;
         case NLTK_CMD_NECHUNK:
             args = nltk_make_args_from_string(info);
@@ -210,7 +214,7 @@ static int __message_send(rpc_port_stub_message_context_h context,
             bundle_add_str(reply, "command", "ne_chunk");
             bundle_add_str_array(reply, "return_tag", s_tag, len);
             bundle_add_str_array(reply, "return_token", s_token, len);
-            LOGD("ne_chunk process done");
+            PINFO("ne_chunk process done");
             break;
         case NLTK_CMD_LEMMATIZE:
             args = nltk_make_args_from_string(info);
@@ -223,7 +227,7 @@ static int __message_send(rpc_port_stub_message_context_h context,
             strcpy(lem_buf[0] , nltk_get_string_from_element(lm_result));
             bundle_add_str(reply, "command", "lemmatize");
             bundle_add_str_array(reply, "return_token", lem_buf, 1);
-            LOGD("lemmatize process done");
+            PINFO("lemmatize process done");
             break;
         case NLTK_CMD_LANGDETECT:
             args = nltk_make_args_from_string(info);
@@ -234,9 +238,10 @@ static int __message_send(rpc_port_stub_message_context_h context,
             strcpy(lang_buf[0] , nltk_get_string_from_element(ld_result));
             bundle_add_str(reply, "command", "langdetect");
             bundle_add_str_array(reply, "return_token", lang_buf, 1);
-            LOGD("langdetect process done");
+            PINFO("langdetect process done");
             break;
         default:
+            PWARNING("unkonw command, command = [%s]", message);
             bundle_add_str(reply, "command", "Exception happens");
             break;
     }
@@ -254,7 +259,7 @@ static void __message_unregister(rpc_port_stub_message_context_h context,
     if (client == NULL)
         return;
     rpc_port_stub_message_context_set_tag(context, NULL);
-    LOGI("[__RPC_PORT__] name(%s)", client->id);
+    PLOG("[__RPC_PORT__] name(%s)", client->id);
     __destroy_client(client);
 }
 
@@ -263,11 +268,11 @@ bool service_app_create(void *data)
 {
     nltk_initialize();
     globe_nltk = nltk_get_module("nltk");
-    LOGD("nltk library loaded success: ");
+    PINFO("nltk library loaded success: ");
     globe_lemm = nltk_get_module("nltk.stem");
-    LOGD("nltk stem library loaded success: ");
+    PINFO("nltk stem library loaded success: ");
     globe_lang = nltk_get_module("langdetect");
-    LOGD("langdetect library loaded success: ");
+    PINFO("langdetect library loaded success: ");
 
     int ret;
     rpc_port_stub_message_callback_s callback = {
@@ -344,16 +349,19 @@ int main(int argc, char* argv[])
 
 void nltk_initialize()
 {
+    PENTER();
     Py_Initialize();
 }
 
 void nltk_finalize()
 {
+    PENTER();
     Py_Finalize();
 }
 
 PyObject* nltk_get_module(char* name)
 {
+    PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
     return PyImport_ImportModuleNoBlock(name);
 }
 
@@ -405,6 +413,7 @@ PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index)
 
 PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
 {
+    PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
     PyObject* element;
     if PyTuple_Check(tuple)
     {
@@ -427,6 +436,7 @@ PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
 
 char* nltk_get_string_from_element(PyObject* elm)
 {
+    PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
     char* ch = (char*) malloc(255);
     strcpy(ch, PyString_AsString(elm));
     return ch;
@@ -434,11 +444,14 @@ char* nltk_get_string_from_element(PyObject* elm)
 
 PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
 {
+    PRET_VM(!module, NULL, "Input parameter [module] is NULL!");
+    PRET_VM(!func_name, NULL, "Input parameter [func_name] is NULL!");
     return PyObject_GetAttrString(module, func_name);
 }
 
 PyObject* nltk_make_args_from_string(char* info)
 {
+    PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
     PyObject *pArgs;
     //create args tuple struct to fill the arg one by one  ,here , only create one string with 1
     pArgs = PyTuple_New(1);
@@ -448,6 +461,7 @@ PyObject* nltk_make_args_from_string(char* info)
 
 PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
 {
+    PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
     PyObject *pArgs;
     //create args tuple struct to fill the arg one by one  ,here ,only create one python object with 1
     pArgs = PyTuple_New(1);
@@ -458,6 +472,7 @@ PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
 
 PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
 {
+    PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
     return PyObject_CallObject(func, args);
 }