FileAppender.hh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * FileAppender.hh
  3. *
  4. * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2000, Bastiaan Bakker. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_FILEAPPENDER_HH
  10. #define _LOG4CPP_FILEAPPENDER_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <log4cpp/LayoutAppender.hh>
  13. #include <string>
  14. #include <stdarg.h>
  15. LOG4CPP_NS_BEGIN
  16. class LOG4CPP_EXPORT FileAppender : public LayoutAppender {
  17. public:
  18. /**
  19. Constructs a FileAppender.
  20. @param name the name of the Appender.
  21. @param fileName the name of the file to which the Appender has
  22. to log.
  23. @param append whether the Appender has to truncate the file or
  24. just append to it if it already exists. Defaults to 'true'.
  25. @param mode file mode to open the logfile with. Defaults to 00644.
  26. **/
  27. FileAppender(const std::string& name, const std::string& fileName,
  28. bool append = true, mode_t mode = 00644);
  29. /**
  30. Constructs a FileAppender to an already open file descriptor.
  31. @param name the name of the Appender.
  32. @param fd the file descriptor to which the Appender has to log.
  33. **/
  34. FileAppender(const std::string& name, int fd);
  35. virtual ~FileAppender();
  36. /**
  37. Reopens the logfile.
  38. This can be useful for logfiles that are rotated externally,
  39. e.g. by logrotate. This method is a NOOP for FileAppenders that
  40. have been constructed with a file descriptor.
  41. @returns true if the reopen succeeded.
  42. **/
  43. virtual bool reopen();
  44. /**
  45. Closes the logfile.
  46. **/
  47. virtual void close();
  48. /**
  49. Sets the append vs truncate flag.
  50. NB. currently the FileAppender opens the logfile in the
  51. constructor. Therefore this method is too late to influence the
  52. first file opening. We'll need something similar to log4j's
  53. activateOptions().
  54. @param append false to truncate, true to append
  55. **/
  56. virtual void setAppend(bool append);
  57. /**
  58. Gets the value of the 'append' option.
  59. **/
  60. virtual bool getAppend() const;
  61. /**
  62. Sets the file open mode.
  63. **/
  64. virtual void setMode(mode_t mode);
  65. /**
  66. Gets the file open mode.
  67. **/
  68. virtual mode_t getMode() const;
  69. protected:
  70. virtual void _append(const LoggingEvent& event);
  71. const std::string _fileName;
  72. int _fd;
  73. int _flags;
  74. mode_t _mode;
  75. };
  76. LOG4CPP_NS_END
  77. #endif // _LOG4CPP_FILEAPPENDER_HH