Skip to content

Commit

Permalink
refactor: refactor logger initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Daeyeon Jeong <[email protected]>
  • Loading branch information
daeyeon authored and hs0225 committed Mar 27, 2024
1 parent 2ac0924 commit 2917b87
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
53 changes: 34 additions & 19 deletions deps/node/src/lwnode/aul-event-receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

#include "aul-event-receiver.h"
#include <assert.h>
#include <unistd.h> // getpid
#include <uv.h>
#include <sstream>
#include "trace.h"

#ifdef HOST_TIZEN
Expand Down Expand Up @@ -50,18 +52,27 @@ int AULEventReceiver::aulEventHandler(aul_type type, bundle* b, void* data) {
return 0;
}

bool AULEventReceiver::hasAulArguments(int argc, char* argv[]) {
bool AULEventReceiver::hasAulArguments(int argc,
char* argv[],
std::string& parsed_bundle) {
bool result = false;

bundle* parsed = bundle_import_from_argv(argc, argv);
if (parsed) {
if (bundle_get_val(parsed, AUL_K_STARTTIME)) {
std::ostringstream oss;

bundle_iterate(
parsed,
[](const char* key, const char* value, void* d) {
LWNODE_DEV_LOGF("bundle - key: %s, value: %s", key, value);
[](const char* key, const char* value, void* data) {
std::ostringstream* oss =
reinterpret_cast<std::ostringstream*>(data);
*oss << "bundle - key: " << key << ", value: " << value
<< std::endl;
},
NULL);
&oss);

parsed_bundle = oss.str();
result = true;
}
bundle_free(parsed);
Expand All @@ -71,21 +82,21 @@ bool AULEventReceiver::hasAulArguments(int argc, char* argv[]) {
}

bool AULEventReceiver::start(int argc, char* argv[]) {
isEventReceiverRunning_ = false;
std::string parsed_bundle;

if (hasAulArguments(argc, argv)) {
isEventReceiverRunning_ = true;
if (hasAulArguments(argc, argv, parsed_bundle)) {
aul_launch_init(aulEventHandler, nullptr);
aul_launch_argv_handler(argc, argv);

char appid[kMaxPackageNameSize + 1];
aul_app_get_appid_bypid(getpid(), appid, kMaxPackageNameSize);
appid_ = appid;

initLoggerOutput(appid_);
appid_ = appid;

LWNODE_DEV_LOG("appid:", appid_);
initLoggerOutput(true, appid_);

aul_launch_init(aulEventHandler, nullptr);
aul_launch_argv_handler(argc, argv);
LWNODE_DEV_LOG(parsed_bundle);
LWNODE_DEV_LOG("appid: ", appid_);

char* path = app_get_resource_path();
if (uv_chdir(path) != 0) {
Expand All @@ -94,11 +105,14 @@ bool AULEventReceiver::start(int argc, char* argv[]) {
exit(-errno);
}
free(path);
return isEventReceiverRunning_;

assert(!appid_.empty());
return isEventReceiverRunning();
}

initLoggerOutput();
return isEventReceiverRunning_;
initLoggerOutput(false);
assert(appid_.empty());
return isEventReceiverRunning();
}
#endif

Expand All @@ -108,18 +122,19 @@ AULEventReceiver* AULEventReceiver::getInstance() {
}

bool AULEventReceiver::isEventReceiverRunning() {
return isEventReceiverRunning_;
return !appid_.empty();
}

void AULEventReceiver::initLoggerOutput(const std::string tag) {
void AULEventReceiver::initLoggerOutput(bool isEventReceiverRunning,
const std::string tag) {
if (!tag.empty()) {
LogKind::user()->tag = tag;
}

LogOption::setDefaultOutputInstantiator([&]() {
LogOption::setDefaultOutputInstantiator([&isEventReceiverRunning]() {
static thread_local std::shared_ptr<Logger::Output> s_loggerOutput;
if (s_loggerOutput == nullptr) {
s_loggerOutput = isEventReceiverRunning()
s_loggerOutput = isEventReceiverRunning
? std::static_pointer_cast<Logger::Output>(
std::make_shared<DlogOut>())
: std::static_pointer_cast<Logger::Output>(
Expand Down
10 changes: 4 additions & 6 deletions deps/node/src/lwnode/aul-event-receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ class AULEventReceiver {

#ifdef HOST_TIZEN
static int aulEventHandler(aul_type type, bundle* b, void* data);
bool hasAulArguments(int argc, char* argv[]);
bool hasAulArguments(int argc, char* argv[], std::string& parsed_bundle);
bool start(int argc, char* argv[]);
#else
bool start(int argc, char* argv[]) {
return false;
}
bool start(int argc, char* argv[]) { return false; }
#endif

void initLoggerOutput(const std::string tag = "");
void initLoggerOutput(bool isEventReceiverRunning,
const std::string tag = "");
bool isEventReceiverRunning();

private:
AULEventReceiver() = default;
bool isEventReceiverRunning_{false};
static const int kMaxPackageNameSize{512};
std::string appid_;
};
5 changes: 1 addition & 4 deletions src/api/utils/logger/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ void DlogOut::flush(std::stringstream& ss,
std::shared_ptr<Output::Config> config) {
auto c =
config ? std::static_pointer_cast<DLogConfig>(config) : LogKind::lwnode();

// TODO: handle the case where users manually select a logging method.

#ifdef HOST_TIZEN
dlog_print(DLOG_INFO, c->tag.c_str(), "%s", ss.str().c_str());
#else
// For testing. StdOut will be used to flush buffers through stdout.
std::cerr << std::left << std::setfill(' ') << std::setw(6) << c->tag << " "
std::cout << std::left << std::setfill(' ') << std::setw(6) << c->tag << " "
<< ss.str();
#endif
}
Expand Down

0 comments on commit 2917b87

Please sign in to comment.