00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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
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 }