]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
trim.seqs couts
[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::closeLog()  {\r
44         try {\r
45                 \r
46                 #ifdef USE_MPI\r
47                         int pid;\r
48                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
49                                         \r
50                         if (pid == 0) { //only one process should output to screen\r
51                 #endif\r
52                 \r
53                 out.close();\r
54                 \r
55                 #ifdef USE_MPI\r
56                         }\r
57                 #endif\r
58         }\r
59         catch(exception& e) {\r
60                 errorOut(e, "MothurOut", "closeLog");\r
61                 exit(1);\r
62         }\r
63 }\r
64 \r
65 /*********************************************************************************************/\r
66 MothurOut::~MothurOut() {\r
67         try {\r
68                 _uniqueInstance = 0;\r
69                 \r
70         }\r
71         catch(exception& e) {\r
72                 errorOut(e, "MothurOut", "MothurOut");\r
73                 exit(1);\r
74         }\r
75 }\r
76 /*********************************************************************************************/\r
77 void MothurOut::mothurOut(string output) {\r
78         try {\r
79                 \r
80                 #ifdef USE_MPI\r
81                         int pid;\r
82                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
83                                         \r
84                         if (pid == 0) { //only one process should output to screen\r
85                 #endif\r
86                 \r
87                 cout << output;\r
88                 out << output;\r
89                 \r
90                 #ifdef USE_MPI\r
91                         }\r
92                 #endif\r
93         }\r
94         catch(exception& e) {\r
95                 errorOut(e, "MothurOut", "MothurOut");\r
96                 exit(1);\r
97         }\r
98 }\r
99 /*********************************************************************************************/\r
100 void MothurOut::mothurOutEndLine() {\r
101         try {\r
102                 #ifdef USE_MPI\r
103                         int pid;\r
104                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
105                                         \r
106                         if (pid == 0) { //only one process should output to screen\r
107                 #endif\r
108                 \r
109                 cout << endl;\r
110                 out << endl;\r
111                 \r
112                 #ifdef USE_MPI\r
113                         }\r
114                 #endif\r
115         }\r
116         catch(exception& e) {\r
117                 errorOut(e, "MothurOut", "MothurOutEndLine");\r
118                 exit(1);\r
119         }\r
120 }\r
121 /*********************************************************************************************/\r
122 void MothurOut::mothurOutJustToLog(string output) {\r
123         try {\r
124                 #ifdef USE_MPI\r
125                         int pid;\r
126                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); \r
127                                         \r
128                         if (pid == 0) { //only one process should output to screen\r
129                 #endif\r
130                 \r
131                 out << output;\r
132                 \r
133                 #ifdef USE_MPI\r
134                         }\r
135                 #endif\r
136         }\r
137         catch(exception& e) {\r
138                 errorOut(e, "MothurOut", "MothurOutJustToLog");\r
139                 exit(1);\r
140         }\r
141 }\r
142 /*********************************************************************************************/\r
143 void MothurOut::errorOut(exception& e, string object, string function) {\r
144         double vm, rss;\r
145         mem_usage(vm, rss);\r
146         \r
147         mothurOut("Error: ");\r
148         mothurOut(toString(e.what()));\r
149         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
150         mothurOutEndLine();\r
151 }\r
152 /*********************************************************************************************/\r
153 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c \r
154 // process_mem_usage(double &, double &) - takes two doubles by reference,\r
155 // attempts to read the system-dependent data for a process' virtual memory\r
156 // size and resident set size, and return the results in KB.\r
157 //\r
158 // On failure, returns 0.0, 0.0\r
159 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {\r
160   #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
161   \r
162            vm_usage     = 0.0;\r
163            resident_set = 0.0;\r
164 \r
165            // 'file' stat seems to give the most reliable results\r
166            //\r
167            ifstream stat_stream("/proc/self/stat",ios_base::in);\r
168 \r
169            // dummy vars for leading entries in stat that we don't care about\r
170            //\r
171            string pid, comm, state, ppid, pgrp, session, tty_nr;\r
172            string tpgid, flags, minflt, cminflt, majflt, cmajflt;\r
173            string utime, stime, cutime, cstime, priority, nice;\r
174            string O, itrealvalue, starttime;\r
175 \r
176            // the two fields we want\r
177            //\r
178            unsigned long vsize;\r
179            long rss;\r
180 \r
181            stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr\r
182                                    >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt\r
183                                    >> utime >> stime >> cutime >> cstime >> priority >> nice\r
184                                    >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest\r
185 \r
186            long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages\r
187            vm_usage     = vsize / 1024.0;\r
188            resident_set = rss * page_size_kb;\r
189            \r
190            mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");\r
191                 return 0;\r
192 \r
193         #else\r
194 /*              //windows memory usage\r
195                 // Get the list of process identifiers.\r
196                 DWORD aProcesses[1024], cbNeeded, cProcesses;\r
197                 \r
198                 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }\r
199 \r
200                 // Calculate how many process identifiers were returned.\r
201                 cProcesses = cbNeeded / sizeof(DWORD);\r
202 \r
203                 // Print the memory usage for each process\r
204                 for (int i = 0; i < cProcesses; i++ ) {\r
205                         DWORD processID = aProcesses[i];\r
206                         \r
207                         PROCESS_MEMORY_COUNTERS pmc;\r
208 \r
209                         HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);\r
210 \r
211                         // Print the process identifier.\r
212                         printf( "\nProcess ID: %u\n", processID);\r
213                         \r
214                         if (NULL != hProcess) {\r
215 \r
216                                 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {\r
217                                         printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );\r
218                                         printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );\r
219                                         printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );\r
220                                         printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );\r
221                                         printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );\r
222                                         printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );\r
223                                         printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );\r
224                                         printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); \r
225                                         printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );\r
226                                 }\r
227                                 CloseHandle(hProcess);\r
228                         }\r
229                 }\r
230 */\r
231                         return 0;\r
232 \r
233         #endif\r
234 }\r
235 /*********************************************************************************************/\r
236 \r
237 \r
238 \r
239 \r
240 \r
241 \r