Skip to content

Commit

Permalink
Add command-line option to disable logs (closes #48) (#82)
Browse files Browse the repository at this point in the history
* Add command-line option to disable logs (closes #48)

Set the logger to /dev/null when run with -s or --silent.
  • Loading branch information
DrDub authored Dec 21, 2023
1 parent 80ee126 commit 0a35b06
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions man/btfs.1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ download metadata only
\fB\-k\fR \fB\-\-keep\fR
keep files after unmount
.TP
\fB\-s\fR \fB\-\-silent\fR
do not create logs
.TP
\fB\-\-utp\-only\fR
do not use TCP
.TP
Expand Down
5 changes: 4 additions & 1 deletion src/btfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ btfs_init(struct fuse_conn_info *conn) {
#endif

pthread_create(&alert_thread, NULL, alert_queue_loop,
new Log(p->save_path + "/../log.txt"));
new Log(params.silent ? std::string() : (p->save_path + "/../log.txt")));

#ifdef HAVE_PTHREAD_SETNAME_NP
pthread_setname_np(alert_thread, "alert");
Expand Down Expand Up @@ -927,6 +927,8 @@ static const struct fuse_opt btfs_opts[] = {
BTFS_OPT("--browse-only", browse_only, 1),
BTFS_OPT("-k", keep, 1),
BTFS_OPT("--keep", keep, 1),
BTFS_OPT("-s", silent, 1),
BTFS_OPT("--silent", silent, 1),
BTFS_OPT("--utp-only", utp_only, 1),
BTFS_OPT("--data-directory=%s", data_directory, 4),
BTFS_OPT("--min-port=%lu", min_port, 4),
Expand Down Expand Up @@ -964,6 +966,7 @@ print_help() {
printf(" --help-fuse print all fuse options\n");
printf(" --browse-only -b download metadata only\n");
printf(" --keep -k keep files after unmount\n");
printf(" --silent -s do not create logs\n");
printf(" --utp-only do not use TCP\n");
printf(" --data-directory=dir directory in which to put btfs data\n");
printf(" --min-port=N start of listen port range\n");
Expand Down
5 changes: 3 additions & 2 deletions src/btfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Array
class Log : public std::ofstream
{
public:
Log(std::string p) : std::ofstream(p.c_str()), path(p) {
Log(std::string p) : std::ofstream(p.empty() ? "/dev/null" : p.c_str()), path(p) {
if (!is_open())
// If open log file fails, write to a dummy file
open("/dev/null");
Expand All @@ -109,7 +109,7 @@ class Log : public std::ofstream
~Log() {
close();

if (remove(path.c_str()))
if (!path.empty() && remove(path.c_str()))
perror("Failed to remove log");
}

Expand All @@ -123,6 +123,7 @@ struct btfs_params {
int help_fuse;
int browse_only;
int keep;
int silent;
int utp_only;
char *data_directory;
int min_port;
Expand Down

0 comments on commit 0a35b06

Please sign in to comment.