1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_antitrackinglog_h
8 #define mozilla_antitrackinglog_h
9 
10 #include "mozilla/Logging.h"
11 #include "nsString.h"
12 
13 namespace mozilla {
14 
15 extern LazyLogModule gAntiTrackingLog;
16 static const nsCString::size_type sMaxSpecLength = 128;
17 
18 #define LOG(format) MOZ_LOG(gAntiTrackingLog, mozilla::LogLevel::Debug, format)
19 
20 #define LOG_SPEC(format, uri)                                       \
21   PR_BEGIN_MACRO                                                    \
22   if (MOZ_LOG_TEST(gAntiTrackingLog, mozilla::LogLevel::Debug)) {   \
23     nsAutoCString _specStr("(null)"_ns);                            \
24     if (uri) {                                                      \
25       _specStr = (uri)->GetSpecOrDefault();                         \
26     }                                                               \
27     _specStr.Truncate(std::min(_specStr.Length(), sMaxSpecLength)); \
28     const char* _spec = _specStr.get();                             \
29     LOG(format);                                                    \
30   }                                                                 \
31   PR_END_MACRO
32 
33 #define LOG_SPEC2(format, uri1, uri2)                                 \
34   PR_BEGIN_MACRO                                                      \
35   if (MOZ_LOG_TEST(gAntiTrackingLog, mozilla::LogLevel::Debug)) {     \
36     nsAutoCString _specStr1("(null)"_ns);                             \
37     if (uri1) {                                                       \
38       _specStr1 = (uri1)->GetSpecOrDefault();                         \
39     }                                                                 \
40     _specStr1.Truncate(std::min(_specStr1.Length(), sMaxSpecLength)); \
41     const char* _spec1 = _specStr1.get();                             \
42     nsAutoCString _specStr2("(null)"_ns);                             \
43     if (uri2) {                                                       \
44       _specStr2 = (uri2)->GetSpecOrDefault();                         \
45     }                                                                 \
46     _specStr2.Truncate(std::min(_specStr2.Length(), sMaxSpecLength)); \
47     const char* _spec2 = _specStr2.get();                             \
48     LOG(format);                                                      \
49   }                                                                   \
50   PR_END_MACRO
51 
52 #define LOG_PRIN(format, principal)                                 \
53   PR_BEGIN_MACRO                                                    \
54   if (MOZ_LOG_TEST(gAntiTrackingLog, mozilla::LogLevel::Debug)) {   \
55     nsAutoCString _specStr("(null)"_ns);                            \
56     if (principal) {                                                \
57       (principal)->GetAsciiSpec(_specStr);                          \
58     }                                                               \
59     _specStr.Truncate(std::min(_specStr.Length(), sMaxSpecLength)); \
60     const char* _spec = _specStr.get();                             \
61     LOG(format);                                                    \
62   }                                                                 \
63   PR_END_MACRO
64 }  // namespace mozilla
65 
66 #endif  // mozilla_antitrackinglog_h
67