ffead.server.doc
Logger.h
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.h
18  *
19  * Created on: Sep 5, 2009
20  * Author: sumeet
21  */
22 
23 #ifndef LOGGER_H_
24 #define LOGGER_H_
25 #include "PropFileReader.h"
26 #include "DateFormat.h"
27 #include "Mutex.h"
28 class Logger {
29 public:
30  static string LEVEL_ERROR;
31  static string LEVEL_DEBUG;
32  static string LEVEL_INFO;
33  static Logger getLogger(string className);
34  void info(string);
35  void debug(string);
36  void error(string);
37  static void init();
38  static void destroy();
39  static void init(string file);
40  static void init(string level,string mode,string file);
41  Logger();
42  virtual ~Logger();
43  template <typename T>
44  friend Logger& operator<< (Logger& logger, T msg)
45  {
46  logger.write(msg,"info",false);
47  return logger;
48  }
49  friend Logger& operator<< (Logger& logger, ostream& (*pf) (ostream&));
50 private:
51  Logger(string);
52  Logger(string,string,string);
53  string className;
54  static DateFormat* datFormat;
55  static string* level;
56  static string* mode;
57  static string* filepath;
58  static ofstream* out;
59  static Mutex* _theLogmutex;
60  void write(string msg,string mod,bool newline);
61  template <typename T>
62  void write(T tmsg, string mod,bool newline)
63  {
64  Date dat;
65  string te = datFormat->format(dat);
66  string msg = "[" + te + "] ("+this->className + ") <"+mod+"> :";
67  if(*mode=="FILE")
68  {
69  _theLogmutex->lock();
70  *out << msg << tmsg;
71  if(newline)
72  *out << endl;
73  else
74  *out << flush;
75  _theLogmutex->unlock();
76  }
77  else
78  {
79  _theLogmutex->lock();
80  cout << msg << tmsg;
81  if(newline)
82  cout << endl;
83  else
84  cout << flush;
85  _theLogmutex->unlock();
86  }
87  }
88  void write(ostream& (*pf) (ostream&), string mod);
89 };
90 #endif /* LOGGER_H_ */