1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
base / check_op.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/check_op.h"
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <sstream>
#include "base/containers/span.h"
#include "base/logging.h"
#include "base/strings/cstring_view.h"
namespace logging {
char* CheckOpValueStr(int v) {
char buf[50];
snprintf(buf, sizeof(buf), "%d", v);
return strdup(buf);
}
char* CheckOpValueStr(unsigned v) {
char buf[50];
snprintf(buf, sizeof(buf), "%u", v);
return strdup(buf);
}
char* CheckOpValueStr(long v) {
char buf[50];
snprintf(buf, sizeof(buf), "%ld", v);
return strdup(buf);
}
char* CheckOpValueStr(unsigned long v) {
char buf[50];
snprintf(buf, sizeof(buf), "%lu", v);
return strdup(buf);
}
char* CheckOpValueStr(long long v) {
char buf[50];
snprintf(buf, sizeof(buf), "%lld", v);
return strdup(buf);
}
char* CheckOpValueStr(unsigned long long v) {
char buf[50];
snprintf(buf, sizeof(buf), "%llu", v);
return strdup(buf);
}
char* CheckOpValueStr(const void* v) {
char buf[50];
snprintf(buf, sizeof(buf), "%p", v);
return strdup(buf);
}
char* CheckOpValueStr(std::nullptr_t v) {
return strdup("nullptr");
}
char* CheckOpValueStr(const std::string& v) {
return strdup(v.c_str());
}
char* CheckOpValueStr(std::string_view v) {
// Ideally this would be `strndup`, but `strndup` is not portable. We have to
// use malloc() instead of HeapArray in order to match strdup() in the other
// overloads. The API contract is that the caller uses free() to release the
// pointer returned here.
char* ret = static_cast<char*>(malloc(v.size() + 1u));
auto [val, nul] =
// SAFETY: We allocated `ret` as `v.size() + 1` bytes above.
UNSAFE_BUFFERS(base::span<char>(ret, v.size() + 1u)).split_at(v.size());
val.copy_from(v);
nul.copy_from(base::span_from_ref('\0'));
return ret;
}
char* CheckOpValueStr(base::cstring_view v) {
return strdup(v.c_str());
}
char* CheckOpValueStr(double v) {
char buf[50];
snprintf(buf, sizeof(buf), "%.6lf", v);
return strdup(buf);
}
char* StreamValToStr(const void* v,
void (*stream_func)(std::ostream&, const void*)) {
std::stringstream ss;
stream_func(ss, v);
return strdup(ss.str().c_str());
}
char* CreateCheckOpLogMessageString(const char* expr_str,
char* v1_str,
char* v2_str) {
std::stringstream ss;
ss << "Check failed: " << expr_str << " (" << v1_str << " vs. " << v2_str
<< ")";
free(v1_str);
free(v2_str);
return strdup(ss.str().c_str());
}
} // namespace logging