#pragma once

namespace Halley {
	class RedirectStream : public std::streambuf {
	public:
		RedirectStream(std::ostream& source, bool exclusive);
		virtual ~RedirectStream();

	protected:
		std::streamsize xsputn(const char *msg, std::streamsize count);
		int sync() { return 0; }
		int overflow(int c);

		virtual void onText(const char *msg, std::streamsize count) { (void) msg; (void) count; }
		virtual void onFlush() {}

	private:
		bool exclusive;
		std::streambuf* orgbuf;
		std::ostream& orgstream;
		shared_ptr<std::ostream> newstream;
	};

	class RedirectStreamToStream : public RedirectStream {
	public:
		RedirectStreamToStream(std::ostream& source, shared_ptr<std::ostream> dst, bool exclusive);
		~RedirectStreamToStream();

	protected:
		void onText(const char *msg, std::streamsize count);
		void onFlush();

		shared_ptr<std::ostream> dst;
	};
}
