|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef Logging_h |
|
6 #define Logging_h |
|
7 |
|
8 #include "mozilla/Likely.h" |
|
9 |
|
10 #ifdef ANDROID |
|
11 #include <android/log.h> |
|
12 #define LOG(...) __android_log_print(ANDROID_LOG_ERROR, "GeckoLinker", __VA_ARGS__) |
|
13 #else |
|
14 #include <cstdio> |
|
15 |
|
16 /* Expand to 1 or m depending on whether there is one argument or more |
|
17 * given. */ |
|
18 #define MOZ_ONE_OR_MORE_ARGS_IMPL2(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) \ |
|
19 N |
|
20 #define MOZ_ONE_OR_MORE_ARGS_IMPL(args) MOZ_ONE_OR_MORE_ARGS_IMPL2 args |
|
21 #define MOZ_ONE_OR_MORE_ARGS(...) \ |
|
22 MOZ_ONE_OR_MORE_ARGS_IMPL((__VA_ARGS__, m, m, m, m, m, m, m, m, 1, 0)) |
|
23 |
|
24 #define MOZ_MACRO_GLUE(a, b) a b |
|
25 #define MOZ_CONCAT2(a, b) a ## b |
|
26 #define MOZ_CONCAT1(a, b) MOZ_CONCAT2(a, b) |
|
27 #define MOZ_CONCAT(a, b) MOZ_CONCAT1(a, b) |
|
28 |
|
29 /* Some magic to choose between LOG1 and LOGm depending on the number of |
|
30 * arguments */ |
|
31 #define MOZ_CHOOSE_LOG(...) \ |
|
32 MOZ_MACRO_GLUE(MOZ_CONCAT(LOG, MOZ_ONE_OR_MORE_ARGS(__VA_ARGS__)), \ |
|
33 (__VA_ARGS__)) |
|
34 |
|
35 #define LOG1(format) fprintf(stderr, format "\n") |
|
36 #define LOGm(format, ...) fprintf(stderr, format "\n", __VA_ARGS__) |
|
37 #define LOG(...) MOZ_CHOOSE_LOG(__VA_ARGS__) |
|
38 |
|
39 #endif |
|
40 |
|
41 class Logging |
|
42 { |
|
43 public: |
|
44 static bool isVerbose() |
|
45 { |
|
46 return Singleton.verbose; |
|
47 } |
|
48 |
|
49 private: |
|
50 bool verbose; |
|
51 |
|
52 public: |
|
53 static void Init() |
|
54 { |
|
55 const char *env = getenv("MOZ_DEBUG_LINKER"); |
|
56 if (env && *env == '1') |
|
57 Singleton.verbose = true; |
|
58 } |
|
59 |
|
60 private: |
|
61 static Logging Singleton; |
|
62 }; |
|
63 |
|
64 #define DEBUG_LOG(...) \ |
|
65 do { \ |
|
66 if (MOZ_UNLIKELY(Logging::isVerbose())) { \ |
|
67 LOG(__VA_ARGS__); \ |
|
68 } \ |
|
69 } while(0) |
|
70 |
|
71 #if defined(__LP64__) |
|
72 # define PRIxAddr "lx" |
|
73 # define PRIxSize "lx" |
|
74 # define PRIdSize "ld" |
|
75 # define PRIuSize "lu" |
|
76 #else |
|
77 # define PRIxAddr "x" |
|
78 # define PRIxSize "x" |
|
79 # define PRIdSize "d" |
|
80 # define PRIuSize "u" |
|
81 #endif |
|
82 |
|
83 #endif /* Logging_h */ |