common.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2012 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. // Common helper functions/classes used both in the host and device forwarder.
  5. #ifndef TOOLS_ANDROID_FORWARDER2_COMMON_H_
  6. #define TOOLS_ANDROID_FORWARDER2_COMMON_H_
  7. #include <errno.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include "base/compiler_specific.h"
  11. #include "base/logging.h"
  12. #include "base/macros.h"
  13. #include "base/posix/eintr_wrapper.h"
  14. // Preserving errno for Close() is important because the function is very often
  15. // used in cleanup code, after an error occurred, and it is very easy to pass an
  16. // invalid file descriptor to close() in this context, or more rarely, a
  17. // spurious signal might make close() return -1 + setting errno to EINTR,
  18. // masking the real reason for the original error. This leads to very unpleasant
  19. // debugging sessions.
  20. #define PRESERVE_ERRNO_HANDLE_EINTR(Func) \
  21. do { \
  22. int local_errno = errno; \
  23. (void) HANDLE_EINTR(Func); \
  24. errno = local_errno; \
  25. } while (false);
  26. // Wrapper around RAW_LOG() which is signal-safe. The only purpose of this macro
  27. // is to avoid documenting uses of RawLog().
  28. #define SIGNAL_SAFE_LOG(Level, Msg) \
  29. RAW_LOG(Level, Msg);
  30. namespace forwarder2 {
  31. // Note that the two following functions are not signal-safe.
  32. // Chromium logging-aware implementation of libc's perror().
  33. void PError(const char* msg);
  34. // Closes the provided file descriptor and logs an error if it failed.
  35. void CloseFD(int fd);
  36. // Helps build a formatted C-string allocated in a fixed-size array. This is
  37. // useful in signal handlers where base::StringPrintf() can't be used safely
  38. // (due to its use of LOG()).
  39. template <int BufferSize>
  40. class FixedSizeStringBuilder {
  41. public:
  42. FixedSizeStringBuilder() {
  43. Reset();
  44. }
  45. const char* buffer() const { return buffer_; }
  46. void Reset() {
  47. buffer_[0] = 0;
  48. write_ptr_ = buffer_;
  49. }
  50. // Returns the number of bytes appended to the underlying buffer or -1 if it
  51. // failed.
  52. int Append(const char* format, ...) PRINTF_FORMAT(/* + 1 for 'this' */ 2, 3) {
  53. if (write_ptr_ >= buffer_ + BufferSize)
  54. return -1;
  55. va_list ap;
  56. va_start(ap, format);
  57. const int bytes_written = vsnprintf(
  58. write_ptr_, BufferSize - (write_ptr_ - buffer_), format, ap);
  59. va_end(ap);
  60. if (bytes_written > 0)
  61. write_ptr_ += bytes_written;
  62. return bytes_written;
  63. }
  64. private:
  65. char* write_ptr_;
  66. char buffer_[BufferSize];
  67. static_assert(BufferSize >= 1, "size of buffer must be at least one");
  68. DISALLOW_COPY_AND_ASSIGN(FixedSizeStringBuilder);
  69. };
  70. } // namespace forwarder2
  71. #endif // TOOLS_ANDROID_FORWARDER2_COMMON_H_