ffead.server.doc
DLogger.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  * DLogger.cpp
18  *
19  * Created on: Sep 5, 2009
20  * Author: sumeet
21  */
22 
23 #include "DLogger.h"
24 
25 // Global static pointer used to ensure a single instance of the class.
26 DLogger* DLogger::m_pInstance = NULL;
27 
28 DLogger* DLogger::getDLogger()
29 {
30  if(m_pInstance==NULL)
31  {
32  m_pInstance = new DLogger();
33  }
34  return m_pInstance;
35 }
36 
37 void DLogger::init()
38 {
39  if(m_pInstance==NULL)
40  {
41  m_pInstance = new DLogger();
42  }
43 }
44 
45 void DLogger::init(string file)
46 {
47  if(m_pInstance==NULL)
48  {
49  m_pInstance = new DLogger(file);
50  }
51 }
52 
53 void DLogger::init(string level,string mode,string file)
54 {
55  if(m_pInstance==NULL)
56  {
57  m_pInstance = new DLogger(level,mode,file);
58  }
59 }
60 
61 DLogger::DLogger()
62 {
63  PropFileReader pf;
64  level = "ERROR";
65  mode = "CONSOLE";
66  datFormat.setFormatspec("dd/mm/yyyy hh:mi:ss");
67 }
68 
69 DLogger::DLogger(string file)
70 {
71  PropFileReader pf;
72  propMap props = pf.getProperties(file);
73  if(props.size()==0)
74  {
75  level = "ERROR";
76  mode = "CONSOLE";
77  datFormat.setFormatspec("dd/mm/yyyy hh:mi:ss");
78  return;
79  }
80  level = props["LEVEL"];
81  mode = props["MODE"];
82  filepath = props["FILEPATH"];
83  if(mode=="FILE")
84  out.open(filepath.c_str(),ios::app | ios::binary);
85  datFormat.setFormatspec(props["DATEFMT"]);
86 }
87 DLogger::DLogger(string level,string mode,string filepath)
88 {
89  this->level = level;
90  this->mode = mode;
91  this->filepath = filepath;
92 }
93 
94 DLogger::~DLogger()
95 {
96  out.close();
97 }
98 
99 void DLogger::write(string msg,string mod)
100 {
101  Date dat;
102  string te = this->datFormat.format(dat);
103  if(mode=="FILE")
104  {
105  msg = "[" + te + "] <"+mod+"> :"+msg+"\n";
106  m_pInstance->p_mutex.lock();
107  m_pInstance->out.write(msg.c_str(),msg.length());
108  m_pInstance->out << flush;
109  m_pInstance->p_mutex.unlock();
110  }
111  else
112  {
113  msg = "[" + te + "] <"+mod+"> :"+msg+"\n";
114  m_pInstance->p_mutex.lock();
115  cout << msg << flush;
116  m_pInstance->p_mutex.unlock();
117  }
118 }
119 
120 void DLogger::info(string msg)
121 {
122  m_pInstance->write(msg,"info");
123 }
124 
125 void DLogger::debug(string msg)
126 {
127  m_pInstance->write(msg,"debug");
128 }
129 
130 void DLogger::error(string msg)
131 {
132  m_pInstance->write(msg,"error");
133 }