From e121084fb943222785b6f45d91f61875e0ac7e40 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Wed, 24 Sep 2014 14:35:05 -0700 Subject: [PATCH] c_logger implementation Patch 2: rebase on master Patch 3: adding license headers to missing files Patch 4: rebase on master Change-Id: I8c3d16000432df1de4b08633142ecc01ee423e0d --- README | 2 +- csdk/libcoap-4.1.1/makefile | 11 +- csdk/logger/include/logger.h | 31 +++- csdk/logger/src/logger.c | 34 +++- csdk/makefile | 3 + .../arduino/SimpleClientServer/ocserver/makefile | 3 +- .../SimpleClientServer/ocserver/makefiledue | 3 +- .../samples/linux/SimpleClientServer/makefile | 3 + csdk/stack/test/linux/makefile | 2 + examples/makefile | 28 +-- examples/ocicuc/Makefile | 39 +++- include/IClientWrapper.h | 9 + include/IServerWrapper.h | 7 + include/InProcClientWrapper.h | 5 +- include/InProcServerWrapper.h | 2 +- include/OCApi.h | 1 + include/OCPlatform.h | 9 + include/OCResourceResponse.h | 46 ++++- include/OutOfProcClientWrapper.h | 4 +- include/OutOfProcServerWrapper.h | 4 +- include/WrapperFactory.h | 16 +- makefile | 10 +- oc_logger/Makefile | 55 ++++++ oc_logger/c/Makefile | 52 ++++++ oc_logger/c/oc_console_logger.c | 100 +++++++++++ oc_logger/c/oc_logger.c | 159 ++++++++++++++++ oc_logger/cpp/Makefile | 47 +++++ oc_logger/cpp/oc_ostream_logger.cpp | 199 +++++++++++++++++++++ oc_logger/examples/Makefile | 56 ++++++ oc_logger/examples/test_logging.c | 82 +++++++++ oc_logger/examples/test_logging.cpp | 96 ++++++++++ oc_logger/include/oc_console_logger.h | 43 +++++ oc_logger/include/oc_log.hpp | 27 +++ oc_logger/include/oc_log_stream.hpp | 80 +++++++++ oc_logger/include/oc_logger.h | 54 ++++++ oc_logger/include/oc_logger.hpp | 31 ++++ oc_logger/include/oc_logger_types.h | 84 +++++++++ oc_logger/include/targets/oc_console_logger.h | 43 +++++ oc_logger/include/targets/oc_ostream_logger.h | 49 +++++ oc_logger/samples/linux/README | 31 ++++ oc_logger/samples/linux/makefile | 94 ++++++++++ oc_logger/samples/linux/test_logging.c | 80 +++++++++ oc_logger/test/test_logging.cpp | 54 ++++++ src/InProcClientWrapper.cpp | 105 +++++------ src/InProcServerWrapper.cpp | 70 ++------ src/OCPlatform.cpp | 34 ++-- 46 files changed, 1822 insertions(+), 175 deletions(-) create mode 100644 oc_logger/Makefile create mode 100644 oc_logger/c/Makefile create mode 100644 oc_logger/c/oc_console_logger.c create mode 100644 oc_logger/c/oc_logger.c create mode 100644 oc_logger/cpp/Makefile create mode 100644 oc_logger/cpp/oc_ostream_logger.cpp create mode 100644 oc_logger/examples/Makefile create mode 100644 oc_logger/examples/test_logging.c create mode 100644 oc_logger/examples/test_logging.cpp create mode 100644 oc_logger/include/oc_console_logger.h create mode 100644 oc_logger/include/oc_log.hpp create mode 100644 oc_logger/include/oc_log_stream.hpp create mode 100644 oc_logger/include/oc_logger.h create mode 100644 oc_logger/include/oc_logger.hpp create mode 100644 oc_logger/include/oc_logger_types.h create mode 100644 oc_logger/include/targets/oc_console_logger.h create mode 100644 oc_logger/include/targets/oc_ostream_logger.h create mode 100644 oc_logger/samples/linux/README create mode 100644 oc_logger/samples/linux/makefile create mode 100644 oc_logger/samples/linux/test_logging.c create mode 100644 oc_logger/test/test_logging.cpp diff --git a/README b/README index 2f789a5..234f15d 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ - +This file is completely obsolete. :-) Builds with g++ 4.6, 4.8, 4.9. diff --git a/csdk/libcoap-4.1.1/makefile b/csdk/libcoap-4.1.1/makefile index 2d0b938..4a1469c 100644 --- a/csdk/libcoap-4.1.1/makefile +++ b/csdk/libcoap-4.1.1/makefile @@ -34,12 +34,14 @@ OBJ_DIR := $(OUT_DIR)/obj ROOT_DIR = .. OCSOCK_DIR = $(ROOT_DIR)/ocsocket LOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger RANDOM_DIR = $(ROOT_DIR)/ocrandom STACK_DIR = $(ROOT_DIR)/stack OCMALLOC_DIR = $(ROOT_DIR)/ocmalloc -INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(RANDOM_DIR)/include -I$(OCMALLOC_DIR)/include -# Note for Arduino: The CC flag is set to the C++ compiler since Arduino build +INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(RANDOM_DIR)/include -I$(OCMALLOC_DIR)/include -I$(OC_LOG_DIR)/include + +# Note for Arduino: The CC flag is set to the C++ compiler since Arduino build # includes Time.h header file which has C++ style definitions. ifeq ($(PLATFORM),android) CXX=arm-linux-androideabi-g++ @@ -71,8 +73,9 @@ CC_FLAGS.debug := -O0 -g3 -Wall -ffunction-sections -fdata-sections -fno-excepti CC_FLAGS.release := -Os -Wall -ffunction-sections -fdata-sections -fno-exceptions SOURCES:= pdu.c net.c debug.c encode.c uri.c coap_list.c resource.c hashkey.c \ - str.c option.c async.c subscribe.c block.c logger.c ocrandom.c ocmalloc.c -VPATH := $(OCSOCK_DIR)/src:$(LOGGER_DIR)/src:$(RANDOM_DIR)/src:$(OCMALLOC_DIR)/src + str.c option.c async.c subscribe.c block.c logger.c ocrandom.c ocmalloc.c oc_logger.c oc_console_logger.c +VPATH := $(OCSOCK_DIR)/src:$(LOGGER_DIR)/src:$(RANDOM_DIR)/src:$(OCMALLOC_DIR)/src:$(OC_LOG_DIR)/c + ifeq (arduino, $(findstring arduino,$(PLATFORM))) ifeq ($(ARDUINOWIFI),1) SOURCES += ocsocket_arduino_wifi.c diff --git a/csdk/logger/include/logger.h b/csdk/logger/include/logger.h index 6009984..b475464 100644 --- a/csdk/logger/include/logger.h +++ b/csdk/logger/include/logger.h @@ -24,6 +24,8 @@ #include #include #include +#include "oc_logger.h" +#include "oc_console_logger.h" #ifdef __ANDROID__ #include @@ -58,6 +60,26 @@ typedef enum { #if defined(__ANDROID__) || defined(__linux__) + + /** + * Configure logger to use a context that defines a custom logger function + * + * @param ctx - pointer to oc_log_ctx_t struct that defines custom logging functions + */ + void OCLogConfig(oc_log_ctx_t *ctx); + + /** + * Initialize the logger. Optional on Android and Linux. Configures serial port on Arduino + */ + void OCLogInit(); + + /** + * Called to Free dyamically allocated resources used with custom logging. + * Not necessary if default logging is used + * + */ + void OCLogShutdown(); + /** * Output a variable argument list log string with the specified priority level. * Only defined for Linux and Android @@ -128,22 +150,27 @@ typedef enum { #ifdef TB_LOG // These macros are defined for Linux, Android, and Arduino + #define OC_LOG_INIT() OCLogInit() #define OC_LOG(level, tag, logStr) OCLog((level), (tag), (logStr)) #define OC_LOG_BUFFER(level, tag, buffer, bufferSize) OCLogBuffer((level), (tag), (buffer), (bufferSize)) #ifdef ARDUINO + #define OC_LOG_CONFIG(ctx) + #define OC_LOG_SHUTDOWN() // Use full namespace for logInit to avoid function name collision #define OC_LOG_INIT() OCLogInit() // Don't define variable argument log function for Arduino #define OC_LOG_V(level, tag, ...) OCLogv((level), (tag), __VA_ARGS__) #else - // Don't define LOG_INIT for Linux and Android - #define OC_LOG_INIT() + #define OC_LOG_CONFIG(ctx) OCLogConfig((ctx)) + #define OC_LOG_SHUTDOWN() OCLogShutdown() // Define variable argument log function for Linux and Android #define OC_LOG_V(level, tag, ...) OCLogv((level), (tag), __VA_ARGS__) #endif #else + #define OC_LOG_CONFIG(ctx) + #define OC_LOG_SHUTDOWN() #define OC_LOG(level, tag, logStr) #define OC_LOG_V(level, tag, ...) #define OC_LOG_BUFFER(level, tag, buffer, bufferSize) diff --git a/csdk/logger/src/logger.c b/csdk/logger/src/logger.c index cdbe403..065b29b 100644 --- a/csdk/logger/src/logger.c +++ b/csdk/logger/src/logger.c @@ -21,6 +21,12 @@ #include "logger.h" #include "string.h" +#include "oc_logger.h" +#include "oc_console_logger.h" + +static oc_log_ctx_t *logCtx = 0; + +static oc_log_level LEVEL_XTABLE[] = {OC_LOG_DEBUG, OC_LOG_INFO, OC_LOG_WARNING, OC_LOG_ERROR, OC_LOG_FATAL}; static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination @@ -46,6 +52,24 @@ static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1; // Show 16 bytes, 2 #if defined(__ANDROID__) || defined(__linux__) + +void OCLogConfig(oc_log_ctx_t *ctx) { + logCtx = ctx; +} + +void OCLogInit() { + +} + +void OCLogShutdown() { +#ifdef __linux__ + if (logCtx && logCtx->destroy) + { + logCtx->destroy(logCtx); + } +#endif +} + /** * Output a variable argument list log string with the specified priority level. * Only defined for Linux and Android @@ -83,7 +107,15 @@ void OCLog(LogLevel level, const char * tag, const char * logStr) { #ifdef __ANDROID__ __android_log_write(LEVEL[level], tag, logStr); #elif defined __linux__ - printf("%s: %s: %s\n", LEVEL[level], tag, logStr); + if (logCtx && logCtx->write_level) + { + logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr); + + } + else + { + printf("%s: %s: %s\n", LEVEL[level], tag, logStr); + } #endif } diff --git a/csdk/makefile b/csdk/makefile index 1565a20..d9e2b49 100644 --- a/csdk/makefile +++ b/csdk/makefile @@ -62,6 +62,7 @@ else endif OCLOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger OCRANDOM_DIR = $(ROOT_DIR)/ocrandom OCSOCKET_DIR = $(ROOT_DIR)/ocsocket LCOAP_DIR = $(ROOT_DIR)/libcoap-4.1.1 @@ -77,6 +78,7 @@ OCMALLOC_SRC = $(OCMALLOC_DIR)/src CJSON_SRC = $(CJSON_DIR) OCLOGGER_INC = $(OCLOGGER_DIR)/include +OC_LOG_INC = $(OC_LOG_DIR)/include OCRANDOM_INC = $(OCRANDOM_DIR)/include OCSOCKET_INC = $(OCSOCKET_DIR)/include LCOAP_INC = $(LCOAP_DIR) @@ -86,6 +88,7 @@ OCMALLOC_INC = $(OCMALLOC_DIR)/include CJSON_INC = $(CJSON_DIR) INC_DIRS := -I$(OCLOGGER_INC) +INC_DIRS += -I$(OC_LOG_INC) INC_DIRS += -I$(OCRANDOM_INC) INC_DIRS += -I$(OCSOCKET_INC) INC_DIRS += -I$(LCOAP_INC) diff --git a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile index 5e134eb..c791775 100644 --- a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile +++ b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefile @@ -32,6 +32,7 @@ OBJ_DIR := ./bin ROOT_DIR = ../../../../.. LOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger TBSTACK_DIR = $(ROOT_DIR)/stack TBSOCKET_DIR = $(ROOT_DIR)/ocsocket @@ -44,7 +45,7 @@ VPATH := $(SDIR_ARD_PLATFORM) OCSOCK_DIR = $(ROOT_DIR)/ocsocket LOGGER_DIR = $(ROOT_DIR)/logger STACK_DIR = $(ROOT_DIR)/stack -INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(STACK_DIR)/include +INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(OC_LOG_DIR)/include -I$(LOGGER_DIR)/include -I$(STACK_DIR)/include CFLAGS := -Os -Wall -c -DTB_LOG diff --git a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefiledue b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefiledue index 3c436fb..9ea530e 100644 --- a/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefiledue +++ b/csdk/stack/samples/arduino/SimpleClientServer/ocserver/makefiledue @@ -32,6 +32,7 @@ OBJ_DIR := ./bin ROOT_DIR = ../../../../.. LOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger TBSTACK_DIR = $(ROOT_DIR)/stack TBSOCKET_DIR = $(ROOT_DIR)/ocsocket @@ -44,7 +45,7 @@ VPATH := $(SDIR_ARD_PLATFORM) OCSOCK_DIR = $(ROOT_DIR)/ocsocket LOGGER_DIR = $(ROOT_DIR)/logger STACK_DIR = $(ROOT_DIR)/stack -INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(LOGGER_DIR)/include -I$(STACK_DIR)/include +INC_DIRS = -I$(OCSOCK_DIR)/include/ -I$(OC_LOG_DIR)/include -I$(LOGGER_DIR)/include -I$(STACK_DIR)/include CFLAGS := -Os -Wall -c -DTB_LOG diff --git a/csdk/stack/samples/linux/SimpleClientServer/makefile b/csdk/stack/samples/linux/SimpleClientServer/makefile index 000c6f9..91d8678 100644 --- a/csdk/stack/samples/linux/SimpleClientServer/makefile +++ b/csdk/stack/samples/linux/SimpleClientServer/makefile @@ -33,6 +33,7 @@ endif OUT_DIR = $(PWD) OCLOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger OCRANDOM_DIR = $(ROOT_DIR)/ocrandom OCSOCKET_DIR = $(ROOT_DIR)/ocsocket LCOAP_DIR = $(ROOT_DIR)/libcoap-4.1.1 @@ -40,6 +41,7 @@ OCCOAP_DIR = $(ROOT_DIR)/occoap OCTBSTACK_DIR = $(ROOT_DIR)/stack OCLOGGER_INC = $(OCLOGGER_DIR)/include +OC_LOG_INC = $(OC_LOG_DIR)/include OCRANDOM_INC = $(OCRANDOM_DIR)/include OCSOCKET_INC = $(OCSOCKET_DIR)/include LCOAP_INC = $(LCOAP_DIR) @@ -47,6 +49,7 @@ OCCOAP_INC = $(OCCOAP_DIR)/include OCTBSTACK_INC = $(OCTBSTACK_DIR)/include INC_DIRS := -I$(OCLOGGER_INC) +INC_DIRS += -I$(OC_LOG_INC) INC_DIRS += -I$(OCRANDOM_INC) INC_DIRS += -I$(OCSOCKET_INC) INC_DIRS += -I$(LCOAP_INC) diff --git a/csdk/stack/test/linux/makefile b/csdk/stack/test/linux/makefile index 1ff017c..e7c2844 100644 --- a/csdk/stack/test/linux/makefile +++ b/csdk/stack/test/linux/makefile @@ -31,6 +31,7 @@ ROOT_DIR = ../../.. OCSOCK_DIR = $(ROOT_DIR)/ocsocket OCSTACK_DIR = $(ROOT_DIR)/stack LOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger RANDOM_DIR = $(ROOT_DIR)/ocrandom OBJ_DIR = ./ @@ -38,6 +39,7 @@ OBJ_DIR = ./ INC_DIRS := -I$(OCSTACK_DIR)/include INC_DIRS += -I$(OCSOCK_DIR)/include INC_DIRS += -I$(LOGGER_DIR)/include +INC_DIRS += -I$(OC_LOG_DIR)/include INC_DIRS += -I$(RANDOM_DIR)/include ifeq ($(PLATFORM),android) diff --git a/examples/makefile b/examples/makefile index ff86c72..0cf6d19 100644 --- a/examples/makefile +++ b/examples/makefile @@ -30,12 +30,18 @@ CXX_FLAGS.debug := -O0 -g3 -std=c++0x -Wall -pthread CXX_FLAGS.release := -O3 -std=c++0x -Wall -pthread CXX_INC := -I../include/ +CXX_INC += -I../oc_logger/include + CXX_INC += -I../csdk/stack/include CXX_INC += -I../csdk/ocsocket/include CXX_INC += -I../csdk/ocrandom/include CXX_INC += -I../csdk/logger/include CXX_INC += -I../csdk/libcoap +LIB_OC_LOGGER := ../oc_logger/lib/oc_logger.a + +CXX_LIBS := ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a $(LIB_OC_LOGGER) + # Force metatargets to build: .PHONY: prep_dirs simpleserver simpleclient simpleclientserver roomserver roomclient presenceserver presenceclient garageserver garageclient fridgeserver fridgeclient ocicuc @@ -45,37 +51,37 @@ prep_dirs: -mkdir -p $(OUT_DIR) simpleserver: simpleserver.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ simpleserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ simpleserver.cpp $(CXX_INC) $(CXX_LIBS) simpleclient: simpleclient.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ simpleclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ simpleclient.cpp $(CXX_INC) $(CXX_LIBS) fridgeserver: fridgeserver.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ fridgeserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ fridgeserver.cpp $(CXX_INC) $(CXX_LIBS) fridgeclient: fridgeclient.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ fridgeclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ fridgeclient.cpp $(CXX_INC) $(CXX_LIBS) presenceserver: presenceserver.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ presenceserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ presenceserver.cpp $(CXX_INC) $(CXX_LIBS) presenceclient: presenceclient.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ presenceclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ presenceclient.cpp $(CXX_INC) $(CXX_LIBS) simpleclientserver: simpleclientserver.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ simpleclientserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ simpleclientserver.cpp $(CXX_INC) $(CXX_LIBS) roomserver: roomserver.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ roomserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ roomserver.cpp $(CXX_INC) $(CXX_LIBS) roomclient: roomclient.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ roomclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ roomclient.cpp $(CXX_INC) $(CXX_LIBS) garageserver: garageserver.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ garageserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ garageserver.cpp $(CXX_INC) $(CXX_LIBS) garageclient: garageclient.cpp - $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ garageclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a + $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ garageclient.cpp $(CXX_INC) $(CXX_LIBS) ocicuc: diff --git a/examples/ocicuc/Makefile b/examples/ocicuc/Makefile index ecbbf0e..4328818 100644 --- a/examples/ocicuc/Makefile +++ b/examples/ocicuc/Makefile @@ -1,29 +1,50 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# +BUILD:=release + OCLIB=../.. OCLIB_LIB=../../$(BUILD)/obj/liboc.a #$(OCLIB)/release/obj/liboc.a -BOOST_BASE=/usr/local/boost -BOOST_INC=$(BOOST_BASE)/include -BOOST_LIB=$(BOOST_BASE)/lib - CXX_FLAGS.debug := -g3 -O0 CXX_FLAGS.release := -O3 -CXX_FLAGS=-Wall -std=c++0x -ggdb $(CXX_FLAGS.$(BUILD)) +CXX_FLAGS:=-Wall -std=c++0x -ggdb $(CXX_FLAGS.$(BUILD)) -pthread # There's probably nicer Makefile magic for this, but hopefully it will suffice: CXX_INC=-I$(OCLIB)/include \ - -I$(BOOST_INC) \ -I../../include/ \ + -I../../oc_logger/include/ \ -I../../csdk/stack/include \ -I../../csdk/ocsocket/include \ -I../../csdk/ocrandom/include \ -I../../csdk/logger/include -CXX_LIBS=-L$(BOOST_LIB) $(OCLIB_LIB) ../../csdk/release/liboctbstack.a -lpthread -lboost_program_options +BOOST_LIBS=-lboost_program_options + +LIB_OC_LOGGER:=../../oc_logger/lib/oc_logger.a + +CXX_LIBS=$(OCLIB_LIB) ../../csdk/$(BUILD)/liboctbstack.a $(LIB_OC_LOGGER) $(BOOST_LIBS) .PHONY: client server all: client server monoprocess - @echo Remember to \"export LD_LIBRARY_PATH=$(BOOST_LIB)\:\$$LD_LIBRARY_PATH\" %.o: %.cpp $(CXX) $(CXXFLAGS) $(CXX_FLAGS) $(CXX_INC) -c -o $@ $< @@ -38,4 +59,4 @@ monoprocess: monoprocess.o driver.o utility.o light_resource.o $(CXX) $(CXX_FLAGS) -o $@ $^ $(CXX_LIBS) clean: - rm -f *.o server client monoprocess + rm -f *\.o server client monoprocess diff --git a/include/IClientWrapper.h b/include/IClientWrapper.h index 1adee61..d8edc3a 100644 --- a/include/IClientWrapper.h +++ b/include/IClientWrapper.h @@ -28,11 +28,20 @@ namespace OC { + class OCPlatform; + class IClientWrapper : public std::enable_shared_from_this { + protected: + OCPlatform& m_owner; + public: typedef std::shared_ptr Ptr; + IClientWrapper(OCPlatform& owner) + : m_owner(owner) + {} + virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, FindCallback& callback) = 0; diff --git a/include/IServerWrapper.h b/include/IServerWrapper.h index d0f6a03..044677b 100644 --- a/include/IServerWrapper.h +++ b/include/IServerWrapper.h @@ -33,9 +33,16 @@ namespace OC { class IServerWrapper { + protected: + OCPlatform& m_owner; + public: typedef std::shared_ptr Ptr; + IServerWrapper(OCPlatform& owner) + : m_owner(owner) + {} + virtual ~IServerWrapper(){}; virtual OCStackResult registerResource( diff --git a/include/InProcClientWrapper.h b/include/InProcClientWrapper.h index 60fbe3a..5b38f1d 100644 --- a/include/InProcClientWrapper.h +++ b/include/InProcClientWrapper.h @@ -40,7 +40,7 @@ namespace OC class InProcClientWrapper : public IClientWrapper { public: - InProcClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg); + InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg); virtual ~InProcClientWrapper(); virtual OCStackResult ListenForResource(const std::string& serviceUrl, @@ -77,7 +77,8 @@ namespace OC std::weak_ptr m_csdkLock; private: - PlatformConfig m_cfg; + OC::OCPlatform& m_owner; + PlatformConfig m_cfg; }; } diff --git a/include/InProcServerWrapper.h b/include/InProcServerWrapper.h index 574374c..1ce7ef2 100644 --- a/include/InProcServerWrapper.h +++ b/include/InProcServerWrapper.h @@ -32,7 +32,7 @@ namespace OC class InProcServerWrapper : public IServerWrapper { public: - InProcServerWrapper(std::weak_ptr csdkLock, PlatformConfig cfg); + InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg); virtual ~InProcServerWrapper(); virtual OCStackResult registerResource( diff --git a/include/OCApi.h b/include/OCApi.h index 34a57af..d603a12 100644 --- a/include/OCApi.h +++ b/include/OCApi.h @@ -36,6 +36,7 @@ namespace OC { + class OCPlatform; class OCResource; class OCResourceRequest; class OCResourceResponse; diff --git a/include/OCPlatform.h b/include/OCPlatform.h index 4c2c97a..40848e1 100644 --- a/include/OCPlatform.h +++ b/include/OCPlatform.h @@ -36,6 +36,8 @@ #include "OCResourceResponse.h" #include "OCRepresentation.h" +#include "oc_logger.hpp" + namespace OC { /** @@ -46,6 +48,9 @@ namespace OC */ class OCPlatform { + private: + mutable boost::iostreams::stream m_log_stream; + public: // typedef for handle to cancel presence info with typedef OCDoHandle OCPresenceHandle; @@ -57,6 +62,7 @@ namespace OC * in-proc/out-of-proc etc. */ OCPlatform(const PlatformConfig& config); + OCPlatform(const PlatformConfig& config, OC::oc_log_stream& log_target); /** * Virtual destructor @@ -345,6 +351,9 @@ namespace OC bool isObservable, const std::vector& resourceTypes, const std::vector& interfaces); + public: + inline boost::iostreams::stream& log() const { return m_log_stream; } + private: PlatformConfig m_cfg; diff --git a/include/OCResourceResponse.h b/include/OCResourceResponse.h index a8d96fc..2bed33e 100644 --- a/include/OCResourceResponse.h +++ b/include/OCResourceResponse.h @@ -18,9 +18,9 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -/// @file OCResourceResponse.h +/// @file OCResourceResponse.h -/// @brief This file contains the declaration of classes and its members related to +/// @brief This file contains the declaration of classes and its members related to /// ResourceResponse. #ifndef __OCRESOURCERESPONSE_H @@ -279,6 +279,39 @@ namespace OC m_payload = payload.str(); } + + /** TODO remove this once after above function stabilize. + * API to set the entire resource attribute representation + * @param attributeMap reference containing the name value pairs representing the resource's attributes + */ + void setResourceRepresentation(AttributeMap& attributes) { + + // TODO To be refactored + ostringstream payload; + + payload << "{"; + + // TODO fix this (do this programmatically) + payload << "\"href\":\"/a/room\""; + + payload << ",\"rep\":{"; + + for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr) + { + if(itr != attributes.begin()) + { + payload << ','; + } + // cout << itr->first << ":" <, itr->second.front() << endl; + payload << "\""<first<<"\":\""<< itr->second.front()<<"\""; + + } + + payload << "}}"; + + m_payload = payload.str(); + } + private: std::string m_payload; int m_errorCode; @@ -286,10 +319,15 @@ namespace OC // TODO only stack should have visibility and apps should not public: + /** + * Get error code + */ + int getErrorCode() const; + /** - * Get error code + * Get the resource attribute representation */ - int getErrorCode() const; + AttributeMap& getResourceRepresentation() const; // TODO This should go away & just use getResourceRepresentation std::string getPayload() diff --git a/include/OutOfProcClientWrapper.h b/include/OutOfProcClientWrapper.h index 2a2abde..47de42a 100644 --- a/include/OutOfProcClientWrapper.h +++ b/include/OutOfProcClientWrapper.h @@ -28,7 +28,9 @@ namespace OC class OutOfProcClientWrapper : public IClientWrapper { public: - OutOfProcClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) { } + OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) + : IClientWrapper(owner) + {} virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, FindCallback& callback) {return OC_STACK_NOTIMPL;} diff --git a/include/OutOfProcServerWrapper.h b/include/OutOfProcServerWrapper.h index e276d12..c190e6c 100644 --- a/include/OutOfProcServerWrapper.h +++ b/include/OutOfProcServerWrapper.h @@ -28,7 +28,9 @@ namespace OC class OutOfProcServerWrapper : public IServerWrapper { public: - OutOfProcServerWrapper(PlatformConfig cfg) {}; + OutOfProcServerWrapper(OC::OCPlatform& owner, PlatformConfig cfg) + : IServerWrapper(owner) + {}; virtual OCStackResult registerResource( OCResourceHandle& resourceHandle, diff --git a/include/WrapperFactory.h b/include/WrapperFactory.h index 332dc51..baf60d7 100644 --- a/include/WrapperFactory.h +++ b/include/WrapperFactory.h @@ -38,8 +38,8 @@ namespace OC public: typedef std::shared_ptr Ptr; - virtual IClientWrapper::Ptr CreateClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) =0; - virtual IServerWrapper::Ptr CreateServerWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) =0; + virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) =0; + virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) =0; virtual ~IWrapperFactory(){} }; @@ -49,29 +49,29 @@ namespace OC public: WrapperFactory(){} - virtual IClientWrapper::Ptr CreateClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) + virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) { switch(cfg.serviceType) { case ServiceType::InProc: - return std::make_shared(csdkLock, cfg); + return std::make_shared(owner, csdkLock, cfg); break; case ServiceType::OutOfProc: - return std::make_shared(csdkLock, cfg); + return std::make_shared(owner, csdkLock, cfg); break; } return nullptr; } - virtual IServerWrapper::Ptr CreateServerWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) + virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) { switch(cfg.serviceType) { case ServiceType::InProc: - return std::make_shared(csdkLock, cfg); + return std::make_shared(owner, csdkLock, cfg); break; case ServiceType::OutOfProc: - // return std::make_shared(csdkLock, cfg); + throw OC::OCException("ServiceType::OutOfProc", OC_STACK_NOTIMPL); break; } return nullptr; diff --git a/makefile b/makefile index f60fc1b..41067b5 100644 --- a/makefile +++ b/makefile @@ -30,6 +30,8 @@ CXX_FLAGS.debug := -g3 -std=c++0x -Wall -pthread -O0 CXX_FLAGS.release := -std=c++0x -Wall -pthread -O3 CXX_INC := -I./include/ +CXX_INC += -I./oc_logger/include + CXX_INC += -I./csdk/stack/include CXX_INC += -I./csdk/ocsocket/include CXX_INC += -I./csdk/ocrandom/include @@ -37,7 +39,7 @@ CXX_INC += -I./csdk/logger/include CXX_INC += -I./csdk/libcoap # Force metatargets to build: -.PHONY: prep_dirs c_sdk liboc.a examples +.PHONY: prep_dirs c_sdk oc_logger liboc.a examples all: .PHONY @@ -48,7 +50,10 @@ prep_dirs: c_sdk: cd csdk && $(MAKE) "BUILD=$(BUILD)" -examples: liboc.a +oc_logger: + cd oc_logger && $(MAKE) "BUILD=$(BUILD)" + +examples: cd examples && $(MAKE) "BUILD=$(BUILD)" liboc.a: OCPlatform.o OCResource.o OCUtilities.o InProcServerWrapper.o InProcClientWrapper.o @@ -75,5 +80,6 @@ clean: clean_legacy cd csdk && $(MAKE) clean cd csdk && $(MAKE) deepclean cd examples && $(MAKE) clean + cd oc_logger && $(MAKE) clean clean_legacy: -rm -f -v $(OBJ_DIR)/liboc.a $(OBJ_DIR)/*.o diff --git a/oc_logger/Makefile b/oc_logger/Makefile new file mode 100644 index 0000000..e9aaf18 --- /dev/null +++ b/oc_logger/Makefile @@ -0,0 +1,55 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# +# override with `make BUILD=debug` +# default to release build +BUILD := release + +LIB_DIR=./lib/ +BIN_DIR=./bin/ + +.PHONY: c_lib cpp_lib static_libs examples samples + +all: dirs c_lib cpp_lib static_libs examples samples + +dirs: + -mkdir -p $(LIB_DIR) $(BIN_DIR) + +c_lib: + cd c && $(MAKE) "BUILD=$(BUILD)" + +cpp_lib: + cd cpp && $(MAKE) "BUILD=$(BUILD)" + +static_libs: + ar -cvq $(LIB_DIR)/oc_logger_core.a $(BIN_DIR)/oc_logger.o + ar -cvq $(LIB_DIR)/oc_logger.a $(BIN_DIR)/oc_logger.o $(BIN_DIR)/targets/*.o + +examples: + cd examples && $(MAKE) "BUILD=$(BUILD)" + +samples: + cd samples/linux && $(MAKE) "BUILD=$(BUILD)" + +clean: + cd examples && $(MAKE) clean + rm -rf $(BIN_DIR)/targets/*.o + cd cpp && $(MAKE) clean + cd c && $(MAKE) clean diff --git a/oc_logger/c/Makefile b/oc_logger/c/Makefile new file mode 100644 index 0000000..5d55318 --- /dev/null +++ b/oc_logger/c/Makefile @@ -0,0 +1,52 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# + +BUILD := release + +CC_FLAGS.debug=-Wall -std=c99 -Werror -O0 -g +CC_FLAGS.release=-Wall -std=c99 -Werror -O2 + +INC=-I../include/ +CC:=gcc + +BIN_DIR=../bin +LIB_DIR=../lib +TEST_DIR=../test + +LIB=$(LIB_DIR)/oc_logger.a + +CCX=$(CC) $(CC_FLAGS.$(BUILD)) + +all: setup c_lib c_targets + +setup: + mkdir -p $(BIN_DIR) $(LIB_DIR) + mkdir -p $(BIN_DIR)/targets + mkdir -p $(TEST_DIR) + +c_lib: + $(CCX) $(INC) -c -o $(BIN_DIR)/oc_logger.o oc_logger.c + +c_targets: + $(CCX) $(INC) -c -o $(BIN_DIR)/targets/oc_console_logger.o oc_console_logger.c + +clean: + rm -rf -v \*\.o test_logging $(BIN_DIR)/* $(LIB_DIR)/* diff --git a/oc_logger/c/oc_console_logger.c b/oc_logger/c/oc_console_logger.c new file mode 100644 index 0000000..a84de53 --- /dev/null +++ b/oc_logger/c/oc_console_logger.c @@ -0,0 +1,100 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "oc_logger.h" +#include "targets/oc_console_logger.h" + +#include +#include + +typedef struct +{ + FILE *out; +} oc_console_logger_ctx; + +oc_log_ctx_t *oc_make_console_logger() +{ + return oc_log_make_ctx( + NULL, + OC_LOG_ALL, + oc_console_logger_init, + oc_console_logger_destroy, + oc_console_logger_flush, + oc_console_logger_set_level, + oc_console_logger_write, + oc_console_logger_set_module + ); +} + +int oc_console_logger_init(oc_log_ctx_t *ctx, void *world) +{ + oc_console_logger_ctx *my_ctx; + + my_ctx = (oc_console_logger_ctx *)malloc(sizeof(oc_console_logger_ctx)); + + if(0 == my_ctx) + return 0; + + my_ctx->out = stderr; + + ctx->ctx = (void *)my_ctx; + + return 1; +} + +void oc_console_logger_destroy(oc_log_ctx_t *ctx) +{ + oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx; + + fflush(lctx->out); + + free(lctx); +} + +void oc_console_logger_flush(oc_log_ctx_t *ctx) +{ + oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx; + + fflush(lctx->out); +} + +void oc_console_logger_set_level(oc_log_ctx_t *ctx, const int level) +{ + /* We don't have any special thing we need to do when a log level changes. */ + return; +} + +size_t oc_console_logger_write(oc_log_ctx_t *ctx, const int level, const char *msg) +{ + oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx; + + /* A "real" implementation might want to replace the loglevel with a mnemonic: */ + + if(0 == ctx->module_name) + return 1 + fprintf(lctx->out, "%d: %s\n", level, msg); + + return 1 + fprintf(lctx->out, "%d: [%s]: %s\n", level, ctx->module_name, msg); +} + +int oc_console_logger_set_module(oc_log_ctx_t *ctx, const char *module_name) +{ + /* We don't do anything special when the module name changes: */ + return 1; +} diff --git a/oc_logger/c/oc_logger.c b/oc_logger/c/oc_logger.c new file mode 100644 index 0000000..4c3db1a --- /dev/null +++ b/oc_logger/c/oc_logger.c @@ -0,0 +1,159 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "oc_logger.h" + +#include +#include + +oc_log_ctx_t *oc_log_make_ctx( + void* world, + const oc_log_level level, + oc_log_init_t init, + oc_log_destroy_t destroy, + oc_log_flush_t flush, + oc_log_set_level_t set_level, + oc_log_write_level_t write_level, + oc_log_set_module_t set_module + ) +{ + oc_log_ctx_t *log_ctx; + + if(0 == init || + 0 == destroy || + 0 == flush || + 0 == set_level || + 0 == write_level || + 0 == set_module) + return 0; + + if(__OC_LOG_MIN__ > level || __OC_LOG_MAX__ < level) + return 0; + + log_ctx = (oc_log_ctx_t *)malloc(sizeof(oc_log_ctx_t)); + + if(0 == log_ctx) + return 0; + + log_ctx->ctx = 0; /* we'll get to this in a sec... */ + log_ctx->log_level = level; + log_ctx->module_name = 0; + log_ctx->init = init; + log_ctx->destroy = destroy; + log_ctx->flush = flush; + log_ctx->set_level = set_level; + log_ctx->set_module = set_module; + + log_ctx->write_level = write_level; + + if(0 == log_ctx->init(log_ctx, world)) + { + free(log_ctx); + return 0; + } + + return log_ctx; +} + +void oc_log_destroy(oc_log_ctx_t *ctx) +{ + if(0 == ctx) + return; + + ctx->destroy(ctx); + + if(0 != ctx->module_name) + free(ctx->module_name); + + free(ctx); +} + +int oc_log_init(oc_log_ctx_t *ctx, void *world) +{ + if(0 == ctx) + return 0; + + return ctx->init(ctx, world); +} + +void oc_log_flush(oc_log_ctx_t *ctx) +{ + if(0 == ctx) + { + return; + } + ctx->flush(ctx); +} + +void oc_log_set_level(oc_log_ctx_t *ctx, const oc_log_level ll) +{ + if(0 == ctx) + { + return; + } + ctx->set_level(ctx, ll); +} + +size_t oc_log_write(oc_log_ctx_t *ctx, const char *msg) +{ + if(0 == ctx) + return 0; + + return oc_log_write_level(ctx, ctx->log_level, msg); +} + +size_t oc_log_write_level(oc_log_ctx_t *ctx, const oc_log_level ll, const char *msg) +{ + if(0 == ctx) + return 0; + + ctx->log_level = ll; + + /* Notify: */ + return ctx->write_level(ctx, ll, msg); +} + +int oc_log_set_module(oc_log_ctx_t *ctx, const char *module_name) +{ + char *mn; + size_t l; + + if(0 == ctx) + return 0; + + /* Swap pointers so that module data's not erased in the event of failure: */ + l = strlen(module_name); + + mn = (char *)malloc(1 + l); + + if(0 == mn) + return 0; + + memcpy(mn, module_name, 1 + l); + + if(0 != ctx->module_name) + free(ctx->module_name); + + ctx->module_name = mn; + + /* Notify: */ + return ctx->set_module(ctx, ctx->module_name); +} + diff --git a/oc_logger/cpp/Makefile b/oc_logger/cpp/Makefile new file mode 100644 index 0000000..0b2c41e --- /dev/null +++ b/oc_logger/cpp/Makefile @@ -0,0 +1,47 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# + +BIN_DIR=../bin +LIB_DIR=../lib + +INC=-I../include/ + +LIB=$(LIB_DIR)/oc_logger.a + +BUILD := release + +CXX_FLAGS.debug=-Wall -std=c++0x -O0 -g +CXX_FLAGS.release=-Wall -std=c++0x -O2 + +CXX := g++ + +CXX_X=$(CXX) $(CXX_FLAGS.$(BUILD)) $(INC) + +all: dirs targets + +dirs: + -mkdir -p $(BIN_DIR)/targets + +targets: + $(CXX_X) -c -o $(BIN_DIR)/targets/oc_ostream_logger.o oc_ostream_logger.cpp + +clean: + rm -f ./test_logging ./*.o diff --git a/oc_logger/cpp/oc_ostream_logger.cpp b/oc_logger/cpp/oc_ostream_logger.cpp new file mode 100644 index 0000000..cadf871 --- /dev/null +++ b/oc_logger/cpp/oc_ostream_logger.cpp @@ -0,0 +1,199 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "oc_logger.hpp" +#include "targets/oc_ostream_logger.h" + +#include +#include + +#include +#include +#include +#include + +namespace { + +struct oc_ostream_logger_ctx +{ + std::ostream* os_ptr; + std::ostream& os; + + std::mutex mutex; + + oc_ostream_logger_ctx(std::ostream *here) + : os_ptr(here), + os(*os_ptr) + {} +}; + +} // namespace + +/* Courtesy-function: */ +oc_log_ctx_t *oc_make_ostream_logger() +{ + return oc_log_make_ctx( + nullptr, + OC_LOG_ALL, + oc_ostream_log_init, + oc_ostream_log_destroy, + oc_ostream_log_flush, + oc_ostream_log_set_level, + oc_ostream_log_write, + oc_ostream_log_set_module + ); +} + +int oc_ostream_log_init(oc_log_ctx_t *ctx, void *world) +try +{ + auto *target = reinterpret_cast(world); + + if(nullptr == world) + target = &std::cout; + + oc_ostream_logger_ctx *my_ctx = new oc_ostream_logger_ctx(target); + + ctx->ctx = static_cast(my_ctx); + + return 1; +} +catch(...) +{ + return 0; +} + +void oc_ostream_log_destroy(oc_log_ctx_t *ctx) +try +{ + static std::mutex dtor_mtx; + + oc_ostream_logger_ctx *lctx = static_cast(ctx->ctx); + + { + std::unique_lock ul(dtor_mtx); + + lctx->os << std::flush; + + delete lctx; + } +} +catch(...) +{ +} + +void oc_ostream_log_flush(oc_log_ctx_t *ctx) +try +{ + oc_ostream_logger_ctx *lctx = static_cast(ctx->ctx); + + std::lock_guard lg(lctx->mutex); + + lctx->os << std::flush; +} +catch(...) +{ +} + +void oc_ostream_log_set_level(oc_log_ctx_t *ctx, const int level) +try +{ + /* We don't have any special thing we need to do when a log level changes. */ + return; +} +catch(...) +{ +} + +size_t oc_ostream_log_write(oc_log_ctx_t *ctx, const int level, const char *msg) +try +{ + oc_ostream_logger_ctx *lctx = static_cast(ctx->ctx); + + std::lock_guard lg(lctx->mutex); + + std::ostringstream os; + + os << level << ": "; + + if(nullptr != ctx->module_name) + os << '[' << ctx->module_name << "] "; + + os << msg << '\n'; + + lctx->os << os.str().c_str(); + + return 1 + os.str().length(); +} +catch(...) +{ + return 0; +} + +int oc_ostream_log_set_module(oc_log_ctx_t *ctx, const char *module_name) +try +{ + // Nothing special needs to happen for a module name change: + return 1; +} +catch(...) +{ + return 0; +} + +int oc_ostream_log_lock(oc_log_ctx_t *ctx) +try +{ + oc_ostream_logger_ctx *lctx = static_cast(ctx->ctx); + + lctx->mutex.lock(); + + return 1; +} +catch(...) +{ + return 0; +} + +int oc_ostream_log_unlock(oc_log_ctx_t *ctx) +try +{ + oc_ostream_logger_ctx *lctx = static_cast(ctx->ctx); + + lctx->mutex.unlock(); + + return 1; +} +catch(...) +{ + return 0; +} + +int oc_ostream_log_try_lock(oc_log_ctx_t *ctx) +try +{ + oc_ostream_logger_ctx *lctx = static_cast(ctx->ctx); + + return lctx->mutex.try_lock(); +} +catch(...) +{ + return 0; +} diff --git a/oc_logger/examples/Makefile b/oc_logger/examples/Makefile new file mode 100644 index 0000000..7cf1073 --- /dev/null +++ b/oc_logger/examples/Makefile @@ -0,0 +1,56 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# + +BUILD := release + +CC_FLAGS.debug=-Wall -std=c99 -Werror -O0 -g +CC_FLAGS.release=-Wall -std=c99 -Werror -O2 + +CXX_FLAGS.debug=-Wall -std=c++0x -O0 -g +CXX_FLAGS.release=-Wall -std=c++0x -O2 + +CXX_STDLIB=-lstdc++ + +CC:=gcc +CXX:=g++ +CXX_X=$(CXX) $(CXX_FLAGS.$(BUILD)) $(INC) + +INC=-I../include/ + +BIN_DIR=../bin +LIB_DIR=../lib + +CC_LIB=$(LIB_DIR)/oc_logger.a $(CXX_STDLIB) +CXX_LIB=$(LIB_DIR)/oc_logger.a $(CXX_STDLIB) + +CCX=$(CC) $(CC_FLAGS.$(BUILD)) + +all: examples_c examples_cpp + +examples_c: + $(CCX) $(INC) -o $(BIN_DIR)/test_logging test_logging.c $(CC_LIB) + +examples_cpp: + $(CXX_X) -o $(BIN_DIR)/test_logging_cpp test_logging.cpp $(CXX_LIB) + +clean: + rm -f $(BIN_DIR)/test_logging $(BIN_DIR)/test_logging_cpp + diff --git a/oc_logger/examples/test_logging.c b/oc_logger/examples/test_logging.c new file mode 100644 index 0000000..7db0443 --- /dev/null +++ b/oc_logger/examples/test_logging.c @@ -0,0 +1,82 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "oc_logger.h" +#include "targets/oc_console_logger.h" +#include "targets/oc_ostream_logger.h" + +#include + +/* Example of basic usage of the C library: */ +void basic_demo(void) +{ + oc_log_ctx_t *log; + + log = oc_make_console_logger(); + + if(0 == log) + { + fprintf(stderr, "Unable to initialize logging subsystem.\n"); + return; + } + + oc_log_write(log, "Hello, World!"); + + oc_log_set_module(log, "FabulousModule"); + + oc_log_set_level(log, 50); + + oc_log_write(log, "Hello again, World!"); + + oc_log_destroy(log); +} + +/* Example of calling a C++ log implementation from C: */ +void cpp_demo() +{ + oc_log_ctx_t *log; + + log = oc_make_ostream_logger(); + + if(0 == log) + { + fprintf(stderr, "Unable to initialize logging subsystem.\n"); + return; + } + + oc_log_write(log, "Hello from C++, World!"); + + oc_log_set_module(log, "BestModuleEver"); + + oc_log_set_level(log, 50); + + oc_log_write(log, "Hello again from C++, World!"); + oc_log_write(log, "Hello once more from C++, World!"); + + oc_log_destroy(log); +} + +int main() +{ + basic_demo(); + cpp_demo(); + + return 0; +} diff --git a/oc_logger/examples/test_logging.cpp b/oc_logger/examples/test_logging.cpp new file mode 100644 index 0000000..0128006 --- /dev/null +++ b/oc_logger/examples/test_logging.cpp @@ -0,0 +1,96 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "oc_logger.hpp" + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +void basic_demo() +{ + using OC::oc_log_stream; + + oc_log_stream ols(oc_make_ostream_logger); + + boost::iostreams::stream os(ols); + + os << "Greetings from the nifty world of logging!" << std::flush; + + ols.set_level(OC_LOG_ALL); + ols.set_module("TheHappyModule"); + ols.set_module("TheModule"); + os << "Whee!" << std::flush; + + // Setting the module name by getting the device from the stream itself: + (*os).set_module("TheHappiestModuleEver"); + os << "Whee! Again!" << std::flush; +} + +/* Show that we can use a C logger from C++: */ +void c_demo() +{ + using OC::oc_log_stream; + + oc_log_stream ols(oc_make_console_logger); + + boost::iostreams::stream os(ols); + + os << "Greetings from the nifty world of logging!" << std::flush; + + ols.set_level(OC_LOG_ALL); + ols.set_module("TheHappyModule"); + os << "Whee!" << std::flush; + + (*os).set_module("TheHappiestModuleEver"); + os << "Whee!" << std::flush; +} + +void alternative_demo() +{ + /* Annother way to create a context: */ + auto logger = []() -> boost::iostreams::stream& + { + static OC::oc_log_stream ols(oc_make_ostream_logger); + static boost::iostreams::stream os(ols); + + return os; + }; + + logger()->set_module("FantasticModule"); + logger() << "Hello, logging world!" << std::flush; +} + +int main() +{ + basic_demo(); + c_demo(); + alternative_demo(); +} + diff --git a/oc_logger/include/oc_console_logger.h b/oc_logger/include/oc_console_logger.h new file mode 100644 index 0000000..02adb4f --- /dev/null +++ b/oc_logger/include/oc_console_logger.h @@ -0,0 +1,43 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_CONSOLE_LOGGER_H_2014_09_5 + #define __OC_CONSOLE_LOGGER_H_2014_09_5 + +#include "oc_logger_types.h" + +#ifdef __cplusplus + extern "C" { +#endif + +oc_log_ctx_t *oc_make_console_logger(); + +int oc_console_logger_init(oc_log_ctx_t *ctx, void *world); +void oc_console_logger_destroy(oc_log_ctx_t *ctx); +void oc_console_logger_flush(oc_log_ctx_t *ctx); +void oc_console_logger_set_level(oc_log_ctx_t *ctx, const int level); +size_t oc_console_logger_write(oc_log_ctx_t *ctx, const int level, const char *msg); +int oc_console_logger_set_module(oc_log_ctx_t *ctx, const char *module_name); + +#ifdef __cplusplus + } // extern "C" +#endif + +#endif diff --git a/oc_logger/include/oc_log.hpp b/oc_logger/include/oc_log.hpp new file mode 100644 index 0000000..dd145dd --- /dev/null +++ b/oc_logger/include/oc_log.hpp @@ -0,0 +1,27 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_LOG_HPP_20140910 + #define __OC_LOG_HPP_20140910 + +#include "oc_ostream_logger.hpp" +#include "oc_log_stream.hpp" + +#endif diff --git a/oc_logger/include/oc_log_stream.hpp b/oc_logger/include/oc_log_stream.hpp new file mode 100644 index 0000000..bafa141 --- /dev/null +++ b/oc_logger/include/oc_log_stream.hpp @@ -0,0 +1,80 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_LOG_STREAM_HPP_20140910 + #define __OC_LOG_STREAM_HPP_20140910 + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "oc_logger.h" + +namespace OC { + +class oc_log_stream : boost::iostreams::sink +{ + std::shared_ptr m_log; + + public: + typedef char char_type; + typedef boost::iostreams::sink_tag category; + + public: + template + oc_log_stream(ContextCtor& c) + : m_log { c(), oc_log_destroy } + {} + + template + oc_log_stream(ContextCtor& c, void *world) + : m_log { c(world), oc_log_destroy } + {} + + public: + inline void flush() noexcept { return oc_log_flush(m_log.get()); } + inline void set_level(const oc_log_level new_level) noexcept { return oc_log_set_level(m_log.get(), new_level); } + inline int set_module(const std::string& module_name) noexcept { return oc_log_set_module(m_log.get(), module_name.c_str()); } + + public: + std::streamsize write(const char_type *s, std::streamsize n) + { + /* It may seem strange to do this here, but it's a consequence of the + underlying library not supporting ptr+len style buffers at this time: */ + std::string s2(s, n + s); + + oc_log_write(m_log.get(), s2.c_str()); + + return n; + } + + private: + oc_log_stream operator=(const oc_log_stream&) = delete; +}; + +} // namespace OC + +#endif diff --git a/oc_logger/include/oc_logger.h b/oc_logger/include/oc_logger.h new file mode 100644 index 0000000..8368db0 --- /dev/null +++ b/oc_logger/include/oc_logger.h @@ -0,0 +1,54 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_LOGGER_H_2014_09_5 + #define __OC_LOGGER_H_2014_09_5 + +#include "oc_logger_types.h" + +#ifdef __cplusplus + extern "C" { +#endif + +/* Basic interface: */ +oc_log_ctx_t *oc_log_make_ctx( + void* world, + const oc_log_level level, + oc_log_init_t init, + oc_log_destroy_t destroy, + oc_log_flush_t flush, + oc_log_set_level_t set_level, + oc_log_write_level_t write_level, + oc_log_set_module_t set_module + ); + +void oc_log_destroy(oc_log_ctx_t *ctx); + +void oc_log_flush(oc_log_ctx_t *ctx); +void oc_log_set_level(oc_log_ctx_t *ctx, const oc_log_level ll); +size_t oc_log_write(oc_log_ctx_t *ctx, const char *msg); +size_t oc_log_write_level(oc_log_ctx_t *ctx, const oc_log_level ll, const char *msg); +int oc_log_set_module(oc_log_ctx_t *ctx, const char *module_name); + +#ifdef __cplusplus + } // extern "C" +#endif + +#endif diff --git a/oc_logger/include/oc_logger.hpp b/oc_logger/include/oc_logger.hpp new file mode 100644 index 0000000..2d0df21 --- /dev/null +++ b/oc_logger/include/oc_logger.hpp @@ -0,0 +1,31 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_LOG_HPP_20140910 + #define __OC_LOG_HPP_20140910 + +#include "oc_logger.h" + +#include "oc_log_stream.hpp" + +#include "targets/oc_console_logger.h" +#include "targets/oc_ostream_logger.h" + +#endif diff --git a/oc_logger/include/oc_logger_types.h b/oc_logger/include/oc_logger_types.h new file mode 100644 index 0000000..4789711 --- /dev/null +++ b/oc_logger/include/oc_logger_types.h @@ -0,0 +1,84 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_LOGGER_TYPES_H_2024_09_5 + #define __OC_LOGGER_TYPES_H_2024_09_5 + +#include + +#ifdef __cplusplus + extern "C" { +#endif + + typedef enum { + __OC_LOG_MIN__ = -1, + OC_LOG_ALL = 0, + OC_LOG_FATAL, + OC_LOG_ERROR, + OC_LOG_WARNING, + OC_LOG_INFO, + OC_LOG_DEBUG, + OC_LOG_DISABLED, + __OC_LOG_MAX__ + } oc_log_level; + +typedef struct _oc_log_ctx +{ + void* ctx; + + oc_log_level log_level; + + char* module_name; + + /* Required interface: */ + int (*init) (struct _oc_log_ctx *, void *); + void (*destroy) (struct _oc_log_ctx *); + void (*flush) (struct _oc_log_ctx *); + void (*set_level) (struct _oc_log_ctx *, const int); + size_t (*write_level) (struct _oc_log_ctx *, const int, const char *); + int (*set_module) (struct _oc_log_ctx *, const char *); + + /* Optional interface (if one is implemented, all must be implemented): */ + int (*lock) (struct _oc_log_ctx *); + int (*unlock) (struct _oc_log_ctx *); + int (*try_lock) (struct _oc_log_ctx *); + int (*locked_destroy) (struct _oc_log_ctx *); + +} oc_log_ctx_t; + +/* Notice that these are all passed the /top level/ ctx-- it's "public" with respect to +these functions, they have full access to fiddle with the structure all they want (but, +generally should avoid doing that); I could certainly be convinced to go the other direction, +and have most functions only take the inner context: */ +typedef int (*oc_log_init_t) (oc_log_ctx_t *, void *); +typedef void (*oc_log_destroy_t) (oc_log_ctx_t *); +typedef void (*oc_log_flush_t) (oc_log_ctx_t *); +typedef void (*oc_log_set_level_t) (oc_log_ctx_t *, const int); +typedef size_t (*oc_log_write_level_t) (oc_log_ctx_t *, const int, const char *); +typedef int (*oc_log_set_module_t) (oc_log_ctx_t *, const char *); +typedef int (*oc_log_lock_t) (oc_log_ctx_t *); +typedef int (*oc_log_unlock_t) (oc_log_ctx_t *); +typedef int (*oc_log_try_lock_t) (oc_log_ctx_t *); + +#ifdef __cplusplus + } // extern "C" +#endif + +#endif diff --git a/oc_logger/include/targets/oc_console_logger.h b/oc_logger/include/targets/oc_console_logger.h new file mode 100644 index 0000000..02adb4f --- /dev/null +++ b/oc_logger/include/targets/oc_console_logger.h @@ -0,0 +1,43 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_CONSOLE_LOGGER_H_2014_09_5 + #define __OC_CONSOLE_LOGGER_H_2014_09_5 + +#include "oc_logger_types.h" + +#ifdef __cplusplus + extern "C" { +#endif + +oc_log_ctx_t *oc_make_console_logger(); + +int oc_console_logger_init(oc_log_ctx_t *ctx, void *world); +void oc_console_logger_destroy(oc_log_ctx_t *ctx); +void oc_console_logger_flush(oc_log_ctx_t *ctx); +void oc_console_logger_set_level(oc_log_ctx_t *ctx, const int level); +size_t oc_console_logger_write(oc_log_ctx_t *ctx, const int level, const char *msg); +int oc_console_logger_set_module(oc_log_ctx_t *ctx, const char *module_name); + +#ifdef __cplusplus + } // extern "C" +#endif + +#endif diff --git a/oc_logger/include/targets/oc_ostream_logger.h b/oc_logger/include/targets/oc_ostream_logger.h new file mode 100644 index 0000000..0431469 --- /dev/null +++ b/oc_logger/include/targets/oc_ostream_logger.h @@ -0,0 +1,49 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef __OC_OSTREAM_LOGGER_H_2014_09_5 + #define __OC_OSTREAM_LOGGER_H_2014_09_5 + +#include "oc_logger_types.h" + +/* Example of a C-callable C++ logger: */ +#ifdef __cplusplus + extern "C" { +#endif + +oc_log_ctx_t *oc_make_ostream_logger(); + +int oc_ostream_log_init(oc_log_ctx_t *ctx, void *world); +void oc_ostream_log_destroy(oc_log_ctx_t *ctx); +void oc_ostream_log_flush(oc_log_ctx_t *ctx); +void oc_ostream_log_set_level(oc_log_ctx_t *ctx, const int level); +size_t oc_ostream_log_write(oc_log_ctx_t *ctx, const int level, const char *msg); +int oc_ostream_log_set_module(oc_log_ctx_t *ctx, const char *module_name); + +int oc_ostream_log_lock(oc_log_ctx_t *ctx); +int oc_ostream_log_unlock(oc_log_ctx_t *ctx); +int oc_ostream_log_try_lock(oc_log_ctx_t *ctx); // non-blocking +int oc_ostream_log_locked_destroy(oc_log_ctx_t *ctx); + +#ifdef __cplusplus + } // extern "C" +#endif + +#endif diff --git a/oc_logger/samples/linux/README b/oc_logger/samples/linux/README new file mode 100644 index 0000000..966cff9 --- /dev/null +++ b/oc_logger/samples/linux/README @@ -0,0 +1,31 @@ +To run the oc_logger C sample app, first build liboctbstack.a + +cd /csdk + +To enable logging +make BUILD=debug +else +make BUILD=release + +Next, build the oc_logger C sample app + +cd /oc_logger/samples/linux + +To enable logging +make BUILD=debug +else +make BUILD=release + +The logger sample has two options, default logging or +a custom logger that can be supplied by the user application + +To run the application with the default logger, run + +./debug/test_logging -c 0 + +To run the application using a built in custom console logger, run + +./debug/test_logging -c 1 + + + diff --git a/oc_logger/samples/linux/makefile b/oc_logger/samples/linux/makefile new file mode 100644 index 0000000..8b16e11 --- /dev/null +++ b/oc_logger/samples/linux/makefile @@ -0,0 +1,94 @@ +# //****************************************************************** +# // +# // Copyright 2014 Intel Mobile Communications GmbH 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. +# // +# //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +# +# override with `make BUILD=debug` +# default to release build +BUILD := release +PLATFORM := linux +CC := g++ +OUT_DIR := $(PWD)/$(BUILD) +OBJ_DIR := $(OUT_DIR)/obj + +ifeq ($(ROOT_DIR), ) +ROOT_DIR = $(PWD)/../../../csdk +endif + +OUT_DIR = $(PWD) + +OCLOGGER_DIR = $(ROOT_DIR)/logger +OC_LOG_DIR = $(ROOT_DIR)/../oc_logger +OCRANDOM_DIR = $(ROOT_DIR)/ocrandom +OCSOCKET_DIR = $(ROOT_DIR)/ocsocket +LCOAP_DIR = $(ROOT_DIR)/libcoap-4.1.1 +OCCOAP_DIR = $(ROOT_DIR)/occoap +OCTBSTACK_DIR = $(ROOT_DIR)/stack + +OC_LOG_INC = $(OC_LOG_DIR)/include +OC_LOG_LIB = $(OC_LOG_DIR)/lib/oc_logger.a + +OCLOGGER_INC = $(OCLOGGER_DIR)/include +OCRANDOM_INC = $(OCRANDOM_DIR)/include +OCSOCKET_INC = $(OCSOCKET_DIR)/include +LCOAP_INC = $(LCOAP_DIR) +OCCOAP_INC = $(OCCOAP_DIR)/include +OCTBSTACK_INC = $(OCTBSTACK_DIR)/include + +INC_DIRS := -I$(OCLOGGER_INC) +INC_DIRS += -I$(OC_LOG_INC) +INC_DIRS += -I$(OCRANDOM_INC) +INC_DIRS += -I$(OCSOCKET_INC) +INC_DIRS += -I$(LCOAP_INC) +INC_DIRS += -I$(OCCOAP_INC) +INC_DIRS += -I$(OCTBSTACK_INC) + +CC_FLAGS.debug := -O0 -g3 -Wall -ffunction-sections -fdata-sections \ + -std=c99 $(INC_DIRS) -L$(ROOT_DIR)/$(BUILD) -DTB_LOG +CC_FLAGS.release := -Os -Wall -fdata-sections -Wl,--gc-sections -Wl,-s \ + -std=c99 $(INC_DIRS) -L$(ROOT_DIR)/$(BUILD) + +LDLIBS += $(OC_LOG_LIB) -loctbstack -lpthread +CPPFLAGS += $(CC_FLAGS.$(BUILD)) $(LDLIBS) + +SOURCES := test_logging.c + +OBJECTS:= $(patsubst %.c, $(OBJ_DIR)/%.o, $(SOURCES)) + +PROGRAMS += test_logging + +all: prep_dirs $(OBJECTS) $(PROGRAMS) + +prep_dirs: + -mkdir -p $(OUT_DIR) + -mkdir -p $(OBJ_DIR) + +$(OBJ_DIR)/%.o: %.c + $(CC) -c $(CPPFLAGS) $< -o $@ + +test_logging: $(OBJ_DIR)/test_logging.o + $(CC) $^ $(CPPFLAGS) -o $(OUT_DIR)/$(BUILD)/$@ + +.PHONY: clean + +clean: legacy_clean + -rm -rf release + -rm -rf debug + +legacy_clean: + rm -f *.o $(PROGRAMS) diff --git a/oc_logger/samples/linux/test_logging.c b/oc_logger/samples/linux/test_logging.c new file mode 100644 index 0000000..2ddd9ff --- /dev/null +++ b/oc_logger/samples/linux/test_logging.c @@ -0,0 +1,80 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "logger.h" +#include +#include +#include +#include + +#define TAG PCF("MAIN") + +static int customLogger = 0; + +static void PrintUsage() +{ + OC_LOG(INFO, TAG, "Usage : test_logging -c <0|1>"); + OC_LOG(INFO, TAG, "-u <0|1> : 0 - default logging, 1 - custom console logging"); +} + +int main(int argc, char* argv[]) +{ + int opt; + + while ((opt = getopt(argc, argv, "c:")) != -1) + { + switch(opt) + { + case 'c': + customLogger = atoi(optarg); + break; + default: + PrintUsage(); + return -1; + } + } + + if (customLogger == 0) + { + // Default logger + OC_LOG(DEBUG, TAG, "This is a DEBUG"); + OC_LOG(INFO, TAG, "This is a INFO"); + OC_LOG(WARNING, TAG, "This is a WARNING"); + OC_LOG(ERROR, TAG, "This is a ERROR"); + OC_LOG(FATAL, TAG, "This is a FATAL"); + } + else + { + // Custom logger, in this case, the console logger + oc_log_ctx_t *log = oc_make_console_logger(); + + OC_LOG_CONFIG(log); + + OC_LOG(DEBUG, TAG, "This is a DEBUG"); + OC_LOG(INFO, TAG, "This is a INFO"); + OC_LOG(WARNING, TAG, "This is a WARNING"); + OC_LOG(ERROR, TAG, "This is a ERROR"); + OC_LOG(FATAL, TAG, "This is a FATAL"); + OC_LOG_SHUTDOWN(); + } + + + return 0; +} diff --git a/oc_logger/test/test_logging.cpp b/oc_logger/test/test_logging.cpp new file mode 100644 index 0000000..7b24268 --- /dev/null +++ b/oc_logger/test/test_logging.cpp @@ -0,0 +1,54 @@ +//****************************************************************** +// +// Copyright 2014 Intel Mobile Communications GmbH 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "oc_log.hpp" +#include "oc_console_logger.h" +#include "oc_ostream_logger.hpp" + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +int main() +{ + using OC::oc_log_stream; + + oc_log_stream ols(oc_make_console_logger); + + boost::iostreams::stream os(ols); + + os << "Greetings from the nifty world of loggin'!" << std::flush; + + (*os).set_module("TheHappiestModuleEver"); + + os << "Whee!" << std::flush; + + return 0; +} + diff --git a/src/InProcClientWrapper.cpp b/src/InProcClientWrapper.cpp index 4ef606d..5916d53 100644 --- a/src/InProcClientWrapper.cpp +++ b/src/InProcClientWrapper.cpp @@ -23,14 +23,17 @@ #include "InProcClientWrapper.h" #include "ocstack.h" +#include "OCPlatform.h" #include "OCResource.h" -using namespace std; +using namespace std; namespace OC { - InProcClientWrapper::InProcClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) - : m_threadRun(false), m_csdkLock(csdkLock), + InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) + : IClientWrapper(owner), + m_threadRun(false), m_csdkLock(csdkLock), + m_owner(m_owner), m_cfg { cfg } { // if the config type is server, we ought to never get called. If the config type @@ -109,8 +112,9 @@ namespace OC struct ListenContext { - FindCallback callback; - IClientWrapper::Ptr clientWrapper; + FindCallback callback; + IClientWrapper::Ptr clientWrapper; + OC::OCPlatform const* owner; // observing ptr }; @@ -154,34 +158,34 @@ namespace OC OCStackApplicationResult listenCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) { - if(clientResponse->result == OC_STACK_OK) - { - ListenContext* context = static_cast(ctx); + ListenContext* context = static_cast(ctx); - std::stringstream requestStream; - requestStream << clientResponse->resJSONPayload; + if(clientResponse->result != OC_STACK_OK) + { + context->owner->log() << "listenCallback(): failed to create resource. clientResponse: " << clientResponse->result << std::flush; + return OC_STACK_KEEP_TRANSACTION; + } - // TODO this should got logger - // std::cout << "Listen: " << clientResponse->resJSONPayload << std::endl; + std::stringstream requestStream; + requestStream << clientResponse->resJSONPayload; - boost::property_tree::ptree root; + boost::property_tree::ptree root; - try - { + try + { boost::property_tree::read_json(requestStream, root); - } - catch(boost::property_tree::json_parser::json_parser_error &e) - { - std::cout << "read_json failed: "<< e.what() <owner->log() << "listenCallback(): read_json() failed: " << e.what() << std::flush; return OC_STACK_KEEP_TRANSACTION; - } + } - boost::property_tree::ptree payload = + boost::property_tree::ptree payload = root.get_child("oc", boost::property_tree::ptree()); - for(auto payloadItr : payload) - { + for(auto payloadItr : payload) + { try { std::string host = convertOCAddrToString(*clientResponse->addr); @@ -199,18 +203,11 @@ namespace OC } catch(ResourceInitException& e) { - std::cout << "Failed to create resource: "<< e.what() <owner->log() << "listenCallback(): failed to create resource: " << e.what() << std::flush; } - } - return OC_STACK_KEEP_TRANSACTION; - - } - else - { - std::cout<<"listen Callback got failed result: " << clientResponse->result<callback = callback; context->clientWrapper = shared_from_this(); + context->owner = &m_owner; cbdata.context = static_cast(context); cbdata.cb = listenCallback; @@ -370,8 +368,6 @@ namespace OC { GetContext* context = static_cast(ctx); - std::cout << "GET JSON: " << (char*) clientResponse->resJSONPayload << endl; - OCRepresentation rep; if(clientResponse->result == OC_STACK_OK) @@ -395,15 +391,13 @@ namespace OC cbdata.cb = &getResourceCallback; cbdata.cd = [](void* c){delete static_cast(c);}; - // TODO: in the future the cstack should be combining these two strings! - ostringstream os; - os << host << assembleSetResourceUri(uri, queryParams).c_str(); - std::cout << "GET URI: " << os.str() << std::endl; - // TODO: end of above - auto cLock = m_csdkLock.lock(); + if(cLock) { + std::ostringstream os; + os << host << "/oc/presence"; + std::lock_guard lock(*cLock); OCDoHandle handle; result = OCDoResource(&handle, OC_REST_GET, os.str().c_str(), @@ -560,15 +554,14 @@ namespace OC method = OC_REST_OBSERVE_ALL; } - // TODO: in the future the cstack should be combining these two strings! - ostringstream os; - os << host << assembleSetResourceUri(uri, queryParams).c_str(); - // std::cout << "OBSERVE URI: " << os.str() << std::endl; - // TODO: end of above - auto cLock = m_csdkLock.lock(); + if(cLock) { + std::ostringstream os; + + os << host << "/oc/presence"; + std::lock_guard lock(*cLock); result = OCDoResource(handle, method, os.str().c_str(), nullptr, @@ -621,7 +614,6 @@ namespace OC OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle, const std::string& host, SubscribeCallback& presenceHandler) { - OCStackResult result; OCCallbackData cbdata = {0}; SubscribePresenceContext* ctx = new SubscribePresenceContext(); @@ -635,18 +627,11 @@ namespace OC os << host << "/oc/presence"; - std::cout << "Subscribe Presence: " << os.str() << std::endl; - - if(cLock) - { - result = OCDoResource(handle, OC_REST_PRESENCE, os.str().c_str(), nullptr, nullptr, - OC_NON_CONFIRMABLE, &cbdata); - } - else - { + if(!cLock) return OC_STACK_ERROR; - } - return result; + + return OCDoResource(handle, OC_REST_PRESENCE, os.str().c_str(), nullptr, nullptr, + OC_NON_CONFIRMABLE, &cbdata); } OCStackResult InProcClientWrapper::UnsubscribePresence(OCDoHandle handle) diff --git a/src/InProcServerWrapper.cpp b/src/InProcServerWrapper.cpp index ec66e55..5bd2604 100644 --- a/src/InProcServerWrapper.cpp +++ b/src/InProcServerWrapper.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include using namespace std; @@ -41,20 +42,17 @@ std::map entityHandlerMap; void defaultEntityHandler(const OC::OCResourceRequest::Ptr request, const OC::OCResourceResponse::Ptr response) { - cout << "\nSomething wrong: We are in default entity handler: " << endl; + // TODO: 1) why is this ever even possible? 2) throw exception? + std::clog << "Error: defaultEntityHandler() invoked" << std::endl; } -OCEntityHandlerResult EntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) { - - // TODO we need to have a better way of logging (with various levels of logging) - cout << "\nIn C entity handler: " << endl; - - // TODO do we need shared pointer? - auto pRequest = std::make_shared(); +OCEntityHandlerResult EntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest ) +{ + auto pRequest = std::make_shared(); auto pResponse = std::make_shared(); // TODO Utility to convert from C to C++ (every). - + if(flag & OC_INIT_FLAG) { // TODO We can fill the common data (resource Handle, etc.. ) @@ -117,12 +115,11 @@ OCEntityHandlerResult EntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerReq } else { - // TODO Logging - std::cout << "C stack should not call again for parent resource\n"; + std::clog << "C stack should not call again for parent resource" << std::endl; } } else { - std::cout << "No entity handler found." << endl; + std::clog << "No entity handler found." << std::endl; return OC_EH_ERROR; } @@ -131,28 +128,14 @@ OCEntityHandlerResult EntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerReq // TODO we could use const reference std::string payLoad = pResponse->getPayload(); - if(OC_REST_GET == entityHandlerRequest->method) - { - cout << "\t\t\tGoing from stack for GET: "; - } - else if (OC_REST_PUT == entityHandlerRequest->method) - { - cout << "\t\t\tGoing from stack for PUT: "; - } - else - { - cout << "\t\t\tUnknown method...!!!" << endl; - } - if (payLoad.size() < entityHandlerRequest->resJSONPayloadLen) { strncpy((char*)entityHandlerRequest->resJSONPayload, payLoad.c_str(), entityHandlerRequest->resJSONPayloadLen); - cout << "Payload: " << (char*)entityHandlerRequest->resJSONPayload << endl; } else { // TODO throw appropriate runtime error - cout << "Payload is larger than the PayloadLen" << endl; + // cout << "Payload is larger than the PayloadLen" << endl; } } @@ -161,8 +144,9 @@ OCEntityHandlerResult EntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerReq namespace OC { - InProcServerWrapper::InProcServerWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) - :m_csdkLock(csdkLock) + InProcServerWrapper::InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) + : IServerWrapper(owner), + m_csdkLock(csdkLock) { OCMode initType; @@ -199,20 +183,17 @@ namespace OC while(cLock && m_threadRun) { OCStackResult result; + { std::lock_guard lock(*cLock); result = OCProcess(); } - if(result != OC_STACK_OK) - { - cout << "Something wrong in OCProcess" << endl; - // TODO - } + // ...the value of variable result is simply ignored for now. + if(OC_STACK_ERROR == result) + ; std::this_thread::sleep_for(std::chrono::milliseconds(10)); - // To minimize CPU utilization we may wish to do this with sleep - //std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } @@ -227,11 +208,6 @@ namespace OC { OCStackResult result; - cout << "Registering Resource: \n"; - cout << "\tResource URI: " << resourceURI << endl; - cout << "\tResource TypeName: " << resourceTypeName << endl; - cout << "\tResource Interface: " << resourceInterface << endl; - auto cLock = m_csdkLock.lock(); if(cLock) @@ -261,14 +237,10 @@ namespace OC if(result != OC_STACK_OK) { - cout << "\tSomething wrong in creating the resource" << endl; resourceHandle = (OCResourceHandle) 0; - // TODO } else { - cout << "\tResource creation is successful with resource handle: " - << resourceHandle << endl; entityHandlerMap[resourceHandle] = eHandler; } } @@ -282,8 +254,6 @@ namespace OC OCStackResult InProcServerWrapper::unregisterResource(const OCResourceHandle& resourceHandle) { - cout << "Unregistering Resource: \n"; - auto cLock = m_csdkLock.lock(); OCStackResult result = OC_STACK_ERROR; if(cLock) @@ -296,9 +266,6 @@ namespace OC OCStackResult InProcServerWrapper::bindTypeToResource(const OCResourceHandle& resourceHandle, const std::string& resourceTypeName) { - cout << "Binding Type to Resource: \n"; - cout << "\tTypeName: " << resourceTypeName << endl; - auto cLock = m_csdkLock.lock(); OCStackResult result; if(cLock) @@ -321,9 +288,6 @@ namespace OC OCStackResult InProcServerWrapper::bindInterfaceToResource(const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) { - cout << "Binding Interface to Resource: \n"; - cout << "\tInterfaceName: " << resourceInterfaceName << endl; - auto cLock = m_csdkLock.lock(); OCStackResult result; if(cLock) diff --git a/src/OCPlatform.cpp b/src/OCPlatform.cpp index f699a21..77f4428 100644 --- a/src/OCPlatform.cpp +++ b/src/OCPlatform.cpp @@ -34,18 +34,26 @@ #include "OCApi.h" #include "OCException.h" +#include "oc_logger.hpp" + namespace OC { - // Constructor. Internally calls private init function OCPlatform::OCPlatform(const PlatformConfig& config) - : m_cfg(config) + : m_log_stream(std::move(oc_log_stream {oc_make_ostream_logger})), + m_cfg(config) + { + init(m_cfg); + } + + OCPlatform::OCPlatform(const PlatformConfig& config, OC::oc_log_stream& log_target) + : m_log_stream(log_target), + m_cfg(config) { init(m_cfg); } OCPlatform::~OCPlatform(void) { - std::cout << "platform destructor called" << std::endl; } OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle) @@ -93,18 +101,18 @@ namespace OC if(config.mode == ModeType::Server) { // Call server wrapper init - m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config); + m_server = m_WrapperInstance->CreateServerWrapper(*this, m_csdkLock, config); } else if(config.mode == ModeType::Client) { // Call client wrapper init - m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config); + m_client = m_WrapperInstance->CreateClientWrapper(*this, m_csdkLock, config); } else { // This must be both server and client - m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config); - m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config); + m_server = m_WrapperInstance->CreateServerWrapper(*this, m_csdkLock, config); + m_client = m_WrapperInstance->CreateClientWrapper(*this, m_csdkLock, config); } } @@ -267,9 +275,9 @@ namespace OC } catch (OCException& e) { - cout << "Caught an exception..." << endl; - cout << "\tMessage: " << e.what() << endl; - cout << "\t Reason: " << e.reason() << endl; + log() << "Caught an exception..." << endl; + log() << "\tMessage: " << e.what() << endl; + log() << "\t Reason: " << e.reason() << endl; } } return result; @@ -287,9 +295,9 @@ namespace OC } catch (OCException& e) { - cout << "Caught an exception..." << endl; - cout << "\tMessage: " << e.what() << endl; - cout << "\t Reason: " << e.reason() << endl; + log() << "Caught an exception..." << endl; + log() << "\tMessage: " << e.what() << endl; + log() << "\t Reason: " << e.reason() << endl; } } return result; -- 2.7.4