util/posix_logger.h

filter: Statementstmt 86.0% 37 / 43 Branchbran 42.8% 12 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.0% 49 / 71 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/dbformat.h ↑ Summary table/iterator_wrapper.h »
1 // Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors.
4  
5 // Logger implementation that can be shared by all environments
6 // where enough Posix functionality is available.
7  
8 #ifndef STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
9 #define STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
10  
11 #include <algorithm>
12 #include <stdio.h>
13 #include <sys/time.h>
14 #include <time.h>
15 #include "leveldb/env.h"
16  
17 namespace leveldb {
18  
19 class PosixLogger : public Logger {
20 private:
21 FILE* file_;
22 uint64_t (*gettid_)(); // Return the thread id for the current thread
23 public:
24 4 PosixLogger(FILE* f, uint64_t (*gettid)()) : file_(f), gettid_(gettid) { }
25 8 virtual ~PosixLogger() {
26 4 fclose(file_);
27 8 }
28 7 virtual void Logv(const char* format, va_list ap) {
29 7 ▶ const uint64_t thread_id = (*gettid_)();
Branch: 1 branch
BranchTrueFalseTotal
const uint64_t thread_id = (*gettid_)(); 7 0 7
30  
31 // We try twice: the first time with a fixed-size stack allocated buffer,
32 // and the second time with a much larger dynamically allocated buffer.
33 char buffer[500];
34 7 ▶ for (int iter = 0; iter < 2; iter++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int iter = 0; iter < 2; iter++) { 7 0 7
35 char* base;
36 int bufsize;
37 7 ▶ if (iter == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter == 0) { 7 0 7
38 7 bufsize = sizeof(buffer);
39 7 base = buffer;
40 } else {
41 0 bufsize = 30000;
42 0 ▶ base = new char[bufsize];
Branch: 1 branch
BranchTrueFalseTotal
base = new char[bufsize]; 0 0 0
43 }
44 7 char* p = base;
45 7 char* limit = base + bufsize;
46  
47 struct timeval now_tv;
48 7 gettimeofday(&now_tv, NULL);
49 7 const time_t seconds = now_tv.tv_sec;
50 struct tm t;
51 7 localtime_r(&seconds, &t);
52 7 p += snprintf(p, limit - p,
53 "%04d/%02d/%02d-%02d:%02d:%02d.%06d %llx ",
54 7 t.tm_year + 1900,
55 7 t.tm_mon + 1,
56 t.tm_mday,
57 t.tm_hour,
58 t.tm_min,
59 t.tm_sec,
60 7 static_cast<int>(now_tv.tv_usec),
61 static_cast<long long unsigned int>(thread_id));
62  
63 // Print the message
64 7 ▶ if (p < limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (p < limit) { 7 0 7
65 va_list backup_ap;
66 7 va_copy(backup_ap, ap);
67 7 p += vsnprintf(p, limit - p, format, backup_ap);
68 7 va_end(backup_ap);
69 }
70  
71 // Truncate to available space if necessary
72 7 ▶ if (p >= limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (p >= limit) { 0 7 7
73 0 ▶ if (iter == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter == 0) { 0 0 0
74 0 continue; // Try again with larger buffer
75 } else {
76 0 p = limit - 1;
77 }
78 }
79  
80 // Add newline if necessary
81 7 ▶ if (p == base || p[-1] != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (p == base || p[-1] != '\n') { 7 0 7
if (p == base || p[-1] != '\n') { 3 4 7
82 3 *p++ = '\n';
83 }
84  
85 7 ▶ assert(p <= limit);
Branch: 1 branch
BranchTrueFalseTotal
assert(p <= limit); 0 7 7
86 7 ▶ fwrite(base, 1, p - base, file_);
Branch: 1 branch
BranchTrueFalseTotal
fwrite(base, 1, p - base, file_); 7 0 7
87 7 ▶ fflush(file_);
Branch: 1 branch
BranchTrueFalseTotal
fflush(file_); 7 0 7
88 7 ▶ if (base != buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (base != buffer) { 0 7 7
89 0 ▶ delete[] base;
Branch: 1 branch
BranchTrueFalseTotal
delete[] base; 0 0 0
90 }
91 7 break;
92 }
93 7 }
94 };
95  
96 }
97  
98 #endif // STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
« db/dbformat.h ↑ Summary table/iterator_wrapper.h »