Skip to content

Commit 67593b8

Browse files
authored
Merge pull request #332 from Organic-Code/enhancement/spdlog
Adding spdlog
2 parents f2edc8f + cff39bc commit 67593b8

21 files changed

+89
-69
lines changed

include/bar.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class Bar {
3838
bool vertical = false;
3939

4040
private:
41-
static inline const std::string MIN_HEIGHT_MSG =
41+
static constexpr const char* MIN_HEIGHT_MSG =
4242
"Requested height: {} exceeds the minimum height: {} required by the modules";
43-
static inline const std::string MIN_WIDTH_MSG =
43+
static constexpr const char* MIN_WIDTH_MSG =
4444
"Requested width: {} exceeds the minimum width: {} required by the modules";
45-
static inline const std::string BAR_SIZE_MSG =
45+
static constexpr const char* BAR_SIZE_MSG =
4646
"Bar configured (width: {}, height: {}) for output: {}";
47-
static inline const std::string SIZE_DEFINED =
47+
static constexpr const char* SIZE_DEFINED =
4848
"{} size is defined in the config file so it will stay like that";
4949
static void layerSurfaceHandleConfigure(void *, struct zwlr_layer_surface_v1 *, uint32_t,
5050
uint32_t, uint32_t);

include/modules/battery.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <sys/inotify.h>
1010
#include <algorithm>
1111
#include <fstream>
12-
#include <iostream>
12+
#include <string>
13+
#include <vector>
1314
#include "ALabel.hpp"
1415
#include "util/sleeper_thread.hpp"
1516

include/modules/cpu.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include <fmt/format.h>
44
#include <sys/sysinfo.h>
55
#include <fstream>
6-
#include <iostream>
6+
#include <cstdint>
77
#include <numeric>
88
#include <vector>
9+
#include <string>
10+
#include <utility>
911
#include "ALabel.hpp"
1012
#include "util/sleeper_thread.hpp"
1113

include/modules/custom.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <fmt/format.h>
44
#include <csignal>
5-
#include <iostream>
5+
#include <string>
66
#include "ALabel.hpp"
77
#include "util/command.hpp"
88
#include "util/json.hpp"

meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ add_global_link_arguments(cpp_link_args, language : 'cpp')
4848
thread_dep = dependency('threads')
4949
libinput = dependency('libinput')
5050
fmt = dependency('fmt', version : ['>=5.3.0'], fallback : ['fmt', 'fmt_dep'])
51+
spdlog = dependency('spdlog', version : ['>=1.3.1'], fallback : ['spdlog', 'spdlog_dep'])
5152
wayland_client = dependency('wayland-client')
5253
wayland_cursor = dependency('wayland-cursor')
5354
wayland_protos = dependency('wayland-protocols')
@@ -127,6 +128,7 @@ executable(
127128
client_protos,
128129
wayland_client,
129130
fmt,
131+
spdlog,
130132
sigcpp,
131133
jsoncpp,
132134
libinput,

src/bar.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "bar.hpp"
2+
#include <spdlog/spdlog.h>
23
#include "client.hpp"
34
#include "factory.hpp"
45

@@ -114,7 +115,7 @@ void waybar::Bar::setMarginsAndZone(uint32_t height, uint32_t width) {
114115
.left = std::stoi(margins[3], nullptr, 10)};
115116
}
116117
} catch (...) {
117-
std::cerr << "Invalid margins: " + config["margin"].asString() << std::endl;
118+
spdlog::warn("Invalid margins: {}", config["margin"].asString());
118119
}
119120
} else if (config["margin"].isInt()) {
120121
auto gaps = config["margin"].asInt();
@@ -132,21 +133,21 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
132133
if (ev->height > static_cast<int>(height_)) {
133134
// Default minimal value
134135
if (height_ != 1) {
135-
std::cout << fmt::format(MIN_HEIGHT_MSG, height_, ev->height) << std::endl;
136+
spdlog::warn(MIN_HEIGHT_MSG, height_, ev->height);
136137
}
137138
if (config["height"].isUInt()) {
138-
std::cout << fmt::format(SIZE_DEFINED, "Height") << std::endl;
139+
spdlog::info(SIZE_DEFINED, "Height");
139140
} else {
140141
tmp_height = ev->height;
141142
}
142143
}
143144
if (ev->width > static_cast<int>(width_)) {
144145
// Default minimal value
145146
if (width_ != 1) {
146-
std::cout << fmt::format(MIN_WIDTH_MSG, width_, ev->width) << std::endl;
147+
spdlog::warn(MIN_WIDTH_MSG, width_, ev->width);
147148
}
148149
if (config["width"].isUInt()) {
149-
std::cout << fmt::format(SIZE_DEFINED, "Width") << std::endl;
150+
spdlog::info(SIZE_DEFINED, "Width");
150151
} else {
151152
tmp_width = ev->width;
152153
}
@@ -227,11 +228,10 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf
227228
o->window.resize(o->width_, o->height_);
228229
auto zone = o->vertical ? width + o->margins_.right : height + o->margins_.bottom;
229230
zwlr_layer_surface_v1_set_exclusive_zone(o->layer_surface, zone);
230-
std::cout << fmt::format(BAR_SIZE_MSG,
231+
spdlog::info(BAR_SIZE_MSG,
231232
o->width_ == 1 ? "auto" : std::to_string(o->width_),
232233
o->height_ == 1 ? "auto" : std::to_string(o->height_),
233-
o->output->name)
234-
<< std::endl;
234+
o->output->name);
235235
wl_surface_commit(o->surface);
236236
}
237237
zwlr_layer_surface_v1_ack_configure(surface, serial);
@@ -277,12 +277,12 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos) {
277277
try {
278278
module->update();
279279
} catch (const std::exception& e) {
280-
std::cerr << name.asString() + ": " + e.what() << std::endl;
280+
spdlog::error("{}: {}", name.asString(), e.what());
281281
}
282282
});
283283
});
284284
} catch (const std::exception& e) {
285-
std::cerr << e.what() << std::endl;
285+
spdlog::warn("module {}: {}", name.asString(), e.what());
286286
}
287287
}
288288
}

src/client.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "client.hpp"
22
#include <fstream>
33
#include <iostream>
4+
#include <spdlog/spdlog.h>
45
#include "util/clara.hpp"
56
#include "util/json.hpp"
67

@@ -55,7 +56,7 @@ void waybar::Client::handleGlobalRemove(void * data, struct wl_registry * /*re
5556
auto output_name = (*it)->output->name;
5657
(*it)->window.close();
5758
it = client->bars.erase(it);
58-
std::cout << "Bar removed from output: " + output_name << std::endl;
59+
spdlog::info("Bar removed from output: {}", output_name);
5960
} else {
6061
++it;
6162
}
@@ -191,7 +192,7 @@ void waybar::Client::setupConfigs(const std::string &config, const std::string &
191192
if (css_file_.empty() || config_file_.empty()) {
192193
throw std::runtime_error("Missing required resources files");
193194
}
194-
std::cout << "Resources files: " + config_file_ + ", " + css_file_ << std::endl;
195+
spdlog::info("Resources files: {}, {}", config_file_, css_file_);
195196
}
196197

197198
auto waybar::Client::setupConfig() -> void {
@@ -249,7 +250,7 @@ int waybar::Client::main(int argc, char *argv[]) {
249250
clara::detail::Opt(bar_id, "id")["-b"]["--bar"]("Bar id");
250251
auto res = cli.parse(clara::detail::Args(argc, argv));
251252
if (!res) {
252-
std::cerr << "Error in command line: " << res.errorMessage() << std::endl;
253+
spdlog::error("Error in command line: {}", res.errorMessage());
253254
return 1;
254255
}
255256
if (show_help) {

src/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <csignal>
2-
#include <iostream>
2+
#include <spdlog/spdlog.h>
33
#include "client.hpp"
44

55
int main(int argc, char* argv[]) {
@@ -23,10 +23,10 @@ int main(int argc, char* argv[]) {
2323
delete client;
2424
return ret;
2525
} catch (const std::exception& e) {
26-
std::cerr << e.what() << std::endl;
26+
spdlog::error("{}", e.what());
2727
return 1;
2828
} catch (const Glib::Exception& e) {
29-
std::cerr << e.what().c_str() << std::endl;
29+
spdlog::error("{}", static_cast<std::string>(e.what()));
3030
return 1;
3131
}
3232
}

src/modules/battery.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "modules/battery.hpp"
2+
#include <spdlog/spdlog.h>
23

34
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config)
45
: ALabel(config, "{capacity}%", 60) {
@@ -103,7 +104,7 @@ const std::tuple<uint8_t, uint32_t, std::string> waybar::modules::Battery::getIn
103104
uint16_t capacity = total / batteries_.size();
104105
return {capacity, total_current, status};
105106
} catch (const std::exception& e) {
106-
std::cerr << e.what() << std::endl;
107+
spdlog::error("Battery: {}", e.what());
107108
return {0, 0, "Unknown"};
108109
}
109110
}

src/modules/cpu.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "modules/cpu.hpp"
2+
#include <numeric>
23

34
waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config)
45
: ALabel(config, "{usage}%", 10) {

src/modules/custom.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "modules/custom.hpp"
2+
#include <spdlog/spdlog.h>
23

34
waybar::modules::Custom::Custom(const std::string& name, const Json::Value& config)
45
: ALabel(config, "{}"), name_(name), fp_(nullptr), pid_(-1) {
@@ -58,7 +59,7 @@ void waybar::modules::Custom::continuousWorker() {
5859
if (exit_code != 0) {
5960
output_ = {exit_code, ""};
6061
dp.emit();
61-
std::cerr << name_ + " just stopped unexpectedly, is it endless?" << std::endl;
62+
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
6263
}
6364
return;
6465
}

src/modules/mpd.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "modules/mpd.hpp"
22

33
#include <fmt/chrono.h>
4-
#include <iostream>
4+
#include <spdlog/spdlog.h>
55

66
waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
77
: ALabel(config, "{album} - {artist} - {title}", 5),
@@ -14,11 +14,11 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
1414
status_(nullptr, &mpd_status_free),
1515
song_(nullptr, &mpd_song_free) {
1616
if (!config_["port"].isNull() && !config_["port"].isUInt()) {
17-
std::cerr << module_name_ << ": `port` configuration should be an unsigned int" << std::endl;
17+
spdlog::warn("{}: `port` configuration should be an unsigned int", module_name_);
1818
}
1919
2020
if (!config_["timeout"].isNull() && !config_["timeout"].isUInt()) {
21-
std::cerr << module_name_ << ": `timeout` configuration should be an unsigned int" << std::endl;
21+
spdlog::warn("{}: `timeout` configuration should be an unsigned int", module_name_);
2222
}
2323
2424
label_.set_name("mpd");
@@ -28,7 +28,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value& config)
2828
2929
if (!config["server"].isNull()) {
3030
if (!config_["server"].isString()) {
31-
std::cerr << module_name_ << "`server` configuration should be a string" << std::endl;
31+
spdlog::warn("{}:`server` configuration should be a string", module_name_);
3232
}
3333
server_ = config["server"].asCString();
3434
}
@@ -51,7 +51,7 @@ auto waybar::modules::MPD::update() -> void {
5151
periodic_updater().detach();
5252
}
5353
} catch (const std::exception& e) {
54-
std::cerr << module_name_ + ": " + e.what() << std::endl;
54+
spdlog::error("{}: {}", module_name_, e.what());
5555
state_ = MPD_STATE_UNKNOWN;
5656
}
5757
}
@@ -72,7 +72,7 @@ std::thread waybar::modules::MPD::event_listener() {
7272
dp.emit();
7373
}
7474
} catch (const std::exception& e) {
75-
std::cerr << module_name_ + ": " + e.what() << std::endl;
75+
spdlog::warn("{}: {}", module_name_, e.what());
7676
}
7777
}
7878
});
@@ -206,12 +206,12 @@ std::string waybar::modules::MPD::getStateIcon() {
206206
}
207207
208208
if (connection_ == nullptr) {
209-
std::cerr << module_name_ << ": Trying to fetch state icon while disconnected" << std::endl;
209+
spdlog::warn("{}: Trying to fetch state icon while disconnected", module_name_);
210210
return "";
211211
}
212212
213213
if (stopped()) {
214-
std::cerr << module_name_ << ": Trying to fetch state icon while stopped" << std::endl;
214+
spdlog::warn("{}: Trying to fetch state icon while stopped", module_name_);
215215
return "";
216216
}
217217
@@ -228,7 +228,7 @@ std::string waybar::modules::MPD::getOptionIcon(std::string optionName, bool act
228228
}
229229
230230
if (connection_ == nullptr) {
231-
std::cerr << module_name_ << ": Trying to fetch option icon while disconnected" << std::endl;
231+
spdlog::warn("{}: Trying to fetch option icon while disconnected", module_name_);
232232
return "";
233233
}
234234
@@ -251,17 +251,17 @@ void waybar::modules::MPD::tryConnect() {
251251
unique_connection(mpd_connection_new(server_, port_, timeout_), &mpd_connection_free);
252252
253253
if (connection_ == nullptr || alternate_connection_ == nullptr) {
254-
std::cerr << module_name_ << ": Failed to connect to MPD" << std::endl;
254+
spdlog::error("{}: Failed to connect to MPD", module_name_);
255255
connection_.reset();
256256
alternate_connection_.reset();
257257
return;
258258
}
259259
260260
try {
261261
checkErrors(connection_.get());
262-
std::cerr << module_name_ << ": Connected to MPD" << std::endl;
262+
spdlog::info("{}: Connected to MPD", module_name_);
263263
} catch (std::runtime_error& e) {
264-
std::cerr << module_name_ << ": Failed to connect to MPD: " << e.what() << std::endl;
264+
spdlog::error("{}: Failed to connect to MPD: {}", module_name_, e.what());
265265
connection_.reset();
266266
alternate_connection_.reset();
267267
}

src/modules/network.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "modules/network.hpp"
2+
#include <spdlog/spdlog.h>
23
#include <sys/eventfd.h>
34
#include <fstream>
4-
#include <iostream>
55

66
namespace {
77

@@ -13,7 +13,7 @@ namespace {
1313
std::ifstream netstat(NETSTAT_FILE);
1414
std::optional<unsigned long long> read_netstat(std::string_view category, std::string_view key) {
1515
if (!netstat) {
16-
std::cerr << "Failed to open netstat file " << NETSTAT_FILE << '\n' << std::flush;
16+
spdlog::warn("Failed to open netstat file {}", NETSTAT_FILE);
1717
return {};
1818
}
1919
netstat.seekg(std::ios_base::beg);
@@ -28,7 +28,7 @@ namespace {
2828
std::string read;
2929
while (std::getline(netstat, read) && !starts_with(read, category));
3030
if (!starts_with(read, category)) {
31-
std::cerr << "Category '" << category << "' not found in netstat file " << NETSTAT_FILE << '\n' << std::flush;
31+
spdlog::warn("Category '{}' not found in netstat file {}", category, NETSTAT_FILE);
3232
return {};
3333
}
3434

@@ -52,7 +52,7 @@ namespace {
5252
}
5353

5454
if (r_it == read.end() && k_it != key.end()) {
55-
std::cerr << "Key '" << key << "' not found in category '" << category << "' of netstat file " << NETSTAT_FILE << '\n' << std::flush;
55+
spdlog::warn("Key '{}' not found in category '{}' of netstat file {}", key, category, NETSTAT_FILE);
5656
return {};
5757
}
5858

0 commit comments

Comments
 (0)