00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "peo_debug.h"
00010
00011 #include <stdio.h>
00012 #include <time.h>
00013 #include <unistd.h>
00014 #include <string.h>
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <vector>
00018
00019 #include "peo_debug.h"
00020
00021 #define MAX_BUFF_SIZE 1000
00022
00023 #define DEBUG_PATH "./log/"
00024
00025 static bool debug = true;
00026
00027 static char host [MAX_BUFF_SIZE];
00028
00029 std :: vector <FILE *> files;
00030
00031 void setDebugMode (bool __dbg) {
00032
00033 debug = __dbg;
00034 gethostname (host, MAX_BUFF_SIZE);
00035 }
00036
00037 extern int getNodeRank ();
00038
00039 void initDebugging () {
00040
00041 mkdir (DEBUG_PATH, S_IRWXU);
00042
00043 char buff [MAX_BUFF_SIZE];
00044 sprintf (buff, "%s/%d", DEBUG_PATH, getNodeRank ());
00045 files.push_back (fopen (buff, "w"));
00046 }
00047
00048 void endDebugging () {
00049
00050 for (unsigned i = 0; i < files.size (); i ++)
00051 if (files [i] != stdout)
00052 fclose (files [i]);
00053 }
00054
00055 void printDebugMessage (const char * __mess) {
00056
00057 if (debug) {
00058
00059 char buff [MAX_BUFF_SIZE];
00060 time_t t = time (0);
00061
00062
00063 sprintf (buff, "[%s][%s: ", host, ctime (& t));
00064 * strchr (buff, '\n') = ']';
00065 for (unsigned i = 0; i < files.size (); i ++)
00066 fprintf (files [i], buff);
00067
00068
00069 sprintf (buff, "%s", __mess);
00070
00071 for (unsigned i = 0; i < files.size (); i ++) {
00072 fputs (buff, files [i]);
00073 fputs ("\n", files [i]);
00074 fflush (files [i]);
00075 }
00076 }
00077 }