Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change visibility for the symbols of logger #74

Merged
merged 2 commits into from
Mar 27, 2024
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
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_;
};
13 changes: 10 additions & 3 deletions src/api/utils/logger/logger-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include <sstream>
#include <string>

#ifndef LWNODE_EXPORT
#define LWNODE_EXPORT __attribute__((visibility("default")))
#endif

class LogFormatter {
public:
std::string header();
Expand Down Expand Up @@ -154,7 +158,7 @@ class StdOut : public Logger::Output {

using OutputInstantiator = std::function<std::shared_ptr<Logger::Output>()>;

class LogOption {
class LWNODE_EXPORT LogOption {
public:
static void setDefaultOutputInstantiator(OutputInstantiator fn) {
s_outputInstantiator_ = fn;
Expand All @@ -166,8 +170,11 @@ class LogOption {

static std::shared_ptr<Logger::Output> getDefalutOutput() {
if (s_outputInstantiator_ == nullptr) {
// Set default output
s_outputInstantiator_ = []() { return std::make_shared<StdOut>(); };
static std::shared_ptr<Logger::Output> default_output;
if (default_output == nullptr) {
default_output = std::make_shared<StdOut>();
}
return default_output;
}
return s_outputInstantiator_();
}
Expand Down
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
6 changes: 3 additions & 3 deletions src/api/utils/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ struct DLogConfig : Logger::Output::Config {
std::string tag;
};

class LWNodeLogger : public Logger {
class LWNODE_EXPORT LWNodeLogger : public Logger {
public:
LWNodeLogger(std::shared_ptr<DLogConfig> config) { outConfig_ = config; }
};

class DlogOut : public Logger::Output {
class LWNODE_EXPORT DlogOut : public Logger::Output {
public:
void flush(std::stringstream& ss,
std::shared_ptr<Output::Config> config = nullptr) override;
void appendEndOfLine(std::stringstream& ss) override;
bool hasAutoAppendEndOfLine() override { return true; }
};

class LogKind {
class LWNODE_EXPORT LogKind {
public:
static std::shared_ptr<DLogConfig> user() { return getInstance()->user_; }
static std::shared_ptr<DLogConfig> lwnode() { return getInstance()->lwnode_; }
Expand Down
Loading