1/*
2 * Copyright (c) 2010-2017 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 <framework/core/resourcemanager.h>
27#include <framework/core/graphicalapplication.h>
28
29#ifdef FW_GRAPHICS
30#include <framework/platform/platformwindow.h>
31#include <framework/platform/platform.h>
32#include <framework/luaengine/luainterface.h>
33#endif
34
35Logger g_logger;
36
37void Logger::log(Fw::LogLevel level, const std::string& message)
38{
39 std::unique_lock<std::recursive_mutex> lock(m_mutex, std::try_to_lock);
40 if (!lock.owns_lock()) {
41 return;
42 }
43
44#ifdef NDEBUG
45 if(level == Fw::LogDebug)
46 return;
47#endif
48
49 static bool ignoreLogs = false;
50 if(ignoreLogs)
51 return;
52
53 const static std::string logPrefixes[] = { "", "", "WARNING: ", "ERROR: ", "FATAL ERROR: " };
54 std::string outmsg = logPrefixes[level] + message;
55#ifdef ANDROID
56 const static int logPriorities[] = { ANDROID_LOG_INFO, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL };
57 __android_log_print(logPriorities[level], "OTCLIENTV8", "%s", outmsg.c_str());
58#else
59 std::cout << outmsg << std::endl;
60
61 if(m_outFile.good()) {
62 m_outFile << outmsg << std::endl;
63 m_outFile.flush();
64 }
65#endif
66
67 std::size_t now = std::time(NULL);
68 m_logMessages.push_back(LogMessage(level, outmsg, now));
69 if(m_logMessages.size() > MAX_LOG_HISTORY)
70 m_logMessages.pop_front();
71
72 if(m_onLog) {
73 // schedule log callback, because this callback can run lua code that may affect the current state
74 g_dispatcher.addEvent([=] {
75 if(m_onLog)
76 m_onLog(level, outmsg, now);
77 });
78 }
79
80 if(level == Fw::LogFatal || (m_testingMode && level == Fw::LogError)) {
81#ifdef FW_GRAPHICS
82 if (!m_testingMode) {
83 g_window.displayFatalError(message);
84 }
85#endif
86 ignoreLogs = true;
87#ifdef _MSC_VER
88 ::quick_exit(-1);
89#else
90 exit(-1);
91#endif
92 }
93}
94
95void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string prettyFunction)
96{
97 std::lock_guard<std::recursive_mutex> lock(m_mutex);
98
99 prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('('));
100 if(prettyFunction.find_last_of(' ') != std::string::npos)
101 prettyFunction = prettyFunction.substr(prettyFunction.find_last_of(' ') + 1);
102
103
104 std::stringstream ss;
105 ss << message;
106
107 if(!prettyFunction.empty()) {
108 if(g_lua.isInCppCallback())
109 ss << g_lua.traceback("", 1);
110 ss << g_platform.traceback(prettyFunction, 1, 8);
111 }
112
113 log(level, ss.str());
114}
115
116void Logger::fireOldMessages()
117{
118 std::lock_guard<std::recursive_mutex> lock(m_mutex);
119
120 if(m_onLog) {
121 auto backup = m_logMessages;
122 for(const LogMessage& logMessage : backup) {
123 m_onLog(logMessage.level, logMessage.message, logMessage.when);
124 }
125 }
126}
127
128void Logger::setLogFile(const std::string& file)
129{
130#ifndef ANDROID
131 std::lock_guard<std::recursive_mutex> lock(m_mutex);
132 m_outFile.open(stdext::utf8_to_latin1(file.c_str()).c_str(), std::ios::in | std::ios::binary);
133 if (m_outFile.is_open()) {
134 m_outFile.seekg(0, m_outFile.end);
135 int length = m_outFile.tellg();
136 int offset = std::max<int>(0, length - 100000);
137 length -= offset;
138 m_outFile.seekg(offset, m_outFile.beg);
139 if (length > 0) {
140 m_lastLog.resize(length);
141 m_outFile.read(&m_lastLog[0], length);
142 m_lastLog.resize(m_outFile.gcount());
143 }
144 m_outFile.close();
145 }
146
147 m_outFile.open(stdext::utf8_to_latin1(file.c_str()).c_str(), std::ios::out | std::ios::app);
148 if(!m_outFile.is_open() || !m_outFile.good()) {
149 g_logger.error(stdext::format("Unable to save log to '%s'", file));
150 return;
151 }
152 m_outFile.flush();
153#endif
154}
155
156void fatalError(const char* error, const char* file, int line)
157{
158 g_logger.fatal(stdext::format("Fatal error: %s\nIn: %s:%i", error, file, line));
159}
160