jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | #include <cstdio> |
| 18 | |
| 19 | #ifdef _WINDOWS |
| 20 | #include <windows.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "Debug.h" |
| 24 | |
| 25 | #ifdef GWT_DEBUGDISABLE |
| 26 | // Dummy implementations for when debugging has been disabled |
| 27 | Debug::DebugStream& Debug::flush(Debug::DebugStream& dbg) { |
| 28 | return dbg; |
| 29 | } |
| 30 | |
| 31 | void Debug::logFinish() {} |
| 32 | |
| 33 | void Debug::logString(const char* str) {} |
| 34 | |
| 35 | #else |
| 36 | // Warning: this function is inlined in the manipulator output operator in DebugStream. |
| 37 | // It only remains here because some compiler/linker combinations such as MSVC will |
| 38 | // give unresolved symbol errors if it isn't -- GCC, for example, will completely remove |
| 39 | // all traces of this method. |
| 40 | Debug::DebugStream& Debug::flush(Debug::DebugStream& dbg) { |
| 41 | Debug::logFinish(); |
| 42 | return dbg; |
| 43 | } |
| 44 | |
| 45 | // These methods are implemented in an Objective-C++ file on OSX |
| 46 | #if !defined(__APPLE_CC__) || defined(__mac) |
| 47 | |
| 48 | #ifdef _WINDOWS |
| 49 | #define DEBUG_BUF_SIZE 2048 |
| 50 | |
| 51 | static char buf[DEBUG_BUF_SIZE + 3]; // room for CR NL Null |
| 52 | static char *bufPtr = buf; |
| 53 | #endif |
| 54 | |
| 55 | void Debug::logFinish() { |
| 56 | #ifdef _WINDOWS |
| 57 | logString("\r\n"); |
| 58 | ::OutputDebugStringA(buf); |
| 59 | bufPtr = buf; |
| 60 | #else |
| 61 | putchar('\n'); |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | // logStart may be called multiple times per logFinish |
| 66 | void Debug::logStart(LogLevel level) { |
| 67 | } |
| 68 | |
| 69 | void Debug::logString(const char* str) { |
| 70 | #ifdef _WINDOWS |
| 71 | size_t len = strlen(str); |
| 72 | size_t buflen = DEBUG_BUF_SIZE - (bufPtr - buf); |
| 73 | if (len >= buflen) { |
| 74 | len = buflen - 1; |
| 75 | } |
| 76 | strncpy_s(bufPtr, buflen, str, len); |
| 77 | bufPtr += len; |
| 78 | #else |
| 79 | printf("%s", str); |
| 80 | #endif |
| 81 | } |
| 82 | #endif |
| 83 | #endif |
| 84 | |