47ee96ae4d9531e7cd2a94a47fdf11d7e02903c7
[platform/framework/web/crosswalk.git] / src / tools / relocation_packer / src / debug.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Logging and checks.
6 //
7 // Log messages to stdout.  LOG() prints normal user messages, VLOG()
8 // is verbose, for tracing and debugging.  SetVerbose() enables/disables
9 // VLOG() output.
10 //
11 // LOG() and VLOG() are printf-like, and arguments are checked by gcc.
12 // LOG_IF() and VLOG_IF() call LOG/VLOG if their predicate is true.
13 // CHECK() aborts if its predicate is false.  NOTREACHED() always aborts.
14 // Logging is not thread-safe.
15 //
16
17 #ifndef TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_
18 #define TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_
19
20 #ifdef NDEBUG
21 #undef NDEBUG
22 #include <assert.h>
23 #define NDEBUG
24 #else
25 #include <assert.h>
26 #endif
27
28 #include <stdarg.h>
29 #include <string.h>
30
31 namespace relocation_packer {
32
33 // If gcc, define PRINTF_ATTRIBUTE so that gcc checks Log() as printf-like.
34 #if defined(__GNUC__) && (__GNUC__ >= 3)
35 #define PRINTF_ATTRIBUTE(string_index, first_to_check) \
36     __attribute__((__format__(__printf__, string_index, first_to_check)))
37 #else
38 #define PRINTF_ATTRIBUTE(string_index, first_to_check)
39 #endif
40
41 // Logging and checking macros.
42 #define LOG(...) ::relocation_packer::Logger::Log(__VA_ARGS__)
43 #define VLOG(...) ::relocation_packer::Logger::VLog(__VA_ARGS__)
44 #define LOG_IF(cond, ...)  \
45   do {                     \
46     if ((cond))            \
47       LOG(__VA_ARGS__);    \
48   } while (0)
49 #define VLOG_IF(cond, ...) \
50   do {                     \
51     if ((cond))            \
52       VLOG(__VA_ARGS__);   \
53   } while (0)
54
55 #define CHECK(expr) assert((expr))
56 #define NOTREACHED(_) assert(false)
57
58 class Logger {
59  public:
60   // Log and verbose log to stdout.
61   // |format| is a printf format string.
62   static void Log(const char* format, ...) PRINTF_ATTRIBUTE(1, 2);
63   static void VLog(const char* format, ...) PRINTF_ATTRIBUTE(1, 2);
64
65   // Set verbose mode.
66   // |flag| is true to enable verbose logging, false to disable it.
67   static void SetVerbose(bool flag);
68
69  private:
70   Logger() : is_verbose_(false) { }
71   ~Logger() {}
72
73   // Implementation of log to stdout.
74   // |format| is a printf format string.
75   // |args| is a varargs list of printf arguments.
76   void Log(const char* format, va_list args);
77
78   // If set, VLOG is enabled, otherwise it is a no-op.
79   bool is_verbose_;
80
81   // Singleton support.  Not thread-safe.
82   static Logger* GetInstance();
83   static Logger* instance_;
84 };
85
86 }  // namespace relocation_packer
87
88 #endif  // TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_