1 // Copyright 2012 The Chromium Authors
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 "partition_alloc/partition_alloc_base/logging.h"
6 
7 // TODO(1151236): After finishing copying //base files to PA library, remove
8 // defined(BASE_CHECK_H_) from here.
9 #if defined(                                                                                 \
10     BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_PARTITION_ALLOC_BASE_CHECK_H_) || \
11     defined(BASE_CHECK_H_) ||                                                                \
12     defined(                                                                                 \
13         BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_PARTITION_ALLOC_CHECK_H_)
14 #error "logging.h should not include check.h"
15 #endif
16 
17 #include "build/build_config.h"
18 #include "partition_alloc/partition_alloc_base/component_export.h"
19 #include "partition_alloc/partition_alloc_base/debug/alias.h"
20 #include "partition_alloc/partition_alloc_base/immediate_crash.h"
21 
22 #include <algorithm>
23 
24 #if BUILDFLAG(IS_WIN)
25 #include <windows.h>
26 
27 #include <io.h>
28 #endif
29 
30 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
31 #include <unistd.h>
32 
33 #include <cerrno>
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstring>
37 #endif
38 
39 #include "partition_alloc/partition_alloc_base/posix/eintr_wrapper.h"
40 
41 namespace partition_alloc::internal::logging {
42 
43 namespace {
44 
45 int g_min_log_level = 0;
46 
47 #if !BUILDFLAG(IS_WIN)
WriteToStderr(const char * data,size_t length)48 void WriteToStderr(const char* data, size_t length) {
49   size_t bytes_written = 0;
50   int rv;
51   while (bytes_written < length) {
52     rv = WrapEINTR(write)(STDERR_FILENO, data + bytes_written,
53                           length - bytes_written);
54     if (rv < 0) {
55       // Give up, nothing we can do now.
56       break;
57     }
58     bytes_written += rv;
59   }
60 }
61 #else   // !BUILDFLAG(IS_WIN)
WriteToStderr(const char * data,size_t length)62 void WriteToStderr(const char* data, size_t length) {
63   HANDLE handle = ::GetStdHandle(STD_ERROR_HANDLE);
64   const char* ptr = data;
65   const char* ptr_end = data + length;
66   while (ptr < ptr_end) {
67     DWORD bytes_written = 0;
68     if (!::WriteFile(handle, ptr, ptr_end - ptr, &bytes_written, nullptr) ||
69         bytes_written == 0) {
70       // Give up, nothing we can do now.
71       break;
72     }
73     ptr += bytes_written;
74   }
75 }
76 #endif  // !BUILDFLAG(IS_WIN)
77 
78 }  // namespace
79 
SetMinLogLevel(int level)80 void SetMinLogLevel(int level) {
81   g_min_log_level = std::min(LOGGING_FATAL, level);
82 }
83 
GetMinLogLevel()84 int GetMinLogLevel() {
85   return g_min_log_level;
86 }
87 
ShouldCreateLogMessage(int severity)88 bool ShouldCreateLogMessage(int severity) {
89   if (severity < g_min_log_level) {
90     return false;
91   }
92 
93   // Return true here unless we know ~LogMessage won't do anything.
94   return true;
95 }
96 
GetVlogVerbosity()97 int GetVlogVerbosity() {
98   return std::max(-1, LOGGING_INFO - GetMinLogLevel());
99 }
100 
RawLog(int level,const char * message)101 void RawLog(int level, const char* message) {
102   if (level >= g_min_log_level && message) {
103 #if !BUILDFLAG(IS_WIN)
104     const size_t message_len = strlen(message);
105 #else   // !BUILDFLAG(IS_WIN)
106     const size_t message_len = ::lstrlenA(message);
107 #endif  // !BUILDFLAG(IS_WIN)
108     WriteToStderr(message, message_len);
109 
110     if (message_len > 0 && message[message_len - 1] != '\n') {
111       WriteToStderr("\n", 1);
112     }
113   }
114 }
115 
116 }  // namespace partition_alloc::internal::logging
117