Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / leveldatabase / chromium_logger.h
blob933fb5106efb4a2367c3316ff8feec4ac584baa2
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
3 // found in the LICENSE file.
5 #ifndef THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_
6 #define THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_
8 #include "base/files/file.h"
9 #include "base/format_macros.h"
10 #include "base/strings/string_util.h"
11 #include "base/time/time.h"
12 #include "third_party/leveldatabase/src/include/leveldb/env.h"
14 namespace leveldb {
16 class ChromiumLogger : public Logger {
17 public:
18 explicit ChromiumLogger(base::File* f) : file_(f) {}
19 virtual ~ChromiumLogger() {}
20 virtual void Logv(const char* format, va_list ap) {
21 const base::PlatformThreadId thread_id = base::PlatformThread::CurrentId();
23 // We try twice: the first time with a fixed-size stack allocated buffer,
24 // and the second time with a much larger dynamically allocated buffer.
25 char buffer[500];
26 for (int iter = 0; iter < 2; iter++) {
27 char* base;
28 int bufsize;
29 if (iter == 0) {
30 bufsize = sizeof(buffer);
31 base = buffer;
32 } else {
33 bufsize = 30000;
34 base = new char[bufsize];
36 char* p = base;
37 char* limit = base + bufsize;
39 base::Time::Exploded t;
40 base::Time::Now().LocalExplode(&t);
42 p += base::snprintf(p, limit - p,
43 "%04d/%02d/%02d-%02d:%02d:%02d.%03d %" PRIu64 " ",
44 t.year,
45 t.month,
46 t.day_of_month,
47 t.hour,
48 t.minute,
49 t.second,
50 t.millisecond,
51 static_cast<uint64>(thread_id));
53 // Print the message
54 if (p < limit) {
55 va_list backup_ap;
56 va_copy(backup_ap, ap);
57 p += vsnprintf(p, limit - p, format, backup_ap);
58 va_end(backup_ap);
61 // Truncate to available space if necessary
62 if (p >= limit) {
63 if (iter == 0) {
64 continue; // Try again with larger buffer
65 } else {
66 p = limit - 1;
70 // Add newline if necessary
71 if (p == base || p[-1] != '\n') {
72 *p++ = '\n';
75 assert(p <= limit);
76 file_->WriteAtCurrentPos(base, p - base);
77 if (base != buffer) {
78 delete[] base;
80 break;
84 private:
85 scoped_ptr<base::File> file_;
88 } // namespace leveldb
90 #endif // THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_