Skip to content

Commit 4888eaf

Browse files
committed
[tidy] start to replace strerror()
1 parent f53d491 commit 4888eaf

File tree

12 files changed

+67
-50
lines changed

12 files changed

+67
-50
lines changed

src/base/auto_fd.cc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
* @file auto_fd.cc
3030
*/
3131

32+
#include <string>
33+
3234
#include "auto_fd.hh"
3335

3436
#include <fcntl.h>
3537
#include <unistd.h>
3638

39+
#include "fmt/format.h"
40+
#include "intern_string.hh"
3741
#include "lnav_log.hh"
42+
#include "result.h"
3843

3944
int
4045
auto_fd::pipe(auto_fd* af)
@@ -73,24 +78,18 @@ auto_fd::openpt(int flags)
7378
auto rc = posix_openpt(flags);
7479
if (rc == -1) {
7580
return Err(fmt::format(FMT_STRING("posix_openpt() failed: {}"),
76-
strerror(errno)));
81+
lnav::from_errno()));
7782
}
7883

7984
return Ok(auto_fd{rc});
8085
}
8186

82-
auto_fd::
83-
auto_fd(int fd)
84-
: af_fd(fd)
87+
auto_fd::auto_fd(int fd) : af_fd(fd)
8588
{
8689
require(fd >= -1);
8790
}
8891

89-
auto_fd::
90-
auto_fd(auto_fd&& af) noexcept
91-
: af_fd(af.release())
92-
{
93-
}
92+
auto_fd::auto_fd(auto_fd&& af) noexcept : af_fd(af.release()) {}
9493

9594
auto_fd
9695
auto_fd::dup() const
@@ -104,8 +103,7 @@ auto_fd::dup() const
104103
return auto_fd{new_fd};
105104
}
106105

107-
auto_fd::~
108-
auto_fd()
106+
auto_fd::~auto_fd()
109107
{
110108
this->reset();
111109
}
@@ -191,14 +189,13 @@ auto_pipe::for_child_fd(int child_fd)
191189
auto_pipe retval(child_fd);
192190

193191
if (retval.open() == -1) {
194-
return Err(std::string(strerror(errno)));
192+
return Err(lnav::from_errno().message());
195193
}
196194

197195
return Ok(std::move(retval));
198196
}
199197

200-
auto_pipe::
201-
auto_pipe(int child_fd, int child_flags)
198+
auto_pipe::auto_pipe(int child_fd, int child_flags)
202199
: ap_child_flags(child_flags), ap_child_fd(child_fd)
203200
{
204201
switch (child_fd) {

src/base/fs_util.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include <filesystem>
3131
#include <fstream>
32+
#include <optional>
33+
#include <string>
3234
#include <utility>
3335

3436
#include "fs_util.hh"
@@ -84,7 +86,8 @@ self_path()
8486

8587
auto rc = proc_pidpath(pid, pathbuf, sizeof(pathbuf));
8688
if (rc <= 0) {
87-
log_error("unable to determine self path: %s", strerror(errno));
89+
log_error("unable to determine self path: %s",
90+
lnav::from_errno().message().c_str());
8891
} else {
8992
log_info("self path: %s", pathbuf);
9093
return std::filesystem::path(pathbuf);
@@ -351,7 +354,7 @@ realpath(const std::filesystem::path& path)
351354
auto rc = ::realpath(path.c_str(), resolved);
352355

353356
if (rc == nullptr) {
354-
return Err(std::string(strerror(errno)));
357+
return Err(lnav::from_errno().message());
355358
}
356359

357360
return Ok(std::filesystem::path(resolved));
@@ -365,7 +368,7 @@ create_file(const std::filesystem::path& path, int flags, mode_t mode)
365368
if (fd == -1) {
366369
return Err(fmt::format(FMT_STRING("Failed to open: {} -- {}"),
367370
path.string(),
368-
strerror(errno)));
371+
lnav::from_errno()));
369372
}
370373

371374
return Ok(auto_fd(fd));
@@ -379,7 +382,7 @@ open_file(const std::filesystem::path& path, int flags)
379382
if (fd == -1) {
380383
return Err(fmt::format(FMT_STRING("Failed to open: {} -- {}"),
381384
path.string(),
382-
strerror(errno)));
385+
lnav::from_errno()));
383386
}
384387

385388
return Ok(auto_fd(fd));
@@ -406,7 +409,7 @@ open_temp_file(const std::filesystem::path& pattern)
406409
return Err(
407410
fmt::format(FMT_STRING("unable to create temporary file: {} -- {}"),
408411
pattern.string(),
409-
strerror(errno)));
412+
lnav::from_errno()));
410413
}
411414

412415
return Ok(std::make_pair(std::filesystem::path(pattern_copy), auto_fd(fd)));
@@ -419,7 +422,7 @@ read_file(const std::filesystem::path& path)
419422
std::ifstream file_stream(path);
420423

421424
if (!file_stream) {
422-
return Err(std::string(strerror(errno)));
425+
return Err(lnav::from_errno().message());
423426
}
424427

425428
std::string retval;
@@ -449,7 +452,7 @@ write_file(const std::filesystem::path& path,
449452
return Err(fmt::format(
450453
FMT_STRING("unable to write to temporary file {}: {}"),
451454
tmp_pair.first.string(),
452-
strerror(errno)));
455+
lnav::from_errno()));
453456
}
454457

455458
if (bytes_written != sf.length()) {
@@ -535,7 +538,7 @@ stat_file(const std::filesystem::path& path)
535538

536539
return Err(fmt::format(FMT_STRING("failed to find file: {} -- {}"),
537540
path.string(),
538-
strerror(errno)));
541+
lnav::from_errno()));
539542
}
540543

541544
file_lock::file_lock(const std::filesystem::path& archive_path)

src/base/intern_string.hh

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <ostream>
3737
#include <string>
3838
#include <string_view>
39+
#include <system_error>
3940
#include <tuple>
4041
#include <vector>
4142

@@ -818,10 +819,7 @@ public:
818819

819820
virtual ~string_fragment_producer() {}
820821

821-
virtual size_t estimated_size() const
822-
{
823-
return 1024;
824-
}
822+
virtual size_t estimated_size() const { return 1024; }
825823

826824
virtual next_result next() = 0;
827825

@@ -979,16 +977,25 @@ struct formatter<intern_string_t> : formatter<string_view> {
979977
template<typename FormatContext>
980978
auto format(const intern_string_t& is, FormatContext& ctx)
981979
{
982-
return formatter<string_view>::format(
983-
string_view{is.get(), (size_t) is.size()}, ctx);
980+
return formatter<string_view>::format(string_view{is.get(), is.size()},
981+
ctx);
982+
}
983+
};
984+
985+
template<>
986+
struct formatter<std::error_code> : formatter<string_view> {
987+
template<typename FormatContext>
988+
auto format(const std::error_code& ec, FormatContext& ctx)
989+
{
990+
return formatter<string_view>::format(ec.message(), ctx);
984991
}
985992
};
986993
} // namespace fmt
987994

988995
namespace std {
989996
template<>
990997
struct hash<const intern_string_t> {
991-
std::size_t operator()(const intern_string_t& ist) const
998+
std::size_t operator()(const intern_string_t& ist) const noexcept
992999
{
9931000
return ist.hash();
9941001
}
@@ -1035,7 +1042,7 @@ operator==(const string_fragment& left, const intern_string_t& right)
10351042
}
10361043

10371044
constexpr string_fragment
1038-
operator"" _frag(const char* str, std::size_t len)
1045+
operator""_frag(const char* str, std::size_t len)
10391046
{
10401047
return string_fragment{str, 0, (int) len};
10411048
}
@@ -1092,4 +1099,12 @@ struct intern_hasher {
10921099
}
10931100
};
10941101

1102+
namespace lnav {
1103+
inline std::error_code
1104+
from_errno()
1105+
{
1106+
return std::error_code{errno, std::generic_category()};
1107+
}
1108+
} // namespace lnav
1109+
10951110
#endif

src/base/lnav.console.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct user_message {
118118

119119
user_message& with_errno_reason()
120120
{
121-
this->um_reason = strerror(errno);
121+
this->um_reason = lnav::from_errno().message();
122122
return *this;
123123
}
124124

src/base/network.tcp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ connect(const char* hostname, const char* servname)
6565
return Err(fmt::format(FMT_STRING("unable to connect to {}:{} -- {}"),
6666
hostname,
6767
servname,
68-
strerror(errno)));
68+
lnav::from_errno()));
6969
}
7070

7171
return Ok(std::move(retval));

src/base/piper.file.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "base/injector.hh"
3636
#include "base/lnav_log.hh"
3737
#include "base/paths.hh"
38+
#include "intern_string.hh"
3839

3940
namespace lnav::piper {
4041

@@ -69,7 +70,8 @@ read_header(int fd, const char* first8)
6970
}
7071
auto meta_prc = pread(fd, meta_buf.in(), meta_size, 8);
7172
if (meta_prc != meta_size) {
72-
log_error("failed to read piper header: %s", strerror(errno));
73+
log_error("failed to read piper header: %s",
74+
lnav::from_errno().message().c_str());
7375
return std::nullopt;
7476
}
7577
meta_buf.resize(meta_size);

src/cmds.io.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ com_pipe_to(exec_context& ec,
18461846
== -1)
18471847
{
18481848
return ec.make_error("Unable to write to pipe -- {}",
1849-
strerror(errno));
1849+
lnav::from_errno());
18501850
}
18511851
log_perror(write(child_fds[0].write_end(), "\n", 1));
18521852
} else {
@@ -1856,7 +1856,7 @@ com_pipe_to(exec_context& ec,
18561856
== -1)
18571857
{
18581858
return ec.make_error("Unable to write to pipe -- {}",
1859-
strerror(errno));
1859+
lnav::from_errno());
18601860
}
18611861
log_perror(write(child_fds[0].write_end(), "\n", 1));
18621862
}
@@ -1870,7 +1870,7 @@ com_pipe_to(exec_context& ec,
18701870
== -1)
18711871
{
18721872
return ec.make_error("Unable to write to pipe -- {}",
1873-
strerror(errno));
1873+
lnav::from_errno());
18741874
}
18751875
log_perror(write(child_fds[0].write_end(), "\n", 1));
18761876
}

src/command_executor.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ execute_file(exec_context& ec, const std::string& path_and_args)
762762
extract_metadata_from_file(meta);
763763
paths_to_exec.push_back(meta);
764764
} else if (errno != ENOENT) {
765-
open_error = strerror(errno);
765+
open_error = lnav::from_errno().message();
766766
} else {
767767
auto script_path = std::filesystem::path(script_name);
768768

@@ -777,7 +777,7 @@ execute_file(exec_context& ec, const std::string& path_and_args)
777777
extract_metadata_from_file(meta);
778778
paths_to_exec.push_back(meta);
779779
} else if (errno != ENOENT) {
780-
open_error = strerror(errno);
780+
open_error = lnav::from_errno().message();
781781
}
782782
}
783783

src/file_collection.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,16 @@ file_collection::watch_logfile(const std::string& filename,
369369
}
370370
if (rc == -1) {
371371
if (required) {
372+
auto ec = lnav::from_errno();
372373
log_error("failed to open required file: %s -- %s",
373374
filename.c_str(),
374-
strerror(errno));
375+
ec.message().c_str());
375376
file_collection retval;
376-
retval.fc_name_to_errors->writeAccess()->emplace(
377-
filename,
378-
file_error_info{
379-
time(nullptr),
380-
std::string(strerror(errno)),
381-
});
377+
retval.fc_name_to_errors->writeAccess()->emplace(filename,
378+
file_error_info{
379+
time(nullptr),
380+
ec.message(),
381+
});
382382
return lnav::futures::make_ready_future(std::move(retval));
383383
}
384384
return std::nullopt;

src/file_format.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ detect_file_format(const std::filesystem::path& filename)
6969
filename.c_str(),
7070
strerror(errno));
7171
} else {
72-
static auto SQLITE3_HEADER = "SQLite format 3";
72+
static const auto* SQLITE3_HEADER = "SQLite format 3";
7373
auto header_frag = string_fragment::from_bytes(buffer, rc);
7474

7575
if (header_frag.startswith(SQLITE3_HEADER)) {

0 commit comments

Comments
 (0)