1 // Copyright (C) 2003 Dolphin Project.
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
11
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
17
18 #include <string>
19
20 #include "ppsspp_config.h"
21
22 #include "Common.h"
23 #include "Common/Log.h"
24 #include "StringUtils.h"
25 #include "Common/Data/Encoding/Utf8.h"
26
27 #if PPSSPP_PLATFORM(ANDROID)
28 #include <android/log.h>
29 #elif PPSSPP_PLATFORM(WINDOWS)
30 #include "CommonWindows.h"
31 #endif
32
33 #define LOG_BUF_SIZE 2048
34
HandleAssert(const char * function,const char * file,int line,const char * expression,const char * format,...)35 bool HandleAssert(const char *function, const char *file, int line, const char *expression, const char* format, ...) {
36 // Read message and write it to the log
37 char text[LOG_BUF_SIZE];
38 const char *caption = "Critical";
39 va_list args;
40 va_start(args, format);
41 vsnprintf(text, sizeof(text), format, args);
42 va_end(args);
43
44 // Secondary formatting. Wonder if this can be combined into the vsnprintf somehow.
45 char formatted[LOG_BUF_SIZE + 128];
46 snprintf(formatted, sizeof(formatted), "(%s:%s:%d) %s: [%s] %s", file, function, line, caption, expression, text);
47
48 // Normal logging (will also log to Android log)
49 ERROR_LOG(SYSTEM, "%s", formatted);
50 // Also do a simple printf for good measure, in case logging of SYSTEM is disabled (should we disallow that?)
51 printf("%s\n", formatted);
52
53 #if defined(USING_WIN_UI)
54 int msgBoxStyle = MB_ICONINFORMATION | MB_YESNO;
55 std::wstring wtext = ConvertUTF8ToWString(formatted) + L"\n\nTry to continue?";
56 std::wstring wcaption = ConvertUTF8ToWString(caption);
57 OutputDebugString(wtext.c_str());
58 if (IDYES != MessageBox(0, wtext.c_str(), wcaption.c_str(), msgBoxStyle)) {
59 return false;
60 } else {
61 return true;
62 }
63 #elif PPSSPP_PLATFORM(ANDROID)
64 __android_log_assert(expression, "PPSSPP", "%s", formatted);
65 // Doesn't matter what we return here.
66 return false;
67 #else
68 OutputDebugStringUTF8(text);
69 return false;
70 #endif
71 }
72