[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4cccc22 | 2010-04-02 22:12:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 5 | #ifndef SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ |
6 | #define SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ | ||||
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 4cccc22 | 2010-04-02 22:12:56 | [diff] [blame] | 8 | |
[email protected] | 4cccc22 | 2010-04-02 22:12:56 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 11 | #include "sql/connection.h" |
[email protected] | d452696 | 2011-11-10 21:40:28 | [diff] [blame] | 12 | #include "sql/sql_export.h" |
[email protected] | 4cccc22 | 2010-04-02 22:12:56 | [diff] [blame] | 13 | |
14 | namespace sql { | ||||
15 | |||||
16 | // This class handles the exceptional sqlite errors that we might encounter | ||||
17 | // if for example the db is corrupted. Right now we just generate a UMA | ||||
18 | // histogram for release and an assert for debug builds. | ||||
19 | // | ||||
20 | // Why is it a template you ask? well, that is a funny story. The histograms | ||||
21 | // need to be singletons that is why they are always static at the function | ||||
22 | // scope, but we cannot use the Singleton class because they are not default | ||||
23 | // constructible. The template parameter makes the compiler to create unique | ||||
24 | // classes that don't share the same static variable. | ||||
25 | template <class UniqueT> | ||||
26 | class DiagnosticErrorDelegate : public ErrorDelegate { | ||||
27 | public: | ||||
28 | |||||
29 | virtual int OnError(int error, Connection* connection, | ||||
30 | Statement* stmt) { | ||||
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame] | 31 | LOG(ERROR) << "sqlite error " << error |
32 | << ", errno " << connection->GetLastErrno() | ||||
33 | << ": " << connection->GetErrorMessage(); | ||||
[email protected] | 4cccc22 | 2010-04-02 22:12:56 | [diff] [blame] | 34 | RecordErrorInHistogram(error); |
35 | return error; | ||||
36 | } | ||||
37 | |||||
38 | private: | ||||
39 | static void RecordErrorInHistogram(int error) { | ||||
[email protected] | 658f833 | 2010-09-18 04:40:43 | [diff] [blame] | 40 | // Trim off the extended error codes. |
41 | error &= 0xff; | ||||
42 | |||||
[email protected] | 4cccc22 | 2010-04-02 22:12:56 | [diff] [blame] | 43 | // The histogram values from sqlite result codes go currently from 1 to |
44 | // 26 currently but 50 gives them room to grow. | ||||
45 | UMA_HISTOGRAM_ENUMERATION(UniqueT::name(), error, 50); | ||||
46 | } | ||||
47 | }; | ||||
48 | |||||
49 | } // namespace sql | ||||
50 | |||||
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 51 | #endif // SQL_DIAGNOSTIC_ERROR_DELEGATE_H_ |