1 /* Copyright Jukka Jyl�nki
2
3    Licensed under the Apache License, Version 2.0 (the "License");
4    you may not use this file except in compliance with the License.
5    You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9    Unless required by applicable law or agreed to in writing, software
10    distributed under the License is distributed on an "AS IS" BASIS,
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12    See the License for the specific language governing permissions and
13    limitations under the License. */
14
15 /** @file Log.cpp
16         @author Jukka Jyl�nki
17         @brief The LOG and LOGUSER macros. Provides an unified mechanism for logging. */
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 #endif
29
30 #if defined(ANDROID)
31 /// This will require you to pass '-llog' on the command line to link against the Android logging libraries.
32 #include <android/log.h>
33 #endif
34
35 #ifdef _MSC_VER
36 #define snprintf _snprintf
37 #endif
38
39 MATH_BEGIN_NAMESPACE
40
41 #if defined(__native_client__) || defined(NPAPI)
42
43 extern void PrintToConsole(MathLogChannel channel, const char *str); ///< Implemented in gfxapi to route access to application instance.
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         else
66                 (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); // Red
88                 fprintf(stderr, "Error: %s\n", str);
89                 SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_INTENSITY); // Dark gray
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); // Light gray/default
96         }
97         else if (channel == MathLogWarning || channel == MathLogWarningNoCallstack)
98         {
99                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Yellow
100                 printf("Warning: %s\n", str);
101                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); // Dark gray
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); // Light gray/default
108         }
109         else
110                 printf("%s\n", str);
111 }
112
113 #else
114
115 void PrintToConsole(MathLogChannel channel, const char *str)
116 {
117 #ifdef WIN8RT
118         OutputDebugStringA(str);
119 #endif
120         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         else
139                 printf("%s\n", str);
140 }
141
142 #endif
143
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; // We only support logging a fixed-length string so don't care if we fail/truncate, just make sure we zero-terminate so there won't be any issues.
154         PrintToConsole(channel, str);
155 }
156
157 MATH_END_NAMESPACE

Go back to previous page