ftpiiu_plugin/include/log.h

120 lines
3.2 KiB
C
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "platform.h"
#include <cstdarg>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
class Log;
using SharedLog = std::shared_ptr<Log>;
using WeakLog = std::weak_ptr<Log>;
2020-04-06 07:36:03 +02:00
/// \brief Log object
2020-04-05 21:16:16 +02:00
class Log
{
public:
2020-04-06 07:36:03 +02:00
/// \brief Log level
2020-04-05 21:16:16 +02:00
enum Level
{
DEBUG,
INFO,
ERROR,
COMMAND,
RESPONSE,
};
~Log ();
2020-04-06 07:36:03 +02:00
/// \brief Draw log
2020-04-05 21:16:16 +02:00
void draw ();
2020-04-06 07:36:03 +02:00
/// \brief Create log
2020-04-05 21:16:16 +02:00
static SharedLog create ();
2020-04-06 07:36:03 +02:00
/// \brief Bind log
/// \param log_ Log to bind
2020-04-05 21:16:16 +02:00
static void bind (SharedLog log_);
2020-04-06 07:36:03 +02:00
/// \brief Add debug message to bound log
/// \param fmt_ Message format
2020-04-05 21:16:16 +02:00
__attribute__ ((format (printf, 1, 2))) static void debug (char const *fmt_, ...);
2020-04-06 07:36:03 +02:00
/// \brief Add info message to bound log
/// \param fmt_ Message format
2020-04-05 21:16:16 +02:00
__attribute__ ((format (printf, 1, 2))) static void info (char const *fmt_, ...);
2020-04-06 07:36:03 +02:00
/// \brief Add error message to bound log
/// \param fmt_ Message format
2020-04-05 21:16:16 +02:00
__attribute__ ((format (printf, 1, 2))) static void error (char const *fmt_, ...);
2020-04-06 07:36:03 +02:00
/// \brief Add command message to bound log
/// \param fmt_ Message format
2020-04-05 21:16:16 +02:00
__attribute__ ((format (printf, 1, 2))) static void command (char const *fmt_, ...);
2020-04-06 07:36:03 +02:00
/// \brief Add response message to bound log
/// \param fmt_ Message format
2020-04-05 21:16:16 +02:00
__attribute__ ((format (printf, 1, 2))) static void response (char const *fmt_, ...);
2020-04-06 07:36:03 +02:00
/// \brief Add log message to bound log
/// \param level_ Log level
/// \param fmt_ Message format
/// \param ap_ Message arguments
2020-04-05 21:16:16 +02:00
static void log (Level level_, char const *fmt_, va_list ap_);
2020-04-06 07:36:03 +02:00
/// \brief Add log message to bound log
/// \param level_ Log level
/// \param message_ Message to log
2020-04-05 21:16:16 +02:00
static void log (Level level_, std::string_view message_);
private:
Log ();
2020-04-06 07:36:03 +02:00
/// \brief Add log message
/// \param level_ Log level
/// \param fmt_ Message format
/// \param ap_ Message arguments
2020-04-05 21:16:16 +02:00
void _log (Level level_, char const *fmt_, va_list ap_);
2020-04-06 07:36:03 +02:00
/// \brief Log message
2020-04-05 21:16:16 +02:00
struct Message
{
2020-04-06 07:36:03 +02:00
/// \brief Parameterized constructor
/// \param level_ Log level
/// \param message_ Log message
2020-04-05 21:16:16 +02:00
Message (Level const level_, std::string message_)
: level (level_), message (std::move (message_))
{
}
2020-04-06 07:36:03 +02:00
/// \brief Log level
2020-04-05 21:16:16 +02:00
Level level;
2020-04-06 07:36:03 +02:00
/// \brief Log message
2020-04-05 21:16:16 +02:00
std::string message;
};
2020-04-06 07:36:03 +02:00
/// \brief Log messages
2020-04-05 21:16:16 +02:00
std::vector<Message> m_messages;
2020-04-06 07:36:03 +02:00
/// \brief Log lock
2020-04-05 21:16:16 +02:00
platform::Mutex m_lock;
};