Index: trunk/FACT++/src/dataLogger.cc
===================================================================
--- trunk/FACT++/src/dataLogger.cc	(revision 10270)
+++ trunk/FACT++/src/dataLogger.cc	(revision 10271)
@@ -45,7 +45,10 @@
 #include "Time.h"
 #include "StateMachineDim.h"
+#include "WindowLog.h"
+#include "Configuration.h"
 #include "ServiceList.h"
 #include "Converter.h"
 #include "MessageImp.h"
+#include "LocalControl.h"
 
 //#define HAS_FITS
@@ -72,5 +75,5 @@
 	} localstates_t;
 	
-	DataLogger();
+        DataLogger(std::ostream &out);
 	~DataLogger(); 
 	
@@ -274,5 +277,5 @@
 //!Setup the allows states, configs and transitions for the data logger
 //
-DataLogger::DataLogger() : StateMachineDim(std::cout, "DATA_LOGGER")
+DataLogger::DataLogger(std::ostream &out) : StateMachineDim(out, "DATA_LOGGER")
 {
 		//initialize member data
@@ -281,9 +284,9 @@
 		fRunNumber = 12345;
 		//Give a name to this machine's specific states
-		AddStateName(kSM_DailyOpen, "Daily_File_Openened");
-		AddStateName(kSM_WaitingRun, "Waiting_Run_Number");
-		AddStateName(kSM_Logging, "Logging data");
-		AddStateName(kSM_BadDailyConfig, "Folder_For_Daily_Logging_Badly_Configured");
-		AddStateName(kSM_BadRunConfig, "Folder_For_Run_Logging_Badly Configured");
+		AddStateName(kSM_DailyOpen,      "DailyFileOpen");
+		AddStateName(kSM_WaitingRun,     "WaitForRun");
+		AddStateName(kSM_Logging,        "Logging");
+		AddStateName(kSM_BadDailyConfig, "ErrDailyFolder");
+		AddStateName(kSM_BadRunConfig,   "ErrRunFolder");
 
 		/*Add the possible transitions for this machine*/
@@ -596,4 +599,5 @@
 	}
 }
+
 // --------------------------------------------------------------------------
 //
@@ -635,13 +639,19 @@
 		header << fMs << " " << I->getTimestamp() << " ";
 
-		Converter conv(std::cout, I->getFormat(), I->getData(), I->getSize());
-
-		if (conv.Ptr() == NULL)
-			return;
-		
-		std::string text(conv.Ptr());
-	
+                const Converter conv(Out(), I->getFormat());
+
+                std::string text = conv.GetString(I->getData(), I->getSize());
+                if (!conv.valid() || text.empty()!=conv.empty())
+                {
+                    Error("Couldn't properly parse the data... ignored.");
+                    return;
+                }
+
 		if (text.empty())
-			return;
+                    return;
+
+                //replace bizarre characters by white space
+                replace(text.begin(), text.end(), '\n', '\\');
+                replace_if(text.begin(), text.end(), std::ptr_fun<int, int>(&std::iscntrl), ' ');
 	
 		if (fDailyReportFile.is_open())
@@ -649,15 +659,6 @@
 		if (fRunReportFile.is_open())
 			fRunReportFile << header.str();
-	
-		//replace bizarre characters by white space
-		for (unsigned int i=0; i<text.size(); i++)
-		{
-			if (text[i] == '\n') 
-				text[i] = '\\';
-			else
-				if (iscntrl(text[i])) 
-					text[i] = ' ';	
-		}
-		if (fDailyReportFile.is_open())
+
+                if (fDailyReportFile.is_open())
 			fDailyReportFile << text << std::endl;
 		if (fRunReportFile.is_open())
@@ -692,6 +693,8 @@
 //! @returns 
 //!		the new state. Currently, always the current state
-//
-//DEPREC I guess that this function should not be any longer
+//!
+//! @deprecated
+//!    I guess that this function should not be any longer
+//
 int DataLogger::LogMessagePlease(const Event& evt)
 {
@@ -706,33 +709,31 @@
 		
 //	std::string text = ToString(evt.GetFormat().c_str(), evt.GetData(), evt.GetSize());
-	Converter conv(std::cout, evt.GetFormat().c_str(), evt.GetData(), evt.GetSize());
-	if (conv.Ptr() == NULL)
-		return GetCurrentState();
-		
-	std::string text(conv.Ptr());
-	
-	if (!text.empty())
-	{
-		if (fDailyLogFile.is_open())
-			fDailyLogFile << header;
-		if (fRunLogFile.is_open())
-			fRunLogFile << header;
-		
-		//replace bizarre characters by white space
-		for (unsigned int i=0; i<text.size(); i++)
-		{
-			if (text[i] == '\n') 
-				text[i] = '\\';
-			else
-				if (iscntrl(text[i])) 
-					text[i] = ' ';	
-		}
-		if (fDailyLogFile.is_open())
-			fDailyLogFile << text;
-		if (fRunLogFile.is_open())
-			fRunLogFile << text;
-	}
-	
-	return GetCurrentState();	
+        const Converter conv(Out(), evt.GetFormat());
+
+        std::string text = conv.GetString(evt.GetData(), evt.GetSize());
+        if (!conv.valid() || text.empty()!=conv.empty())
+        {
+            Error("Couldn't properly parse the data... ignored.");
+            return GetCurrentState();
+        }
+
+        if (text.empty())
+            return GetCurrentState();
+
+        //replace bizarre characters by white space
+        replace(text.begin(), text.end(), '\n', '\\');
+        replace_if(text.begin(), text.end(), std::ptr_fun<int, int>(&std::iscntrl), ' ');
+
+	if (fDailyLogFile.is_open())
+		fDailyLogFile << header;
+	if (fRunLogFile.is_open())
+		fRunLogFile << header;
+
+	if (fDailyLogFile.is_open())
+		fDailyLogFile << text;
+	if (fRunLogFile.is_open())
+		fRunLogFile << text;
+
+	return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -897,7 +898,14 @@
 //	 sub.runFile.InitCol("MilliSec", "int", &fMs);
 	 sub.runFile.InitCol("QoS", "int", &fQuality);
-	 
-	 Converter::FormatList flist = Converter::Convert(std::cout, std::string(format));
-	 
+
+         const Converter::FormatList flist = Converter::Compile(Out(), format);
+
+         // Compilation failed
+         if (fList.empty() || fList.back().first.second!=0)
+         {
+             Error("Compilation of format string failed.");
+             return;
+         }
+
 	 //we've got a nice structure describing the format of this service's messages.
 	 //Let's create the appropriate FITS columns
@@ -1055,17 +1063,110 @@
 
 // --------------------------------------------------------------------------
-// 
-int main()
-{
-//No more Dim Checking ??
-//	if (!CheckDim())
-//		return -1;
-	
-	DataLogger log; 
-	while (!log.fExitRequested)
-	{
-		usleep(10);
-	}
-	return 0;
-	
-}
+
+int RunDim(Configuration &conf)
+{
+    WindowLog wout;
+
+    //log.SetWindow(stdscr);
+    if (conf.Has("log"))
+        if (!wout.OpenLogFile(conf.Get<std::string>("log")))
+            wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<std::string>("log") << ": " << strerror(errno) << std::endl;
+
+    // Start io_service.Run to use the StateMachineImp::Run() loop
+    // Start io_service.run to only use the commandHandler command detaching
+    DataLogger logger(wout);
+
+    while (1)
+        usleep(1);
+
+    return 0;
+}
+
+template<class T>
+int RunShell(Configuration &conf)
+{
+    static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
+
+    WindowLog &win  = shell.GetStreamIn();
+    WindowLog &wout = shell.GetStreamOut();
+
+    if (conf.Has("log"))
+        if (!wout.OpenLogFile(conf.Get<std::string>("log")))
+            win << kRed << "ERROR - Couldn't open log-file " << conf.Get<std::string>("log") << ": " << strerror(errno) << std::endl;
+
+    DataLogger io_service(wout);
+
+    shell.SetReceiver(io_service);
+    shell.Run();                 // Run the shell
+
+    return 0;
+}
+
+void SetupConfiguration(Configuration &conf)
+{
+    const std::string n = conf.GetName()+".log";
+
+    po::options_description config("Program options");
+    config.add_options()
+        ("dns",       var<std::string>("localhost"),       "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
+        ("log,l",     var<std::string>(n), "Write log-file")
+        ("console,c", var<int>(),     "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
+        ;
+
+    conf.AddEnv("dns", "DIM_DNS_NODE");
+
+    conf.AddOptions(config);
+}
+
+int main(int argc, char* argv[])
+{
+    Configuration conf(argv[0]);
+    SetupConfiguration(conf);
+
+    po::variables_map vm;
+    try
+    {
+        vm = conf.Parse(argc, argv);
+    }
+#if BOOST_VERSION > 104000
+    catch (po::multiple_occurrences &e)
+    {
+        std::cout << "Error: " << e.what() << " of '" << e.get_option_name() << "' option." << std::endl;
+        std::cout << std::endl;
+        return -1;
+    }
+#endif
+    catch (std::exception &e)
+    {
+        std::cout << "Error: " << e.what() << std::endl;
+        std::cout << std::endl;
+
+        return -1;
+    }
+
+    if (conf.HasHelp() || conf.HasPrint())
+        return -1;
+
+    // To allow overwriting of DIM_DNS_NODE set 0 to 1
+    setenv("DIM_DNS_NODE", conf.Get<std::string>("dns").c_str(), 1);
+
+    try
+    {
+        // No console access at all
+        if (!conf.Has("console"))
+            return RunDim(conf);
+
+        // Cosole access w/ and w/o Dim
+        if (conf.Get<int>("console")==0)
+            return RunShell<LocalShell>(conf);
+        else
+            return RunShell<LocalConsole>(conf);
+    }
+    catch (std::exception& e)
+    {
+        cerr << "Exception: " << e.what() << endl;
+        return -1;
+    }
+
+    return 0;
+}
