1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Bas Schouten <bschouten@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef MOZILLA_GFX_LOGGING_H_
39 : #define MOZILLA_GFX_LOGGING_H_
40 :
41 : #include <string>
42 : #include <sstream>
43 : #include <stdio.h>
44 :
45 : #include "Point.h"
46 : #include "Matrix.h"
47 :
48 : #ifdef WIN32
49 : #include <windows.h>
50 : #endif
51 :
52 : #ifdef PR_LOGGING
53 : #include <prlog.h>
54 :
55 : extern PRLogModuleInfo *sGFX2DLog;
56 : #endif
57 :
58 : namespace mozilla {
59 : namespace gfx {
60 :
61 : const int LOG_DEBUG = 1;
62 : const int LOG_WARNING = 2;
63 :
64 : #ifdef PR_LOGGING
65 :
66 : inline PRLogModuleLevel PRLogLevelForLevel(int aLevel) {
67 : switch (aLevel) {
68 : case LOG_DEBUG:
69 : return PR_LOG_DEBUG;
70 : case LOG_WARNING:
71 : return PR_LOG_WARNING;
72 : }
73 : return PR_LOG_DEBUG;
74 : }
75 :
76 : #endif
77 :
78 : extern int sGfxLogLevel;
79 :
80 0 : static void OutputMessage(const std::string &aString, int aLevel) {
81 : #if defined(WIN32) && !defined(PR_LOGGING)
82 : if (aLevel >= sGfxLogLevel) {
83 : ::OutputDebugStringA(aString.c_str());
84 : }
85 : #elif defined(PR_LOGGING)
86 : if (PR_LOG_TEST(sGFX2DLog, PRLogLevelForLevel(aLevel))) {
87 : PR_LogPrint(aString.c_str());
88 : }
89 : #else
90 0 : if (aLevel >= sGfxLogLevel) {
91 0 : printf("%s", aString.c_str());
92 : }
93 : #endif
94 0 : }
95 :
96 : class NoLog
97 : {
98 : public:
99 : NoLog() {}
100 : ~NoLog() {}
101 :
102 : template<typename T>
103 : NoLog &operator <<(const T &aLogText) { return *this; }
104 : };
105 :
106 : template<int L>
107 : class Log
108 : {
109 : public:
110 0 : Log() {}
111 0 : ~Log() { mMessage << '\n'; WriteLog(mMessage.str()); }
112 :
113 0 : Log &operator <<(const std::string &aLogText) { mMessage << aLogText; return *this; }
114 0 : Log &operator <<(unsigned int aInt) { mMessage << aInt; return *this; }
115 : Log &operator <<(const Size &aSize)
116 : { mMessage << "(" << aSize.width << "x" << aSize.height << ")"; return *this; }
117 0 : Log &operator <<(const IntSize &aSize)
118 0 : { mMessage << "(" << aSize.width << "x" << aSize.height << ")"; return *this; }
119 : Log &operator<<(const Matrix& aMatrix)
120 : { mMessage << "[ " << aMatrix._11 << " " << aMatrix._12 << " ; " << aMatrix._21 << " " << aMatrix._22 << " ; " << aMatrix._31 << " " << aMatrix._32 << " ]"; return *this; }
121 :
122 :
123 : private:
124 :
125 0 : void WriteLog(const std::string &aString) {
126 0 : OutputMessage(aString, L);
127 0 : }
128 :
129 : std::stringstream mMessage;
130 : };
131 :
132 : typedef Log<LOG_DEBUG> DebugLog;
133 : typedef Log<LOG_WARNING> WarningLog;
134 :
135 : #ifdef GFX_LOG_DEBUG
136 : #define gfxDebug DebugLog
137 : #else
138 : #define gfxDebug if (1) ; else NoLog
139 : #endif
140 : #ifdef GFX_LOG_WARNING
141 : #define gfxWarning WarningLog
142 : #else
143 : #define gfxWarning if (1) ; else NoLog
144 : #endif
145 :
146 : }
147 : }
148 :
149 : #endif /* MOZILLA_GFX_LOGGING_H_ */
|