Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ set (JSRUNTIME_COMMON_FILES
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/NativeJSRenderer.cpp
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/ModuleSettings.cpp
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/TimeUtils.cpp
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/NativeJSLogger.cpp
)

if (ENABLE_JSRUNTIME_ESSOS)
Expand Down Expand Up @@ -113,6 +114,7 @@ endif ( ENABLE_JSRUNTIME_SERVER )

set(JSRUNTIME_CLIENT_FILES
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeClient.cpp
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/NativeJSLogger.cpp
)

add_library(${JSRUNTIME_LIBRARY_NAME} SHARED
Expand Down
40 changes: 40 additions & 0 deletions include/NativeJSLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2024 RDK Management
*
* 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 NATIVEJS_LOGGER_H
#define NATIVEJS_LOGGER_H

enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
FATAL
};

class NativeJSLogger
{
public:
static void setLogLevel(const char* loglevel);
static void log(LogLevel level, const char* format, ...);
private:
static LogLevel sLogLevel;
};

#endif // NATIVEJS_LOGGER_H
3 changes: 2 additions & 1 deletion include/jsc/JavaScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptContextBase.h>
#include <NativeJSLogger.h>
#include "rtScriptJSCPrivate.h"
#include <KeyListener.h>
#include <KeyInput.h>
Expand Down Expand Up @@ -84,7 +85,7 @@ class JavaScriptContext: public JavaScriptContextBase, public NetworkMetricsList
void setPlaybackStartTime(double time) {
mPerformanceMetrics.playbackStartTime = time;
double launchTime = mPerformanceMetrics.playbackStartTime - mPerformanceMetrics.startTime;
std::cout << "\n-----LAUNCH TIME-----: " << std::fixed << std::setprecision(3) << launchTime << " ms\n";
NativeJSLogger::log(INFO, "------LAUNCH_TIME-----:%.3f ms\n", launchTime);
}

virtual void onMetricsData (NetworkMetrics *net) override;
Expand Down
3 changes: 2 additions & 1 deletion src/EssosInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "EssosInstance.h"
#include "KeyInput.h"
#include "NativeJSLogger.h"

#include <iostream>

Expand Down Expand Up @@ -196,7 +197,7 @@ bool EssosInstance::initialize(bool useWayland)
if ( essosError )
{
const char *errorDetail = EssContextGetLastErrorDetail(mEssosContext);
std::cout << "Essos error during initialization: " << errorDetail;
NativeJSLogger::log(ERROR, "Essos error during initialization: %s\n", errorDetail);
}
}
return !essosError;
Expand Down
30 changes: 15 additions & 15 deletions src/JSRuntimeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
**/
#include <JSRuntimeClient.h>
#include <NativeJSLogger.h>
#include "jsc_lib.h"
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -58,7 +59,7 @@ bool JSRuntimeClient::run()
WsClient::connection_ptr con = mEndPoint.get_connection(uri, ec);
if (ec)
{
std::cout << "Could not create connection because: " << ec.message() << std::endl;
NativeJSLogger::log(ERROR, "Could not create connection because: %s\n", ec.message().c_str());
return false;
}

Expand All @@ -75,12 +76,11 @@ bool JSRuntimeClient::run()

bool JSRuntimeClient::send(const std::string &message)
{
std::cout << "Enter: " << __func__ << " : " << message << std::endl;

NativeJSLogger::log(INFO, "Enter: %s : %s\n", __func__, message.c_str());
if (message.empty())
{
std::cout << "Can't send empty message\n";
return false;
NativeJSLogger::log(WARN, "Can't send empty message\n");
return false;
}

try
Expand All @@ -89,15 +89,15 @@ bool JSRuntimeClient::send(const std::string &message)
}
catch (websocketpp::exception const &e)
{
std::cout << "Send failure: " << e.what() << std::endl;
NativeJSLogger::log(ERROR, "Send failure: %s\n", e.what());
}

return true;
}

bool JSRuntimeClient::close()
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);

websocketpp::lib::error_code ec;
mEndPoint.close(mConnectionHdl, websocketpp::close::status::going_away, "", ec);
Expand All @@ -120,26 +120,26 @@ void JSRuntimeClient::setState(const std::string &state)
void JSRuntimeClient::onMessage(websocketpp::connection_hdl hdl, message_ptr msg)
{
std::string msgstr = msg->get_payload();
std::cout << "Enter: " << __func__ << " : " << msgstr << std::endl;
NativeJSLogger::log(INFO, "Enter: %s : %s\n", __func__, msgstr.c_str());

CommandInterface<JSRuntimeClient>::onMessage(msgstr);
}

void JSRuntimeClient::onOpen(websocketpp::connection_hdl hdl)
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
setState("open");
}

void JSRuntimeClient::onFail(websocketpp::connection_hdl hdl)
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
setState("fail");
}

void JSRuntimeClient::onClose(websocketpp::connection_hdl hdl)
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
setState("close");
}

Expand All @@ -150,15 +150,15 @@ int main(int argc, char **argv)

if (argc > 1)
{
std::cout << "Send input commands at ws://localhost:" << std::to_string(WS_SERVER_PORT) << std::endl;
NativeJSLogger::log(INFO, "Send input commands at ws://localhost:%s\n", std::to_string(WS_SERVER_PORT).c_str());
return -1;
}

JSRuntimeClient *client = JSRuntimeClient::getInstance();
client->initialize(WS_SERVER_PORT);
if (!client->run())
{
std::cout << "Unable to connect server" << std::endl;
NativeJSLogger::log(ERROR, "Unable to connect to server\n");
return -1;
}

Expand All @@ -167,11 +167,11 @@ int main(int argc, char **argv)
client->sendCommand(command, response);
if (!response.empty())
{
std::cout << "Response: " << response << std::endl;
NativeJSLogger::log(INFO, "Response: %s\n", response.c_str());
}
else
{
std::cout << "Missing response" << std::endl;
NativeJSLogger::log(WARN, "Missing response\n");
break;
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/JSRuntimeServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "jsc_lib.h"
#include <JSRuntimeServer.h>
#include <NativeJSLogger.h>
#include <iostream>
#include <sstream>
#include <string>
Expand All @@ -35,17 +36,17 @@ class JsonWrap
mPtr = cJSON_Parse(jsonStr.c_str());
if (mPtr == nullptr)
{
std::cerr << "Error parsing JSON" << std::endl;
}
NativeJSLogger::log(ERROR, "Error parsing JSON\n");
}
}
JsonWrap(JsonWrap &root, const char *name)
{
mIsRoot = false;
cJSON *itm = cJSON_GetObjectItem(root.get(), name);
if (!itm || !cJSON_IsObject(itm))
{
std::cerr << "Error: " << name << "is not an object" << std::endl;
itm = nullptr;
NativeJSLogger::log(ERROR, "Error: %s is not an object\n", name);
itm = nullptr;
}
mPtr = itm;
}
Expand All @@ -61,8 +62,8 @@ class JsonWrap
cJSON *itm = cJSON_GetObjectItem(mPtr, name);
if (!itm || !cJSON_IsString(itm))
{
std::cerr << "Error: " << name << "is not a string" << std::endl;
err = true;
NativeJSLogger::log(ERROR, "Error: %s is not a string\n", name);
err = true;
}
else
{
Expand Down Expand Up @@ -94,15 +95,15 @@ JSRuntimeServer::JSRuntimeServer() : mServerPort(0)

void JSRuntimeServer::initialize(int serverport, std::shared_ptr<JsRuntime::NativeJSRenderer> renderer)
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);

mServerPort = serverport;
mRenderer = renderer;
}

bool JSRuntimeServer::start()
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);

mServer.set_access_channels(websocketpp::log::alevel::all);
mServer.clear_access_channels(websocketpp::log::alevel::frame_payload);
Expand All @@ -126,8 +127,7 @@ bool JSRuntimeServer::start()

bool JSRuntimeServer::stop()
{
std::cout << "Enter: " << __func__ << std::endl;

NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
mServer.stop_listening();

// Close all existing connections
Expand All @@ -153,14 +153,15 @@ void JSRuntimeServer::send(websocketpp::connection_hdl hdl, const std::string &m
}
catch (websocketpp::exception const &e)
{
std::cout << "Send failure: " << e.what() << std::endl;
NativeJSLogger::log(ERROR, "Send failure: %s\n", e.what());
}

}

void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg)
{
std::string msgstr = msg->get_payload();
std::cout << "Enter: " << __func__ << ": " << msgstr << std::endl;
NativeJSLogger::log(INFO, "Enter: %s : %s\n", __func__, msgstr.c_str());

// Example input:
// {"method": "launchApplication", "params":{"url":"/opt/www/demo/player.js", "options":"player,xhr"}}
Expand Down Expand Up @@ -252,15 +253,15 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg

void JSRuntimeServer::onOpen(websocketpp::connection_hdl hdl)
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);

std::lock_guard<std::mutex> lock(mDataMutex);
mConnections.insert(hdl);
}

void JSRuntimeServer::onClose(websocketpp::connection_hdl hdl)
{
std::cout << "Enter: " << __func__ << std::endl;
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);

std::lock_guard<std::mutex> lock(mDataMutex);
mConnections.erase(hdl);
Expand Down
11 changes: 6 additions & 5 deletions src/JavaScriptContextBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
**/

#include <JavaScriptContextBase.h>
#include <NativeJSLogger.h>
#include <fstream>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -62,12 +63,12 @@ void JavaScriptContextBase::registerCommonUtils()
{
if (mEmbedThunderJS)
{
std::cout << "executing thunder js code " << std::endl;
NativeJSLogger::log(INFO, "Executing Thunder JS code\n");
runScript(sThunderJSCode.c_str());
}
if (mEmbedWebBridge)
{
std::cout << "executing rdk webbridge js code " << std::endl;
NativeJSLogger::log(INFO, "Executing rdk webbridge JS code\n");
runScript(sWebBridgeCode.c_str());
}
}
Expand All @@ -84,7 +85,7 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA
{
if (!file)
{
printf(" %s ... no script given.",__PRETTY_FUNCTION__);
NativeJSLogger::log(WARN, "%s ... no script given.\n", __PRETTY_FUNCTION__);
fflush(stdout);
return false;
}
Expand All @@ -96,10 +97,10 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA
std::string fileName("/home/root/");
fileName.append(file);
scriptToRun = readFile(fileName.c_str());
printf("checking in [%s] \n", fileName.c_str());
NativeJSLogger::log(INFO, "Checking in [%s]\n", fileName.c_str());
if(scriptToRun.empty())
{
printf(" %s ... load error / not found. %s",__PRETTY_FUNCTION__, file);
NativeJSLogger::log(ERROR, "%s ... load error / not found. %s\n", __PRETTY_FUNCTION__, file);
fflush(stdout);
return false;
}
Expand Down
Loading
Loading