A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBLogger.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef PDB_LOGGER_C
20 #define PDB_LOGGER_C
21 
22 #include <iostream>
23 #include "PDBDebug.h"
24 #include "LockGuard.h"
25 #include "PDBLogger.h"
26 #include <stdio.h>
27 #include <sys/stat.h>
28 #include <pthread.h>
29 #include "LogLevel.h"
30 
31 //#include "boost/filesystem.hpp"
32 
33 namespace pdb {
34 
35 PDBLogger::PDBLogger(std::string fName) {
36  // bool folder = boost::filesystem::create_directories("logs");
37  // if (folder==true) std :: cout << "logs folder created." << std :: endl;
38 
39  // create a director logs if not exists
40  const int dir_err = mkdir("logs", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
41  if (-1 == dir_err) {
42  PDB_COUT << "logs folder created." << std::endl;
43  }
44 
45  outputFile = fopen(std::string("logs/" + fName).c_str(), "a");
46  if (outputFile == nullptr) {
47  std::cout << "Unable to open logging file.\n";
48  perror(nullptr);
49  exit(-1);
50  }
51 
52  pthread_mutex_init(&fileLock, nullptr);
53  loglevel = WARN;
54  this->enabled = true;
55 }
56 
57 void PDBLogger::open(std::string fName) {
58  const LockGuard guard{fileLock};
59  if (outputFile != nullptr) {
60  fclose(outputFile);
61  }
62  outputFile = fopen((std::string("logs/") + fName).c_str(), "a");
63  if (outputFile == nullptr) {
64  std::cout << "Unable to open logging file.\n";
65  perror(nullptr);
66  exit(-1);
67  }
68 }
69 
70 /*PDBLogger::PDBLogger() {
71  pthread_mutex_init(&fileLock, nullptr);
72  loglevel = WARN;
73 }*/
74 
76 
77  if (outputFile != nullptr)
78  fclose(outputFile);
79 
80  pthread_mutex_destroy(&fileLock);
81 }
82 
83 // void PDBLogger::writeLn(std :: string writeMe) {
84 // if (!this->enabled) {
85 // return;
86 // }
87 // const LockGuard guard{fileLock};
88 // if (writeMe[writeMe.length() - 1] != '\n') {
89 // fprintf(outputFile, "%s\n", writeMe.c_str());
90 // } else {
91 // fprintf(outputFile, "%s", writeMe.c_str());
92 // }
93 // fflush(outputFile);
94 //}
95 
96 void PDBLogger::writeInt(int writeMe) {
97  if (!this->enabled) {
98  return;
99  }
100  writeLn(std::to_string(writeMe));
101  // const LockGuard guard{fileLock};
102  // fprintf(outputFile, "%d\n", writeMe);
103  // fflush(outputFile);
104 }
105 
106 
107 // Log Levels are:
108 //
109 // OFF,
110 // FATAL,
111 // ERROR,
112 // WARN,
113 // INFO,
114 // DEBUG,
115 // TRACE
116 
117 void PDBLogger::trace(std::string writeMe) {
118  if (!this->enabled || this->loglevel == OFF || this->loglevel == FATAL ||
119  this->loglevel == ERROR || this->loglevel == WARN || this->loglevel == INFO ||
120  this->loglevel == DEBUG) {
121  return;
122  }
123  this->writeLn("[TRACE] " + writeMe);
124 }
125 
126 void PDBLogger::debug(std::string writeMe) {
127  if (!this->enabled || this->loglevel == OFF || this->loglevel == FATAL ||
128  this->loglevel == ERROR || this->loglevel == WARN || this->loglevel == INFO) {
129  return;
130  }
131  this->writeLn("[DEBUG] " + writeMe);
132 }
133 
134 
135 void PDBLogger::info(std::string writeMe) {
136  if (!this->enabled || this->loglevel == OFF || this->loglevel == FATAL ||
137  this->loglevel == ERROR || this->loglevel == WARN) {
138  return;
139  }
140  this->writeLn("[INFO] " + writeMe);
141 }
142 
143 
144 void PDBLogger::warn(std::string writeMe) {
145  if (!this->enabled || this->loglevel == OFF || this->loglevel == FATAL ||
146  this->loglevel == ERROR) {
147  return;
148  }
149  this->writeLn("[WARN] " + writeMe);
150 }
151 
152 
153 void PDBLogger::error(std::string writeMe) {
154  if (!this->enabled || this->loglevel == OFF || this->loglevel == FATAL) {
155  return;
156  }
157  this->writeLn("[ERROR] " + writeMe);
158 }
159 
160 
161 void PDBLogger::fatal(std::string writeMe) {
162  if (!this->enabled || this->loglevel == OFF) {
163  return;
164  }
165  this->writeLn("[FATAL] " + writeMe);
166 }
167 
168 
169 // Added date/time to the logger
170 void PDBLogger::writeLn(std::string writeMe) {
171 
172  if (!this->enabled) {
173  return;
174  }
175 
176  const LockGuard guard{fileLock};
177 
178  // get the current time and date
179  time_t now = time(0);
180  struct tm tstruct;
181  char buf[80];
182  tstruct = *localtime(&now);
183  strftime(buf, sizeof(buf), "[%Y-%m-%d-%X] ", &tstruct);
184 
185  // add date and time to the log
186  writeMe = buf + writeMe;
187 
188  // JiaNote: to get thread id for debugging
189  pthread_t threadId = pthread_self();
190  if (writeMe[writeMe.length() - 1] != '\n') {
191  fprintf(outputFile, "[%lu]%s\n", threadId, writeMe.c_str());
192  } else {
193  fprintf(outputFile, "[%lu]%s", threadId, writeMe.c_str());
194  }
195  fflush(outputFile);
196 }
197 
198 
199 void PDBLogger::write(char* data, unsigned int length) {
200  if (!this->enabled) {
201  return;
202  }
203  const LockGuard guard{fileLock};
204  fwrite(data, sizeof(char), length, outputFile);
205 }
206 
207 // added by Jia
208 void PDBLogger::setEnabled(bool enabled) {
209  this->enabled = enabled;
210 }
211 
213  return this->loglevel;
214 }
215 
217  this->loglevel = loglevel;
218 }
219 }
220 
221 #endif
Definition: LogLevel.h:40
Definition: LogLevel.h:40
void writeInt(int writeMe)
Definition: PDBLogger.cc:96
void fatal(std::string writeMe)
Definition: PDBLogger.cc:161
void setLoglevel(LogLevel loglevel)
Definition: PDBLogger.cc:216
void writeLn(std::string writeMe)
Definition: PDBLogger.cc:170
Definition: LogLevel.h:40
void info(std::string writeMe)
Definition: PDBLogger.cc:135
void error(std::string writeMe)
Definition: PDBLogger.cc:153
pthread_mutex_t fileLock
Definition: PDBLogger.h:106
#define PDB_COUT
Definition: PDBDebug.h:31
FILE * outputFile
Definition: PDBLogger.h:109
void write(char *data, unsigned int length)
Definition: PDBLogger.cc:199
LogLevel getLoglevel()
Definition: PDBLogger.cc:212
PDBLogger(std::string fName)
Definition: PDBLogger.cc:35
void debug(std::string writeMe)
Definition: PDBLogger.cc:126
void open(std::string fName)
Definition: PDBLogger.cc:57
void setEnabled(bool enabled)
Definition: PDBLogger.cc:208
void warn(std::string writeMe)
Definition: PDBLogger.cc:144
Definition: LogLevel.h:40
Definition: LogLevel.h:40
void trace(std::string writeMe)
Definition: PDBLogger.cc:117
LogLevel loglevel
Definition: PDBLogger.h:113
Definition: LogLevel.h:40
LogLevel
Definition: LogLevel.h:40