FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
log4cplus.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Sep 30, 2010
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2010 Facebook, Inc
13 \**********************************************************/
14 
15 #include <string>
16 #include <boost/scoped_ptr.hpp>
17 
18 #include "FactoryBase.h"
19 #include "logging.h"
20 #include "utf8_tools.h"
21 
22 #include "precompiled_headers.h" // On windows, everything above this line in PCH
23 #include "log4cplus/config/defines.hxx"
24 #include "log4cplus/loggingmacros.h"
25 #include "log4cplus/logger.h"
26 #include "log4cplus/loglevel.h"
27 #include "log4cplus/layout.h"
28 #include "log4cplus/ndc.h"
29 #include "log4cplus/fileappender.h"
30 #include "log4cplus/win32debugappender.h"
31 #include "log4cplus/consoleappender.h"
32 #include "log4cplus/nullappender.h"
33 
34 namespace
35 {
36  bool logging_started = false;
37 }
38 
39 static log4cplus::LogLevel translate_logLevel(FB::Log::LogLevel ll);
40 
41 void FB::Log::initLogging()
42 {
43  if (logging_started)
44  return;
45 
46  bool addedAppender = false;
47 
48  log4cplus::Logger logger = log4cplus::Logger::getInstance(L"FireBreath");
49 
50  FB::Log::LogLevel ll = getFactoryInstance()->getLogLevel();
51  logger.setLogLevel(translate_logLevel(ll));
52 
53  FB::Log::LogMethodList mlist;
54  getFactoryInstance()->getLoggingMethods(mlist);
55 
56  for (FB::Log::LogMethodList::const_iterator it = mlist.begin(); it != mlist.end(); ++it) {
57  switch( it->first ) {
58  case FB::Log::LogMethod_Console: {
59 #ifdef FB_WIN
60  log4cplus::SharedAppenderPtr debugAppender(new log4cplus::Win32DebugAppender());
61  std::auto_ptr<log4cplus::Layout> layout2(new log4cplus::TTCCLayout());
62  debugAppender->setLayout(layout2);
63  logger.addAppender(debugAppender);
64  addedAppender = true;
65 #else
66  log4cplus::SharedAppenderPtr debugAppender(new log4cplus::ConsoleAppender());
67  std::auto_ptr<log4cplus::Layout> layout2(new log4cplus::TTCCLayout());
68  debugAppender->setLayout(layout2);
69  logger.addAppender(debugAppender);
70  addedAppender = true;
71 #endif
72  } break;
73  case FB::Log::LogMethod_File: {
74  log4cplus::SharedAppenderPtr fileAppender(new log4cplus::FileAppender(FB::utf8_to_wstring(it->second)));
75  std::auto_ptr<log4cplus::Layout> layout(new log4cplus::TTCCLayout());
76  fileAppender->setLayout(layout);
77  logger.addAppender(fileAppender);
78  addedAppender = true;
79  }
80  }
81  }
82 
83  // If there are no other appenders, add a NULL appender
84  if (!addedAppender) {
85  log4cplus::SharedAppenderPtr nullAppender(new log4cplus::NullAppender());
86  std::auto_ptr<log4cplus::Layout> layout3(new log4cplus::TTCCLayout());
87  nullAppender->setLayout(layout3);
88  logger.addAppender(nullAppender);
89  }
90 
91  logging_started = true;
92 }
93 
94 void FB::Log::stopLogging()
95 {
96  log4cplus::Logger logger = log4cplus::Logger::getInstance(L"FireBreath");
97  logger.shutdown();
98  logging_started = false;
99 }
100 
101 static log4cplus::LogLevel translate_logLevel(FB::Log::LogLevel ll){
102 
103  switch(ll) {
104  case(FB::Log::LogLevel_Trace) :
105  return log4cplus::TRACE_LOG_LEVEL;
106  case(FB::Log::LogLevel_Debug) :
107  return log4cplus::DEBUG_LOG_LEVEL;
108  case(FB::Log::LogLevel_Info) :
109  return log4cplus::INFO_LOG_LEVEL;
110  case(FB::Log::LogLevel_Warn) :
111  return log4cplus::WARN_LOG_LEVEL;
112  case(FB::Log::LogLevel_Error) :
113  return log4cplus::ERROR_LOG_LEVEL;
114  default:
115  return log4cplus::WARN_LOG_LEVEL;
116  }
117 }
118 
119 void FB::Log::trace(const std::string& src, const std::string& msg, const char *file, int line, const char *fn)
120 {
121  LOG4CPLUS_TRACE(log4cplus::Logger::getInstance(L"FireBreath"),
122  file << ":" << line << " - " << fn << " - " << FB::utf8_to_wstring(msg));
123 }
124 void FB::Log::debug(const std::string& src, const std::string& msg, const char *file, int line, const char *fn)
125 {
126  LOG4CPLUS_DEBUG(log4cplus::Logger::getInstance(L"FireBreath"),
127  file << ":" << line << " - " << fn << " - " << FB::utf8_to_wstring(msg));
128 }
129 void FB::Log::info(const std::string& src, const std::string& msg, const char *file, int line, const char *fn)
130 {
131  LOG4CPLUS_INFO(log4cplus::Logger::getInstance(L"FireBreath"),
132  file << ":" << line << " - " << fn << " - " << FB::utf8_to_wstring(msg));
133 }
134 void FB::Log::warn(const std::string& src, const std::string& msg, const char *file, int line, const char *fn)
135 {
136  LOG4CPLUS_WARN(log4cplus::Logger::getInstance(L"FireBreath"),
137  file << ":" << line << " - " << fn << " - " << FB::utf8_to_wstring(msg));
138 }
139 void FB::Log::error(const std::string& src, const std::string& msg, const char *file, int line, const char *fn)
140 {
141  LOG4CPLUS_ERROR(log4cplus::Logger::getInstance(L"FireBreath"),
142  file << ":" << line << " - " << fn << " - " << FB::utf8_to_wstring(msg));
143 }
144 void FB::Log::fatal(const std::string& src, const std::string& msg, const char *file, int line, const char *fn)
145 {
146  LOG4CPLUS_ERROR(log4cplus::Logger::getInstance(L"FireBreath"),
147  file << ":" << line << " - " << fn << " - " << FB::utf8_to_wstring(msg));
148 }
149 
std::wstring utf8_to_wstring(const std::string &src)
Accepts a UTF8-encoded std::string and returns a std::wstring.
Definition: utf8_tools.cpp:50