• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2018, 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   virtual void Error(const char *format, ...) = 0;
66   virtual void Warning(const char *format, ...) = 0;
67   virtual void Info(const char *format, ...) = 0;
68   virtual void Debug(const char *format, ...) = 0;
69   virtual void Verbose(const char *format, ...) = 0;
70   virtual void BeginTrace(const char *class_name, const char *function_name,
71                           const char *custom_string) = 0;
72   virtual void EndTrace() = 0;
73   virtual int GetProperty(const char *property_name, int *value) = 0;
74   virtual int GetProperty(const char *property_name, char *value) = 0;
75 
Get()76   static inline DebugHandler *Get() { return debug_handler_; }
77   static void Set(DebugHandler *debug_handler);
GetLogMask()78   static inline std::bitset<32> & GetLogMask() { return log_mask_; }
SetLogMask(const std::bitset<32> & log_mask)79   static void SetLogMask(const std::bitset<32> &log_mask) { log_mask_ = log_mask; }
80 
81  protected:
~DebugHandler()82   virtual ~DebugHandler() { }
83 
84  private:
85   static DebugHandler *debug_handler_;
86   static std::bitset<32> log_mask_;
87 };
88 
89 template <class T>
90 class ScopeTracer {
91  public:
ScopeTracer(const char * class_name,const char * function_name)92   ScopeTracer(const char *class_name, const char *function_name) {
93     T::Get()->BeginTrace(class_name, function_name, "");
94   }
95 
~ScopeTracer()96   ~ScopeTracer() { T::Get()->EndTrace(); }
97 };
98 
99 }  // namespace display
100 
101 #endif  // __DEBUG_HANDLER_H__
102