Skip to content

Commit 4ddd51c

Browse files
migrate to using the SAME Macro logger
1 parent 56486a1 commit 4ddd51c

105 files changed

Lines changed: 398 additions & 333 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/cfbox/error.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <cstdio>
23
#include <expected>
34
#include <string>
45
#include <string_view>
@@ -19,6 +20,13 @@ template <typename T> using Result = std::expected<T, Error>;
1920

2021
} // namespace cfbox::base
2122

23+
#ifndef CFBOX_ERR
24+
# define CFBOX_ERR(cmd, fmt, ...) \
25+
std::fprintf(stderr, "cfbox " cmd ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__)
26+
# define CFBOX_ERR_V(cmd, fmt, ...) \
27+
std::fprintf(stderr, "cfbox %s: " fmt "\n", cmd __VA_OPT__(,) __VA_ARGS__)
28+
#endif
29+
2230
#ifndef CFBOX_TRY
2331
# define CFBOX_TRY(var, expr) \
2432
auto var = (expr); \

include/cfbox/io.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct FileCloser {
1919
using unique_file = std::unique_ptr<std::FILE, FileCloser>;
2020

2121
[[nodiscard]] inline auto open_file(std::string_view path, const char* mode) -> base::Result<unique_file> {
22-
auto* f = std::fopen(std::string{path}.c_str(), mode);
22+
auto* f = std::fopen(path.data(), mode);
2323
if (!f) {
2424
return std::unexpected(base::Error{errno, "cannot open file: " + std::string{path}});
2525
}

include/cfbox/term.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ namespace detail {
6868
// Utility: wrap text with a color and reset
6969
inline auto colored(std::string_view text, std::string_view color_code) -> std::string {
7070
if (!color_enabled()) return std::string{text};
71-
return std::string{color_code} + std::string{text} + std::string{reset()};
71+
auto r = reset();
72+
std::string result;
73+
result.reserve(color_code.size() + text.size() + r.size());
74+
result.append(color_code);
75+
result.append(text);
76+
result.append(r);
77+
return result;
7278
}
7379

7480
} // namespace cfbox::term

src/applets/ar.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cfbox/args.hpp>
88
#include <cfbox/help.hpp>
99
#include <cfbox/io.hpp>
10+
#include <cfbox/error.hpp>
1011

1112
namespace {
1213
constexpr cfbox::help::HelpEntry HELP = {
@@ -37,7 +38,7 @@ auto ar_main(int argc, char* argv[]) -> int {
3738

3839
const auto& pos = parsed.positional();
3940
if (pos.empty()) {
40-
std::fprintf(stderr, "cfbox ar: missing archive name\n");
41+
CFBOX_ERR("ar", "missing archive name");
4142
return 1;
4243
}
4344

@@ -65,7 +66,7 @@ auto ar_main(int argc, char* argv[]) -> int {
6566
}
6667
auto wresult = cfbox::io::write_all(archive, output);
6768
if (!wresult) {
68-
std::fprintf(stderr, "cfbox ar: %s\n", wresult.error().msg.c_str());
69+
CFBOX_ERR("ar", "%s", wresult.error().msg.c_str());
6970
return 1;
7071
}
7172
return 0;
@@ -74,12 +75,12 @@ auto ar_main(int argc, char* argv[]) -> int {
7475
if (list || extract) {
7576
auto input = cfbox::io::read_all(archive);
7677
if (!input) {
77-
std::fprintf(stderr, "cfbox ar: %s\n", input.error().msg.c_str());
78+
CFBOX_ERR("ar", "%s", input.error().msg.c_str());
7879
return 1;
7980
}
8081
const auto& data = *input;
8182
if (data.size() < 8 || data.substr(0, 8) != "!<arch>\n") {
82-
std::fprintf(stderr, "cfbox ar: not a valid archive\n");
83+
CFBOX_ERR("ar", "not a valid archive");
8384
return 1;
8485
}
8586
std::size_t offset = 8;
@@ -95,7 +96,7 @@ auto ar_main(int argc, char* argv[]) -> int {
9596
} else if (extract) {
9697
auto content = data.substr(offset + 60, fsize);
9798
if (!cfbox::io::write_all(name, content)) {
98-
std::fprintf(stderr, "cfbox ar: write failed: %s\n", name.c_str());
99+
CFBOX_ERR("ar", "write failed: %s", name.c_str());
99100
return 1;
100101
}
101102
}
@@ -105,6 +106,6 @@ auto ar_main(int argc, char* argv[]) -> int {
105106
return 0;
106107
}
107108

108-
std::fprintf(stderr, "cfbox ar: must specify -r, -t, or -x\n");
109+
CFBOX_ERR("ar", "must specify -r, -t, or -x");
109110
return 1;
110111
}

src/applets/awk/awk_executor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sstream>
66

77
#include <cfbox/regex.hpp>
8+
#include <cfbox/error.hpp>
89

910
namespace cfbox::awk {
1011

@@ -450,7 +451,7 @@ class Executor {
450451

451452
auto process_file(const std::string& path, NodePtr prog) -> void {
452453
FILE* f = std::fopen(path.c_str(), "r");
453-
if (!f) { std::fprintf(stderr, "cfbox awk: cannot open '%s'\n", path.c_str()); return; }
454+
if (!f) { CFBOX_ERR("awk", "cannot open '%s'", path.c_str()); return; };
454455
st_.filename = path;
455456
char line[65536];
456457
while (std::fgets(line, sizeof(line), f)) {

src/applets/awk/awk_main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "awk.hpp"
66
#include <cfbox/args.hpp>
77
#include <cfbox/help.hpp>
8+
#include <cfbox/error.hpp>
89

910
namespace {
1011
constexpr cfbox::help::HelpEntry HELP = {
@@ -43,7 +44,7 @@ auto awk_main(int argc, char* argv[]) -> int {
4344

4445
const auto& pos = parsed.positional();
4546
if (pos.empty()) {
46-
std::fprintf(stderr, "cfbox awk: missing program\n");
47+
CFBOX_ERR("awk", "missing program");
4748
return 1;
4849
}
4950

src/applets/awk/awk_parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "awk.hpp"
22
#include <cstdlib>
3+
#include <cfbox/error.hpp>
34

45
namespace cfbox::awk {
56

@@ -30,8 +31,7 @@ class Parser {
3031
auto expect(TokType t) -> Token {
3132
auto tok = advance();
3233
if (tok.type != t) {
33-
std::fprintf(stderr, "cfbox awk: expected token type %d, got '%s'\n",
34-
static_cast<int>(t), tok.text.c_str());
34+
CFBOX_ERR("awk", "expected token type %d, got '%s'", static_cast<int>(t), tok.text.c_str());
3535
}
3636
return tok;
3737
}

src/applets/basename.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <cfbox/args.hpp>
66
#include <cfbox/help.hpp>
7+
#include <cfbox/error.hpp>
78

89
namespace {
910
constexpr cfbox::help::HelpEntry HELP = {
@@ -56,7 +57,7 @@ auto basename_main(int argc, char* argv[]) -> int {
5657

5758
const auto& pos = parsed.positional();
5859
if (pos.empty()) {
59-
std::fprintf(stderr, "cfbox basename: missing operand\n");
60+
CFBOX_ERR("basename", "missing operand");
6061
return 1;
6162
}
6263

src/applets/cat.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cfbox/args.hpp>
55
#include <cfbox/help.hpp>
66
#include <cfbox/io.hpp>
7+
#include <cfbox/error.hpp>
78

89
namespace {
910

@@ -68,7 +69,7 @@ auto cat_file(std::string_view path, bool n_flag, bool b_flag, bool A_flag) -> i
6869

6970
auto result = cfbox::io::open_file(path, "rb");
7071
if (!result) {
71-
std::fprintf(stderr, "cfbox cat: %s\n", result.error().msg.c_str());
72+
CFBOX_ERR("cat", "%s", result.error().msg.c_str());
7273
return 1;
7374
}
7475

src/applets/chgrp.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cfbox/args.hpp>
1010
#include <cfbox/help.hpp>
11+
#include <cfbox/error.hpp>
1112

1213
namespace {
1314

@@ -37,7 +38,7 @@ auto chgrp_main(int argc, char* argv[]) -> int {
3738
const auto& pos = parsed.positional();
3839

3940
if (pos.size() < 2) {
40-
std::fprintf(stderr, "cfbox chgrp: missing operand\n");
41+
CFBOX_ERR("chgrp", "missing operand");
4142
return 2;
4243
}
4344

@@ -52,7 +53,7 @@ auto chgrp_main(int argc, char* argv[]) -> int {
5253

5354
auto chgrp_one = [&](const std::string& path) -> int {
5455
if (::chown(path.c_str(), static_cast<uid_t>(-1), gid) != 0) {
55-
std::fprintf(stderr, "cfbox chgrp: %s: %s\n", path.c_str(), std::strerror(errno));
56+
CFBOX_ERR("chgrp", "%s: %s", path.c_str(), std::strerror(errno));
5657
return 1;
5758
}
5859
if (verbose) std::printf("group of '%s' changed\n", path.c_str());

0 commit comments

Comments
 (0)