Otclient  14/8/2020
logger.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "logger.h"
24 #include "eventdispatcher.h"
25 
26 //#include <boost/regex.hpp>
28 
29 #ifdef FW_GRAPHICS
33 #endif
34 
36 
37 void Logger::log(Fw::LogLevel level, const std::string& message)
38 {
39  std::lock_guard<std::recursive_mutex> lock(m_mutex);
40 
41 #ifdef NDEBUG
42  if(level == Fw::LogDebug)
43  return;
44 #endif
45 
46  static bool ignoreLogs = false;
47  if(ignoreLogs)
48  return;
49 
50  const static std::string logPrefixes[] = { "", "", "WARNING: ", "ERROR: ", "FATAL ERROR: " };
51 
52  std::string outmsg = logPrefixes[level] + message;
53 
54  /*
55 #if !defined(NDEBUG) && !defined(WIN32)
56  // replace paths for improved debug with vim
57  std::stringstream tmp;
58  boost::smatch m;
59  boost::regex e ("/[^ :]+");
60  while(boost::regex_search(outmsg,m,e)) {
61  tmp << m.prefix().str();
62  tmp << g_resources.getRealDir(m.str()) << m.str();
63  outmsg = m.suffix().str();
64  }
65  if(!tmp.str().empty())
66  outmsg = tmp.str();
67 #endif
68  */
69 
70  std::cout << outmsg << std::endl;
71 
72  if(m_outFile.good()) {
73  m_outFile << outmsg << std::endl;
74  m_outFile.flush();
75  }
76 
77  std::size_t now = std::time(nullptr);
78  m_logMessages.emplace_back(level, outmsg, now);
79  if(m_logMessages.size() > MAX_LOG_HISTORY)
80  m_logMessages.pop_front();
81 
82  if(m_onLog) {
83  // schedule log callback, because this callback can run lua code that may affect the current state
85  if(m_onLog)
86  m_onLog(level, outmsg, now);
87  });
88  }
89 
90  if(level == Fw::LogFatal) {
91 #ifdef FW_GRAPHICS
92  g_window.displayFatalError(message);
93 #endif
94  ignoreLogs = true;
95  exit(-1);
96  }
97 }
98 
99 void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string prettyFunction)
100 {
101  std::lock_guard<std::recursive_mutex> lock(m_mutex);
102 
103  prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('('));
104  if(prettyFunction.find_last_of(' ') != std::string::npos)
105  prettyFunction = prettyFunction.substr(prettyFunction.find_last_of(' ') + 1);
106 
107 
108  std::stringstream ss;
109  ss << message;
110 
111  if(!prettyFunction.empty()) {
112  if(g_lua.isInCppCallback())
113  ss << g_lua.traceback("", 1);
114  ss << g_platform.traceback(prettyFunction, 1, 8);
115  }
116 
117  log(level, ss.str());
118 }
119 
121 {
122  std::lock_guard<std::recursive_mutex> lock(m_mutex);
123 
124  if(m_onLog) {
125  auto backup = m_logMessages;
126  for(const LogMessage& logMessage : backup) {
127  m_onLog(logMessage.level, logMessage.message, logMessage.when);
128  }
129  }
130 }
131 
132 void Logger::setLogFile(const std::string& file)
133 {
134  std::lock_guard<std::recursive_mutex> lock(m_mutex);
135 
136  m_outFile.open(stdext::utf8_to_latin1(file).c_str(), std::ios::out | std::ios::app);
137  if(!m_outFile.is_open() || !m_outFile.good()) {
138  g_logger.error(stdext::format("Unable to save log to '%s'", file));
139  return;
140  }
141  m_outFile.flush();
142 }
eventdispatcher.h
platformwindow.h
logger.h
Fw::LogLevel
LogLevel
Definition: const.h:178
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
resourcemanager.h
luainterface.h
Logger::error
void error(const std::string &what)
Definition: logger.h:54
stdext::format
std::string format()
Definition: format.h:82
stdext::time
ticks_t time()
Definition: time.cpp:33
LuaInterface::traceback
std::string traceback(const std::string &errorMessage="", int level=0)
Definition: luainterface.cpp:380
Fw::LogDebug
@ LogDebug
Definition: const.h:179
Platform::traceback
std::string traceback(const std::string &where, int level=1, int maxDepth=32)
Definition: unixplatform.cpp:172
g_window
PlatformWindow & g_window
Definition: platformwindow.cpp:37
Logger::log
void log(Fw::LogLevel level, const std::string &message)
Definition: logger.cpp:37
g_logger
Logger g_logger
Definition: logger.cpp:35
g_lua
LuaInterface g_lua
Definition: luainterface.cpp:31
EventDispatcher::addEvent
EventPtr addEvent(const std::function< void()> &callback, bool pushFront=false)
Definition: eventdispatcher.cpp:104
Logger::logFunc
void logFunc(Fw::LogLevel level, const std::string &message, std::string prettyFunction)
Definition: logger.cpp:99
platform.h
stdext::utf8_to_latin1
std::string utf8_to_latin1(const std::string &src)
Definition: string.cpp:146
LuaInterface::isInCppCallback
bool isInCppCallback()
Definition: luainterface.h:200
PlatformWindow::displayFatalError
virtual void displayFatalError(const std::string &message)
Definition: platformwindow.h:54
Logger
Definition: logger.h:39
Logger::fireOldMessages
void fireOldMessages()
Definition: logger.cpp:120
Logger::setLogFile
void setLogFile(const std::string &file)
Definition: logger.cpp:132
Fw::LogFatal
@ LogFatal
Definition: const.h:183
LogMessage
Definition: logger.h:31
g_platform
Platform g_platform
Definition: platform.cpp:25