Skip to content

Commit af6534e

Browse files
trupthi1403Truphti
authored andcommitted
Ticket Number:RDKEMW-3430: Implementing log levels
Reason for change: Added log statements to quickjs/JavaScriptUtils.cpp Test Procedure: build should be successful and able to set loglevels Risks: low Priority: P2
1 parent 81fff9c commit af6534e

18 files changed

Lines changed: 229 additions & 97 deletions

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ set (JSRUNTIME_COMMON_FILES
6565
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/NativeJSRenderer.cpp
6666
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/ModuleSettings.cpp
6767
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/TimeUtils.cpp
68+
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/NativeJSLogger.cpp
6869
)
6970

7071
if (ENABLE_JSRUNTIME_ESSOS)
@@ -113,6 +114,7 @@ endif ( ENABLE_JSRUNTIME_SERVER )
113114

114115
set(JSRUNTIME_CLIENT_FILES
115116
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeClient.cpp
117+
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/NativeJSLogger.cpp
116118
)
117119

118120
add_library(${JSRUNTIME_LIBRARY_NAME} SHARED

include/NativeJSLogger.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* If not stated otherwise in this file or this component's LICENSE
3+
* file the following copyright and licenses apply:
4+
*
5+
* Copyright 2024 RDK Management
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
**/
19+
20+
#ifndef NATIVEJS_LOGGER_H
21+
#define NATIVEJS_LOGGER_H
22+
23+
enum LogLevel {
24+
DEBUG,
25+
INFO,
26+
WARN,
27+
ERROR,
28+
FATAL
29+
};
30+
31+
class NativeJSLogger
32+
{
33+
public:
34+
static void setLogLevel(const char* loglevel);
35+
static void log(LogLevel level, const char* format, ...);
36+
private:
37+
static LogLevel sLogLevel;
38+
};
39+
40+
#endif // NATIVEJS_LOGGER_H

include/jsc/JavaScriptContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <JavaScriptCore/JavaScript.h>
3434
#include <JavaScriptContextBase.h>
35+
#include <NativeJSLogger.h>
3536
#include "rtScriptJSCPrivate.h"
3637
#include <KeyListener.h>
3738
#include <KeyInput.h>

src/EssosInstance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "EssosInstance.h"
2121
#include "KeyInput.h"
22+
#include "NativeJSLogger.h"
2223

2324
#include <iostream>
2425

@@ -196,7 +197,7 @@ bool EssosInstance::initialize(bool useWayland)
196197
if ( essosError )
197198
{
198199
const char *errorDetail = EssContextGetLastErrorDetail(mEssosContext);
199-
std::cout << "Essos error during initialization: " << errorDetail;
200+
NativeJSLogger::log(ERROR, "Essos error during initialization: %s\n", errorDetail);
200201
}
201202
}
202203
return !essosError;

src/JSRuntimeClient.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
**/
1919
#include <JSRuntimeClient.h>
20+
#include <NativeJSLogger.h>
2021
#include "jsc_lib.h"
2122
#include <iostream>
2223
#include <sstream>
@@ -58,7 +59,7 @@ bool JSRuntimeClient::run()
5859
WsClient::connection_ptr con = mEndPoint.get_connection(uri, ec);
5960
if (ec)
6061
{
61-
std::cout << "Could not create connection because: " << ec.message() << std::endl;
62+
NativeJSLogger::log(ERROR, "Could not create connection because: %s\n", ec.message().c_str());
6263
return false;
6364
}
6465

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

7677
bool JSRuntimeClient::send(const std::string &message)
7778
{
78-
std::cout << "Enter: " << __func__ << " : " << message << std::endl;
79-
79+
NativeJSLogger::log(INFO, "Enter: %s : %s\n", __func__, message.c_str());
8080
if (message.empty())
8181
{
82-
std::cout << "Can't send empty message\n";
83-
return false;
82+
NativeJSLogger::log(WARN, "Can't send empty message\n");
83+
return false;
8484
}
8585

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

9595
return true;
9696
}
9797

9898
bool JSRuntimeClient::close()
9999
{
100-
std::cout << "Enter: " << __func__ << std::endl;
100+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
101101

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

125125
CommandInterface<JSRuntimeClient>::onMessage(msgstr);
126126
}
127127

128128
void JSRuntimeClient::onOpen(websocketpp::connection_hdl hdl)
129129
{
130-
std::cout << "Enter: " << __func__ << std::endl;
130+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
131131
setState("open");
132132
}
133133

134134
void JSRuntimeClient::onFail(websocketpp::connection_hdl hdl)
135135
{
136-
std::cout << "Enter: " << __func__ << std::endl;
136+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
137137
setState("fail");
138138
}
139139

140140
void JSRuntimeClient::onClose(websocketpp::connection_hdl hdl)
141141
{
142-
std::cout << "Enter: " << __func__ << std::endl;
142+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
143143
setState("close");
144144
}
145145

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

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

157157
JSRuntimeClient *client = JSRuntimeClient::getInstance();
158158
client->initialize(WS_SERVER_PORT);
159159
if (!client->run())
160160
{
161-
std::cout << "Unable to connect server" << std::endl;
161+
NativeJSLogger::log(ERROR, "Unable to connect to server\n");
162162
return -1;
163163
}
164164

@@ -167,11 +167,11 @@ int main(int argc, char **argv)
167167
client->sendCommand(command, response);
168168
if (!response.empty())
169169
{
170-
std::cout << "Response: " << response << std::endl;
170+
NativeJSLogger::log(INFO, "Response: %s\n", response.c_str());
171171
}
172172
else
173173
{
174-
std::cout << "Missing response" << std::endl;
174+
NativeJSLogger::log(WARN, "Missing response\n");
175175
break;
176176
}
177177
}

src/JSRuntimeServer.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "jsc_lib.h"
2121
#include <JSRuntimeServer.h>
22+
#include <NativeJSLogger.h>
2223
#include <iostream>
2324
#include <sstream>
2425
#include <string>
@@ -35,17 +36,17 @@ class JsonWrap
3536
mPtr = cJSON_Parse(jsonStr.c_str());
3637
if (mPtr == nullptr)
3738
{
38-
std::cerr << "Error parsing JSON" << std::endl;
39-
}
39+
NativeJSLogger::log(ERROR, "Error parsing JSON\n");
40+
}
4041
}
4142
JsonWrap(JsonWrap &root, const char *name)
4243
{
4344
mIsRoot = false;
4445
cJSON *itm = cJSON_GetObjectItem(root.get(), name);
4546
if (!itm || !cJSON_IsObject(itm))
4647
{
47-
std::cerr << "Error: " << name << "is not an object" << std::endl;
48-
itm = nullptr;
48+
NativeJSLogger::log(ERROR, "Error: %s is not an object\n", name);
49+
itm = nullptr;
4950
}
5051
mPtr = itm;
5152
}
@@ -61,8 +62,8 @@ class JsonWrap
6162
cJSON *itm = cJSON_GetObjectItem(mPtr, name);
6263
if (!itm || !cJSON_IsString(itm))
6364
{
64-
std::cerr << "Error: " << name << "is not a string" << std::endl;
65-
err = true;
65+
NativeJSLogger::log(ERROR, "Error: %s is not a string\n", name);
66+
err = true;
6667
}
6768
else
6869
{
@@ -94,15 +95,15 @@ JSRuntimeServer::JSRuntimeServer() : mServerPort(0)
9495

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

99100
mServerPort = serverport;
100101
mRenderer = renderer;
101102
}
102103

103104
bool JSRuntimeServer::start()
104105
{
105-
std::cout << "Enter: " << __func__ << std::endl;
106+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
106107

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

127128
bool JSRuntimeServer::stop()
128129
{
129-
std::cout << "Enter: " << __func__ << std::endl;
130-
130+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
131131
mServer.stop_listening();
132132

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

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

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

253254
void JSRuntimeServer::onOpen(websocketpp::connection_hdl hdl)
254255
{
255-
std::cout << "Enter: " << __func__ << std::endl;
256+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
256257

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

261262
void JSRuntimeServer::onClose(websocketpp::connection_hdl hdl)
262263
{
263-
std::cout << "Enter: " << __func__ << std::endl;
264+
NativeJSLogger::log(INFO, "Enter: %s\n", __func__);
264265

265266
std::lock_guard<std::mutex> lock(mDataMutex);
266267
mConnections.erase(hdl);

src/JavaScriptContextBase.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
**/
1919

2020
#include <JavaScriptContextBase.h>
21+
#include <NativeJSLogger.h>
2122
#include <fstream>
2223
#include <iostream>
2324
#include <sstream>
@@ -62,12 +63,12 @@ void JavaScriptContextBase::registerCommonUtils()
6263
{
6364
if (mEmbedThunderJS)
6465
{
65-
std::cout << "executing thunder js code " << std::endl;
66+
NativeJSLogger::log(INFO, "Executing Thunder JS code\n");
6667
runScript(sThunderJSCode.c_str());
6768
}
6869
if (mEmbedWebBridge)
6970
{
70-
std::cout << "executing rdk webbridge js code " << std::endl;
71+
NativeJSLogger::log(INFO, "Executing rdk webbridge JS code\n");
7172
runScript(sWebBridgeCode.c_str());
7273
}
7374
}
@@ -84,7 +85,7 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA
8485
{
8586
if (!file)
8687
{
87-
printf(" %s ... no script given.",__PRETTY_FUNCTION__);
88+
NativeJSLogger::log(WARN, "%s ... no script given.\n", __PRETTY_FUNCTION__);
8889
fflush(stdout);
8990
return false;
9091
}
@@ -96,10 +97,10 @@ bool JavaScriptContextBase::runFile(const char *file, const char* args, bool isA
9697
std::string fileName("/home/root/");
9798
fileName.append(file);
9899
scriptToRun = readFile(fileName.c_str());
99-
printf("checking in [%s] \n", fileName.c_str());
100+
NativeJSLogger::log(INFO, "Checking in [%s]\n", fileName.c_str());
100101
if(scriptToRun.empty())
101102
{
102-
printf(" %s ... load error / not found. %s",__PRETTY_FUNCTION__, file);
103+
NativeJSLogger::log(ERROR, "%s ... load error / not found. %s\n", __PRETTY_FUNCTION__, file);
103104
fflush(stdout);
104105
return false;
105106
}

0 commit comments

Comments
 (0)