1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef MOZILLA_GFX_LOGGING_H_
7 #define MOZILLA_GFX_LOGGING_H_
8 
9 #include <string>
10 #include <sstream>
11 #include <stdio.h>
12 #include <vector>
13 
14 #ifdef MOZ_LOGGING
15 #include "mozilla/Logging.h"
16 #endif
17 #include "mozilla/Tuple.h"
18 
19 #if defined(MOZ_WIDGET_ANDROID)
20 #include "nsDebug.h"
21 #endif
22 #include "Point.h"
23 #include "BaseRect.h"
24 #include "Matrix.h"
25 #include "LoggingConstants.h"
26 
27 #if defined(MOZ_LOGGING)
28 extern GFX2D_API mozilla::LogModule* GetGFX2DLog();
29 #endif
30 
31 namespace mozilla {
32 namespace gfx {
33 
34 #if defined(MOZ_LOGGING)
PRLogLevelForLevel(int aLevel)35 inline mozilla::LogLevel PRLogLevelForLevel(int aLevel) {
36   switch (aLevel) {
37   case LOG_CRITICAL:
38     return LogLevel::Error;
39   case LOG_WARNING:
40     return LogLevel::Warning;
41   case LOG_DEBUG:
42     return LogLevel::Debug;
43   case LOG_DEBUG_PRLOG:
44     return LogLevel::Debug;
45   case LOG_EVERYTHING:
46     return LogLevel::Error;
47   }
48   return LogLevel::Debug;
49 }
50 #endif
51 
52 class LoggingPrefs
53 {
54 public:
55   // Used to choose the level of logging we get.  The higher the number,
56   // the more logging we get.  Value of zero will give you no logging,
57   // 1 just errors, 2 adds warnings and 3 or 4 add debug logging.
58   // In addition to setting the value to 4, you will need to set the
59   // environment variable MOZ_LOG to gfx:4. See mozilla/Logging.h for details.
60   static int32_t sGfxLogLevel;
61 };
62 
63 /// Graphics logging is available in both debug and release builds and is
64 /// controlled with a gfx.logging.level preference. If not set, the default
65 /// for the preference is 5 in the debug builds, 1 in the release builds.
66 ///
67 /// gfxDebug only works in the debug builds, and is used for information
68 /// level messages, helping with debugging.  In addition to only working
69 /// in the debug builds, the value of the above preference of 3 or higher
70 /// is required.
71 ///
72 /// gfxWarning messages are available in both debug and release builds,
73 /// on by default in the debug builds, and off by default in the release builds.
74 /// Setting the preference gfx.logging.level to a value of 2 or higher will
75 /// show the warnings.
76 ///
77 /// gfxCriticalError is available in debug and release builds by default.
78 /// It is only unavailable if gfx.logging.level is set to 0 (or less.)
79 /// It outputs the message to stderr or equivalent, like gfxWarning.
80 /// In the event of a crash, the crash report is annotated with first and
81 /// the last few of these errors, under the key GraphicsCriticalError.
82 /// The total number of errors stored in the crash report is controlled
83 /// by preference gfx.logging.crash.length.
84 ///
85 /// On platforms that support MOZ_LOGGING, the story is slightly more involved.
86 /// In that case, unless gfx.logging.level is set to 4 or higher, the output
87 /// is further controlled by the "gfx2d" logging module.  However, in the case
88 /// where such module would disable the output, in all but gfxDebug cases,
89 /// we will still send a printf.
90 
91 // The range is due to the values set in Histograms.json
92 enum class LogReason : int {
93   MustBeMoreThanThis = -1,
94   // Start.  Do not insert, always add at end.  If you remove items,
95   // make sure the other items retain their values.
96   D3D11InvalidCallDeviceRemoved = 0,
97   D3D11InvalidCall,
98   D3DLockTimeout,
99   D3D10FinalizeFrame,
100   D3D11FinalizeFrame,
101   D3D10SyncLock,
102   D3D11SyncLock,
103   D2D1NoWriteMap,
104   JobStatusError,
105   FilterInputError,
106   FilterInputData, // 10
107   FilterInputRect,
108   FilterInputSet,
109   FilterInputFormat,
110   FilterNodeD2D1Target,
111   FilterNodeD2D1Backend,
112   SourceSurfaceIncompatible,
113   GlyphAllocFailedCairo,
114   GlyphAllocFailedCG,
115   InvalidRect,
116   CannotDraw3D, // 20
117   IncompatibleBasicTexturedEffect,
118   InvalidFont,
119   PAllocTextureBackendMismatch,
120   GetFontFileDataFailed,
121   MessageChannelCloseFailure,
122   MessageChannelInvalidHandle,
123   TextureAliveAfterShutdown,
124   InvalidContext,
125   InvalidCommandList,
126   AsyncTransactionTimeout, // 30
127   TextureCreation,
128   InvalidCacheSurface,
129   AlphaWithBasicClient,
130   UnbalancedClipStack,
131   ProcessingError,
132   NativeFontResourceNotFound,
133   // End
134   MustBeLessThanThis = 101,
135 };
136 
137 struct BasicLogger
138 {
139   // For efficiency, this method exists and copies the logic of the
140   // OutputMessage below.  If making any changes here, also make it
141   // in the appropriate places in that method.
ShouldOutputMessageBasicLogger142   static bool ShouldOutputMessage(int aLevel) {
143     if (LoggingPrefs::sGfxLogLevel >= aLevel) {
144 #if defined(MOZ_WIDGET_ANDROID)
145       return true;
146 #else
147 #if defined(MOZ_LOGGING)
148       if (MOZ_LOG_TEST(GetGFX2DLog(), PRLogLevelForLevel(aLevel))) {
149         return true;
150       } else
151 #endif
152       if ((LoggingPrefs::sGfxLogLevel >= LOG_DEBUG_PRLOG) ||
153                  (aLevel < LOG_DEBUG)) {
154         return true;
155       }
156 #endif
157     }
158     return false;
159   }
160 
161   // Only for really critical errors.
CrashActionBasicLogger162   static void CrashAction(LogReason aReason) {}
163 
OutputMessageBasicLogger164   static void OutputMessage(const std::string &aString,
165                             int aLevel,
166                             bool aNoNewline) {
167     // This behavior (the higher the preference, the more we log)
168     // is consistent with what prlog does in general.  Note that if prlog
169     // is in the build, but disabled, we will printf if the preferences
170     // requires us to log something (see sGfxLogLevel for the special
171     // treatment of LOG_DEBUG and LOG_DEBUG_PRLOG)
172     //
173     // If making any logic changes to this method, you should probably
174     // make the corresponding change in the ShouldOutputMessage method
175     // above.
176     if (LoggingPrefs::sGfxLogLevel >= aLevel) {
177 #if defined(MOZ_WIDGET_ANDROID)
178       printf_stderr("%s%s", aString.c_str(), aNoNewline ? "" : "\n");
179 #else
180 #if defined(MOZ_LOGGING)
181       if (MOZ_LOG_TEST(GetGFX2DLog(), PRLogLevelForLevel(aLevel))) {
182         PR_LogPrint("%s%s", aString.c_str(), aNoNewline ? "" : "\n");
183       } else
184 #endif
185       if ((LoggingPrefs::sGfxLogLevel >= LOG_DEBUG_PRLOG) ||
186                  (aLevel < LOG_DEBUG)) {
187         printf("%s%s", aString.c_str(), aNoNewline ? "" : "\n");
188       }
189 #endif
190     }
191   }
192 };
193 
194 struct CriticalLogger {
195   static void OutputMessage(const std::string &aString, int aLevel, bool aNoNewline);
196   static void CrashAction(LogReason aReason);
197 };
198 
199 // The int is the index of the Log call; if the number of logs exceeds some preset
200 // capacity we may not get all of them, so the indices help figure out which
201 // ones we did save.  The double is expected to be the "TimeDuration",
202 // time in seconds since the process creation.
203 typedef mozilla::Tuple<int32_t,std::string,double> LoggingRecordEntry;
204 
205 // Implement this interface and init the Factory with an instance to
206 // forward critical logs.
207 typedef std::vector<LoggingRecordEntry> LoggingRecord;
208 class LogForwarder {
209 public:
~LogForwarder()210   virtual ~LogForwarder() {}
211   virtual void Log(const std::string &aString) = 0;
212   virtual void CrashAction(LogReason aReason) = 0;
213   virtual bool UpdateStringsVector(const std::string& aString) = 0;
214 
215   // Provide a copy of the logs to the caller.
216   virtual LoggingRecord LoggingRecordCopy() = 0;
217 };
218 
219 class NoLog
220 {
221 public:
NoLog()222   NoLog() {}
~NoLog()223   ~NoLog() {}
224 
225   // No-op
NoLog(const NoLog &)226   MOZ_IMPLICIT NoLog(const NoLog&) {}
227 
228   template<typename T>
229   NoLog &operator <<(const T &aLogText) { return *this; }
230 };
231 
232 enum class LogOptions : int {
233   NoNewline = 0x01,
234   AutoPrefix = 0x02,
235   AssertOnCall = 0x04,
236   CrashAction = 0x08,
237 };
238 
239 template<typename T>
240 struct Hexa {
HexaHexa241   explicit Hexa(T aVal) : mVal(aVal) {}
242   T mVal;
243 };
244 template<typename T>
hexa(T val)245 Hexa<T> hexa(T val) { return Hexa<T>(val); }
246 
247 template<int L, typename Logger = BasicLogger>
248 class Log
249 {
250 public:
251   // The default is to have the prefix, have the new line, and for critical
252   // logs assert on each call.
253   static int DefaultOptions(bool aWithAssert = true) {
254     return (int(LogOptions::AutoPrefix) |
255             (aWithAssert ? int(LogOptions::AssertOnCall) : 0));
256   }
257 
258   // Note that we're calling BasicLogger::ShouldOutputMessage, rather than
259   // Logger::ShouldOutputMessage.  Since we currently don't have a different
260   // version of that method for different loggers, this is OK. Once we do,
261   // change BasicLogger::ShouldOutputMessage to Logger::ShouldOutputMessage.
262   explicit Log(int aOptions = Log::DefaultOptions(L == LOG_CRITICAL),
263                LogReason aReason = LogReason::MustBeMoreThanThis)
264   : mOptions(0)
265   , mLogIt(false)
266   {
267     Init(aOptions, BasicLogger::ShouldOutputMessage(L), aReason);
268   }
269 
~Log()270   ~Log() {
271     Flush();
272   }
273 
Flush()274   void Flush() {
275     if (MOZ_LIKELY(!LogIt())) return;
276 
277     std::string str = mMessage.str();
278     if (!str.empty()) {
279       WriteLog(str);
280     }
281     mMessage.str("");
282   }
283 
284   Log &operator <<(char aChar) {
285     if (MOZ_UNLIKELY(LogIt())) {
286       mMessage << aChar;
287     }
288     return *this;
289   }
290   Log &operator <<(const std::string &aLogText) {
291     if (MOZ_UNLIKELY(LogIt())) {
292       mMessage << aLogText;
293     }
294     return *this;
295   }
296   Log &operator <<(const char aStr[]) {
297     if (MOZ_UNLIKELY(LogIt())) {
298       mMessage << static_cast<const char*>(aStr);
299     }
300     return *this;
301   }
302   Log &operator <<(bool aBool) {
303     if (MOZ_UNLIKELY(LogIt())) {
304       mMessage << (aBool ? "true" : "false");
305     }
306     return *this;
307   }
308   Log &operator <<(int aInt) {
309     if (MOZ_UNLIKELY(LogIt())) {
310       mMessage << aInt;
311     }
312     return *this;
313   }
314   Log &operator <<(unsigned int aInt) {
315     if (MOZ_UNLIKELY(LogIt())) {
316       mMessage << aInt;
317     }
318     return *this;
319   }
320   Log &operator <<(long aLong) {
321     if (MOZ_UNLIKELY(LogIt())) {
322       mMessage << aLong;
323     }
324     return *this;
325   }
326   Log &operator <<(unsigned long aLong) {
327     if (MOZ_UNLIKELY(LogIt())) {
328       mMessage << aLong;
329     }
330     return *this;
331   }
332   Log &operator <<(long long aLong) {
333     if (MOZ_UNLIKELY(LogIt())) {
334       mMessage << aLong;
335     }
336     return *this;
337   }
338   Log &operator <<(unsigned long long aLong) {
339     if (MOZ_UNLIKELY(LogIt())) {
340       mMessage << aLong;
341     }
342     return *this;
343   }
344   Log &operator <<(Float aFloat) {
345     if (MOZ_UNLIKELY(LogIt())) {
346       mMessage << aFloat;
347     }
348     return *this;
349   }
350   Log &operator <<(double aDouble) {
351     if (MOZ_UNLIKELY(LogIt())) {
352       mMessage << aDouble;
353     }
354     return *this;
355   }
356   template <typename T, typename Sub, typename Coord>
357   Log &operator <<(const BasePoint<T, Sub, Coord>& aPoint) {
358     if (MOZ_UNLIKELY(LogIt())) {
359       mMessage << "Point" << aPoint;
360     }
361     return *this;
362   }
363   template <typename T, typename Sub>
364   Log &operator <<(const BaseSize<T, Sub>& aSize) {
365     if (MOZ_UNLIKELY(LogIt())) {
366       mMessage << "Size(" << aSize.width << "," << aSize.height << ")";
367     }
368     return *this;
369   }
370   template <typename T, typename Sub, typename Point, typename SizeT, typename Margin>
371   Log &operator <<(const BaseRect<T, Sub, Point, SizeT, Margin>& aRect) {
372     if (MOZ_UNLIKELY(LogIt())) {
373       mMessage << "Rect" << aRect;
374     }
375     return *this;
376   }
377   Log &operator<<(const Matrix& aMatrix) {
378     if (MOZ_UNLIKELY(LogIt())) {
379       mMessage << "Matrix(" << aMatrix._11 << " " << aMatrix._12 << " ; " << aMatrix._21 << " " << aMatrix._22 << " ; " << aMatrix._31 << " " << aMatrix._32 << ")";
380     }
381     return *this;
382   }
383   template<typename T>
384   Log &operator<<(Hexa<T> aHex) {
385     if (MOZ_UNLIKELY(LogIt())) {
386       mMessage << std::showbase << std::hex
387                << aHex.mVal
388                << std::noshowbase << std::dec;
389     }
390     return *this;
391   }
392 
393   Log& operator<<(SurfaceFormat aFormat) {
394     if (MOZ_UNLIKELY(LogIt())) {
395       switch(aFormat) {
396         case SurfaceFormat::B8G8R8A8:
397           mMessage << "SurfaceFormat::B8G8R8A8";
398           break;
399         case SurfaceFormat::B8G8R8X8:
400           mMessage << "SurfaceFormat::B8G8R8X8";
401           break;
402         case SurfaceFormat::R8G8B8A8:
403           mMessage << "SurfaceFormat::R8G8B8A8";
404           break;
405         case SurfaceFormat::R8G8B8X8:
406           mMessage << "SurfaceFormat::R8G8B8X8";
407           break;
408         case SurfaceFormat::R5G6B5_UINT16:
409           mMessage << "SurfaceFormat::R5G6B5_UINT16";
410           break;
411         case SurfaceFormat::A8:
412           mMessage << "SurfaceFormat::A8";
413           break;
414         case SurfaceFormat::YUV:
415           mMessage << "SurfaceFormat::YUV";
416           break;
417         case SurfaceFormat::UNKNOWN:
418           mMessage << "SurfaceFormat::UNKNOWN";
419           break;
420         default:
421           mMessage << "Invalid SurfaceFormat (" << (int)aFormat << ")";
422           break;
423       }
424     }
425     return *this;
426   }
427 
428   Log& operator<<(SurfaceType aType) {
429     if (MOZ_UNLIKELY(LogIt())) {
430       switch(aType) {
431         case SurfaceType::DATA:
432           mMessage << "SurfaceType::DATA";
433           break;
434         case SurfaceType::D2D1_BITMAP:
435           mMessage << "SurfaceType::D2D1_BITMAP";
436           break;
437         case SurfaceType::D2D1_DRAWTARGET:
438           mMessage << "SurfaceType::D2D1_DRAWTARGET";
439           break;
440         case SurfaceType::CAIRO:
441           mMessage << "SurfaceType::CAIRO";
442           break;
443         case SurfaceType::CAIRO_IMAGE:
444           mMessage << "SurfaceType::CAIRO_IMAGE";
445           break;
446         case SurfaceType::COREGRAPHICS_IMAGE:
447           mMessage << "SurfaceType::COREGRAPHICS_IMAGE";
448           break;
449         case SurfaceType::COREGRAPHICS_CGCONTEXT:
450           mMessage << "SurfaceType::COREGRAPHICS_CGCONTEXT";
451           break;
452         case SurfaceType::SKIA:
453           mMessage << "SurfaceType::SKIA";
454           break;
455         case SurfaceType::DUAL_DT:
456           mMessage << "SurfaceType::DUAL_DT";
457           break;
458         case SurfaceType::D2D1_1_IMAGE:
459           mMessage << "SurfaceType::D2D1_1_IMAGE";
460           break;
461         case SurfaceType::RECORDING:
462           mMessage << "SurfaceType::RECORDING";
463           break;
464         case SurfaceType::TILED:
465           mMessage << "SurfaceType::TILED";
466           break;
467         default:
468           mMessage << "Invalid SurfaceType (" << (int)aType << ")";
469           break;
470       }
471     }
472     return *this;
473   }
474 
LogIt()475   inline bool LogIt() const { return mLogIt; }
NoNewline()476   inline bool NoNewline() const { return mOptions & int(LogOptions::NoNewline); }
AutoPrefix()477   inline bool AutoPrefix() const { return mOptions & int(LogOptions::AutoPrefix); }
ValidReason()478   inline bool ValidReason() const { return (int)mReason > (int)LogReason::MustBeMoreThanThis && (int)mReason < (int)LogReason::MustBeLessThanThis; }
479 
480   // We do not want this version to do any work, and stringstream can't be
481   // copied anyway.  It does come in handy for the "Once" macro defined below.
Log(const Log & log)482   MOZ_IMPLICIT Log(const Log& log) { Init(log.mOptions, false, log.mReason); }
483 
484 private:
485   // Initialization common to two constructors
Init(int aOptions,bool aLogIt,LogReason aReason)486   void Init(int aOptions, bool aLogIt, LogReason aReason) {
487     mOptions = aOptions;
488     mReason = aReason;
489     mLogIt = aLogIt;
490     if (mLogIt) {
491       if (AutoPrefix()) {
492         if (mOptions & int(LogOptions::AssertOnCall)) {
493           mMessage << "[GFX" << L;
494         } else {
495           mMessage << "[GFX" << L << "-";
496         }
497       }
498       if ((mOptions & int(LogOptions::CrashAction)) && ValidReason()) {
499         mMessage << " " << (int)mReason;
500       }
501       if (AutoPrefix()) {
502         mMessage << "]: ";
503       }
504     }
505   }
506 
WriteLog(const std::string & aString)507   void WriteLog(const std::string &aString) {
508     if (MOZ_UNLIKELY(LogIt())) {
509       Logger::OutputMessage(aString, L, NoNewline());
510       // Assert if required.  We don't have a three parameter MOZ_ASSERT
511       // so use the underlying functions instead (see bug 1281702):
512 #ifdef DEBUG
513       if (mOptions & int(LogOptions::AssertOnCall)) {
514         MOZ_ReportAssertionFailure(aString.c_str(), __FILE__, __LINE__);
515         MOZ_CRASH("GFX: An assert from the graphics logger");
516       }
517 #endif
518       if ((mOptions & int(LogOptions::CrashAction)) && ValidReason()) {
519         Logger::CrashAction(mReason);
520       }
521     }
522   }
523 
524   std::stringstream mMessage;
525   int mOptions;
526   LogReason mReason;
527   bool mLogIt;
528 };
529 
530 typedef Log<LOG_DEBUG> DebugLog;
531 typedef Log<LOG_WARNING> WarningLog;
532 typedef Log<LOG_CRITICAL, CriticalLogger> CriticalLog;
533 
534 // Macro to glue names to get us less chance of name clashing.
535 #if defined GFX_LOGGING_GLUE1 || defined GFX_LOGGING_GLUE
536 #error "Clash of the macro GFX_LOGGING_GLUE1 or GFX_LOGGING_GLUE"
537 #endif
538 #define GFX_LOGGING_GLUE1(x, y)  x##y
539 #define GFX_LOGGING_GLUE(x, y)   GFX_LOGGING_GLUE1(x, y)
540 
541 // This log goes into crash reports, use with care.
542 #define gfxCriticalError mozilla::gfx::CriticalLog
543 #define gfxCriticalErrorOnce static gfxCriticalError GFX_LOGGING_GLUE(sOnceAtLine,__LINE__) = gfxCriticalError
544 
545 // This is a shortcut for errors we want logged in crash reports/about support
546 // but we do not want asserting.  These are available in all builds, so it is
547 // not worth trying to do magic to avoid matching the syntax of gfxCriticalError.
548 // So, this one is used as
549 // gfxCriticalNote << "Something to report and not assert";
550 // while the critical error is
551 // gfxCriticalError() << "Something to report and assert";
552 #define gfxCriticalNote gfxCriticalError(gfxCriticalError::DefaultOptions(false))
553 #define gfxCriticalNoteOnce static gfxCriticalError GFX_LOGGING_GLUE(sOnceAtLine,__LINE__) = gfxCriticalNote
554 
555 // The "once" versions will only trigger the first time through. You can do this:
556 // gfxCriticalErrorOnce() << "This message only shows up once;
557 // instead of the usual:
558 // static bool firstTime = true;
559 // if (firstTime) {
560 //   firstTime = false;
561 //   gfxCriticalError() << "This message only shows up once;
562 // }
563 #if defined(DEBUG)
564 #define gfxDebug mozilla::gfx::DebugLog
565 #define gfxDebugOnce static gfxDebug GFX_LOGGING_GLUE(sOnceAtLine,__LINE__) = gfxDebug
566 #else
567 #define gfxDebug if (1) ; else mozilla::gfx::NoLog
568 #define gfxDebugOnce if (1) ; else mozilla::gfx::NoLog
569 #endif
570 
571 // Have gfxWarning available (behind a runtime preference)
572 #define gfxWarning mozilla::gfx::WarningLog
573 #define gfxWarningOnce static gfxWarning GFX_LOGGING_GLUE(sOnceAtLine,__LINE__) = gfxWarning
574 
575 // In the debug build, this is equivalent to the default gfxCriticalError.
576 // In the non-debug build, on nightly and dev edition, it will MOZ_CRASH.
577 // On beta and release versions, it will telemetry count, but proceed.
578 //
579 // You should create a (new) enum in the LogReason and use it for the reason
580 // parameter to ensure uniqueness.
581 #define gfxDevCrash(reason) gfxCriticalError(int(gfx::LogOptions::AutoPrefix) | int(gfx::LogOptions::AssertOnCall) | int(gfx::LogOptions::CrashAction), (reason))
582 
583 // See nsDebug.h and the NS_WARN_IF macro
584 
585 #ifdef __cplusplus
586  // For now, have MOZ2D_ERROR_IF available in debug and non-debug builds
MOZ2D_error_if_impl(bool aCondition,const char * aExpr,const char * aFile,int32_t aLine)587 inline bool MOZ2D_error_if_impl(bool aCondition, const char* aExpr,
588                                 const char* aFile, int32_t aLine)
589 {
590   if (MOZ_UNLIKELY(aCondition)) {
591     gfxCriticalError() << aExpr << " at " << aFile << ":" << aLine;
592   }
593   return aCondition;
594 }
595 #define MOZ2D_ERROR_IF(condition) \
596   MOZ2D_error_if_impl(condition, #condition, __FILE__, __LINE__)
597 
598 #ifdef DEBUG
MOZ2D_warn_if_impl(bool aCondition,const char * aExpr,const char * aFile,int32_t aLine)599 inline bool MOZ2D_warn_if_impl(bool aCondition, const char* aExpr,
600                                const char* aFile, int32_t aLine)
601 {
602   if (MOZ_UNLIKELY(aCondition)) {
603     gfxWarning() << aExpr << " at " << aFile << ":" << aLine;
604   }
605   return aCondition;
606 }
607 #define MOZ2D_WARN_IF(condition) \
608   MOZ2D_warn_if_impl(condition, #condition, __FILE__, __LINE__)
609 #else
610 #define MOZ2D_WARN_IF(condition) (bool)(condition)
611 #endif
612 #endif
613 
614 const int INDENT_PER_LEVEL = 2;
615 
616 class TreeLog
617 {
618 public:
619   explicit TreeLog(const std::string& aPrefix = "")
mLog(int (LogOptions::NoNewline))620         : mLog(int(LogOptions::NoNewline)),
621           mPrefix(aPrefix),
622           mDepth(0),
623           mStartOfLine(true),
624           mConditionedOnPref(false),
625           mPrefFunction(nullptr) {}
626 
627   template <typename T>
628   TreeLog& operator<<(const T& aObject) {
629     if (mConditionedOnPref && !mPrefFunction()) {
630       return *this;
631     }
632     if (mStartOfLine) {
633       mLog << '[' << mPrefix << "] " << std::string(mDepth * INDENT_PER_LEVEL, ' ');
634       mStartOfLine = false;
635     }
636     mLog << aObject;
637     if (EndsInNewline(aObject)) {
638       // Don't indent right here as the user may change the indent
639       // between now and the first output to the next line.
640       mLog.Flush();
641       mStartOfLine = true;
642     }
643     return *this;
644   }
645 
IncreaseIndent()646   void IncreaseIndent() { ++mDepth; }
DecreaseIndent()647   void DecreaseIndent() {
648     MOZ_ASSERT(mDepth > 0);
649     --mDepth;
650   }
651 
ConditionOnPrefFunction(bool (* aPrefFunction)())652   void ConditionOnPrefFunction(bool(*aPrefFunction)()) {
653     mConditionedOnPref = true;
654     mPrefFunction = aPrefFunction;
655   }
656 private:
657   Log<LOG_DEBUG> mLog;
658   std::string mPrefix;
659   uint32_t mDepth;
660   bool mStartOfLine;
661   bool mConditionedOnPref;
662   bool (*mPrefFunction)();
663 
664   template <typename T>
EndsInNewline(const T & aObject)665   static bool EndsInNewline(const T& aObject) {
666     return false;
667   }
668 
EndsInNewline(const std::string & aString)669   static bool EndsInNewline(const std::string& aString) {
670     return !aString.empty() && aString[aString.length() - 1] == '\n';
671   }
672 
EndsInNewline(char aChar)673   static bool EndsInNewline(char aChar) {
674     return aChar == '\n';
675   }
676 
EndsInNewline(const char * aString)677   static bool EndsInNewline(const char* aString) {
678     return EndsInNewline(std::string(aString));
679   }
680 };
681 
682 class TreeAutoIndent
683 {
684 public:
TreeAutoIndent(TreeLog & aTreeLog)685   explicit TreeAutoIndent(TreeLog& aTreeLog) : mTreeLog(aTreeLog) {
686     mTreeLog.IncreaseIndent();
687   }
688 
TreeAutoIndent(const TreeAutoIndent & aTreeAutoIndent)689   TreeAutoIndent(const TreeAutoIndent& aTreeAutoIndent) :
690       TreeAutoIndent(aTreeAutoIndent.mTreeLog) {
691     mTreeLog.IncreaseIndent();
692   }
693 
694   TreeAutoIndent& operator=(const TreeAutoIndent& aTreeAutoIndent) = delete;
695 
~TreeAutoIndent()696   ~TreeAutoIndent() {
697     mTreeLog.DecreaseIndent();
698   }
699 private:
700   TreeLog& mTreeLog;
701 };
702 
703 } // namespace gfx
704 } // namespace mozilla
705 
706 #endif /* MOZILLA_GFX_LOGGING_H_ */
707