Skip to content

Commit 922c83c

Browse files
Add simple header-only logger with macro
Introduce log.hpp for enable or disable logging at runtime with the specif macro. Signed-off-by: Margherita Milani <margherita.milani@amarulasolutions.com>
1 parent 713eea5 commit 922c83c

11 files changed

Lines changed: 194 additions & 95 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ if(BUILD_CONNMAN)
7676
COMPONENT ${PROJECT_NAME}-dev)
7777
endif(BUILD_CONNMAN)
7878

79+
install(
80+
FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/amarula/log.hpp
81+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/amarula
82+
COMPONENT ${PROJECT_NAME}-dev)
83+
7984
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
8085
if(BUILD_TESTS)
8186
include(CTest)

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
add_compile_definitions(LCM_LOG_DEFAULT_ENABLED=1)
2+
13
if(BUILD_CONNMAN)
24
add_executable(connmanctl_dbus connmanctl.cpp)
35
target_link_libraries(connmanctl_dbus PRIVATE GConnmanDbus)

include/amarula/dbus/gproxy.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <glib.h>
66

77
#include <amarula/dbus/gdbus.hpp>
8+
#include <amarula/log.hpp>
89
#include <any>
910
#include <array>
1011
#include <cstddef>
@@ -74,7 +75,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
7475
self->updateProperties(out_properties);
7576
g_variant_unref(out_properties);
7677
} else {
77-
std::cerr << error->message << '\n';
78+
LCM_LOG(error->message << '\n');
7879
g_error_free(error);
7980
}
8081
self->template executeCallBack<PropertiesCallback>(counter,
@@ -214,7 +215,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
214215

215216
const auto success = finish(G_DBUS_PROXY(proxy), res, &error);
216217
if (!success) {
217-
std::cerr << error->message << '\n';
218+
LCM_LOG(error->message << '\n');
218219
g_error_free(error);
219220
}
220221

include/amarula/log.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <atomic>
4+
#include <iostream>
5+
#include <mutex>
6+
#include <sstream>
7+
8+
namespace Amarula {
9+
10+
class Log {
11+
public:
12+
static void enable(bool enabled) {
13+
state().store(enabled, std::memory_order_relaxed);
14+
}
15+
16+
static auto isEnabled() -> bool {
17+
return state().load(std::memory_order_relaxed);
18+
}
19+
20+
static auto mutex() -> std::mutex& {
21+
static std::mutex mutex;
22+
return mutex;
23+
}
24+
25+
private:
26+
static auto state() -> std::atomic<bool>& {
27+
#ifdef LCM_LOG_DEFAULT_ENABLED
28+
static std::atomic<bool> flag{true};
29+
#else
30+
static std::atomic<bool> flag{false};
31+
#endif
32+
return flag;
33+
}
34+
};
35+
36+
} // namespace Amarula
37+
38+
#ifndef LCM_LOG_STREAM
39+
#define LCM_LOG_STREAM std::cout
40+
#endif
41+
42+
#define LCM_LOG(...) \
43+
do { \
44+
if (::Amarula::Log::isEnabled()) { \
45+
std::ostringstream _lcm_oss; \
46+
_lcm_oss << __VA_ARGS__; \
47+
std::lock_guard<std::mutex> _lcm_guard(::Amarula::Log::mutex()); \
48+
LCM_LOG_STREAM << _lcm_oss.str(); \
49+
} \
50+
} while (0)

src/dbus/gconnman_agent.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <glib.h>
44

55
#include <amarula/dbus/connman/gagent.hpp>
6+
#include <amarula/log.hpp>
67
#include <iostream>
78
#include <stdexcept>
89
#include <string>
@@ -114,7 +115,7 @@ void Agent::dispatch_method_call(GDBusMethodInvocation *invocation,
114115
service = g_variant_get_string(child_service, nullptr);
115116
error_str = g_variant_get_string(child_error, nullptr);
116117

117-
std::cerr << "ReportError:" << service << " " << error_str << "\n";
118+
LCM_LOG("ReportError:" << service << " " << error_str << '\n');
118119

119120
g_variant_unref(child_service);
120121
g_variant_unref(child_error);

src/dbus/gconnman_clock.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <amarula/dbus/connman/gclock.hpp>
44
#include <amarula/dbus/gdbus.hpp>
55
#include <amarula/dbus/gproxy.hpp>
6+
#include <amarula/log.hpp>
67
#include <ctime>
78
#include <iomanip>
89
#include <iostream>
@@ -42,7 +43,7 @@ void ClockProperties::update(const gchar* key, GVariant* value) {
4243
} else if (g_strcmp0(key, TIMESERVERSYNCED_STR) == 0U) {
4344
time_server_synced_ = g_variant_get_boolean(value) == 1U;
4445
} else {
45-
std::cerr << "Unknown property for Clock: " << key << '\n';
46+
LCM_LOG("Unknown property for Clock: " << key << '\n');
4647
}
4748
}
4849

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

99100
void ClockProperties::print() const {
100-
std::cout << "@@@@@@@@@@ ClockProperties: @@@@@@@@@@@@@@@\n";
101-
std::cout << TIME_STR << ": " << time_ << " (";
101+
LCM_LOG("@@@@@@@@@@ ClockProperties: @@@@@@@@@@@@@@@\n");
102+
LCM_LOG(TIME_STR << ": " << time_ << " (");
102103
const auto time_value = static_cast<std::time_t>(time_);
103-
std::cout << std::put_time(std::localtime(&time_value), "%Y-%m-%d %H:%M:%S")
104-
<< ")\n";
105-
std::cout << TIMEUPDATES_STR << ": "
106-
<< TIME_UPDATE_MAP.toString(time_updates_) << '\n';
107-
std::cout << TIMEZONE_STR << ": " << timezone_ << '\n';
108-
std::cout << TIMEZONEUPDATES_STR << ": "
109-
<< TIME_ZONE_UPDATE_MAP.toString(timezone_updates_) << '\n';
110-
std::cout << TIMESERVERSYNCED_STR << ": " << std::boolalpha
111-
<< time_server_synced_ << '\n';
112-
std::cout << TIMESERVERS_STR << ": ";
104+
LCM_LOG(std::put_time(std::localtime(&time_value), "%Y-%m-%d %H:%M:%S")
105+
<< ")\n");
106+
LCM_LOG(TIMEUPDATES_STR << ": " << TIME_UPDATE_MAP.toString(time_updates_)
107+
<< '\n');
108+
LCM_LOG(TIMEZONE_STR << ": " << timezone_ << '\n');
109+
LCM_LOG(TIMEZONEUPDATES_STR
110+
<< ": " << TIME_ZONE_UPDATE_MAP.toString(timezone_updates_)
111+
<< '\n');
112+
LCM_LOG(TIMESERVERSYNCED_STR << ": " << std::boolalpha
113+
<< time_server_synced_ << '\n');
114+
LCM_LOG(TIMESERVERS_STR << ": ");
113115
for (const auto& server : time_servers_) {
114-
std::cout << server << ' ';
116+
LCM_LOG(server << ' ');
115117
}
116-
std::cout << '\n';
117-
std::cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
118+
LCM_LOG('\n');
119+
LCM_LOG("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
118120
}
119121

120122
} // namespace Amarula::DBus::G::Connman

src/dbus/gconnman_manager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <amarula/dbus/connman/gtechnology.hpp>
1212
#include <amarula/dbus/gdbus.hpp>
1313
#include <amarula/dbus/gproxy.hpp>
14+
#include <amarula/log.hpp>
1415
#include <iostream>
1516
#include <memory>
1617
#include <mutex>
@@ -36,7 +37,7 @@ void ManaProperties::update(const gchar* key, GVariant* value) {
3637
} else if (g_strcmp0(key, STATE_STR) == 0U) {
3738
state_ = STATE_MAP.fromString(g_variant_get_string(value, nullptr));
3839
} else {
39-
std::cerr << "Unknown property for Manager: " << key << '\n';
40+
LCM_LOG("Unknown property for Manager: " << key << '\n');
4041
}
4142
}
4243

@@ -270,7 +271,7 @@ void Manager::get_proxies_cb(GObject* proxy, GAsyncResult* res,
270271
}
271272

272273
} else {
273-
std::cerr << error->message << '\n';
274+
LCM_LOG(error->message << '\n');
274275
g_error_free(error);
275276
}
276277
}

0 commit comments

Comments
 (0)