]> SALOME platform Git repositories - tools/libbatch.git/blobdiff - src/Local/BatchManager_Local.cxx
Salome HOME
Add very simple log system
[tools/libbatch.git] / src / Local / BatchManager_Local.cxx
index e47a42a609ab8def2c87c053ce4d8fec6ae7dbbe..6d1ae1a81bfc51a910752990eaa20b6eb53e398c 100644 (file)
@@ -56,6 +56,7 @@
 #include "BatchManager_Local.hxx"
 #include "RunTimeException.hxx"
 #include "Utils.hxx"
+#include "Log.hxx"
 
 using namespace std;
 
@@ -80,8 +81,8 @@ namespace Batch {
       pthread_mutex_lock(&_threads_mutex);
       string state = iter->second.param[STATE];
       if (state != FINISHED && state != FAILED) {
-        UNDER_LOCK( cout << "Warning: Job " << iter->first <<
-                            " is not finished, it will now be canceled." << endl );
+        UNDER_LOCK( LOG("Warning: Job " << iter->first <<
+                        " is not finished, it will now be canceled."));
         pthread_cancel(iter->second.thread_id);
         pthread_cond_wait(&_threadSyncCondition, &_threads_mutex);
       }
@@ -147,8 +148,7 @@ namespace Batch {
         pthread_cancel(_threads[id].thread_id);
         pthread_cond_wait(&_threadSyncCondition, &_threads_mutex);
       } else {
-        cout << "Cannot delete job " << jobid.getReference() <<
-                ". Job is already finished." << endl;
+        LOG("Cannot delete job " << jobid.getReference() << ". Job is already finished.");
       }
     }
     pthread_mutex_unlock(&_threads_mutex);
@@ -165,7 +165,7 @@ namespace Batch {
     istringstream iss(jobid.getReference());
     iss >> id;
 
-    UNDER_LOCK( cout << "BatchManager is sending HOLD command to the thread " << id << endl );
+    UNDER_LOCK( LOG("BatchManager is sending HOLD command to the thread " << id) );
 
     // On introduit une commande dans la queue du thread
     // @@@ --------> SECTION CRITIQUE <-------- @@@
@@ -183,7 +183,7 @@ namespace Batch {
     istringstream iss(jobid.getReference());
     iss >> id;
 
-    UNDER_LOCK( cout << "BatchManager is sending RELEASE command to the thread " << id << endl );
+    UNDER_LOCK( LOG("BatchManager is sending RELEASE command to the thread " << id) );
 
     // On introduit une commande dans la queue du thread
     // @@@ --------> SECTION CRITIQUE <-------- @@@
@@ -377,7 +377,7 @@ namespace Batch {
     tempOutputFile.flush();
     tempOutputFile.close();
 
-    cerr << "Batch script file generated is: " << tmpFileName << endl;
+    LOG("Batch script file generated is: " << tmpFileName);
 
     Utils::chmod(tmpFileName.c_str(), 0x1ED);
     int status = _bm._protocol.copyFile(tmpFileName, "", "",
@@ -391,7 +391,7 @@ namespace Batch {
       // pscp does not preserve access permissions on files
       string subCommand = string("chmod u+x ") + remotePath;
       string command = _bm._protocol.getExecCommand(subCommand, _bm._hostname, _bm._username);
-      cerr << command.c_str() << endl;
+      LOG(command);
       status = system(command.c_str());
       if (status) {
         std::ostringstream oss;
@@ -452,7 +452,7 @@ namespace Batch {
 #else
     child = fork();
     if (child < 0) { // erreur
-      UNDER_LOCK( cerr << "Fork impossible (rc=" << child << ")" << endl );
+      UNDER_LOCK( LOG("Fork impossible (rc=" << child << ")") );
 
     } else if (child > 0) { // pere
       p_ta->pere(child);
@@ -481,7 +481,7 @@ namespace Batch {
     // On invoque la fonction de nettoyage de la memoire
     delete_on_exit(arg);
 
-    UNDER_LOCK( cout << "Father is leaving" << endl );
+    UNDER_LOCK( LOG("Father is leaving") );
     pthread_exit(NULL);
     return NULL;
   }
@@ -524,22 +524,22 @@ namespace Batch {
       DWORD exitCode;
       GetExitCodeProcess(child, &exitCode);
       if (exitCode != STILL_ACTIVE) {
-        UNDER_LOCK( cout << "Father sees his child is DONE: exit code = " << exitCode << endl );
+        UNDER_LOCK( LOG("Father sees his child is DONE: exit code = " << exitCode) );
         break;
       }
 #else
       int child_rc = 0;
       pid_t child_wait_rc = waitpid(child, &child_rc, WNOHANG /* | WUNTRACED */);
       if (child_wait_rc > 0) {
-        UNDER_LOCK( cout << "Status is: " << WIFEXITED( child_rc) << endl);
-        UNDER_LOCK( cout << "Status is: " << WEXITSTATUS( child_rc) << endl);
-        UNDER_LOCK( cout << "Status is: " << WIFSIGNALED( child_rc) << endl);
-        UNDER_LOCK( cout << "Status is: " << WTERMSIG( child_rc) << endl);
-        UNDER_LOCK( cout << "Status is: " << WCOREDUMP( child_rc) << endl);
-        UNDER_LOCK( cout << "Status is: " << WIFSTOPPED( child_rc) << endl);
-        UNDER_LOCK( cout << "Status is: " << WSTOPSIG( child_rc) << endl);
+        UNDER_LOCK( LOG("Status is: " << WIFEXITED( child_rc)) );
+        UNDER_LOCK( LOG("Status is: " << WEXITSTATUS( child_rc)) );
+        UNDER_LOCK( LOG("Status is: " << WIFSIGNALED( child_rc)) );
+        UNDER_LOCK( LOG("Status is: " << WTERMSIG( child_rc)) );
+        UNDER_LOCK( LOG("Status is: " << WCOREDUMP( child_rc)) );
+        UNDER_LOCK( LOG("Status is: " << WIFSTOPPED( child_rc)) );
+        UNDER_LOCK( LOG("Status is: " << WSTOPSIG( child_rc)) );
 #ifdef WIFCONTINUED
-        UNDER_LOCK( cout << "Status is: " << WIFCONTINUED( child_rc) << endl); // not compilable on sarge
+        UNDER_LOCK( LOG("Status is: " << WIFCONTINUED( child_rc)) ); // not compilable on sarge
 #endif
         if (WIFSTOPPED(child_rc)) {
           // NOTA : pour rentrer dans cette section, il faut que le flag WUNTRACED
@@ -553,12 +553,12 @@ namespace Batch {
           _bm._threads[_id].param[STATE] = Batch::PAUSED;
           pthread_mutex_unlock(&_bm._threads_mutex);
           // @@@ --------> SECTION CRITIQUE <-------- @@@
-          UNDER_LOCK( cout << "Father sees his child is STOPPED : " << child_wait_rc << endl );
+          UNDER_LOCK( LOG("Father sees his child is STOPPED : " << child_wait_rc) );
 
         }
         else {
           // Le fils est termine, on sort de la boucle et du if englobant
-          UNDER_LOCK( cout << "Father sees his child is DONE : " << child_wait_rc << " (child_rc=" << (WIFEXITED(child_rc) ? WEXITSTATUS(child_rc) : -1) << ")" << endl );
+          UNDER_LOCK( LOG("Father sees his child is DONE : " << child_wait_rc << " (child_rc=" << (WIFEXITED(child_rc) ? WEXITSTATUS(child_rc) : -1) << ")") );
           break;
         }
       }
@@ -569,7 +569,7 @@ namespace Batch {
         _bm._threads[_id].hasFailed = true;
         pthread_mutex_unlock(&_bm._threads_mutex);
         // @@@ --------> SECTION CRITIQUE <-------- @@@
-        UNDER_LOCK( cout << "Father sees his child is DEAD : " << child_wait_rc << " (Reason : " << strerror(errno) << ")" << endl );
+        UNDER_LOCK( LOG("Father sees his child is DEAD : " << child_wait_rc << " (Reason : " << strerror(errno) << ")") );
         break;
       }
 #endif
@@ -590,7 +590,7 @@ namespace Batch {
         //            << "maxwalltime              = " << maxwalltime            << endl
         //            << "int(maxwalltime * 1.1)   = " << int(maxwalltime * 1.1) << endl;
         if (child_elapsedtime_minutes > long((float)maxwalltime * 1.1) ) { // On se donne 10% de marge avant le KILL
-          UNDER_LOCK( cout << "Father is sending KILL command to the thread " << _id << endl );
+          UNDER_LOCK( LOG("Father is sending KILL command to the thread " << _id) );
           // On introduit une commande dans la queue du thread
           // @@@ --------> SECTION CRITIQUE <-------- @@@
           pthread_mutex_lock(&_bm._threads_mutex);
@@ -601,7 +601,7 @@ namespace Batch {
 
 
         } else if (child_elapsedtime_minutes > maxwalltime ) {
-          UNDER_LOCK( cout << "Father is sending TERM command to the thread " << _id << endl );
+          UNDER_LOCK( LOG("Father is sending TERM command to the thread " << _id) );
           // On introduit une commande dans la queue du thread
           // @@@ --------> SECTION CRITIQUE <-------- @@@
           pthread_mutex_lock(&_bm._threads_mutex);
@@ -624,26 +624,26 @@ namespace Batch {
 
           switch (cmd) {
     case NOP:
-      UNDER_LOCK( cout << "Father does nothing to his child" << endl );
+      UNDER_LOCK( LOG("Father does nothing to his child") );
       break;
 #ifndef WIN32
     case HOLD:
-      UNDER_LOCK( cout << "Father is sending SIGSTOP signal to his child" << endl );
+      UNDER_LOCK( LOG("Father is sending SIGSTOP signal to his child") );
       kill(child, SIGSTOP);
       break;
 
     case RELEASE:
-      UNDER_LOCK( cout << "Father is sending SIGCONT signal to his child" << endl );
+      UNDER_LOCK( LOG("Father is sending SIGCONT signal to his child") );
       kill(child, SIGCONT);
       break;
 
     case TERM:
-      UNDER_LOCK( cout << "Father is sending SIGTERM signal to his child" << endl );
+      UNDER_LOCK( LOG("Father is sending SIGTERM signal to his child") );
       kill(child, SIGTERM);
       break;
 
     case KILL:
-      UNDER_LOCK( cout << "Father is sending SIGKILL signal to his child" << endl );
+      UNDER_LOCK( LOG("Father is sending SIGKILL signal to his child") );
       kill(child, SIGKILL);
       break;
 #endif
@@ -696,7 +696,7 @@ namespace Batch {
         comstr += command[i] + " ";
       }
       argv[command.size()] = NULL;
-      UNDER_LOCK( cout << "*** debug_command = " << comstr << endl );
+      UNDER_LOCK( LOG("*** debug_command = " << comstr) );
 
       // On cree une session pour le fils de facon a ce qu'il ne soit pas
       // detruit lorsque le shell se termine (le shell ouvre une session et
@@ -710,7 +710,7 @@ namespace Batch {
 
       // On execute la commande du fils
       execv(argv[0], argv);
-      UNDER_LOCK( cout << "*** debug_command = " << strerror(errno) << endl );
+      UNDER_LOCK( LOG("*** debug_command = " << strerror(errno)) );
       // No need to deallocate since nothing happens after a successful exec
 
       // Normalement on ne devrait jamais arriver ici
@@ -719,7 +719,7 @@ namespace Batch {
 
     } catch (GenericException & e) {
 
-      std::cerr << "Caught exception : " << e.type << " : " << e.message << std::endl;
+      LOG("Caught exception : " << e.type << " : " << e.message);
     }
 
     exit(99);
@@ -748,7 +748,7 @@ namespace Batch {
         comstr += command[i];
       }
 
-      UNDER_LOCK( cout << "*** debug_command = " << comstr << endl );
+      UNDER_LOCK( LOG("*** debug_command = " << comstr) );
 
       STARTUPINFO si;
       ZeroMemory( &si, sizeof(si) );
@@ -769,7 +769,7 @@ namespace Batch {
 
     } catch (GenericException & e) {
 
-      std::cerr << "Caught exception : " << e.type << " : " << e.message << std::endl;
+      LOG("Caught exception : " << e.type << " : " << e.message);
     }
 
     return pi.hProcess;