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"
16 class ChromiumLogger
: public Logger
{
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.
26 for (int iter
= 0; iter
< 2; iter
++) {
30 bufsize
= sizeof(buffer
);
34 base
= new char[bufsize
];
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
" ",
51 static_cast<uint64
>(thread_id
));
56 va_copy(backup_ap
, ap
);
57 p
+= vsnprintf(p
, limit
- p
, format
, backup_ap
);
61 // Truncate to available space if necessary
64 continue; // Try again with larger buffer
70 // Add newline if necessary
71 if (p
== base
|| p
[-1] != '\n') {
76 file_
->WriteAtCurrentPos(base
, p
- base
);
85 scoped_ptr
<base::File
> file_
;
88 } // namespace leveldb
90 #endif // THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_