log_posix.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #ifdef GPR_POSIX_LOG
22 
23 #include <inttypes.h>
24 #include <pthread.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29 
30 #include <string>
31 
32 #include "absl/strings/str_format.h"
33 
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 #include <grpc/support/time.h>
37 
39 
41 
42 static intptr_t sys_gettid(void) { return (intptr_t)pthread_self(); }
43 
44 void gpr_log(const char* file, int line, gpr_log_severity severity,
45  const char* format, ...) {
46  /* Avoid message construction if gpr_log_message won't log */
47  if (gpr_should_log(severity) == 0) {
48  return;
49  }
50  char buf[64];
51  char* allocated = nullptr;
52  char* message = nullptr;
53  int ret;
54  va_list args;
55  va_start(args, format);
56  ret = vsnprintf(buf, sizeof(buf), format, args);
57  va_end(args);
58  if (ret < 0) {
59  message = nullptr;
60  } else if ((size_t)ret <= sizeof(buf) - 1) {
61  message = buf;
62  } else {
63  message = allocated = (char*)gpr_malloc((size_t)ret + 1);
64  va_start(args, format);
65  vsnprintf(message, (size_t)(ret + 1), format, args);
66  va_end(args);
67  }
69  gpr_free(allocated);
70 }
71 
73  const char* final_slash;
74  const char* display_file;
75  char time_buffer[64];
76  time_t timer;
78  struct tm tm;
79 
80  timer = (time_t)now.tv_sec;
81  final_slash = strrchr(args->file, '/');
82  if (final_slash == nullptr)
83  display_file = args->file;
84  else
85  display_file = final_slash + 1;
86 
87  if (!localtime_r(&timer, &tm)) {
88  strcpy(time_buffer, "error:localtime");
89  } else if (0 ==
90  strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
91  strcpy(time_buffer, "error:strftime");
92  }
93 
95  "%s%s.%09d %7" PRIdPTR " %s:%d]", gpr_log_severity_string(args->severity),
96  time_buffer, (int)(now.tv_nsec), sys_gettid(), display_file, args->line);
97 
98  absl::optional<std::string> stack_trace =
101  : absl::nullopt;
102  if (stack_trace) {
103  fprintf(stderr, "%-70s %s\n%s\n", prefix.c_str(), args->message,
104  stack_trace->c_str());
105  } else {
106  fprintf(stderr, "%-70s %s\n", prefix.c_str(), args->message);
107  }
108 }
109 
110 #endif /* defined(GPR_POSIX_LOG) */
vsnprintf
int __cdecl vsnprintf(char *buffer, size_t count, const char *format, va_list argptr)
Definition: libc.cpp:135
now
static double now(void)
Definition: test/core/fling/client.cc:130
http2_test_server.format
format
Definition: http2_test_server.py:118
log.h
bloat_diff.severity
def severity
Definition: bloat_diff.py:143
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
gpr_should_log
GPRAPI void GPRAPI int gpr_should_log(gpr_log_severity severity)
Definition: log.cc:67
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
time.h
gpr_log_severity_string
const GPRAPI char * gpr_log_severity_string(gpr_log_severity severity)
Definition: log.cc:55
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
grpc_core::GetCurrentStackTrace
absl::optional< std::string > GetCurrentStackTrace()
Definition: src/core/lib/gprpp/examine_stack.cc:36
gpr_log_func_args
Definition: include/grpc/impl/codegen/log.h:77
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
gpr_log_severity
gpr_log_severity
Definition: include/grpc/impl/codegen/log.h:45
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
absl::optional< std::string >
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
examine_stack.h
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
tm
static uv_timer_t tm
Definition: test-tcp-open.c:41
gpr_default_log
void gpr_default_log(gpr_log_func_args *args)
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
regen-readme.line
line
Definition: regen-readme.py:30
gpr_log_message
GPRAPI void gpr_log_message(const char *file, int line, gpr_log_severity severity, const char *message)
Definition: log.cc:81
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
gpr_timespec
Definition: gpr_types.h:50
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
gpr_should_log_stacktrace
int gpr_should_log_stacktrace(gpr_log_severity severity)
Definition: log.cc:74
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:29