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