blob: 66382bd559ee9b8e3eae6ca30baf9965ad58b62a [file] [log] [blame]
jat@google.com134be542009-08-03 15:30:11 +00001/*
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
27Debug::DebugStream& Debug::flush(Debug::DebugStream& dbg) {
28 return dbg;
29}
30
31void Debug::logFinish() {}
32
33void 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.
40Debug::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
51static char buf[DEBUG_BUF_SIZE + 3]; // room for CR NL Null
52static char *bufPtr = buf;
53#endif
54
55void 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
66void Debug::logStart(LogLevel level) {
67}
68
69void 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