Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include <cstdlib> |
| 12 | #include <iostream> |
| 13 | #include <sstream> |
| 14 | |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 15 | #include "platform/impl/logging.h" |
mark a. foltz | d6a5a22 | 2020-07-28 17:28:06 -0700 | [diff] [blame] | 16 | #include "platform/impl/logging_test.h" |
Yuri Wiitala | 8e6db3b | 2019-11-20 11:20:54 -0800 | [diff] [blame] | 17 | #include "util/trace_logging.h" |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 18 | |
| 19 | namespace openscreen { |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | int g_log_fd = STDERR_FILENO; |
| 23 | LogLevel g_log_level = LogLevel::kWarning; |
mark a. foltz | d6a5a22 | 2020-07-28 17:28:06 -0700 | [diff] [blame] | 24 | std::vector<std::string>* g_log_messages_for_test = nullptr; |
| 25 | bool* g_break_was_called_for_test = nullptr; |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 26 | |
| 27 | std::ostream& operator<<(std::ostream& os, const LogLevel& level) { |
| 28 | const char* level_string = ""; |
| 29 | switch (level) { |
| 30 | case LogLevel::kVerbose: |
| 31 | level_string = "VERBOSE"; |
| 32 | break; |
| 33 | case LogLevel::kInfo: |
| 34 | level_string = "INFO"; |
| 35 | break; |
| 36 | case LogLevel::kWarning: |
| 37 | level_string = "WARNING"; |
| 38 | break; |
| 39 | case LogLevel::kError: |
| 40 | level_string = "ERROR"; |
| 41 | break; |
| 42 | case LogLevel::kFatal: |
| 43 | level_string = "FATAL"; |
| 44 | break; |
| 45 | } |
| 46 | os << level_string; |
| 47 | return os; |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | void SetLogFifoOrDie(const char* filename) { |
| 53 | if (g_log_fd != STDERR_FILENO) { |
| 54 | close(g_log_fd); |
| 55 | g_log_fd = STDERR_FILENO; |
| 56 | } |
| 57 | |
mark a. foltz | b0b5891 | 2020-07-09 11:48:23 -0700 | [diff] [blame] | 58 | // Note: The use of OSP_CHECK/OSP_LOG_* here will log to stderr. |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 59 | struct stat st = {}; |
| 60 | int open_result = -1; |
| 61 | if (stat(filename, &st) == -1 && errno == ENOENT) { |
| 62 | if (mkfifo(filename, 0644) == 0) { |
| 63 | open_result = open(filename, O_WRONLY); |
| 64 | OSP_CHECK_NE(open_result, -1) |
| 65 | << "open(" << filename << ") failed: " << strerror(errno); |
| 66 | } else { |
| 67 | OSP_LOG_FATAL << "mkfifo(" << filename << ") failed: " << strerror(errno); |
| 68 | } |
| 69 | } else if (S_ISFIFO(st.st_mode)) { |
| 70 | open_result = open(filename, O_WRONLY); |
| 71 | OSP_CHECK_NE(open_result, -1) |
| 72 | << "open(" << filename << ") failed: " << strerror(errno); |
| 73 | } else { |
| 74 | OSP_LOG_FATAL << "not a FIFO special file: " << filename; |
| 75 | } |
| 76 | |
| 77 | // Direct all logging to the opened FIFO file. |
| 78 | g_log_fd = open_result; |
| 79 | } |
| 80 | |
| 81 | void SetLogLevel(LogLevel level) { |
| 82 | g_log_level = level; |
| 83 | } |
| 84 | |
mark a. foltz | d6a5a22 | 2020-07-28 17:28:06 -0700 | [diff] [blame] | 85 | LogLevel GetLogLevel() { |
| 86 | return g_log_level; |
| 87 | } |
| 88 | |
Yuri Wiitala | 75ea15d | 2019-12-03 16:01:48 -0800 | [diff] [blame] | 89 | bool IsLoggingOn(LogLevel level, const char* file) { |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 90 | // Possible future enhancement: Use glob patterns passed on the command-line |
| 91 | // to use a different logging level for certain files, like in Chromium. |
| 92 | return level >= g_log_level; |
| 93 | } |
| 94 | |
| 95 | void LogWithLevel(LogLevel level, |
Yuri Wiitala | 75ea15d | 2019-12-03 16:01:48 -0800 | [diff] [blame] | 96 | const char* file, |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 97 | int line, |
Yuri Wiitala | 75ea15d | 2019-12-03 16:01:48 -0800 | [diff] [blame] | 98 | std::stringstream message) { |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 99 | if (level < g_log_level) |
| 100 | return; |
| 101 | |
| 102 | std::stringstream ss; |
| 103 | ss << "[" << level << ":" << file << "(" << line << "):T" << std::hex |
Yuri Wiitala | 75ea15d | 2019-12-03 16:01:48 -0800 | [diff] [blame] | 104 | << TRACE_CURRENT_ID << "] " << message.rdbuf() << '\n'; |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 105 | const auto ss_str = ss.str(); |
| 106 | const auto bytes_written = write(g_log_fd, ss_str.c_str(), ss_str.size()); |
| 107 | OSP_DCHECK(bytes_written); |
mark a. foltz | d6a5a22 | 2020-07-28 17:28:06 -0700 | [diff] [blame] | 108 | if (g_log_messages_for_test) { |
| 109 | g_log_messages_for_test->push_back(ss_str); |
| 110 | } |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void Break() { |
mark a. foltz | d6a5a22 | 2020-07-28 17:28:06 -0700 | [diff] [blame] | 114 | if (g_break_was_called_for_test) { |
| 115 | *g_break_was_called_for_test = true; |
| 116 | return; |
| 117 | } |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 118 | #if defined(_DEBUG) |
| 119 | __builtin_trap(); |
| 120 | #else |
| 121 | std::abort(); |
| 122 | #endif |
| 123 | } |
| 124 | |
mark a. foltz | d6a5a22 | 2020-07-28 17:28:06 -0700 | [diff] [blame] | 125 | void SetLogBufferForTest(std::vector<std::string>* messages) { |
| 126 | g_log_messages_for_test = messages; |
| 127 | } |
| 128 | |
| 129 | void DisableBreakForTest(bool* break_was_called) { |
| 130 | g_break_was_called_for_test = break_was_called; |
| 131 | } |
| 132 | |
Yuri Wiitala | b797903 | 2019-11-08 15:17:20 -0800 | [diff] [blame] | 133 | } // namespace openscreen |