turn off colored logging if output is redirected to file

This commit is contained in:
Dennis Luxen 2014-04-01 17:05:19 +02:00
parent 9ccc8a7404
commit 8ae467985f

View File

@ -32,6 +32,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#include <unistd.h>
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>
@ -70,16 +72,20 @@ private:
bool m_is_mute; bool m_is_mute;
}; };
class SimpleLogger { class SimpleLogger
{
public: public:
SimpleLogger() : level(logINFO) { } SimpleLogger() : level(logINFO) { }
std::ostringstream& Write(LogLevel l = logINFO) { std::ostringstream& Write(LogLevel l = logINFO)
try { {
try
{
boost::mutex::scoped_lock lock(logger_mutex); boost::mutex::scoped_lock lock(logger_mutex);
level = l; level = l;
os << "["; os << "[";
switch(level) { switch(level)
{
case logINFO: case logINFO:
os << "info"; os << "info";
break; break;
@ -96,22 +102,26 @@ public:
break; break;
} }
os << "] "; os << "] ";
} catch (...) { } }
catch (...) { }
return os; return os;
} }
virtual ~SimpleLogger() { virtual ~SimpleLogger() {
if(!LogPolicy::GetInstance().IsMute()) { if(!LogPolicy::GetInstance().IsMute())
switch(level) { {
const bool is_terminal = isatty(fileno(stdout));
switch(level)
{
case logINFO: case logINFO:
std::cout << os.str() << std::endl; std::cout << os.str() << (is_terminal ? COL_RESET : "") << std::endl;
break; break;
case logWARNING: case logWARNING:
std::cerr << RED << os.str() << COL_RESET << std::endl; std::cerr << (is_terminal ? RED : "") << os.str() << (is_terminal ? COL_RESET : "") << std::endl;
break; break;
case logDEBUG: case logDEBUG:
#ifndef NDEBUG #ifndef NDEBUG
std::cout << YELLOW << os.str() << COL_RESET << std::endl; std::cout << (is_terminal ? YELLOW : "") << os.str() << (is_terminal ? COL_RESET : "") << std::endl;
#endif #endif
break; break;
default: default: