Skip to content

Commit cccc720

Browse files
committed
Replace direct cout/cerr with shared logging helpers across examples and backends
1 parent 08817de commit cccc720

6 files changed

Lines changed: 86 additions & 71 deletions

File tree

examples/layout_2x2_example.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "gnuplotpp/gnuplot_backend.hpp"
2+
#include "gnuplotpp/logging.hpp"
23
#include "gnuplotpp/plot.hpp"
34
#include "gnuplotpp/presets.hpp"
45
#include <filesystem>
5-
#include <iostream>
66
#include <string>
77
#include <vector>
88

@@ -65,14 +65,14 @@ int main(int argc, char** argv) {
6565
const auto result = fig.save(out_dir / "figures");
6666

6767
if (!result.ok) {
68-
std::cerr << "plot render incomplete: " << result.message << "\n";
69-
std::cerr << "install gnuplot and rerun this example\n";
70-
std::cerr << "script: " << result.script_path << "\n";
68+
gnuplotpp::log::Error("plot render incomplete: ", result.message);
69+
gnuplotpp::log::Error("install gnuplot and rerun this example");
70+
gnuplotpp::log::Error("script: ", result.script_path.string());
7171
return 1;
7272
}
7373

7474
for (const auto& output : result.outputs) {
75-
std::cout << "output: " << output << "\n";
75+
gnuplotpp::log::Info("output: ", output.string());
7676
}
7777

7878
return 0;

examples/three_line_ieee_example.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "gnuplotpp/gnuplot_backend.hpp"
2+
#include "gnuplotpp/logging.hpp"
23
#include "gnuplotpp/plot.hpp"
34
#include "gnuplotpp/presets.hpp"
45

56
#include <filesystem>
6-
#include <iostream>
77
#include <string>
88
#include <vector>
99

@@ -57,13 +57,13 @@ int main(int argc, char** argv) {
5757
fig.set_backend(make_gnuplot_backend());
5858
const auto result = fig.save(out_dir / "figures");
5959
if (!result.ok) {
60-
std::cerr << "plot render incomplete: " << result.message << "\n";
61-
std::cerr << "script: " << result.script_path << "\n";
60+
gnuplotpp::log::Error("plot render incomplete: ", result.message);
61+
gnuplotpp::log::Error("script: ", result.script_path.string());
6262
return 1;
6363
}
6464

6565
for (const auto& output : result.outputs) {
66-
std::cout << "output: " << output << "\n";
66+
gnuplotpp::log::Info("output: ", output.string());
6767
}
6868

6969
return 0;

examples/two_window_example.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "gnuplotpp/gnuplot_backend.hpp"
2+
#include "gnuplotpp/logging.hpp"
23
#include "gnuplotpp/plot.hpp"
34
#include "gnuplotpp/presets.hpp"
45
#include <filesystem>
5-
#include <iostream>
66
#include <string>
77
#include <vector>
88

@@ -57,14 +57,14 @@ int main(int argc, char** argv) {
5757
const auto result = fig.save(out_dir / "figures");
5858

5959
if (!result.ok) {
60-
std::cerr << "plot render incomplete: " << result.message << "\n";
61-
std::cerr << "install gnuplot and rerun this example\n";
62-
std::cerr << "script: " << result.script_path << "\n";
60+
gnuplotpp::log::Error("plot render incomplete: ", result.message);
61+
gnuplotpp::log::Error("install gnuplot and rerun this example");
62+
gnuplotpp::log::Error("script: ", result.script_path.string());
6363
return 1;
6464
}
6565

6666
for (const auto& output : result.outputs) {
67-
std::cout << "output: " << output << "\n";
67+
gnuplotpp::log::Info("output: ", output.string());
6868
}
6969

7070
return 0;

include/gnuplotpp/logging.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @file logging.hpp
3+
* @brief Logging helpers for gnuplotpp examples and runtime diagnostics.
4+
*/
5+
#pragma once
6+
7+
#include <iostream>
8+
#include <sstream>
9+
#include <string>
10+
#include <utility>
11+
12+
#ifdef GNUPLOTPP_HAS_SPDLOG
13+
#include <spdlog/spdlog.h>
14+
#endif
15+
16+
namespace gnuplotpp::log {
17+
18+
template <typename... Args>
19+
inline std::string BuildMessage(Args&&... args) {
20+
std::ostringstream oss;
21+
(oss << ... << std::forward<Args>(args));
22+
return oss.str();
23+
}
24+
25+
template <typename... Args>
26+
inline void Info(Args&&... args) {
27+
const auto msg = BuildMessage(std::forward<Args>(args)...);
28+
#ifdef GNUPLOTPP_HAS_SPDLOG
29+
spdlog::info("{}", msg);
30+
#else
31+
std::clog << "[gnuplotpp] info: " << msg << "\n";
32+
#endif
33+
}
34+
35+
template <typename... Args>
36+
inline void Warn(Args&&... args) {
37+
const auto msg = BuildMessage(std::forward<Args>(args)...);
38+
#ifdef GNUPLOTPP_HAS_SPDLOG
39+
spdlog::warn("{}", msg);
40+
#else
41+
std::clog << "[gnuplotpp] warn: " << msg << "\n";
42+
#endif
43+
}
44+
45+
template <typename... Args>
46+
inline void Error(Args&&... args) {
47+
const auto msg = BuildMessage(std::forward<Args>(args)...);
48+
#ifdef GNUPLOTPP_HAS_SPDLOG
49+
spdlog::error("{}", msg);
50+
#else
51+
std::clog << "[gnuplotpp] error: " << msg << "\n";
52+
#endif
53+
}
54+
55+
} // namespace gnuplotpp::log

src/gnuplot_backend.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "gnuplotpp/gnuplot_backend.hpp"
2+
#include "gnuplotpp/logging.hpp"
23

34
#include <algorithm>
45
#include <cstdlib>
56
#include <filesystem>
67
#include <fstream>
78
#include <iomanip>
8-
#include <iostream>
99
#include <sstream>
1010
#include <string>
1111
#include <utility>
@@ -14,10 +14,6 @@
1414
#include <fmt/format.h>
1515
#endif
1616

17-
#ifdef GNUPLOTPP_HAS_SPDLOG
18-
#include <spdlog/spdlog.h>
19-
#endif
20-
2117
namespace gnuplotpp {
2218
namespace {
2319

@@ -58,22 +54,6 @@ std::string msg_text(const std::string& prefix, const std::string& detail) {
5854
#endif
5955
}
6056

61-
void log_info(const std::string& msg) {
62-
#ifdef GNUPLOTPP_HAS_SPDLOG
63-
spdlog::info("{}", msg);
64-
#else
65-
std::clog << "[gnuplotpp] info: " << msg << "\n";
66-
#endif
67-
}
68-
69-
void log_error(const std::string& msg) {
70-
#ifdef GNUPLOTPP_HAS_SPDLOG
71-
spdlog::error("{}", msg);
72-
#else
73-
std::cerr << "[gnuplotpp] error: " << msg << "\n";
74-
#endif
75-
}
76-
7757
std::string extension_for(OutputFormat format) {
7858
switch (format) {
7959
case OutputFormat::Pdf:
@@ -287,7 +267,7 @@ RenderResult GnuplotBackend::render(const Figure& fig,
287267
result.ok = false;
288268
result.status = RenderStatus::IoError;
289269
result.message = msg_io("failed to create output directories", out_dir / "tmp", ec);
290-
log_error(result.message);
270+
gnuplotpp::log::Error(result.message);
291271
return result;
292272
}
293273

@@ -310,7 +290,7 @@ RenderResult GnuplotBackend::render(const Figure& fig,
310290
result.ok = false;
311291
result.status = RenderStatus::IoError;
312292
result.message = msg_text("failed to open data file for writing", data_path.string());
313-
log_error(result.message);
293+
gnuplotpp::log::Error(result.message);
314294
return result;
315295
}
316296
data_os << std::scientific << std::setprecision(16);
@@ -321,7 +301,7 @@ RenderResult GnuplotBackend::render(const Figure& fig,
321301
result.ok = false;
322302
result.status = RenderStatus::IoError;
323303
result.message = msg_text("failed while writing data file", data_path.string());
324-
log_error(result.message);
304+
gnuplotpp::log::Error(result.message);
325305
return result;
326306
}
327307
}
@@ -335,7 +315,7 @@ RenderResult GnuplotBackend::render(const Figure& fig,
335315
result.ok = false;
336316
result.status = RenderStatus::IoError;
337317
result.message = msg_text("failed to open gnuplot script for writing", script_path.string());
338-
log_error(result.message);
318+
gnuplotpp::log::Error(result.message);
339319
return result;
340320
}
341321
script_os << "set encoding utf8\n";
@@ -353,15 +333,15 @@ RenderResult GnuplotBackend::render(const Figure& fig,
353333
result.ok = false;
354334
result.status = RenderStatus::IoError;
355335
result.message = msg_text("failed while writing gnuplot script", script_path.string());
356-
log_error(result.message);
336+
gnuplotpp::log::Error(result.message);
357337
return result;
358338
}
359339
script_os.close();
360340
if (!script_os) {
361341
result.ok = false;
362342
result.status = RenderStatus::IoError;
363343
result.message = msg_text("failed to finalize gnuplot script", script_path.string());
364-
log_error(result.message);
344+
gnuplotpp::log::Error(result.message);
365345
return result;
366346
}
367347

@@ -371,7 +351,7 @@ RenderResult GnuplotBackend::render(const Figure& fig,
371351
result.status = RenderStatus::ExternalToolMissing;
372352
result.message = msg_text("gnuplot executable not found; generated script/data only",
373353
executable_);
374-
log_error(result.message);
354+
gnuplotpp::log::Error(result.message);
375355
return result;
376356
}
377357

@@ -387,7 +367,7 @@ RenderResult GnuplotBackend::render(const Figure& fig,
387367
#else
388368
result.message = "gnuplot failed; inspect gnuplot.log in output tmp directory";
389369
#endif
390-
log_error(result.message);
370+
gnuplotpp::log::Error(result.message);
391371
return result;
392372
}
393373

@@ -398,15 +378,15 @@ RenderResult GnuplotBackend::render(const Figure& fig,
398378
result.ok = false;
399379
result.status = RenderStatus::ExternalToolFailure;
400380
result.message = msg_text("gnuplot completed but output is missing/empty", out.string());
401-
log_error(result.message);
381+
gnuplotpp::log::Error(result.message);
402382
return result;
403383
}
404384
}
405385

406386
result.ok = true;
407387
result.status = RenderStatus::Success;
408388
result.message = "render completed";
409-
log_info(result.message);
389+
gnuplotpp::log::Info(result.message);
410390
return result;
411391
}
412392

src/svg_backend.cpp

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "gnuplotpp/svg_backend.hpp"
2+
#include "gnuplotpp/logging.hpp"
23

34
#include <algorithm>
45
#include <array>
56
#include <cmath>
67
#include <filesystem>
78
#include <fstream>
89
#include <iomanip>
9-
#include <iostream>
1010
#include <limits>
1111
#include <sstream>
1212
#include <string>
@@ -16,10 +16,6 @@
1616
#include <fmt/format.h>
1717
#endif
1818

19-
#ifdef GNUPLOTPP_HAS_SPDLOG
20-
#include <spdlog/spdlog.h>
21-
#endif
22-
2319
namespace gnuplotpp {
2420
namespace {
2521

@@ -128,22 +124,6 @@ std::string msg_text(const std::string& prefix, const std::string& detail) {
128124
#endif
129125
}
130126

131-
void log_info(const std::string& msg) {
132-
#ifdef GNUPLOTPP_HAS_SPDLOG
133-
spdlog::info("{}", msg);
134-
#else
135-
std::clog << "[gnuplotpp] info: " << msg << "\n";
136-
#endif
137-
}
138-
139-
void log_error(const std::string& msg) {
140-
#ifdef GNUPLOTPP_HAS_SPDLOG
141-
spdlog::error("{}", msg);
142-
#else
143-
std::cerr << "[gnuplotpp] error: " << msg << "\n";
144-
#endif
145-
}
146-
147127
} // namespace
148128

149129
RenderResult SvgBackend::render(const Figure& fig,
@@ -156,7 +136,7 @@ RenderResult SvgBackend::render(const Figure& fig,
156136
result.ok = false;
157137
result.status = RenderStatus::IoError;
158138
result.message = msg_text("failed to create output directory", ec.message());
159-
log_error(result.message);
139+
gnuplotpp::log::Error(result.message);
160140
return result;
161141
}
162142

@@ -172,7 +152,7 @@ RenderResult SvgBackend::render(const Figure& fig,
172152
result.ok = false;
173153
result.status = RenderStatus::UnsupportedFormat;
174154
result.message = "SvgBackend only supports OutputFormat::Svg";
175-
log_error(result.message);
155+
gnuplotpp::log::Error(result.message);
176156
return result;
177157
}
178158

@@ -279,23 +259,23 @@ RenderResult SvgBackend::render(const Figure& fig,
279259
result.ok = false;
280260
result.status = RenderStatus::IoError;
281261
result.message = msg_text("failed to open svg output for writing", out_path.string());
282-
log_error(result.message);
262+
gnuplotpp::log::Error(result.message);
283263
return result;
284264
}
285265
out << svg.str();
286266
if (!out.good()) {
287267
result.ok = false;
288268
result.status = RenderStatus::IoError;
289269
result.message = msg_text("failed while writing svg output", out_path.string());
290-
log_error(result.message);
270+
gnuplotpp::log::Error(result.message);
291271
return result;
292272
}
293273

294274
result.ok = true;
295275
result.status = RenderStatus::Success;
296276
result.message = "native svg render completed";
297277
result.outputs.push_back(out_path);
298-
log_info(result.message);
278+
gnuplotpp::log::Info(result.message);
299279
return result;
300280
}
301281

0 commit comments

Comments
 (0)