peo_debug.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "peo_debug.cpp"
00004 
00005 // (c) OPAC Team, LIFL, August 2005
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: paradiseo-help@lists.gforge.inria.fr
00022 */
00023 
00024 #include "peo_debug.h"
00025 
00026 #include <stdio.h>
00027 #include <time.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <vector>
00033 
00034 #include "peo_debug.h"
00035 
00036 #define MAX_BUFF_SIZE 1000
00037 
00038 #define DEBUG_PATH "./log/"
00039 
00040 static bool debug = true;
00041 
00042 static char host [MAX_BUFF_SIZE];
00043 
00044 std :: vector <FILE *> files;
00045 
00046 void setDebugMode (bool __dbg) {
00047 
00048   debug = __dbg;
00049   gethostname (host, MAX_BUFF_SIZE);
00050 }
00051 
00052 extern int getNodeRank ();
00053 
00054 void initDebugging () {
00055   
00056   mkdir (DEBUG_PATH, S_IRWXU);
00057   //  files.push_back (stdout);
00058   char buff [MAX_BUFF_SIZE];
00059   sprintf (buff, "%s/%d", DEBUG_PATH, getNodeRank ());
00060   files.push_back (fopen (buff, "w"));
00061 }
00062 
00063 void endDebugging () {
00064 
00065   for (unsigned i = 0; i < files.size (); i ++)
00066     if (files [i] != stdout)
00067       fclose (files [i]);
00068 }
00069 
00070 void printDebugMessage (const char * __mess) {
00071 
00072   if (debug) {
00073 
00074     char buff [MAX_BUFF_SIZE];
00075     time_t t = time (0);
00076 
00077     /* Date */
00078     sprintf (buff, "[%s][%s: ", host, ctime (& t));
00079     * strchr (buff, '\n') = ']';
00080     for (unsigned i = 0; i < files.size (); i ++)
00081       fprintf (files [i], buff);
00082 
00083     /* Message */
00084     sprintf (buff, "%s", __mess);
00085     
00086     for (unsigned i = 0; i < files.size (); i ++) {
00087       fputs (buff, files [i]);
00088       fputs ("\n", files [i]);
00089       fflush (files [i]);
00090     }
00091   }
00092 }

Generated on Wed Dec 20 13:45:48 2006 for ParadisEO by  doxygen 1.4.6