scoped_fx_logger.h 1.9 KB

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