-
+This file is completely obsolete. :-)
Builds with g++ 4.6, 4.8, 4.9.
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++
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
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
+#include "oc_logger.h"
+#include "oc_console_logger.h"
#ifdef __ANDROID__
#include <android/log.h>
#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
#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)
#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
#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
#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
}
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
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)
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)
ROOT_DIR = ../../../../..
LOGGER_DIR = $(ROOT_DIR)/logger
+OC_LOG_DIR = $(ROOT_DIR)/../oc_logger
TBSTACK_DIR = $(ROOT_DIR)/stack
TBSOCKET_DIR = $(ROOT_DIR)/ocsocket
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
ROOT_DIR = ../../../../..
LOGGER_DIR = $(ROOT_DIR)/logger
+OC_LOG_DIR = $(ROOT_DIR)/../oc_logger
TBSTACK_DIR = $(ROOT_DIR)/stack
TBSOCKET_DIR = $(ROOT_DIR)/ocsocket
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
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
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)
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)
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 = ./
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)
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
-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:
+# //******************************************************************
+# //
+# // 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 $@ $<
$(CXX) $(CXX_FLAGS) -o $@ $^ $(CXX_LIBS)
clean:
- rm -f *.o server client monoprocess
+ rm -f *\.o server client monoprocess
namespace OC
{
+ class OCPlatform;
+
class IClientWrapper : public std::enable_shared_from_this<IClientWrapper>
{
+ protected:
+ OCPlatform& m_owner;
+
public:
typedef std::shared_ptr<IClientWrapper> Ptr;
+ IClientWrapper(OCPlatform& owner)
+ : m_owner(owner)
+ {}
+
virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType,
FindCallback& callback) = 0;
{
class IServerWrapper
{
+ protected:
+ OCPlatform& m_owner;
+
public:
typedef std::shared_ptr<IServerWrapper> Ptr;
+ IServerWrapper(OCPlatform& owner)
+ : m_owner(owner)
+ {}
+
virtual ~IServerWrapper(){};
virtual OCStackResult registerResource(
class InProcClientWrapper : public IClientWrapper
{
public:
- InProcClientWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg);
+ InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg);
virtual ~InProcClientWrapper();
virtual OCStackResult ListenForResource(const std::string& serviceUrl,
std::weak_ptr<std::mutex> m_csdkLock;
private:
- PlatformConfig m_cfg;
+ OC::OCPlatform& m_owner;
+ PlatformConfig m_cfg;
};
}
class InProcServerWrapper : public IServerWrapper
{
public:
- InProcServerWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg);
+ InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg);
virtual ~InProcServerWrapper();
virtual OCStackResult registerResource(
namespace OC
{
+ class OCPlatform;
class OCResource;
class OCResourceRequest;
class OCResourceResponse;
#include "OCResourceResponse.h"
#include "OCRepresentation.h"
+#include "oc_logger.hpp"
+
namespace OC
{
/**
*/
class OCPlatform
{
+ private:
+ mutable boost::iostreams::stream<OC::oc_log_stream> m_log_stream;
+
public:
// typedef for handle to cancel presence info with
typedef OCDoHandle OCPresenceHandle;
* in-proc/out-of-proc etc.
*/
OCPlatform(const PlatformConfig& config);
+ OCPlatform(const PlatformConfig& config, OC::oc_log_stream& log_target);
/**
* Virtual destructor
bool isObservable, const std::vector<std::string>& resourceTypes,
const std::vector<std::string>& interfaces);
+ public:
+ inline boost::iostreams::stream<OC::oc_log_stream>& log() const { return m_log_stream; }
+
private:
PlatformConfig m_cfg;
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-/// @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
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 << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
+
+ }
+
+ payload << "}}";
+
+ m_payload = payload.str();
+ }
+
private:
std::string m_payload;
int m_errorCode;
// 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()
class OutOfProcClientWrapper : public IClientWrapper
{
public:
- OutOfProcClientWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) { }
+ OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ : IClientWrapper(owner)
+ {}
virtual OCStackResult ListenForResource(const std::string& serviceUrl,
const std::string& resourceType, FindCallback& callback) {return OC_STACK_NOTIMPL;}
class OutOfProcServerWrapper : public IServerWrapper
{
public:
- OutOfProcServerWrapper(PlatformConfig cfg) {};
+ OutOfProcServerWrapper(OC::OCPlatform& owner, PlatformConfig cfg)
+ : IServerWrapper(owner)
+ {};
virtual OCStackResult registerResource(
OCResourceHandle& resourceHandle,
public:
typedef std::shared_ptr<IWrapperFactory> Ptr;
- virtual IClientWrapper::Ptr CreateClientWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
- virtual IServerWrapper::Ptr CreateServerWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
+ virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
+ virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
virtual ~IWrapperFactory(){}
};
public:
WrapperFactory(){}
- virtual IClientWrapper::Ptr CreateClientWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
{
switch(cfg.serviceType)
{
case ServiceType::InProc:
- return std::make_shared<InProcClientWrapper>(csdkLock, cfg);
+ return std::make_shared<InProcClientWrapper>(owner, csdkLock, cfg);
break;
case ServiceType::OutOfProc:
- return std::make_shared<OutOfProcClientWrapper>(csdkLock, cfg);
+ return std::make_shared<OutOfProcClientWrapper>(owner, csdkLock, cfg);
break;
}
return nullptr;
}
- virtual IServerWrapper::Ptr CreateServerWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
{
switch(cfg.serviceType)
{
case ServiceType::InProc:
- return std::make_shared<InProcServerWrapper>(csdkLock, cfg);
+ return std::make_shared<InProcServerWrapper>(owner, csdkLock, cfg);
break;
case ServiceType::OutOfProc:
- // return std::make_shared<OutOfProcServerWrapper>(csdkLock, cfg);
+ throw OC::OCException("ServiceType::OutOfProc", OC_STACK_NOTIMPL);
break;
}
return nullptr;
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
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
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
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
--- /dev/null
+# //******************************************************************
+# //
+# // 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
--- /dev/null
+# //******************************************************************
+# //
+# // 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)/*
--- /dev/null
+//******************************************************************
+//
+// 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 <stdio.h>
+#include <stdlib.h>
+
+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;
+}
--- /dev/null
+//******************************************************************
+//
+// 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 <string.h>
+#include <stdlib.h>
+
+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);
+}
+
--- /dev/null
+# //******************************************************************
+# //
+# // 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
--- /dev/null
+//******************************************************************
+//
+// 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 <cstdio>
+#include <cstdlib>
+
+#include <mutex>
+#include <memory>
+#include <sstream>
+#include <iostream>
+
+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<std::ostream *>(world);
+
+ if(nullptr == world)
+ target = &std::cout;
+
+ oc_ostream_logger_ctx *my_ctx = new oc_ostream_logger_ctx(target);
+
+ ctx->ctx = static_cast<void *>(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<oc_ostream_logger_ctx *>(ctx->ctx);
+
+ {
+ std::unique_lock<std::mutex> 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<oc_ostream_logger_ctx *>(ctx->ctx);
+
+ std::lock_guard<std::mutex> 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<oc_ostream_logger_ctx *>(ctx->ctx);
+
+ std::lock_guard<std::mutex> 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<oc_ostream_logger_ctx *>(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<oc_ostream_logger_ctx *>(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<oc_ostream_logger_ctx *>(ctx->ctx);
+
+ return lctx->mutex.try_lock();
+}
+catch(...)
+{
+ return 0;
+}
--- /dev/null
+# //******************************************************************
+# //
+# // 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
+
--- /dev/null
+//******************************************************************
+//
+// 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 <stdio.h>
+
+/* 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;
+}
--- /dev/null
+//******************************************************************
+//
+// 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 <iosfwd>
+#include <memory>
+#include <cassert>
+#include <sstream>
+#include <iostream>
+
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/categories.hpp>
+
+#include <boost/config.hpp>
+#include <boost/iostreams/categories.hpp>
+#include <boost/iostreams/detail/ios.hpp>
+
+void basic_demo()
+{
+ using OC::oc_log_stream;
+
+ oc_log_stream ols(oc_make_ostream_logger);
+
+ boost::iostreams::stream<oc_log_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<oc_log_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<OC::oc_log_stream>&
+ {
+ static OC::oc_log_stream ols(oc_make_ostream_logger);
+ static boost::iostreams::stream<OC::oc_log_stream> os(ols);
+
+ return os;
+ };
+
+ logger()->set_module("FantasticModule");
+ logger() << "Hello, logging world!" << std::flush;
+}
+
+int main()
+{
+ basic_demo();
+ c_demo();
+ alternative_demo();
+}
+
--- /dev/null
+//******************************************************************
+//
+// 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
--- /dev/null
+//******************************************************************
+//
+// 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
--- /dev/null
+//******************************************************************
+//
+// 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 <iosfwd>
+#include <memory>
+#include <cassert>
+#include <iostream>
+
+#include <boost/config.hpp>
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/categories.hpp>
+#include <boost/iostreams/detail/ios.hpp>
+
+#include "oc_logger.h"
+
+namespace OC {
+
+class oc_log_stream : boost::iostreams::sink
+{
+ std::shared_ptr<oc_log_ctx_t> m_log;
+
+ public:
+ typedef char char_type;
+ typedef boost::iostreams::sink_tag category;
+
+ public:
+ template <class ContextCtor>
+ oc_log_stream(ContextCtor& c)
+ : m_log { c(), oc_log_destroy }
+ {}
+
+ template <class ContextCtor>
+ 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
--- /dev/null
+//******************************************************************
+//
+// 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
--- /dev/null
+//******************************************************************
+//
+// 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
--- /dev/null
+//******************************************************************
+//
+// 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 <stddef.h>
+
+#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
--- /dev/null
+//******************************************************************
+//
+// 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
--- /dev/null
+//******************************************************************
+//
+// 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
--- /dev/null
+To run the oc_logger C sample app, first build liboctbstack.a
+
+cd <root>/csdk
+
+To enable logging
+make BUILD=debug
+else
+make BUILD=release
+
+Next, build the oc_logger C sample app
+
+cd <root>/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
+
+
+
--- /dev/null
+# //******************************************************************
+# //
+# // 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)
--- /dev/null
+//******************************************************************
+//
+// 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#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;
+}
--- /dev/null
+//******************************************************************
+//
+// 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 <iosfwd>
+#include <memory>
+#include <cassert>
+#include <sstream>
+#include <iostream>
+
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/categories.hpp>
+
+#include <boost/config.hpp>
+#include <boost/iostreams/categories.hpp>
+#include <boost/iostreams/detail/ios.hpp>
+
+int main()
+{
+ using OC::oc_log_stream;
+
+ oc_log_stream ols(oc_make_console_logger);
+
+ boost::iostreams::stream<oc_log_stream> os(ols);
+
+ os << "Greetings from the nifty world of loggin'!" << std::flush;
+
+ (*os).set_module("TheHappiestModuleEver");
+
+ os << "Whee!" << std::flush;
+
+ return 0;
+}
+
#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<std::mutex> csdkLock, PlatformConfig cfg)
- : m_threadRun(false), m_csdkLock(csdkLock),
+ InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> 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
struct ListenContext
{
- FindCallback callback;
- IClientWrapper::Ptr clientWrapper;
+ FindCallback callback;
+ IClientWrapper::Ptr clientWrapper;
+ OC::OCPlatform const* owner; // observing ptr
};
OCStackApplicationResult listenCallback(void* ctx, OCDoHandle handle,
OCClientResponse* clientResponse)
{
- if(clientResponse->result == OC_STACK_OK)
- {
- ListenContext* context = static_cast<ListenContext*>(ctx);
+ ListenContext* context = static_cast<ListenContext*>(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() <<std::endl;
- // TODO: Do we want to handle this somehow? Perhaps we need to log this?
+ }
+ catch(boost::property_tree::json_parser::json_parser_error &e)
+ {
+ context->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);
}
catch(ResourceInitException& e)
{
- std::cout << "Failed to create resource: "<< e.what() <<std::endl;
- // TODO: Do we want to handle this somehow? Perhaps we need to log this?
+ context->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<<std::endl;
- return OC_STACK_KEEP_TRANSACTION;
}
+
+ return OC_STACK_KEEP_TRANSACTION;
}
OCStackResult InProcClientWrapper::ListenForResource(const std::string& serviceUrl,
ListenContext* context = new ListenContext();
context->callback = callback;
context->clientWrapper = shared_from_this();
+ context->owner = &m_owner;
cbdata.context = static_cast<void*>(context);
cbdata.cb = listenCallback;
{
GetContext* context = static_cast<GetContext*>(ctx);
- std::cout << "GET JSON: " << (char*) clientResponse->resJSONPayload << endl;
-
OCRepresentation rep;
if(clientResponse->result == OC_STACK_OK)
cbdata.cb = &getResourceCallback;
cbdata.cd = [](void* c){delete static_cast<GetContext*>(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<std::mutex> lock(*cLock);
OCDoHandle handle;
result = OCDoResource(&handle, OC_REST_GET, os.str().c_str(),
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<std::mutex> lock(*cLock);
result = OCDoResource(handle, method,
os.str().c_str(), nullptr,
OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle,
const std::string& host, SubscribeCallback& presenceHandler)
{
- OCStackResult result;
OCCallbackData cbdata = {0};
SubscribePresenceContext* ctx = new SubscribePresenceContext();
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)
#include <OCResourceResponse.h>
#include <ocstack.h>
#include <OCApi.h>
+#include <OCPlatform.h>
#include <OCUtilities.h>
using namespace std;
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<OC::OCResourceRequest>();
+OCEntityHandlerResult EntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest )
+{
+ auto pRequest = std::make_shared<OC::OCResourceRequest>();
auto pResponse = std::make_shared<OC::OCResourceResponse>();
// TODO Utility to convert from C to C++ (every).
-
+
if(flag & OC_INIT_FLAG)
{
// TODO We can fill the common data (resource Handle, etc.. )
}
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;
}
// 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;
}
}
namespace OC
{
- InProcServerWrapper::InProcServerWrapper(std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
- :m_csdkLock(csdkLock)
+ InProcServerWrapper::InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ : IServerWrapper(owner),
+ m_csdkLock(csdkLock)
{
OCMode initType;
while(cLock && m_threadRun)
{
OCStackResult result;
+
{
std::lock_guard<std::mutex> 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));
}
}
{
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)
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;
}
}
OCStackResult InProcServerWrapper::unregisterResource(const OCResourceHandle& resourceHandle)
{
- cout << "Unregistering Resource: \n";
-
auto cLock = m_csdkLock.lock();
OCStackResult result = OC_STACK_ERROR;
if(cLock)
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)
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)
#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)
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);
}
}
}
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;
}
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;