1 /*
2 * Copyright (c) 2018, 2020 The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *     * Redistributions of source code must retain the above copyright
8 *       notice, this list of conditions and the following disclaimer.
9 *     * Redistributions in binary form must reproduce the above
10 *       copyright notice, this list of conditions and the following
11 *       disclaimer in the documentation and/or other materials provided
12 *       with the distribution.
13 *     * Neither the name of The Linux Foundation nor the names of its
14 *       contributors may be used to endorse or promote products derived
15 *       from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef __DEBUG_HANDLER_H__
31 #define __DEBUG_HANDLER_H__
32 
33 #include <bitset>
34 
35 #define DLOG(method, format, ...) \
36   display::DebugHandler::Get()->method(__CLASS__ "::%s: " format, __FUNCTION__, ##__VA_ARGS__)
37 
38 #define DLOG_IF(tag, method, format, ...) \
39   if (display::DebugHandler::GetLogMask()[tag]) { \
40     DLOG(method, format, ##__VA_ARGS__); \
41   }
42 
43 #define DLOGE_IF(tag, format, ...) DLOG_IF(tag, Error, format, ##__VA_ARGS__)
44 #define DLOGW_IF(tag, format, ...) DLOG_IF(tag, Warning, format, ##__VA_ARGS__)
45 #define DLOGI_IF(tag, format, ...) DLOG_IF(tag, Info, format, ##__VA_ARGS__)
46 #define DLOGD_IF(tag, format, ...) DLOG_IF(tag, Debug, format, ##__VA_ARGS__)
47 #define DLOGV_IF(tag, format, ...) DLOG_IF(tag, Verbose, format, ##__VA_ARGS__)
48 
49 #define DLOGE(format, ...) DLOG(Error, format, ##__VA_ARGS__)
50 #define DLOGW(format, ...) DLOG(Warning, format, ##__VA_ARGS__)
51 #define DLOGI(format, ...) DLOG(Info, format, ##__VA_ARGS__)
52 #define DLOGD(format, ...) DLOG(Debug, format, ##__VA_ARGS__)
53 #define DLOGV(format, ...) DLOG(Verbose, format, ##__VA_ARGS__)
54 
55 #define DTRACE_BEGIN(custom_string) display::DebugHandler::Get()->BeginTrace( \
56                                           __CLASS__, __FUNCTION__, custom_string)
57 #define DTRACE_END() display::DebugHandler::Get()->EndTrace()
58 #define DTRACE_SCOPED() display::ScopeTracer <display::DebugHandler> \
59                                           scope_tracer(__CLASS__, __FUNCTION__)
60 
61 namespace display {
62 
63 class DebugHandler {
64  public:
65   // __format__(printf hints the compiler to validate format specifiers vs arguments provided.
66   virtual void Error(const char *format, ...) __attribute__ ((__format__(printf, 2, 3))) = 0;
67   virtual void Warning(const char *format, ...) __attribute__ ((__format__(printf, 2, 3))) = 0;
68   virtual void Info(const char *format, ...) __attribute__ ((__format__(printf, 2, 3))) = 0;
69   virtual void Debug(const char *format, ...) __attribute__ ((__format__(printf, 2, 3))) = 0;
70   virtual void Verbose(const char *format, ...) __attribute__ ((__format__(printf, 2, 3))) = 0;
71   virtual void BeginTrace(const char *class_name, const char *function_name,
72                           const char *custom_string) = 0;
73   virtual void EndTrace() = 0;
74   virtual int GetProperty(const char *property_name, int *value) = 0;
75   virtual int GetProperty(const char *property_name, char *value) = 0;
76 
Get()77   static inline DebugHandler *Get() { return debug_handler_; }
78   static void Set(DebugHandler *debug_handler);
GetLogMask()79   static inline std::bitset<32> & GetLogMask() { return log_mask_; }
SetLogMask(const std::bitset<32> & log_mask)80   static void SetLogMask(const std::bitset<32> &log_mask) { log_mask_ = log_mask; }
81 
82  protected:
~DebugHandler()83   virtual ~DebugHandler() { }
84 
85  private:
86   static DebugHandler *debug_handler_;
87   static std::bitset<32> log_mask_;
88 };
89 
90 template <class T>
91 class ScopeTracer {
92  public:
ScopeTracer(const char * class_name,const char * function_name)93   ScopeTracer(const char *class_name, const char *function_name) {
94     T::Get()->BeginTrace(class_name, function_name, "");
95   }
96 
~ScopeTracer()97   ~ScopeTracer() { T::Get()->EndTrace(); }
98 };
99 
100 }  // namespace display
101 
102 #endif  // __DEBUG_HANDLER_H__
103