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_
10 #include "base/string_util.h"
11 #include "base/time.h"
12 #include "leveldb/env.h"
16 class ChromiumLogger
: public Logger
{
18 ChromiumLogger(FILE* f
) : file_(f
) { }
19 virtual ~ChromiumLogger() {
22 virtual void Logv(const char* format
, va_list ap
) {
23 const long long unsigned int thread_id
=
24 ::base::PlatformThread::CurrentId();
26 // We try twice: the first time with a fixed-size stack allocated buffer,
27 // and the second time with a much larger dynamically allocated buffer.
29 for (int iter
= 0; iter
< 2; iter
++) {
33 bufsize
= sizeof(buffer
);
37 base
= new char[bufsize
];
40 char* limit
= base
+ bufsize
;
42 ::base::Time::Exploded t
;
43 ::base::Time::Now().LocalExplode(&t
);
45 p
+= ::base::snprintf(p
, limit
- p
,
46 "%04d/%02d/%02d-%02d:%02d:%02d.%03d %lld ",
59 GG_VA_COPY(backup_ap
, ap
);
60 p
+= vsnprintf(p
, limit
- p
, format
, backup_ap
);
64 // Truncate to available space if necessary
67 continue; // Try again with larger buffer
73 // Add newline if necessary
74 if (p
== base
|| p
[-1] != '\n') {
79 fwrite(base
, 1, p
- base
, file_
);
91 } // namespace leveldb
93 #endif // THIRD_PARTY_LEVELDATABASE_CHROMIUM_LOGGER_H_