1 // Copyright 2020 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 #ifndef BASE_FUCHSIA_SCOPED_FX_LOGGER_H_ 6 #define BASE_FUCHSIA_SCOPED_FX_LOGGER_H_ 7 8 #include <fidl/fuchsia.logger/cpp/fidl.h> 9 #include <lib/syslog/structured_backend/cpp/fuchsia_syslog.h> 10 #include <lib/zx/socket.h> 11 #include <stdint.h> 12 13 #include <string> 14 #include <string_view> 15 #include <vector> 16 17 #include "base/base_export.h" 18 #include "base/logging.h" 19 20 namespace base { 21 22 // Emits log lines to a logger created via the specified LogSink. 23 // This class is thread-safe. 24 class BASE_EXPORT ScopedFxLogger { 25 public: 26 ScopedFxLogger(); 27 ~ScopedFxLogger(); 28 29 ScopedFxLogger(ScopedFxLogger&& other); 30 ScopedFxLogger& operator=(ScopedFxLogger&& other); 31 32 // Returns an instance connected to the process' incoming LogSink service. 33 // The returned instance has a single tag attributing the calling process in 34 // some way (e.g. by Component or process name). 35 // Additional tags may optionally be specified via `tags`. 36 static ScopedFxLogger CreateForProcess( 37 std::vector<std::string_view> tags = {}); 38 39 // Returns an instance connected to the specified LogSink. 40 static ScopedFxLogger CreateFromLogSink( 41 fidl::ClientEnd<fuchsia_logger::LogSink> client_end, 42 std::vector<std::string_view> tags = {}); 43 44 void LogMessage(std::string_view file, 45 uint32_t line_number, 46 std::string_view msg, 47 logging::LogSeverity severity); 48 is_valid()49 bool is_valid() const { return socket_.is_valid(); } 50 51 private: 52 ScopedFxLogger(std::vector<std::string_view> tags, zx::socket socket); 53 54 // For thread-safety these members should be treated as read-only. 55 // They are non-const only to allow move-assignment of ScopedFxLogger. 56 std::vector<std::string> tags_; 57 zx::socket socket_; 58 }; 59 60 } // namespace base 61 62 #endif // BASE_FUCHSIA_SCOPED_FX_LOGGER_H_ 63