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 THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_ | 5 #ifndef THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_ |
6 #define THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_ | 6 #define THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_ |
7 | 7 |
8 #include <stdio.h> | 8 #include "base/files/file.h" |
9 | |
10 #include <algorithm> | |
11 | |
12 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
13 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
14 #include "base/time/time.h" | 11 #include "base/time/time.h" |
15 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 12 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
16 | 13 |
17 namespace leveldb { | 14 namespace leveldb { |
18 | 15 |
19 class ChromiumLogger : public Logger { | 16 class ChromiumLogger : public Logger { |
20 public: | 17 public: |
21 explicit ChromiumLogger(FILE* f) : file_(f) {} | 18 explicit ChromiumLogger(base::File* f) : file_(f) {} |
22 virtual ~ChromiumLogger() { | 19 virtual ~ChromiumLogger() {} |
23 fclose(file_); | |
24 } | |
25 virtual void Logv(const char* format, va_list ap) { | 20 virtual void Logv(const char* format, va_list ap) { |
26 const base::PlatformThreadId thread_id = | 21 const base::PlatformThreadId thread_id = |
27 ::base::PlatformThread::CurrentId(); | 22 ::base::PlatformThread::CurrentId(); |
28 | 23 |
29 // We try twice: the first time with a fixed-size stack allocated buffer, | 24 // We try twice: the first time with a fixed-size stack allocated buffer, |
30 // and the second time with a much larger dynamically allocated buffer. | 25 // and the second time with a much larger dynamically allocated buffer. |
31 char buffer[500]; | 26 char buffer[500]; |
32 for (int iter = 0; iter < 2; iter++) { | 27 for (int iter = 0; iter < 2; iter++) { |
33 char* base; | 28 char* base; |
34 int bufsize; | 29 int bufsize; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 p = limit - 1; | 67 p = limit - 1; |
73 } | 68 } |
74 } | 69 } |
75 | 70 |
76 // Add newline if necessary | 71 // Add newline if necessary |
77 if (p == base || p[-1] != '\n') { | 72 if (p == base || p[-1] != '\n') { |
78 *p++ = '\n'; | 73 *p++ = '\n'; |
79 } | 74 } |
80 | 75 |
81 assert(p <= limit); | 76 assert(p <= limit); |
82 fwrite(base, 1, p - base, file_); | 77 file_->WriteAtCurrentPos(base, p - base); |
83 fflush(file_); | |
84 if (base != buffer) { | 78 if (base != buffer) { |
85 delete[] base; | 79 delete[] base; |
86 } | 80 } |
87 break; | 81 break; |
88 } | 82 } |
89 } | 83 } |
90 | 84 |
91 private: | 85 private: |
92 FILE* file_; | 86 scoped_ptr<base::File> file_; |
93 }; | 87 }; |
94 | 88 |
95 } // namespace leveldb | 89 } // namespace leveldb |
96 | 90 |
97 #endif // THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_ | 91 #endif // THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_ |
OLD | NEW |