hdfs-tail.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBHDFSPP_TOOLS_HDFS_TAIL
  19. #define LIBHDFSPP_TOOLS_HDFS_TAIL
  20. #include <string>
  21. #include <boost/program_options.hpp>
  22. #include "hdfs-tool.h"
  23. namespace hdfs::tools {
  24. /**
  25. * {@class Tail} is an {@class HdfsTool} displays last kilobyte of the file to
  26. * stdout.
  27. */
  28. class Tail : public HdfsTool {
  29. public:
  30. /**
  31. * {@inheritdoc}
  32. */
  33. Tail(int argc, char **argv);
  34. // Abiding to the Rule of 5
  35. Tail(const Tail &) = default;
  36. Tail(Tail &&) = default;
  37. Tail &operator=(const Tail &) = delete;
  38. Tail &operator=(Tail &&) = delete;
  39. ~Tail() override = default;
  40. /**
  41. * {@inheritdoc}
  42. */
  43. [[nodiscard]] std::string GetDescription() const override;
  44. /**
  45. * {@inheritdoc}
  46. */
  47. [[nodiscard]] bool Do() override;
  48. protected:
  49. /**
  50. * {@inheritdoc}
  51. */
  52. [[nodiscard]] bool Initialize() override;
  53. /**
  54. * {@inheritdoc}
  55. */
  56. [[nodiscard]] bool ValidateConstraints() const override { return argc_ > 1; }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. [[nodiscard]] bool HandleHelp() const override;
  61. /**
  62. * Handle the path argument that's passed to this tool.
  63. *
  64. * @param path The path to the file which needs to be tailed.
  65. * @param follow Append data to the output as the file grows, as in Unix.
  66. *
  67. * @return A boolean indicating the result of this operation.
  68. */
  69. [[nodiscard]] virtual bool HandlePath(const std::string &path,
  70. bool follow) const;
  71. /**
  72. * The tail size in bytes.
  73. */
  74. static constexpr uint64_t tail_size_in_bytes{1024};
  75. /**
  76. * The refresh rate for {@link hdfs::tools::Tail} in seconds.
  77. */
  78. static constexpr int refresh_rate_in_sec{1};
  79. private:
  80. /**
  81. * A boost data-structure containing the description of positional arguments
  82. * passed to the command-line.
  83. */
  84. po::positional_options_description pos_opt_desc_;
  85. };
  86. } // namespace hdfs::tools
  87. #endif