💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.7 KiB

/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code 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.
*
* Antkeeper source code 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 Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_LOGGER_HPP
#define ANTKEEPER_LOGGER_HPP
#include <list>
#include <ostream>
#include <string>
#include <unordered_map>
class logger
{
public:
logger();
~logger();
/**
* Redirects log output to the specified output stream.
*
* @param stream Output stream to which log text will be written.
*/
void redirect(std::ostream* stream);
/**
* Outputs text to the log.
*/
void log(const std::string& text);
void warning(const std::string& text);
void error(const std::string& text);
void success(const std::string& text);
void set_log_prefix(const std::string& prefix);
void set_log_postfix(const std::string& postfix);
void set_warning_prefix(const std::string& prefix);
void set_warning_postfix(const std::string& postfix);
void set_error_prefix(const std::string& prefix);
void set_error_postfix(const std::string& postfix);
void set_success_prefix(const std::string& prefix);
void set_success_postfix(const std::string& postfix);
void push_prefix(const std::string& prefix);
void pop_prefix();
/**
* Opens a task and outputs it to the log.
*
* @return Task ID used to close the task later.
*/
int open_task(const std::string& text);
/**
* Closes a task and outputs its status to the log.
*
* @param id ID of the task to close.
* @param status Exit status of the task. A value of `0` or `EXIT_SUCCESS` indicates the task exited successfully. A non-zero exit status indicates the task failed.
* @return `true` if the task was closed, `false` if no task with the specified ID was found.
*/
bool close_task(int id, int status);
private:
std::ostream* os;
std::string log_prefix;
std::string log_postfix;
std::string warning_prefix;
std::string warning_postfix;
std::string error_prefix;
std::string error_postfix;
std::string success_prefix;
std::string success_postfix;
std::list<std::string> prefix_stack;
int next_task_id;
std::unordered_map<int, std::string> tasks;
};
#endif // ANTKEEPER_LOGGER_HPP