/*
 * SPDX-FileCopyrightText: 2000-2002 Copyright LifeLine Networks BV (www.lifeline.nl)
 * SPDX-FileCopyrightText: 2000-2002 Copyright Bastiaan Bakker
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#ifndef TANGO_COMMON_LOG4TANGO_FILEAPPENDER_H
#define TANGO_COMMON_LOG4TANGO_FILEAPPENDER_H

#include <tango/common/log4tango/LayoutAppender.h>
#include <tango/common/log4tango/Portability.h>

#include <sys/stat.h>

namespace log4tango
{

//-----------------------------------------------------------------------------
// class : FileAppender
//-----------------------------------------------------------------------------
class FileAppender : public LayoutAppender
{
  public:
    /**
    Constructs a FileAppender.
    @param name the name of the Appender.
    @param fileName the name of the file to which the Appender has
    to log.
    @param append whether the Appender has to truncate the file or
    just append to it if it already exists. Defaults to 'true'.
    @param mode file mode to open the logfile with. Defaults to 00644.
    **/
    FileAppender(const std::string &name, const std::string &fileName, bool append = true, mode_t mode = 00644);

    /**
    Constructs a FileAppender to an already open file descriptor.
    @param name the name of the Appender.
    @param fd the file descriptor to which the Appender has to log.
    **/
    FileAppender(const std::string &name, int fd);

    /**
     Destructor
    **/
    ~FileAppender() override;

    /**
    Reopens the logfile.
    This can be useful for logfiles that are rotated externally,
    e.g. by logrotate. This method is a NOOP for FileAppenders that
    have been constructed with a file descriptor.
    @returns true if the reopen succeeded.
    **/
    bool reopen() override;

    /**
    Closes the logfile.
    **/
    void close() override;

    /**
     * Check if the appender is valid.
     *
     * @returns true if the appender is valid, false otherwise.
     **/
    bool is_valid() const override;

    /**
    Sets the append vs truncate flag.
    NB. currently the FileAppender opens the logfile in the
    constructor. Therefore this method is too late to influence the
    first file opening. We'll need something similar to log4j's
    activateOptions().
    @param append false to truncate, true to append
    **/
    virtual void set_append(bool append);

    /**
    Gets the value of the 'append' option.
    **/
    virtual bool get_append() const;

    /**
    Sets the file open mode.
    **/
    virtual void set_mode(mode_t mode);

    /**
    Gets the file open mode.
    **/
    virtual mode_t get_mode() const;

  protected:
    int _append(const LoggingEvent &event) override;

    const std::string _file_name;
    int _fd;
    int _flags;
    mode_t _mode;
};

} // namespace log4tango

#endif // TANGO_COMMON_LOG4TANGO_FILEAPPENDER_H
