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 SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ | 5 #ifndef SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ |
6 #define SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ | 6 #define SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | |
10 #include "sql/connection.h" | 9 #include "sql/connection.h" |
| 10 #include "sql/error_delegate_util.h" |
11 #include "sql/sql_export.h" | 11 #include "sql/sql_export.h" |
12 | 12 |
13 namespace sql { | 13 namespace sql { |
14 | 14 |
15 // This class handles the exceptional sqlite errors that we might encounter | 15 // This class handles the exceptional sqlite errors that we might encounter |
16 // if for example the db is corrupted. Right now we just generate a UMA | 16 // if for example the db is corrupted. Right now we just generate a UMA |
17 // histogram for release and an assert for debug builds. | 17 // histogram for release and an assert for debug builds. |
18 // | 18 // See error_delegate_util.h for an explanation as to why this class is a |
19 // Why is it a template you ask? well, that is a funny story. The histograms | 19 // template. |
20 // need to be singletons that is why they are always static at the function | |
21 // scope, but we cannot use the Singleton class because they are not default | |
22 // constructible. The template parameter makes the compiler to create unique | |
23 // classes that don't share the same static variable. | |
24 template <class UniqueT> | 20 template <class UniqueT> |
25 class DiagnosticErrorDelegate : public ErrorDelegate { | 21 class DiagnosticErrorDelegate : public ErrorDelegate { |
26 public: | 22 public: |
27 DiagnosticErrorDelegate() {} | 23 DiagnosticErrorDelegate() {} |
28 virtual ~DiagnosticErrorDelegate() {} | 24 virtual ~DiagnosticErrorDelegate() {} |
29 | 25 |
30 virtual int OnError(int error, Connection* connection, | 26 virtual int OnError(int error, Connection* connection, |
31 Statement* stmt) { | 27 Statement* stmt) { |
32 LOG(ERROR) << "sqlite error " << error | 28 LogAndRecordErrorInHistogram<UniqueT>(error, connection); |
33 << ", errno " << connection->GetLastErrno() | |
34 << ": " << connection->GetErrorMessage(); | |
35 RecordErrorInHistogram(error); | |
36 return error; | 29 return error; |
37 } | 30 } |
38 | 31 |
39 private: | 32 private: |
40 static void RecordErrorInHistogram(int error) { | 33 DISALLOW_COPY_AND_ASSIGN(DiagnosticErrorDelegate); |
41 // Trim off the extended error codes. | |
42 error &= 0xff; | |
43 | |
44 // The histogram values from sqlite result codes go currently from 1 to | |
45 // 26 currently but 50 gives them room to grow. | |
46 UMA_HISTOGRAM_ENUMERATION(UniqueT::name(), error, 50); | |
47 } | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(DiagnosticErrorDelegate); | |
50 }; | 34 }; |
51 | 35 |
52 } // namespace sql | 36 } // namespace sql |
53 | 37 |
54 #endif // SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ | 38 #endif // SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ |
OLD | NEW |