OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_DEBUG_CRASH_LOGGING_H_ | 5 #ifndef BASE_DEBUG_CRASH_LOGGING_H_ |
6 #define BASE_DEBUG_CRASH_LOGGING_H_ | 6 #define BASE_DEBUG_CRASH_LOGGING_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
| 11 #include <type_traits> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
16 | 17 |
17 // These functions add metadata to the upload payload when sending crash reports | 18 // These functions add metadata to the upload payload when sending crash reports |
18 // to the crash server. | 19 // to the crash server. |
19 // | 20 // |
20 // IMPORTANT: On OS X and Linux, the key/value pairs are only sent as part of | 21 // IMPORTANT: On OS X and Linux, the key/value pairs are only sent as part of |
(...skipping 21 matching lines...) Expand all Loading... |
42 const void* const* addresses, | 43 const void* const* addresses, |
43 size_t count); | 44 size_t count); |
44 | 45 |
45 // A scoper that sets the specified key to value for the lifetime of the | 46 // A scoper that sets the specified key to value for the lifetime of the |
46 // object, and clears it on destruction. | 47 // object, and clears it on destruction. |
47 class BASE_EXPORT ScopedCrashKey { | 48 class BASE_EXPORT ScopedCrashKey { |
48 public: | 49 public: |
49 ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value); | 50 ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value); |
50 ~ScopedCrashKey(); | 51 ~ScopedCrashKey(); |
51 | 52 |
| 53 // Helper to force a static_assert when instantiating a ScopedCrashKey |
| 54 // temporary without a name. The usual idiom is to just #define a macro that |
| 55 // static_asserts with the message; however, that doesn't work well when the |
| 56 // type is in a namespace. |
| 57 // |
| 58 // Instead, we use a templated helper to trigger the static_assert, observing |
| 59 // two rules: |
| 60 // - The static_assert needs to be in a normally uninstantiated template; |
| 61 // otherwise, it will fail to compile =) |
| 62 // - Similarly, the static_assert must be dependent on the template argument, |
| 63 // to prevent it from being evaluated until the template is instantiated. |
| 64 // |
| 65 // To prevent this constructor from being accidentally invoked, it takes a |
| 66 // special enum as an argument. |
| 67 |
| 68 // Finally, note that this can't just be a template function that takes only |
| 69 // one parameter, because this ends up triggering the vexing parse issue. |
| 70 enum ScopedCrashKeyNeedsNameTag { |
| 71 KEY_NEEDS_NAME, |
| 72 }; |
| 73 |
| 74 template <typename... Args> |
| 75 explicit ScopedCrashKey(ScopedCrashKeyNeedsNameTag, const Args&...) { |
| 76 constexpr bool always_false = sizeof...(Args) == 0 && sizeof...(Args) != 0; |
| 77 static_assert( |
| 78 always_false, |
| 79 "scoped crash key objects should not be unnamed temporaries."); |
| 80 } |
| 81 |
52 private: | 82 private: |
53 std::string key_; | 83 std::string key_; |
54 | 84 |
55 DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey); | 85 DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey); |
56 }; | 86 }; |
57 | 87 |
| 88 // Disallow an instantation of ScopedCrashKey without a name, since this results |
| 89 // in a temporary that is immediately destroyed. Doing so will trigger the |
| 90 // static_assert in the templated constructor helper in ScopedCrashKey. |
| 91 #define ScopedCrashKey(...) \ |
| 92 ScopedCrashKey(base::debug::ScopedCrashKey::KEY_NEEDS_NAME, __VA_ARGS__) |
| 93 |
58 // Before setting values for a key, all the keys must be registered. | 94 // Before setting values for a key, all the keys must be registered. |
59 struct BASE_EXPORT CrashKey { | 95 struct BASE_EXPORT CrashKey { |
60 // The name of the crash key, used in the above functions. | 96 // The name of the crash key, used in the above functions. |
61 const char* key_name; | 97 const char* key_name; |
62 | 98 |
63 // The maximum length for a value. If the value is longer than this, it will | 99 // The maximum length for a value. If the value is longer than this, it will |
64 // be truncated. If the value is larger than the |chunk_max_length| passed to | 100 // be truncated. If the value is larger than the |chunk_max_length| passed to |
65 // InitCrashKeys() but less than this value, it will be split into multiple | 101 // InitCrashKeys() but less than this value, it will be split into multiple |
66 // numbered chunks. | 102 // numbered chunks. |
67 size_t max_length; | 103 size_t max_length; |
(...skipping 29 matching lines...) Expand all Loading... |
97 const base::StringPiece& value, | 133 const base::StringPiece& value, |
98 size_t chunk_max_length); | 134 size_t chunk_max_length); |
99 | 135 |
100 // Resets the crash key system so it can be reinitialized. For testing only. | 136 // Resets the crash key system so it can be reinitialized. For testing only. |
101 BASE_EXPORT void ResetCrashLoggingForTesting(); | 137 BASE_EXPORT void ResetCrashLoggingForTesting(); |
102 | 138 |
103 } // namespace debug | 139 } // namespace debug |
104 } // namespace base | 140 } // namespace base |
105 | 141 |
106 #endif // BASE_DEBUG_CRASH_LOGGING_H_ | 142 #endif // BASE_DEBUG_CRASH_LOGGING_H_ |
OLD | NEW |