]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
added compile time default path
[mothur.git] / mothurout.cpp
1 /*\r
2  *  m->mothurOut.cpp\r
3  *  Mothur\r
4  *\r
5  *  Created by westcott on 2/25/10.\r
6  *  Copyright 2010 Schloss Lab. All rights reserved.\r
7  *\r
8  */\r
9 \r
10 #include "mothurout.h"\r
11 \r
12 /******************************************************/\r
13 MothurOut* MothurOut::getInstance() {\r
14         if( _uniqueInstance == 0) {\r
15                 _uniqueInstance = new MothurOut();\r
16         }\r
17         return _uniqueInstance;\r
18 }\r
19 /*********************************************************************************************/\r
20 void MothurOut::setFileName(string filename)  {\r
21         try {\r
22                 logFileName = filename;\r
23                 \r
24                 #ifdef USE_MPI\r
25                         int pid;\r
26                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
27                                         \r
28                         if (pid == 0) { //only one process should output to screen\r
29                 #endif\r
30                 \r
31                 openOutputFile(filename, out);\r
32                 \r
33                 #ifdef USE_MPI\r
34                         }\r
35                 #endif\r
36         }\r
37         catch(exception& e) {\r
38                 errorOut(e, "MothurOut", "setFileName");\r
39                 exit(1);\r
40         }\r
41 }\r
42 /*********************************************************************************************/\r
43 void MothurOut::setDefaultPath(string pathname)  {\r
44         try {\r
45         \r
46                 //add / to name if needed\r
47                 string lastChar = pathname.substr(pathname.length()-1);\r
48                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
49                         if (lastChar != "/") { pathname += "/"; }\r
50                 #else\r
51                         if (lastChar != "\\") { pathname += "\\"; }     \r
52                 #endif\r
53                 \r
54                 defaultPath = pathname;\r
55                 \r
56         }\r
57         catch(exception& e) {\r
58                 errorOut(e, "MothurOut", "setDefaultPath");\r
59                 exit(1);\r
60         }\r
61 }\r
62 /*********************************************************************************************/\r
63 void MothurOut::closeLog()  {\r
64         try {\r
65                 \r
66                 #ifdef USE_MPI\r
67                         int pid;\r
68                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
69                                         \r
70                         if (pid == 0) { //only one process should output to screen\r
71                 #endif\r
72                 \r
73                 out.close();\r
74                 \r
75                 #ifdef USE_MPI\r
76                         }\r
77                 #endif\r
78         }\r
79         catch(exception& e) {\r
80                 errorOut(e, "MothurOut", "closeLog");\r
81                 exit(1);\r
82         }\r
83 }\r
84 \r
85 /*********************************************************************************************/\r
86 MothurOut::~MothurOut() {\r
87         try {\r
88                 _uniqueInstance = 0;\r
89                 \r
90         }\r
91         catch(exception& e) {\r
92                 errorOut(e, "MothurOut", "MothurOut");\r
93                 exit(1);\r
94         }\r
95 }\r
96 /*********************************************************************************************/\r
97 void MothurOut::mothurOut(string output) {\r
98         try {\r
99                 \r
100                 #ifdef USE_MPI\r
101                         int pid;\r
102                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
103                                         \r
104                         if (pid == 0) { //only one process should output to screen\r
105                 #endif\r
106                 \r
107                 cout << output;\r
108                 out << output;\r
109                 \r
110                 #ifdef USE_MPI\r
111                         }\r
112                 #endif\r
113         }\r
114         catch(exception& e) {\r
115                 errorOut(e, "MothurOut", "MothurOut");\r
116                 exit(1);\r
117         }\r
118 }\r
119 /*********************************************************************************************/\r
120 void MothurOut::mothurOutEndLine() {\r
121         try {\r
122                 #ifdef USE_MPI\r
123                         int pid;\r
124                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
125                                         \r
126                         if (pid == 0) { //only one process should output to screen\r
127                 #endif\r
128                 \r
129                 cout << endl;\r
130                 out << endl;\r
131                 \r
132                 #ifdef USE_MPI\r
133                         }\r
134                 #endif\r
135         }\r
136         catch(exception& e) {\r
137                 errorOut(e, "MothurOut", "MothurOutEndLine");\r
138                 exit(1);\r
139         }\r
140 }\r
141 /*********************************************************************************************/\r
142 void MothurOut::mothurOutJustToLog(string output) {\r
143         try {\r
144                 #ifdef USE_MPI\r
145                         int pid;\r
146                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
147                                         \r
148                         if (pid == 0) { //only one process should output to screen\r
149                 #endif\r
150                 \r
151                 out << output;\r
152                 \r
153                 #ifdef USE_MPI\r
154                         }\r
155                 #endif\r
156         }\r
157         catch(exception& e) {\r
158                 errorOut(e, "MothurOut", "MothurOutJustToLog");\r
159                 exit(1);\r
160         }\r
161 }\r
162 /*********************************************************************************************/\r
163 void MothurOut::errorOut(exception& e, string object, string function) {\r
164         double vm, rss;\r
165         //mem_usage(vm, rss);\r
166         \r
167         mothurOut("Error: ");\r
168         mothurOut(toString(e.what()));\r
169         mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");\r
170         mothurOutEndLine();\r
171 }\r
172 /*********************************************************************************************/\r
173 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c \r
174 // process_mem_usage(double &, double &) - takes two doubles by reference,\r
175 // attempts to read the system-dependent data for a process' virtual memory\r
176 // size and resident set size, and return the results in KB.\r
177 //\r
178 // On failure, returns 0.0, 0.0\r
179 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {\r
180   #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
181   \r
182            vm_usage     = 0.0;\r
183            resident_set = 0.0;\r
184 \r
185            // 'file' stat seems to give the most reliable results\r
186            //\r
187            ifstream stat_stream("/proc/self/stat",ios_base::in);\r
188 \r
189            // dummy vars for leading entries in stat that we don't care about\r
190            //\r
191            string pid, comm, state, ppid, pgrp, session, tty_nr;\r
192            string tpgid, flags, minflt, cminflt, majflt, cmajflt;\r
193            string utime, stime, cutime, cstime, priority, nice;\r
194            string O, itrealvalue, starttime;\r
195 \r
196            // the two fields we want\r
197            //\r
198            unsigned long vsize;\r
199            long rss;\r
200 \r
201            stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr\r
202                                    >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt\r
203                                    >> utime >> stime >> cutime >> cstime >> priority >> nice\r
204                                    >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest\r
205 \r
206            long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages\r
207            vm_usage     = vsize / 1024.0;\r
208            resident_set = rss * page_size_kb;\r
209            \r
210            mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");\r
211                 return 0;\r
212 \r
213         #else\r
214 /*              //windows memory usage\r
215                 // Get the list of process identifiers.\r
216                 DWORD aProcesses[1024], cbNeeded, cProcesses;\r
217                 \r
218                 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }\r
219 \r
220                 // Calculate how many process identifiers were returned.\r
221                 cProcesses = cbNeeded / sizeof(DWORD);\r
222 \r
223                 // Print the memory usage for each process\r
224                 for (int i = 0; i < cProcesses; i++ ) {\r
225                         DWORD processID = aProcesses[i];\r
226                         \r
227                         PROCESS_MEMORY_COUNTERS pmc;\r
228 \r
229                         HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);\r
230 \r
231                         // Print the process identifier.\r
232                         printf( "\nProcess ID: %u\n", processID);\r
233                         \r
234                         if (NULL != hProcess) {\r
235 \r
236                                 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {\r
237                                         printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );\r
238                                         printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );\r
239                                         printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );\r
240                                         printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );\r
241                                         printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );\r
242                                         printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );\r
243                                         printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );\r
244                                         printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); \r
245                                         printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );\r
246                                 }\r
247                                 CloseHandle(hProcess);\r
248                         }\r
249                 }\r
250 */\r
251                         return 0;\r
252 \r
253         #endif\r
254 }\r
255 /*********************************************************************************************/\r
256 \r
257 \r
258 \r
259 \r
260 \r
261 \r