Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 8818)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 8819)
@@ -1,3 +1,20 @@
                                                                   -*-*- END -*-*-
+
+ 2007/06/18 Thomas Bretz
+
+   * base/Makefile:
+     - cosmetics
+
+   * catalog/SlaStars.cc, catalog/Slalib.cc, catalog/StarCatalog.h,
+     gui/MGSkyPosition.cc:
+     - replaced kRad2Deg by TMath::RadToDeg()
+     - replaced kDeg2Rad by TMath::DegToRad()
+     - replaced kPiDiv2  by TMath::Pi()/2
+
+   * main/MCaos.cc:
+     - replaced cout by gLog and use color coding
+     - replaced coord.h by MPointing.h
+
+
 
  2007/06/16 Thomas Bretz
@@ -67,5 +84,5 @@
      - replaced MPointing by MSlewing
 
-   * main/MPpointing.[h,cc]:
+   * main/MPointing.[h,cc]:
      - renamed class to MSlewing
      - removed
Index: /trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/cosy.cc	(revision 8818)
+++ /trunk/MagicSoft/Cosy/cosy.cc	(revision 8819)
@@ -5,7 +5,13 @@
 #include <TSystem.h>
 #include <TApplication.h>
-
+#include <TObjectTable.h>
+
+#include "ethernet.h"
+#include "vmodican.h"
+
+#include "MArgs.h"
 #include "MCosy.h"
 #include "MTime.h"
+
 #include "MLogManip.h"
 
@@ -16,66 +22,141 @@
 #define EXPERT
 
-#define clog(txt) \
-    { \
-    const Bool_t is = lout.IsOutputDeviceEnabled(MLog::eStdout); \
-    lout << edev(MLog::eStdout) << txt << endl; \
-    if (is) \
-    lout.EnableOutputDevice(MLog::eStdout); \
-    }
+static void StartUpMessage()
+{
+    gLog << all << endl;
+
+    //                1         2         3         4         5         6
+    //       123456789012345678901234567890123456789012345678901234567890
+    gLog << "========================================================" << endl;
+    gLog << "                         COSY                           " << endl;
+    gLog << "          Magic Drive Control System Software           " << endl;
+    gLog << "     Compiled with ROOT v" << ROOT_RELEASE << " on <" << __DATE__ << ">" << endl;
+    gLog << "========================================================" << endl;
+    gLog << endl;
+}
+
+static void Usage()
+{
+    //                1         2         3         4         5         6         7         8
+    //       12345678901234567890123456789012345678901234567890123456789012345678901234567890
+    gLog << all << endl;
+    gLog << "Sorry the usage is:" << endl;
+    gLog << " cosy [options]" << endl << endl;
+    gLog << " Arguments:" << endl;
+    gLog << "   n/a" << endl;
+    gLog << endl;
+    gLog << " Root Options:" << endl;
+    gLog << "   -b                        Batch mode (no graphical output to screen)" << endl<<endl;
+    gLog << " Options:" << endl;
+    gLog.Usage();
+    gLog << "   --debug-threads           Debug threads" << endl << endl;
+    gLog << "   --version, -V             Show startup message with version number" << endl;
+    gLog << "   -?, -h, --help            This help" << endl << endl;
+}
 
 /* ---------------------------------------------------------------------- */
 int main(int argc, char **argv)
 {
-    gLog << "==================================================" << endl;
-    gLog << "                    Cosy V0.1                     " << endl;
-    gLog << "       Magic Drive Control System Software        " << endl;
-    gLog << "            Compiled on <" << __DATE__ << ">"       << endl;
-    gLog << "               Using ROOT v" << ROOTVER             << endl;
-    gLog << "==================================================" << endl;
-    gLog << endl;
-
-    Int_t channel = 0;
-    for (int i=0; i<argc; i++)
-    {
-        TString arg(argv[i]);
-        if (arg=="-1")
-            channel = 1;
-        if (arg=="-0")
-            channel = 0;
-        if (arg=="-c")
-            channel = -1;
-    }
-
-    //
-    // this must move to MGCosy !!!!
-    //
-    const TString name = MCosy::GetFileName("log", "cosy", "log");
-    cout << "Open Logfile: " << name << endl;
-
-    MLog *l = new MLog(name, kTRUE);
-    MLog &lout = *l;
-    lout.SetNoColors();
-
-    MTime now(-1);
-    clog("Starting Cosy at " << now << " ...");
+    if (!MARS::CheckRootVer())
+        return 0xff;
+
+    MLog::RedirectErrorHandler(MLog::kColor);
+
+    //
+    // Evaluate arguments
+    //
+    MArgs arg(argc, argv, kTRUE);
+    gLog.Setup(arg);
+
+    StartUpMessage();
+
+    if (arg.HasOnly("-V") || arg.HasOnly("--version"))
+        return 0;
+
+    if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
+    {
+        Usage();
+        return 2;
+    }
+
+    const Int_t  channel       = arg.GetIntAndRemove("--channel", 0);
+    const Bool_t kDebugMem     = arg.HasOnlyAndRemove("--debug-mem");
+    const Bool_t kDebugThreads = arg.HasOnlyAndRemove("--debug-threads");
+
+    //
+    // check for the right usage of the program (number of arguments)
+    //
+    if (arg.GetNumArguments()>0)
+    {
+        gLog << warn << "WARNING - Wrong number of arguments..." << endl;
+        Usage();
+        return 2;
+    }
+/*
+    //
+    // Now we access/read the resource file. This will remove all
+    // --rc= from the list of arguments.
+    //
+    MEnv env(kConfig);
+    if (!env.IsValid())
+    {
+        gLog << err << "ERROR - Reading resource file " << kConfig << "." << endl;
+        return 0xfe;
+    }
+
+    // And move the resource options from the command line to the MEnv
+    if (!env.TakeEnv(arg, kDebugEnv>2))
+        return 0xfd;
+*/
+    //
+    // check for the right usage of the program (number of options)
+    //
+    if (arg.GetNumOptions()>0)
+    {
+        gLog << warn << "WARNING - Unknown commandline options..." << endl;
+        arg.Print("options");
+        gLog << endl;
+        return 2;
+    }
+
+    if (!gLog.IsOutputDeviceEnabled(MLog::eFile))
+    {
+        const TString name = MCosy::GetFileName("log", "cosy", "log");
+        gLog << inf << "Open automatic logfile: " << name << endl;
+        gLog.SetOutputFile(name);
+    }
+
+    gLog << all << "Starting Cosy at " << MTime(-1) << " in thread " << TThread::SelfId() << "..." << endl;
 
     //
     // start the main window
     //
-    clog("- Initialising Root environment.");
-
-    // FIXME: Fails deleteing something in TGClient::fWlist
-    TApplication *app=new TApplication("App", &argc, argv);
-
+    gLog << all << "- Initialising Root environment." << endl;
+
+    //
+    // Initialize root
+    //
+    //MArray::Class()->IgnoreTObjectStreamer();
+    //MParContainer::Class()->IgnoreTObjectStreamer();
+
+    TApplication app("cosy", &argc, argv);
     if (gROOT->IsBatch())
     {
-        cout << "ERROR - Cannot run in Batch mode!" << endl;
+        gLog << err << "ERROR - Cannot run in Batch mode!" << endl;
         return 0;
     }
+    if (!gClient)
+    {
+        gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
+        return 1;
+    }
+
+    if (kDebugMem)
+        TObject::SetObjectStat(kTRUE);
 
     //
     // Create the Network. Device: /dev/dpm_00, Rate: 500kbps
     //
-    clog("- Constructing MCosy.");
+    gLog << all <<"- Constructing MCosy." << endl;
 /*
     //
@@ -97,10 +178,11 @@
         }
 */
-    MCosy *cosy = new MCosy("/dev/dpm_00", 125, lout);
-
-    clog("- Starting MCosy.");
-#ifndef EXPERT
-    lout.DisableOutputDevice(MLog::eStdout);
-#endif
+    MCosy *cosy = new MCosy();
+
+    Interface *interface = new Ethernet(cosy);
+    // Interface *interface = new VmodIcan(cosy, "/dev/dpm_00", 125);
+
+    gLog << all << "- Starting MCosy." << endl;
+
     cosy->Start();
 
@@ -108,5 +190,5 @@
     if (channel>=0)
     {
-        clog("- Starting Camera.");
+        gLog << all << "- Starting Camera." << endl;
         client=new MStarguider(MObservatory::kMagic1, channel);
         cosy->SetStarguider(client);
@@ -114,9 +196,10 @@
     }
 
-    clog("- Starting mainloop.");
-#ifndef EXPERT
-    lout.DisableOutputDevice(MLog::eStdout);
-#endif
-    app->Run(kTRUE);
+    gLog << all << "- Starting mainloop." << endl;
+
+    app.Run(kTRUE);
+
+    if (kDebugThreads)
+        TThread::Ps();
 
     if (channel>=0)
@@ -124,21 +207,30 @@
         client->SetCosy(NULL);
         cosy->SetStarguider(NULL);
-        clog("- Stopping starg.");
+        gLog << all << "- Stopping starg." << endl;
         delete client;
     }
 
-    clog("- Stopping cosy.");
+    gLog << all << "- Stopping cosy." << endl;
     cosy->Stop();
 
-    now.Now();
-    clog(now << ": MCosy stopped.");
+    gLog <<  all << MTime(-1) << ": MCosy stopped." << endl;
+
+    delete interface;
+
+    gLog << all << "Deleting cosy at " << MTime(-1) << endl;
 
     delete cosy;
 
-    now.Now();
-    clog("Terminating cosy at " << now);
-
-    delete l;
-
-    cout << "The End." << endl;
+    if (kDebugThreads)
+        TThread::Ps();
+
+    //delete app;
+
+    if (TObject::GetObjectStat())
+    {
+        TObject::SetObjectStat(kFALSE);
+        gObjectTable->Print();
+    }
+
+    gLog << all << "The End." << endl;
 }
