Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #111 from BojanKV/master
Browse files Browse the repository at this point in the history
Initial logging support instead of direct stdout in w3c-visserver-api
  • Loading branch information
daschubert committed Nov 1, 2019
2 parents ce7ec0c + c25c338 commit e2f24df
Show file tree
Hide file tree
Showing 20 changed files with 610 additions and 386 deletions.
5 changes: 3 additions & 2 deletions w3c-visserver-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ add_compile_options(-std=c++11 -pthread -Wall -Wextra -Werror)

if ("${ADDRESS_SAN}" STREQUAL "ON" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(-g -fsanitize=address -fno-omit-frame-pointer)
add_link_options(-g -fsanitize=address)
add_link_options(-g -fsanitize=address -ldl)
endif()


Expand All @@ -61,7 +61,7 @@ set(Boost_USE_STATIC_LIBS ON)
include_directories(${Boost_INCLUDE_DIRS})
message(STATUS " boost includes ${Boost_INCLUDE_DIRS} ")

find_package(Boost 1.58.0 COMPONENTS system thread program_options REQUIRED)
find_package(Boost 1.64.0 COMPONENTS system thread program_options REQUIRED)

target_link_libraries(simple-websocket-server INTERFACE ${Boost_LIBRARIES})
target_include_directories(simple-websocket-server INTERFACE ${Boost_INCLUDE_DIR})
Expand Down Expand Up @@ -114,6 +114,7 @@ if(UNIT_TEST)
${CMAKE_CURRENT_SOURCE_DIR}/src/subscriptionhandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/signing.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/wsserver.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/BasicLogger.cpp
# ${CMAKE_CURRENT_SOURCE_DIR}/unit-test/vssdatabase_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/unit-test/w3cunittest.cpp
)
Expand Down
44 changes: 44 additions & 0 deletions w3c-visserver-api/include/BasicLogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* ******************************************************************************
* Copyright (c) 2019 Robert Bosch GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Robert Bosch GmbH - initial API and functionality
* *****************************************************************************
*/


#ifndef __LOGGER_H__
#define __LOGGER_H__

#include "ILogger.hpp"

#include <mutex>


/**
* \class Logger
* \brief Implementation class of logging utility
*
* Logger shall provide standardized way to put logging information
* to standard output (stdout)
*
*/
class BasicLogger : public ILogger {
private:
std::mutex accessMutex;
const uint8_t logLevels;

public:
BasicLogger(uint8_t logEventsEnabled);
~BasicLogger();

void Log(LogLevel level, std::string logString);
};

#endif /* __LOGGER_H__ */
63 changes: 63 additions & 0 deletions w3c-visserver-api/include/ILogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* ******************************************************************************
* Copyright (c) 2019 Robert Bosch GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Robert Bosch GmbH - initial API and functionality
* *****************************************************************************
*/


#ifndef __ILOGGER_H__
#define __ILOGGER_H__

#include <cstdint>
#include <string>

/**
* \class LogLevel
* \brief Supported logging levels
*
* Enumeration shall be used by implementations of \ref ILogger class to define
* which events to log.
* As enumeration is bitwise compatible, implementations can allow for combining
* different log levels (e.g. INFO & ERROR, other levels shall be ignored)
*/
enum class LogLevel : uint8_t {
NONE = 0x0, //!< No logging allowed
VERBOSE = 0x1, //!< Used for logging verbose or debug information, not relevant in normal execution
INFO = 0x2, //!< Used for logging e.g. positive information relevant to execution
WARNING = 0x4, //!< Used for logging warning states and information that is not critical
ERROR = 0x8, //!< Used for logging any error or fail information
ALL = 0xF //!< Used for selecting all log levels
};


/**
* \class ILogger
* \brief Interface definition for logging utility
*
* Interface for logger allows to easily add support for logging through different
* means or requirements (e.g. resource scarce platforms, socket, serial, ...)
*
*/
class ILogger {
public:
virtual ~ILogger() {}

/**
* \brief Log message of specified notification level
*
* \param[in] Level of importance of log message
* \param[in] Log message
*/
virtual void Log(LogLevel, std::string) = 0;

};

#endif /* __ILOGGER_H__ */
6 changes: 5 additions & 1 deletion w3c-visserver-api/include/authenticator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@
#ifndef __AUTHENTICATOR_H__
#define __AUTHENTICATOR_H__

#include <memory>
#include <string>

using namespace std;

class wschannel;
class vssdatabase;
class ILogger;

class authenticator {
private:
string pubkey = "secret";
string algorithm = "RS256";
std::shared_ptr<ILogger> logger;

int validateToken(wschannel& channel, string authToken);

public:
authenticator(string secretkey, string algorithm);
authenticator(std::shared_ptr<ILogger> loggerUtil, string secretkey, string algorithm);
int validate(wschannel &channel, vssdatabase *database,
string authToken);

Expand Down
5 changes: 4 additions & 1 deletion w3c-visserver-api/include/permmclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#define __PERMMCLIENT_H__

#include <jsoncons/json.hpp>
#include <memory>

using namespace std;
using namespace jsoncons;

json getPermToken(string clientName, string clientSecret);
class ILogger;

json getPermToken(std::shared_ptr<ILogger> logger, string clientName, string clientSecret);



Expand Down
7 changes: 6 additions & 1 deletion w3c-visserver-api/include/signing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@
#include <iostream>
#include <jsoncons/json.hpp>

#include <memory>

using namespace std;
using namespace jsoncons;
using namespace jwt;

class ILogger;

class signing {
private:
std::shared_ptr<ILogger> logger;
string key = "";
string pubkey = "";
string algorithm = "RS256";

public:
signing();
signing(std::shared_ptr<ILogger> loggerUtil);
string getKey(string fileName);
string getPublicKey(string fileName);
string sign(json data);
Expand Down
6 changes: 5 additions & 1 deletion w3c-visserver-api/include/subscriptionhandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "visconf.hpp"
#include <string>
#include <thread>
#include <memory>

#include <jsoncons/json.hpp>

Expand All @@ -28,6 +29,7 @@ class authenticator;
class vssdatabase;
class wschannel;
class wsserver;
class ILogger;

// Subscription ID: Client ID
typedef std::unordered_map<uint32_t, uint32_t> subscriptions_t;
Expand All @@ -37,6 +39,7 @@ typedef std::string uuid_t;

class subscriptionhandler {
private:
std::shared_ptr<ILogger> logger;
std::unordered_map<uuid_t, subscriptions_t> subscribeHandle;
wsserver* server;
authenticator* validator;
Expand All @@ -47,7 +50,8 @@ class subscriptionhandler {
std::queue<std::pair<uint32_t, jsoncons::json>> buffer;

public:
subscriptionhandler(wsserver* wserver,
subscriptionhandler(std::shared_ptr<ILogger> loggerUtil,
wsserver* wserver,
authenticator* authenticate,
accesschecker* checkAccess);
~subscriptionhandler();
Expand Down
7 changes: 6 additions & 1 deletion w3c-visserver-api/include/vsscommandprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define __VSSCOMMANDPROCESSOR_H__

#include <string>
#include <memory>

#include <jsoncons/json.hpp>

Expand All @@ -23,9 +24,11 @@ class subscriptionhandler;
class authenticator;
class accesschecker;
class wschannel;
class ILogger;

class vsscommandprocessor {
private:
std::shared_ptr<ILogger> logger;
vssdatabase* database = NULL;
subscriptionhandler* subHandler = NULL;
authenticator* tokenValidator = NULL;
Expand All @@ -48,7 +51,9 @@ class vsscommandprocessor {


public:
vsscommandprocessor(vssdatabase* database, authenticator* vdator,
vsscommandprocessor(std::shared_ptr<ILogger> loggerUtil,
vssdatabase* database,
authenticator* vdator,
subscriptionhandler* subhandler);
~vsscommandprocessor();
std::string processQuery(std::string req_json, wschannel& channel);
Expand Down
6 changes: 5 additions & 1 deletion w3c-visserver-api/include/vssdatabase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include <string>
#include <list>
#include <mutex>
#include <memory>

#include <jsoncons/json.hpp>

class subscriptionhandler;
class accesschecker;
class wschannel;
class ILogger;

class vssdatabase {
friend class subscriptionhandler;
Expand All @@ -32,6 +34,7 @@ class vssdatabase {
#endif

private:
std::shared_ptr<ILogger> logger;
std::mutex rwMutex;
jsoncons::json data_tree;
jsoncons::json meta_tree;
Expand All @@ -45,7 +48,8 @@ class vssdatabase {
void checkSetPermission(wschannel& channel, jsoncons::json valueJson);

public:
vssdatabase(subscriptionhandler* subHandle,
vssdatabase(std::shared_ptr<ILogger> loggerUtil,
subscriptionhandler* subHandle,
accesschecker* accValidator);
~vssdatabase();
void initJsonTree(std::string fileName);
Expand Down
5 changes: 4 additions & 1 deletion w3c-visserver-api/include/wsserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@

#include "server_wss.hpp"


class vsscommandprocessor;
class vsscommandprocessor;
class subscriptionhandler;
class authenticator;
class vssdatabase;
class accesschecker;
class ILogger;

class wsserver {
private:
SimpleWeb::SocketServer<SimpleWeb::WSS> *secureServer_;
SimpleWeb::SocketServer<SimpleWeb::WS> *insecureServer_;
std::shared_ptr<ILogger> logger;
bool isSecure_;
std::string configFileName_;

Expand All @@ -38,7 +41,7 @@ class wsserver {
vssdatabase* database;
accesschecker* accessCheck;

wsserver(int port, std::string configFileName, bool secure);
wsserver(std::shared_ptr<ILogger> loggerUtil, int port, std::string configFileName, bool secure);
~wsserver();
void startServer(std::string endpointName);
void sendToConnection(uint32_t connID, std::string message);
Expand Down
63 changes: 63 additions & 0 deletions w3c-visserver-api/src/BasicLogger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* ******************************************************************************
* Copyright (c) 2019 Robert Bosch GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Robert Bosch GmbH - initial API and functionality
* *****************************************************************************
*/

#include "BasicLogger.hpp"

#include <iostream>


using namespace std;

namespace {
std::string GetLevelString(LogLevel level) {
std::string retStr;
switch (level) {
case LogLevel::VERBOSE:
retStr = "VERBOSE: ";
break;
case LogLevel::INFO:
retStr = "INFO: ";
break;
case LogLevel::WARNING:
retStr = "WARNING: ";
break;
case LogLevel::ERROR:
retStr = "ERROR: ";
break;
default:
retStr = "UNKNOWN: ";
break;
}
return retStr;
}
}


BasicLogger::BasicLogger(uint8_t logEventsEnabled) : logLevels(logEventsEnabled) {
cout << "Log START" << endl;
}


BasicLogger::~BasicLogger() {
cout << "Log END" << endl;
}

void BasicLogger::Log(LogLevel level, std::string logString) {
std::lock_guard<std::mutex> guard(accessMutex);

/* log only if level in supported */
if (static_cast<uint8_t>(level) & logLevels) {
cout << GetLevelString(level) + logString << endl;
}
}
Loading

0 comments on commit e2f24df

Please sign in to comment.