Skip to content

Commit 0ce3370

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 0ce3370

10 files changed

Lines changed: 190 additions & 94 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ if(BUILD_CONNMAN)
7474
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/amarula/dbus/connman/
7575
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/amarula/dbus/connman
7676
COMPONENT ${PROJECT_NAME}-dev)
77+
install(
78+
FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/amarula/log.hpp
79+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/amarula
80+
COMPONENT ${PROJECT_NAME}-dev)
7781
endif(BUILD_CONNMAN)
7882

7983
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)

include/amarula/dbus/gproxy.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <stdexcept>
1818
#include <string>
1919

20+
#include "amarula/log.hpp"
21+
2022
namespace Amarula::DBus::G {
2123

2224
template <class Properties>
@@ -74,7 +76,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
7476
self->updateProperties(out_properties);
7577
g_variant_unref(out_properties);
7678
} else {
77-
std::cerr << error->message << '\n';
79+
LCM_LOG(error->message);
7880
g_error_free(error);
7981
}
8082
self->template executeCallBack<PropertiesCallback>(counter,
@@ -214,7 +216,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
214216

215217
const auto success = finish(G_DBUS_PROXY(proxy), res, &error);
216218
if (!success) {
217-
std::cerr << error->message << '\n';
219+
LCM_LOG(error->message);
218220
g_error_free(error);
219221
}
220222

include/amarula/log.hpp

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

src/dbus/gconnman_agent.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdexcept>
88
#include <string>
99

10+
#include "amarula/log.hpp"
1011
#include "gconnman_private.hpp"
1112

1213
namespace Amarula::DBus::G::Connman {
@@ -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);
118119

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

src/dbus/gconnman_clock.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <utility>
1111
#include <vector>
1212

13+
#include "amarula/log.hpp"
1314
#include "gconnman_private.hpp"
1415
#include "gdbus_private.hpp"
1516

@@ -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);
4647
}
4748
}
4849

@@ -97,24 +98,23 @@ 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+
LCM_LOG(TIMEZONE_STR << ": " << timezone_);
108+
LCM_LOG(TIMEZONEUPDATES_STR
109+
<< ": " << TIME_ZONE_UPDATE_MAP.toString(timezone_updates_));
110+
LCM_LOG(TIMESERVERSYNCED_STR << ": " << std::boolalpha
111+
<< time_server_synced_);
112+
LCM_LOG(TIMESERVERS_STR << ": ");
113113
for (const auto& server : time_servers_) {
114-
std::cout << server << ' ';
114+
LCM_LOG(server << ' ');
115115
}
116-
std::cout << '\n';
117-
std::cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
116+
LCM_LOG('\n');
117+
LCM_LOG("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
118118
}
119119

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

src/dbus/gconnman_manager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <utility>
1919
#include <vector>
2020

21+
#include "amarula/log.hpp"
2122
#include "gconnman_private.hpp"
2223
#include "gdbus_private.hpp"
2324

@@ -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);
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);
274275
g_error_free(error);
275276
}
276277
}

0 commit comments

Comments
 (0)