Chromium Code Reviews| 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> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 const void* const* addresses, | 42 const void* const* addresses, |
| 43 size_t count); | 43 size_t count); |
| 44 | 44 |
| 45 // A scoper that sets the specified key to value for the lifetime of the | 45 // A scoper that sets the specified key to value for the lifetime of the |
| 46 // object, and clears it on destruction. | 46 // object, and clears it on destruction. |
| 47 class BASE_EXPORT ScopedCrashKey { | 47 class BASE_EXPORT ScopedCrashKey { |
| 48 public: | 48 public: |
| 49 ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value); | 49 ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value); |
| 50 ~ScopedCrashKey(); | 50 ~ScopedCrashKey(); |
| 51 | 51 |
| 52 // Helper to force a static_assert when instantiating a ScopedCrashKey | |
| 53 // temporary without a name. The usual idiom is to just #define a macro that | |
| 54 // static_asserts with the message; however, that doesn't work well when the | |
| 55 // type is in a namespace. | |
| 56 // | |
| 57 // Instead, we use a templated helper to trigger the static_assert, observing | |
| 58 // two rules: | |
| 59 // - The static_assert needs to be in a normally uninstantiated template; | |
| 60 // otherwise, it will fail to compile =) | |
| 61 // - Similarly, the static_assert must be dependent on the template argument, | |
| 62 // to prevent it from being evaluated until the template is instantiated. | |
| 63 template<typename T> | |
| 64 ScopedCrashKey(const T&) { | |
|
danakj
2016/07/12 19:59:26
Why a constructor and not just a free CrashFromBad
dcheng
2016/07/13 00:34:11
This prevents me from having to "using base::debug
Robert Sesek
2016/07/13 19:46:22
What about something like this:
enum class Unname
danakj
2016/07/13 19:48:16
I was just thinking similar things.. but more like
| |
| 65 static_assert( | |
| 66 sizeof(T) == 0, | |
| 67 "scoped crash key objects should not be unnamed temporaries."); | |
| 68 } | |
| 69 | |
| 52 private: | 70 private: |
| 53 std::string key_; | 71 std::string key_; |
| 54 | 72 |
| 55 DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey); | 73 DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey); |
| 56 }; | 74 }; |
| 57 | 75 |
| 76 // Disallow an instantation of ScopedCrashKey without a name, since this results | |
| 77 // in a temporary that is immediately destroyed. Doing so will trigger the | |
| 78 // static_assert in the templated constructor helper in ScopedCrashKey. | |
| 79 #define ScopedCrashKey(key, value) ScopedCrashKey(0xbad) | |
| 80 | |
| 58 // Before setting values for a key, all the keys must be registered. | 81 // Before setting values for a key, all the keys must be registered. |
| 59 struct BASE_EXPORT CrashKey { | 82 struct BASE_EXPORT CrashKey { |
| 60 // The name of the crash key, used in the above functions. | 83 // The name of the crash key, used in the above functions. |
| 61 const char* key_name; | 84 const char* key_name; |
| 62 | 85 |
| 63 // The maximum length for a value. If the value is longer than this, it will | 86 // 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 | 87 // 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 | 88 // InitCrashKeys() but less than this value, it will be split into multiple |
| 66 // numbered chunks. | 89 // numbered chunks. |
| 67 size_t max_length; | 90 size_t max_length; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 97 const base::StringPiece& value, | 120 const base::StringPiece& value, |
| 98 size_t chunk_max_length); | 121 size_t chunk_max_length); |
| 99 | 122 |
| 100 // Resets the crash key system so it can be reinitialized. For testing only. | 123 // Resets the crash key system so it can be reinitialized. For testing only. |
| 101 BASE_EXPORT void ResetCrashLoggingForTesting(); | 124 BASE_EXPORT void ResetCrashLoggingForTesting(); |
| 102 | 125 |
| 103 } // namespace debug | 126 } // namespace debug |
| 104 } // namespace base | 127 } // namespace base |
| 105 | 128 |
| 106 #endif // BASE_DEBUG_CRASH_LOGGING_H_ | 129 #endif // BASE_DEBUG_CRASH_LOGGING_H_ |
| OLD | NEW |