1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include "[MathLog.h]"19 20 #include <cstring>21 #include <cstdarg>22 #include <stdio.h>23 24 #include "[Callstack.h]"25 26 #if defined(WIN32) || defined(WIN8PHONE)27 #include "../Math/InclWindows.h"28 #endif29 30 #if defined(ANDROID)31 32 #include <android/log.h>33 #endif34 35 #ifdef _MSC_VER36 #define snprintf _snprintf37 #endif38 39 [MATH_BEGIN_NAMESPACE]40 41 #if defined(__native_client__) || defined(NPAPI)42 43 extern void [PrintToConsole]([MathLogChannel] channel, const char *str); 44 45 #elif defined(ANDROID)46 47 void [PrintToConsole]([MathLogChannel] channel, const char *str)48 {49 const int capacity = 512;50 char text[capacity];51 if (channel == MathLogError)52 {53 strcpy(text, "Error: ");54 strncat(text, str, capacity-7);55 text[capacity-1] = 0;56 (void)__android_log_print(ANDROID_LOG_ERROR, "native-activity", text);57 }58 else if (channel == MathLogWarning)59 {60 strcpy(text, "Warning: ");61 strncat(text, str, capacity-9);62 text[capacity-1] = 0;63 (void)__android_log_print(ANDROID_LOG_WARN, "native-activity", text);64 }65 else66 (void)__android_log_print(ANDROID_LOG_INFO, "native-activity", str);67 }68 69 #elif defined(WIN8PHONE)70 71 void [PrintToConsole]([MathLogChannel] channel, const char *str)72 {73 if (channel == MathLogError)74 OutputDebugStringA("Error: ");75 else if (channel == MathLogWarning)76 OutputDebugStringA("Warning: ");77 OutputDebugStringA(str);78 OutputDebugStringA("\r\n");79 }80 81 #elif defined(WIN32) && !defined(WIN8RT)82 83 void [PrintToConsole]([MathLogChannel] channel, const char *str)84 {85 if (channel == MathLogError || channel == MathLogErrorNoCallstack)86 {87 SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY); 88 fprintf(stderr, "Error: %s\n", str);89 SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_INTENSITY); 90 if (channel != MathLogErrorNoCallstack)91 {92 std::string callstack = [GetCallstack](" ", "PrintToConsole");93 fprintf(stderr, "%s", callstack.c_str());94 }95 SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 96 }97 else if (channel == MathLogWarning || channel == MathLogWarningNoCallstack)98 {99 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); 100 printf("Warning: %s\n", str);101 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); 102 if (channel != MathLogWarningNoCallstack)103 {104 std::string callstack = [GetCallstack](" ", "PrintToConsole");105 printf("%s", callstack.c_str());106 }107 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 108 }109 else110 printf("%s\n", str);111 }112 113 #else114 115 void [PrintToConsole]([MathLogChannel] channel, const char *str)116 {117 #ifdef WIN8RT118 OutputDebugStringA(str);119 #endif120 if (channel == MathLogError || channel == MathLogErrorNoCallstack)121 {122 fprintf(stderr, "Error: %s\n", str);123 if (channel != MathLogErrorNoCallstack)124 {125 std::string callstack = [GetCallstack](" ", "PrintToConsole");126 fprintf(stderr, "%s", callstack.c_str());127 }128 }129 else if (channel == MathLogWarning || channel == MathLogWarningNoCallstack)130 {131 printf("Warning: %s\n", str);132 if (channel != MathLogWarningNoCallstack)133 {134 std::string callstack = [GetCallstack](" ", "PrintToConsole");135 printf("%s", callstack.c_str());136 }137 }138 else139 printf("%s\n", str);140 }141 142 #endif143 144 void [PrintToConsoleVariadic]([MathLogChannel] channel, const char *format, ...)145 {146 const int capacity = 2048;147 char str[capacity];148 149 va_list args;150 va_start(args, format);151 152 vsnprintf((char *)str, capacity, format, args);153 str[capacity-1] = 0; 154 [PrintToConsole](channel, str);155 }156 157 [MATH_END_NAMESPACE] Go back to previous page