ffead.server.doc
Logger.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * Logger.cpp
18  *
19  * Created on: Sep 5, 2009
20  * Author: sumeet
21  */
22 
23 #include "Logger.h"
24 
25 // Global static pointer used to ensure a single instance of the class.
26 Mutex* Logger::_theLogmutex = NULL;
27 string* Logger::level = NULL;
28 string* Logger::mode = NULL;
29 string* Logger::filepath = NULL;
30 DateFormat* Logger::datFormat = NULL;
31 ofstream* Logger::out = NULL;
32 
33 string Logger::LEVEL_ERROR = "ERROR";
34 string Logger::LEVEL_DEBUG = "DEBUG";
35 string Logger::LEVEL_INFO = "INFO";
36 
37 Logger Logger::getLogger(string className)
38 {
39  init();
40  Logger logger(className);
41  return logger;
42 }
43 
44 void Logger::init()
45 {
46  if(!_theLogmutex)
47  {
48  _theLogmutex = new Mutex();
49  level = new string(LEVEL_ERROR);
50  mode = new string("CONSOLE");
51  datFormat = new DateFormat("dd/mm/yyyy hh:mi:ss");
52  }
53 }
54 
55 void Logger::init(string file)
56 {
57  if(!_theLogmutex)
58  {
59  _theLogmutex = new Mutex();
60 
61  PropFileReader pf;
62  propMap props = pf.getProperties(file);
63  if(props.size()==0)
64  {
65  level = new string(LEVEL_ERROR);
66  mode = new string("CONSOLE");
67  datFormat = new DateFormat("dd/mm/yyyy hh:mi:ss");
68  return;
69  }
70  level = new string(props["LEVEL"]);
71  mode = new string(props["MODE"]);
72  filepath = new string(props["FILEPATH"]);
73  datFormat = new DateFormat(props["DATEFMT"]);
74  if(*mode=="FILE")
75  {
76  out = new ofstream();
77  out->open(filepath->c_str(),ios::app | ios::binary);
78  }
79  }
80 }
81 
82 void Logger::init(string llevel,string lmode,string lfilepath)
83 {
84  if(!_theLogmutex)
85  {
86  _theLogmutex = new Mutex();
87 
88  level = new string(llevel);
89  mode = new string(lmode);
90  filepath = new string(lfilepath);
91  datFormat = new DateFormat("dd/mm/yyyy hh:mi:ss");
92  if(*mode=="FILE")
93  {
94  out = new ofstream();
95  out->open(filepath->c_str(),ios::app | ios::binary);
96  }
97  }
98 }
99 
100 Logger::Logger()
101 {}
102 
103 Logger::Logger(string className)
104 {
105  this->className = className;
106 }
107 
108 Logger::~Logger()
109 {
110 
111 }
112 
113 void Logger::write(string msg,string mod,bool newline)
114 {
115  Date dat;
116  string te = datFormat->format(dat);
117  msg = "[" + te + "] ("+this->className + ") <"+mod+"> :"+msg+(newline?"\n":"");
118  if(*mode=="FILE")
119  {
120  _theLogmutex->lock();
121  out->write(msg.c_str(),msg.length());
122  *out << flush;
123  _theLogmutex->unlock();
124  }
125  else
126  {
127  _theLogmutex->lock();
128  cout << msg << flush;
129  _theLogmutex->unlock();
130  }
131 }
132 
133 void Logger::write(ostream& (*pf) (ostream&), string mod)
134 {
135  if(*mode=="FILE")
136  {
137  _theLogmutex->lock();
138  *out << pf;
139  _theLogmutex->unlock();
140  }
141  else
142  {
143  _theLogmutex->lock();
144  cout << pf;
145  _theLogmutex->unlock();
146  }
147 }
148 
149 void Logger::info(string msg)
150 {
151  if(*level!=LEVEL_DEBUG)
152  {
153  write(msg,"info",true);
154  }
155 }
156 
157 void Logger::debug(string msg)
158 {
159  if(*level==LEVEL_DEBUG)
160  {
161  write(msg,"debug",true);
162  }
163 }
164 
165 void Logger::error(string msg)
166 {
167  write(msg,"error",true);
168 }
169 
170 Logger& operator<< (Logger& logger, ostream& (*pf) (ostream&))
171 {
172  logger.write(pf,"info");
173  return logger;
174 }
175 
176 void Logger::destroy()
177 {
178  if(_theLogmutex)
179  {
180  delete _theLogmutex;
181  delete level;
182  delete mode;
183  delete datFormat;
184  if(filepath!=NULL)delete filepath;
185  if(out!=NULL)delete out;
186  }
187 }