Skip to content

Commit

Permalink
Add log file option
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkunz committed May 5, 2023
1 parent f2949aa commit 214cdf4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ usage: ashuffle [-h] [-n] [-v] [[-e PATTERN ...] ...] [-o NUMBER]
Optional Arguments:
-h,-?,--help Display this help message.
--by-album Same as '--group-by album date'.
-e,--exclude Specify things to remove from shuffle (think
blacklist). A PATTERN should follow the exclude
flag.
Expand All @@ -186,13 +187,13 @@ Optional Arguments:
filename to retrive URI's from standard in. This
can be used to pipe song URI's from another program
into ashuffle.
--by-album Same as '--group-by album date'.
-g,--group-by Shuffle songs grouped by the given tags. For
example 'album' could be used as the tag, and an
entire album's worth of songs would be queued
instead of one song at a time.
--host Specify a hostname or IP address to connect to.
Defaults to `localhost`.
--log-file Path to write log output to. Defaults to stderr.
-n,--no-check When reading URIs from a file, don't check to
ensure that the URIs match the given exclude rules.
This option is most helpful when shuffling songs
Expand Down
12 changes: 11 additions & 1 deletion src/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ constexpr char kHelpMessage[] =
"\n"
"Optional Arguments:\n"
" -h,-?,--help Display this help message.\n"
" --by-album Same as '--group-by album date'.\n"
" -e,--exclude Specify things to remove from shuffle (think\n"
" blacklist). A PATTERN should follow the exclude\n"
" flag.\n"
Expand All @@ -39,13 +40,13 @@ constexpr char kHelpMessage[] =
" filename to retrive URI's from standard in. This\n"
" can be used to pipe song URI's from another program\n"
" into ashuffle.\n"
" --by-album Same as '--group-by album date'.\n"
" -g,--group-by Shuffle songs grouped by the given tags. For\n"
" example 'album' could be used as the tag, and an\n"
" entire album's worth of songs would be queued\n"
" instead of one song at a time.\n"
" --host Specify a hostname or IP address to connect to.\n"
" Defaults to `localhost`.\n"
" --log-file Path to write log output to. Defaults to stderr.\n"
" -n,--no-check When reading URIs from a file, don't check to\n"
" ensure that the URIs match the given exclude rules.\n"
" This option is most helpful when shuffling songs\n"
Expand Down Expand Up @@ -111,6 +112,7 @@ class Parser {
kGroup, // (generic) Expecting tag for group.
kGroupBegin, // Expecting first tag for group.
kHost, // Expecting hostname
kLogFile, // Expecting file path to log file.
kNone, // (generic) Default state, and initial state.
kPort, // Expecting port
kQueue, // Expecting --only value
Expand Down Expand Up @@ -413,6 +415,9 @@ std::variant<Parser::State, ParseError> Parser::ConsumeInternal(
if (arg == "--exclude-from") {
return kExcludeFile;
}
if (arg == "--log-file") {
return kLogFile;
}
}
switch (state_) {
case kExcludeFile:
Expand All @@ -434,6 +439,11 @@ std::variant<Parser::State, ParseError> Parser::ConsumeInternal(
case kHost:
opts_.host = arg;
return kNone;
case kLogFile: {
std::string filepath(arg);
opts_.InternalTakeLog(std::make_unique<std::ofstream>(filepath));
return kNone;
}
case kPort:
if (!absl::SimpleAtoi(arg, &opts_.port)) {
return ParseError(
Expand Down
10 changes: 10 additions & 0 deletions src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Options {
std::vector<Rule> ruleset;
unsigned queue_only = 0;
std::istream *file_in = nullptr;
std::ostream *log_file = nullptr;
bool check_uris = true;
unsigned queue_buffer = 0;
std::optional<std::string> host = {};
Expand Down Expand Up @@ -89,12 +90,21 @@ class Options {
owned_file_ = std::move(is);
};

// Same as InternalTakeIstream but for the log file ostream.
void InternalTakeLog(std::unique_ptr<std::ostream> &&os) {
log_file = os.get();
owned_log_file_ = std::move(os);
}

private:
// The owned_file is set if this Options class owns the file_in ptr.
// The file_in ptr is only *sometimes* owned. For example, the file_in ptr
// may point to std::cin, which has static lifetime, and is not owned by
// this object.
std::unique_ptr<std::istream> owned_file_;

// Same as above.
std::unique_ptr<std::ostream> owned_log_file_;
};

// Print the help message on the given output stream, and return the input
Expand Down
7 changes: 6 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ int main(int argc, const char* argv[]) {
exit(EXIT_FAILURE);
}

log::SetOutput(std::cerr);
if (options.log_file == nullptr) {
// By default, log to stderr.
log::SetOutput(std::cerr);
} else {
log::SetOutput(*options.log_file);
}

bool disable_reconnect = false;
std::function<std::string()> pass_f = [&disable_reconnect] {
Expand Down
4 changes: 4 additions & 0 deletions t/args_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mpd/tag.h>

#include "args.h"
#include "gmock/gmock.h"
#include "rule.h"

#include "t/helper.h"
Expand Down Expand Up @@ -40,6 +41,7 @@ TEST(ParseTest, Empty) {
EXPECT_TRUE(opts.ruleset.empty()) << "there should be no rules by default";
EXPECT_EQ(opts.queue_only, 0U);
EXPECT_THAT(opts.file_in, IsNull());
EXPECT_THAT(opts.log_file, IsNull());
EXPECT_TRUE(opts.check_uris);
EXPECT_EQ(opts.queue_buffer, 0U);
EXPECT_EQ(opts.host, std::nullopt);
Expand Down Expand Up @@ -96,6 +98,7 @@ TEST(ParseTest, Long) {
"--only", "5",
"--no-check",
"--file", "/dev/zero",
"--log-file", "/dev/null",
"--exclude", "artist", "test artist", "artist", "another one",
"--queue-buffer", "10",
"--host", "foo",
Expand All @@ -113,6 +116,7 @@ TEST(ParseTest, Long) {
EXPECT_EQ(opts.ruleset.size(), 1U);
EXPECT_EQ(opts.queue_only, 5U);
EXPECT_THAT(opts.file_in, NotNull());
EXPECT_THAT(opts.log_file, NotNull());
EXPECT_FALSE(opts.check_uris);
EXPECT_EQ(opts.queue_buffer, 10U);
EXPECT_EQ(opts.host, "foo");
Expand Down

0 comments on commit 214cdf4

Please sign in to comment.