Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsLogWriter.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsLogWriter.cpp
4  *
5  * Description: Writes log data to file using atomic operation
6  * This class is a singleton and is Thread Safe
7  *
8  * Created: 02/16/12 10:13:45
9  *
10  * Author: Benoit Daccache, ben.daccache@gmail.com
11  *
12  */
13 
14 #include "WsLogWriter.h"
15 #include <json/json.h>
16 #include <json/reader.h>
17 #include <json/writer.h>
18 #include <json/value.h>
19 #include <Include/WsGlobalConfig.h>
21 
22 using namespace std;
23 
25 
27 {
28  if (m_instance == 0)
29  m_instance = new WsLogWriter();
30  return m_instance;
31 }
32 
34 {
35  pthread_mutex_init(&m_mutex, NULL);
36  readConf();
37  if (m_path != GlobalConfig::StdOut && m_path != GlobalConfig::StdErr)
38  m_logfile.open (m_path.c_str(), ios::app);
39  m_instance = 0;
40 }
41 
43 {
44  pthread_mutex_destroy(&m_mutex);
45  if (m_path != GlobalConfig::StdOut && m_path != GlobalConfig::StdErr)
46  m_logfile.close();
47 }
48 
49 string WsLogWriter::toString(int level)
50 {
51  switch (level) {
52  case 0:
53  return "debug";
54  case 1:
55  return "info";
56  case 2:
57  return "warn";
58  case 3:
59  return "error";
60  default:
61  return "unknown";
62  }
63 }
64 void WsLogWriter::write(const int& level, const string& message)
65 {
66  if (m_logLevel <= level) {
67  if (m_path == GlobalConfig::StdOut) {
68  pthread_mutex_lock(&m_mutex);
69  string msg = message;
70  replace(msg.begin(), msg.end(), '\n', ' ');
71  cout << "[";
72  cout << microsec_clock::local_time();
73  cout << "] ";
74  cout << (int)getpid() << " ";
75  cout << "[" << toString(level) << "] ";
76  cout << msg << endl;
77  pthread_mutex_unlock(&m_mutex);
78  } else if (m_path == GlobalConfig::StdErr) {
79  pthread_mutex_lock(&m_mutex);
80  string msg = message;
81  replace(msg.begin(), msg.end(), '\n', ' ');
82  cerr << "[";
83  cerr << microsec_clock::local_time();
84  cerr << "] ";
85  cerr << (int)getpid() << " ";
86  cerr << "[" << toString(level) << "] ";
87  cerr << msg << endl;
88  pthread_mutex_unlock(&m_mutex);
89  } else {
90  pthread_mutex_lock(&m_mutex);
91  string msg = message;
92  replace(msg.begin(), msg.end(), '\n', ' ');
93  m_logfile << "[";
94  m_logfile << microsec_clock::local_time();
95  m_logfile << "] ";
96  m_logfile << (int)getpid() << " ";
97  m_logfile << "[" << toString(level) << "] ";
98  m_logfile << msg << endl;
99  pthread_mutex_unlock(&m_mutex);
100  }
101  }
102 }
103 
105 {
106  m_path = WsGlobalProperties::instance()->get("global", "log_path", "/dev/stdout");
107  string level = WsGlobalProperties::instance()->get("global", "log_level", "info");
108  if (level == "debug")
109  m_logLevel = 0;
110  else if (level == "info")
111  m_logLevel = 1;
112  else if (level == "warning")
113  m_logLevel = 2;
114  else if (level == "error")
115  m_logLevel = 4;
116  else
117  try {
118  m_logLevel = boost::lexical_cast<int>("info");
119  } catch (boost::bad_lexical_cast&) {
120  m_logLevel = 0;
121  }
122 }
123 
Global properties.
const std::string StdErr
static WsLogWriter * m_instance
Definition: WsLogWriter.h:67
void write(const int &level, const std::string &message)
writes the message to the log file
Definition: WsLogWriter.cpp:64
WsLogWriter()
Constructor.
Definition: WsLogWriter.cpp:33
const std::string StdOut
void readConf()
reads the conf needed to know log path and verbose level
LogWriter, writes logs to file or screen.
Definition: WsLogWriter.h:30
std::string get(const std::string &section, const std::string &id, const std::string &def)
static WsLogWriter * instance()
returns the instance of the WsLogWriter class. If no instance is existing, a new instance will be ret...
Definition: WsLogWriter.cpp:26
static WsGlobalProperties * instance()
std::string toString(int level)
Converts the log level to string.
Definition: WsLogWriter.cpp:49