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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ if(BUILD_CONNMAN)
COMPONENT ${PROJECT_NAME}-dev)
endif(BUILD_CONNMAN)

install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/amarula/log.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/amarula
COMPONENT ${PROJECT_NAME}-dev)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
if(BUILD_TESTS)
include(CTest)
Expand Down
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
add_compile_definitions(LCM_LOG_DEFAULT_ENABLED=1)

if(BUILD_CONNMAN)
add_executable(connmanctl_dbus connmanctl.cpp)
target_link_libraries(connmanctl_dbus PRIVATE GConnmanDbus)
Expand Down
5 changes: 3 additions & 2 deletions include/amarula/dbus/gproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <glib.h>

#include <amarula/dbus/gdbus.hpp>
#include <amarula/log.hpp>
#include <any>
#include <array>
#include <cstddef>
Expand Down Expand Up @@ -74,7 +75,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
self->updateProperties(out_properties);
g_variant_unref(out_properties);
} else {
std::cerr << error->message << '\n';
LCM_LOG(error->message << '\n');
g_error_free(error);
}
self->template executeCallBack<PropertiesCallback>(counter,
Expand Down Expand Up @@ -214,7 +215,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {

const auto success = finish(G_DBUS_PROXY(proxy), res, &error);
if (!success) {
std::cerr << error->message << '\n';
LCM_LOG(error->message << '\n');
g_error_free(error);
}

Expand Down
50 changes: 50 additions & 0 deletions include/amarula/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <atomic>
#include <iostream>
#include <mutex>
#include <sstream>

namespace Amarula {

class Log {
public:
static void enable(bool enabled) {
state().store(enabled, std::memory_order_relaxed);
}

static auto isEnabled() -> bool {
return state().load(std::memory_order_relaxed);
}

static auto mutex() -> std::mutex& {
static std::mutex mutex;
return mutex;
}

private:
static auto state() -> std::atomic<bool>& {
#ifdef LCM_LOG_DEFAULT_ENABLED
static std::atomic<bool> flag{true};
#else
static std::atomic<bool> flag{false};
#endif
return flag;
}
};

} // namespace Amarula

#ifndef LCM_LOG_STREAM
#define LCM_LOG_STREAM std::cout
#endif

#define LCM_LOG(...) \
do { \
if (::Amarula::Log::isEnabled()) { \
std::ostringstream _lcm_oss; \
_lcm_oss << __VA_ARGS__; \
std::lock_guard<std::mutex> _lcm_guard(::Amarula::Log::mutex()); \
LCM_LOG_STREAM << _lcm_oss.str(); \
} \
} while (0)
3 changes: 2 additions & 1 deletion src/dbus/gconnman_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <glib.h>

#include <amarula/dbus/connman/gagent.hpp>
#include <amarula/log.hpp>
#include <iostream>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -114,7 +115,7 @@ void Agent::dispatch_method_call(GDBusMethodInvocation *invocation,
service = g_variant_get_string(child_service, nullptr);
error_str = g_variant_get_string(child_error, nullptr);

std::cerr << "ReportError:" << service << " " << error_str << "\n";
LCM_LOG("ReportError:" << service << " " << error_str << '\n');

g_variant_unref(child_service);
g_variant_unref(child_error);
Expand Down
34 changes: 18 additions & 16 deletions src/dbus/gconnman_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <amarula/dbus/connman/gclock.hpp>
#include <amarula/dbus/gdbus.hpp>
#include <amarula/dbus/gproxy.hpp>
#include <amarula/log.hpp>
#include <ctime>
#include <iomanip>
#include <iostream>
Expand Down Expand Up @@ -42,7 +43,7 @@ void ClockProperties::update(const gchar* key, GVariant* value) {
} else if (g_strcmp0(key, TIMESERVERSYNCED_STR) == 0U) {
time_server_synced_ = g_variant_get_boolean(value) == 1U;
} else {
std::cerr << "Unknown property for Clock: " << key << '\n';
LCM_LOG("Unknown property for Clock: " << key << '\n');
}
}

Expand Down Expand Up @@ -97,24 +98,25 @@ void Clock::setTimeServers(const std::vector<std::string>& servers,
}

void ClockProperties::print() const {
std::cout << "@@@@@@@@@@ ClockProperties: @@@@@@@@@@@@@@@\n";
std::cout << TIME_STR << ": " << time_ << " (";
LCM_LOG("@@@@@@@@@@ ClockProperties: @@@@@@@@@@@@@@@\n");
LCM_LOG(TIME_STR << ": " << time_ << " (");
const auto time_value = static_cast<std::time_t>(time_);
std::cout << std::put_time(std::localtime(&time_value), "%Y-%m-%d %H:%M:%S")
<< ")\n";
std::cout << TIMEUPDATES_STR << ": "
<< TIME_UPDATE_MAP.toString(time_updates_) << '\n';
std::cout << TIMEZONE_STR << ": " << timezone_ << '\n';
std::cout << TIMEZONEUPDATES_STR << ": "
<< TIME_ZONE_UPDATE_MAP.toString(timezone_updates_) << '\n';
std::cout << TIMESERVERSYNCED_STR << ": " << std::boolalpha
<< time_server_synced_ << '\n';
std::cout << TIMESERVERS_STR << ": ";
LCM_LOG(std::put_time(std::localtime(&time_value), "%Y-%m-%d %H:%M:%S")
<< ")\n");
LCM_LOG(TIMEUPDATES_STR << ": " << TIME_UPDATE_MAP.toString(time_updates_)
<< '\n');
LCM_LOG(TIMEZONE_STR << ": " << timezone_ << '\n');
LCM_LOG(TIMEZONEUPDATES_STR
<< ": " << TIME_ZONE_UPDATE_MAP.toString(timezone_updates_)
<< '\n');
LCM_LOG(TIMESERVERSYNCED_STR << ": " << std::boolalpha
<< time_server_synced_ << '\n');
LCM_LOG(TIMESERVERS_STR << ": ");
for (const auto& server : time_servers_) {
std::cout << server << ' ';
LCM_LOG(server << ' ');
}
std::cout << '\n';
std::cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
LCM_LOG('\n');
LCM_LOG("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}

} // namespace Amarula::DBus::G::Connman
5 changes: 3 additions & 2 deletions src/dbus/gconnman_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <amarula/dbus/connman/gtechnology.hpp>
#include <amarula/dbus/gdbus.hpp>
#include <amarula/dbus/gproxy.hpp>
#include <amarula/log.hpp>
#include <iostream>
#include <memory>
#include <mutex>
Expand All @@ -36,7 +37,7 @@ void ManaProperties::update(const gchar* key, GVariant* value) {
} else if (g_strcmp0(key, STATE_STR) == 0U) {
state_ = STATE_MAP.fromString(g_variant_get_string(value, nullptr));
} else {
std::cerr << "Unknown property for Manager: " << key << '\n';
LCM_LOG("Unknown property for Manager: " << key << '\n');
}
}

Expand Down Expand Up @@ -270,7 +271,7 @@ void Manager::get_proxies_cb(GObject* proxy, GAsyncResult* res,
}

} else {
std::cerr << error->message << '\n';
LCM_LOG(error->message << '\n');
g_error_free(error);
}
}
Expand Down
Loading