Index: trunk/src/PacpusLib/Log.cpp
===================================================================
--- trunk/src/PacpusLib/Log.cpp	(revision 231)
+++ trunk/src/PacpusLib/Log.cpp	(revision 236)
@@ -11,15 +11,37 @@
 #include <boost/log/detail/date_time_format_parser.hpp>
 #include <boost/log/expressions.hpp>
+#include <boost/log/expressions/formatters/if.hpp>
 #include <boost/log/sinks/text_file_backend.hpp>
 #include <boost/log/sinks/text_ostream_backend.hpp>
 #include <boost/log/sources/severity_logger.hpp>
-#include <boost/log/sources/record_ostream.hpp>
 #include <boost/log/support/date_time.hpp>
 #include <boost/log/utility/setup/common_attributes.hpp>
 #include <boost/log/utility/setup/file.hpp>
 #include <boost/log/utility/setup/formatter_parser.hpp>
-#include <boost/utility/empty_deleter.hpp>
+#if BOOST_VERSION >= 105500 // header exists from 1.55
+#   include <boost/utility/empty_deleter.hpp>
+#else
+#   include <boost/log/utility/empty_deleter.hpp>
+#endif
 #include <ostream>
 #include <QString>
+
+// could use Boost.Predef with Boost >= 1.55
+#if defined(WIN32) || defined(_WINDOWS)
+#   define PACPUS_OS_WINDOWS 1
+#elif defined(__unix) || defined(__unix__)
+#   define PACPUS_OS_UNIX 1
+#   if defined(__linux) || defined(__linux__)
+#       define PACPUS_OS_LINUX 1
+#   endif
+#elif defined(__APPLE__) || defined(__MACH__) || defined(Macintosh) || defined(macintosh)
+#   define PACPUS_OS_MACOS 1
+#else
+//  unknown system
+#endif
+
+#if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_WINDOWS
+#   include <Windows.h>
+#endif
 
 template< typename CharT, typename TraitsT >
@@ -36,5 +58,94 @@
 namespace pacpus
 {
-   
+
+BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel)
+
+enum Color {
+  COLOR_DEFAULT,
+  COLOR_BLACK,
+  COLOR_RED,
+  COLOR_GREEN,
+  COLOR_YELLOW,
+  COLOR_BLUE,
+  COLOR_MAGENTA,
+  COLOR_CYAN,
+  COLOR_WHITE
+};
+
+//Color getColor(boost::log::trivial::severity_type const& sev)
+//{
+//    using namespace boost::log::trivial;
+
+//    if (sev >= error) {
+//        return COLOR_RED;
+//    } else if (sev == warning) {
+//        return COLOR_YELLOW;
+//    } else if (sev == info) {
+//        return COLOR_GREEN;
+//    } else {
+//        return COLOR_DEFAULT;
+//    }
+//}
+
+#if PACPUS_OS_WINDOWS //&& !PACPUS_OS_WINDOWS_MOBILE
+
+// Returns the character attribute for the given color.
+WORD getColorAttribute(Color color)
+{
+    switch (color) {
+    case COLOR_BLACK:   return 0;
+    case COLOR_RED:     return FOREGROUND_RED;
+    case COLOR_GREEN:   return FOREGROUND_GREEN;
+    case COLOR_YELLOW:  return FOREGROUND_RED | FOREGROUND_GREEN;
+    case COLOR_BLUE:    return FOREGROUND_BLUE;
+    case COLOR_MAGENTA: return FOREGROUND_RED | FOREGROUND_BLUE;
+    case COLOR_CYAN:    return FOREGROUND_GREEN | FOREGROUND_BLUE;
+    case COLOR_WHITE:   return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
+    default:            return 0;
+    }
+}
+
+#else
+
+/// @returns the ANSI color code for the given color. COLOR_DEFAULT is
+/// an invalid input.
+const char* getAnsiColorCode(Color color)
+{
+    const char* kEscapeSequence = "\033[";
+//    const char* kBackground = "";
+    const char* kFontRegular = "0;";
+//    const char* kFontBold = "1;";
+//    const char* kFontWeak = "2;";
+//    const char* kFontStrong = "3;";
+//    const char* kFontUnderline = "4;";
+
+    std::stringstream ss;
+    ss << kEscapeSequence << kFontRegular;
+
+    switch (color) {
+    case COLOR_BLACK:   ss << "30"; break;
+    case COLOR_RED:     ss << "31"; break;
+    case COLOR_GREEN:   ss << "32"; break;
+    case COLOR_YELLOW:  ss << "33"; break;
+    case COLOR_BLUE:    ss << "34"; break;
+    case COLOR_MAGENTA: ss << "35"; break;
+    case COLOR_CYAN:    ss << "36"; break;
+    case COLOR_WHITE:   ss << "37"; break;
+    default:            return "";
+    };
+
+    const char* kPostfix = "m";
+    ss << kPostfix;
+
+    return ss.str().c_str();
+}
+
+const char* getAnsiColorCodeRestoreDefault()
+{
+    return "\033[0m";
+}
+
+#endif // PACPUS_OS_WINDOWS && !PACPUS_OS_WINDOWS_MOBILE
+
 void init_log_factories()
 {
@@ -78,4 +189,7 @@
         "ThreadID",
         attrs::current_thread_id());
+    //logging::core::get()->add_global_attribute(
+    //    "Scope",
+    //    attrs::named_scope());
 
     logging::core::get()->set_filter
@@ -83,12 +197,13 @@
 #ifdef NDEBUG
         // release
-        logging::trivial::severity >= logging::trivial::debug
+        severity >= debug
 #else
         // debug
-        logging::trivial::severity >= logging::trivial::trace
-#endif
-    );
-
-    // Add a file log
+        severity >= trace
+#endif
+    );
+
+    ////////////////////////////////////////////////////////////////////////////////
+    // FILE
     logging::add_file_log
     (
@@ -103,5 +218,6 @@
                 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
                 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
-                << " <" << logging::trivial::severity << ">"
+                //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
+                << " <" << severity << ">"
                 << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
                 << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
@@ -109,7 +225,19 @@
         )
     );
+    
+    ////////////////////////////////////////////////////////////////////////////////
+    // CONSOLE
+#if PACPUS_LOG_COLORED_OUTPUT
+    Color color = COLOR_GREEN;// = getColor(expr::attr<logging::trivial::severity_level>(logging::trivial::severity.get_name()));
+#endif
+
+#if BOOST_VERSION >= 105500
+#else
+    using logging::empty_deleter;
+#endif
 
     // Create a backend and attach a couple of streams to it
-    shared_ptr< sinks::text_ostream_backend > backend = make_shared< sinks::text_ostream_backend >();
+    boost::shared_ptr< sinks::text_ostream_backend > backend =
+        make_shared< sinks::text_ostream_backend >();
     backend->add_stream(
         shared_ptr< std::ostream >(&std::clog, empty_deleter())
@@ -125,5 +253,5 @@
     sink->set_filter
     (
-        logging::trivial::severity >= logging::trivial::info
+        severity >= info
     );
 
@@ -131,10 +259,17 @@
     (
         expr::stream
+#if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX
+//            << getAnsiColorCode(color)
+#endif
             << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
             //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] "
             << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "] "
-            << "<" << logging::trivial::severity << ">"
+            //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
+            << "<" << severity << ">"
             << " "
             << expr::smessage
+#if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX
+            << getAnsiColorCodeRestoreDefault() // Resets the terminal to default.
+#endif
     );
 
