log_linux.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 
19 #ifndef _POSIX_SOURCE
20 #define _POSIX_SOURCE
21 #endif
22 
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26 
28 
29 #ifdef GPR_LINUX_LOG
30 
31 #include <inttypes.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/syscall.h>
36 #include <time.h>
37 #include <unistd.h>
38 
39 #include <string>
40 
41 #include "absl/strings/str_format.h"
42 
43 #include <grpc/support/alloc.h>
44 #include <grpc/support/log.h>
45 #include <grpc/support/time.h>
46 
47 #include "src/core/lib/gpr/tls.h"
49 
51 
52 static long sys_gettid(void) { return syscall(__NR_gettid); }
53 
54 void gpr_log(const char* file, int line, gpr_log_severity severity,
55  const char* format, ...) {
56  /* Avoid message construction if gpr_log_message won't log */
57  if (gpr_should_log(severity) == 0) {
58  return;
59  }
60  char* message = nullptr;
61  va_list args;
62  va_start(args, format);
63  if (vasprintf(&message, format, args) == -1) {
64  va_end(args);
65  return;
66  }
67  va_end(args);
69  /* message has been allocated by vasprintf above, and needs free */
70  free(message);
71 }
72 
74  const char* final_slash;
75  const char* display_file;
76  char time_buffer[64];
77  time_t timer;
79  struct tm tm;
80  static GPR_THREAD_LOCAL(long) tid(0);
81  if (tid == 0) tid = sys_gettid();
82 
83  timer = static_cast<time_t>(now.tv_sec);
84  final_slash = strrchr(args->file, '/');
85  if (final_slash == nullptr) {
86  display_file = args->file;
87  } else {
88  display_file = final_slash + 1;
89  }
90 
91  if (!localtime_r(&timer, &tm)) {
92  strcpy(time_buffer, "error:localtime");
93  } else if (0 ==
94  strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
95  strcpy(time_buffer, "error:strftime");
96  }
97 
99  "%s%s.%09" PRId32 " %7ld %s:%d]", gpr_log_severity_string(args->severity),
100  time_buffer, now.tv_nsec, tid, display_file, args->line);
101 
102  absl::optional<std::string> stack_trace =
105  : absl::nullopt;
106  if (stack_trace) {
107  fprintf(stderr, "%-60s %s\n%s\n", prefix.c_str(), args->message,
108  stack_trace->c_str());
109  } else {
110  fprintf(stderr, "%-60s %s\n", prefix.c_str(), args->message);
111  }
112 }
113 
114 #endif /* GPR_LINUX_LOG */
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
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
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
tid
int tid
Definition: fake_binder_test.cc:236
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 >
syscall
const char * syscall
Definition: third_party/libuv/src/win/internal.h:270
examine_stack.h
GPR_THREAD_LOCAL
#define GPR_THREAD_LOCAL(type)
Definition: tls.h:151
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)
alloc.h
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
regen-readme.line
line
Definition: regen-readme.py:30
tls.h
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