1
0
mirror of https://github.com/phatina/simple-mtpfs synced 2024-12-26 08:32:40 +00:00

introduce StreamHelper

StreamHelper is a helper class, which temporarily redirects stdout and stderr output to /dev/null.
This commit is contained in:
Peter Hatina 2013-10-29 21:24:04 +01:00
parent 0e6e887788
commit c4b47228be
2 changed files with 52 additions and 0 deletions

View File

@ -36,6 +36,44 @@ extern "C" {
#endif // HAVE_LIBUSB1
#include "simple-mtpfs-util.h"
const std::string devnull = "/dev/null";
bool StreamHelper::s_enabled = false;
int StreamHelper::s_stdout = -1;
int StreamHelper::s_stderr = -1;
void StreamHelper::on()
{
if (!s_enabled)
return;
freopen(devnull.c_str(), "w", stdout);
freopen(devnull.c_str(), "w", stderr);
dup2(s_stdout, fileno(stdout));
dup2(s_stderr, fileno(stderr));
close(s_stdout);
close(s_stderr);
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
s_enabled = false;
}
void StreamHelper::off()
{
if (s_enabled)
return;
fflush(stdout);
fflush(stderr);
s_stdout = dup(fileno(stdout));
s_stderr = dup(fileno(stderr));
freopen(devnull.c_str(), "w", stdout);
freopen(devnull.c_str(), "w", stderr);
s_enabled = true;
}
#ifdef HAVE_LIBUSB1
const char smtpfs_path_delimiter = '/';
const std::string smtpfs_devbususb = "/dev/bus/usb/";

View File

@ -25,6 +25,20 @@
# include <libmtp.h>
#endif // HAVE_LIBUSB1
class StreamHelper
{
public:
StreamHelper() = delete;
static void on();
static void off();
private:
static bool s_enabled;
static int s_stdout;
static int s_stderr;
};
std::string smtpfs_dirname(const std::string &path);
std::string smtpfs_basename(const std::string &path);
std::string smtpfs_realpath(const std::string &path);