FileConsumer.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file FileConsumer.hpp
  16. *
  17. */
  18. #ifndef _FASTDDS_FILE_CONSUMER_HPP_
  19. #define _FASTDDS_FILE_CONSUMER_HPP_
  20. #include <fastdds/dds/log/Log.hpp>
  21. #include <fstream>
  22. namespace eprosima {
  23. namespace fastdds {
  24. namespace dds {
  25. /**
  26. * Log consumer that writes the log events to a file.
  27. *
  28. * @file FileConsumer.hpp
  29. */
  30. class FileConsumer : public LogConsumer
  31. {
  32. public:
  33. //! Default constructor: filename = "output.log", append = false.
  34. RTPS_DllAPI FileConsumer();
  35. /** Constructor with parameters.
  36. * @param filename path of the output file where the log will be wrote.
  37. * @param append indicates if the consumer must append the content in the filename.
  38. */
  39. RTPS_DllAPI FileConsumer(
  40. const std::string& filename,
  41. bool append = false);
  42. /** \internal
  43. * Called by Log to ask us to consume the Entry.
  44. * @param Log::Entry to consume.
  45. */
  46. RTPS_DllAPI virtual void Consume(
  47. const Log::Entry&);
  48. virtual ~FileConsumer();
  49. private:
  50. void print_header(
  51. const Log::Entry&);
  52. void print_context(
  53. const Log::Entry&);
  54. std::string output_file_;
  55. std::ofstream file_;
  56. bool append_;
  57. };
  58. } // namespace dds
  59. } // namespace fastdds
  60. } // namespace eprosima
  61. #endif // _FASTDDS_FILE_CONSUMER_HPP_