1 // Copyright 2020 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.
4 
5 #include "base/check_op.h"
6 
7 // check_op.h is a widely included header and its size has significant impact on
8 // build time. Try not to raise this limit unless absolutely necessary. See
9 // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/wmax_tokens.md
10 #ifndef NACL_TC_REV
11 #pragma clang max_tokens_here 244000
12 #endif
13 
14 #include <string.h>
15 
16 #include <cstdio>
17 #include <sstream>
18 
19 namespace logging {
20 
CheckOpValueStr(int v)21 char* CheckOpValueStr(int v) {
22   char buf[50];
23   snprintf(buf, sizeof(buf), "%d", v);
24   return strdup(buf);
25 }
26 
CheckOpValueStr(unsigned v)27 char* CheckOpValueStr(unsigned v) {
28   char buf[50];
29   snprintf(buf, sizeof(buf), "%u", v);
30   return strdup(buf);
31 }
32 
CheckOpValueStr(long v)33 char* CheckOpValueStr(long v) {
34   char buf[50];
35   snprintf(buf, sizeof(buf), "%ld", v);
36   return strdup(buf);
37 }
38 
CheckOpValueStr(unsigned long v)39 char* CheckOpValueStr(unsigned long v) {
40   char buf[50];
41   snprintf(buf, sizeof(buf), "%lu", v);
42   return strdup(buf);
43 }
44 
CheckOpValueStr(long long v)45 char* CheckOpValueStr(long long v) {
46   char buf[50];
47   snprintf(buf, sizeof(buf), "%lld", v);
48   return strdup(buf);
49 }
50 
CheckOpValueStr(unsigned long long v)51 char* CheckOpValueStr(unsigned long long v) {
52   char buf[50];
53   snprintf(buf, sizeof(buf), "%llu", v);
54   return strdup(buf);
55 }
56 
CheckOpValueStr(const void * v)57 char* CheckOpValueStr(const void* v) {
58   char buf[50];
59   snprintf(buf, sizeof(buf), "%p", v);
60   return strdup(buf);
61 }
62 
CheckOpValueStr(std::nullptr_t v)63 char* CheckOpValueStr(std::nullptr_t v) {
64   return strdup("nullptr");
65 }
66 
CheckOpValueStr(double v)67 char* CheckOpValueStr(double v) {
68   char buf[50];
69   snprintf(buf, sizeof(buf), "%.6lf", v);
70   return strdup(buf);
71 }
72 
StreamValToStr(const void * v,void (* stream_func)(std::ostream &,const void *))73 char* StreamValToStr(const void* v,
74                      void (*stream_func)(std::ostream&, const void*)) {
75   std::stringstream ss;
76   stream_func(ss, v);
77   return strdup(ss.str().c_str());
78 }
79 
CheckOpResult(const char * expr_str,char * v1_str,char * v2_str)80 CheckOpResult::CheckOpResult(const char* expr_str, char* v1_str, char* v2_str) {
81   std::ostringstream ss;
82   ss << expr_str << " (" << v1_str << " vs. " << v2_str << ")";
83   message_ = strdup(ss.str().c_str());
84   free(v1_str);
85   free(v2_str);
86 }
87 
88 }  // namespace logging
89