2014-10-28 00:36:00 -07:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-16 21:38:14 -08:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-28 00:36:00 -07:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
#pragma once
|
|
|
|
|
2015-05-11 22:19:44 -07:00
|
|
|
#include <chrono>
|
2018-07-02 10:10:41 -07:00
|
|
|
#include <memory>
|
2015-05-11 22:19:44 -07:00
|
|
|
#include <string>
|
2018-07-20 12:27:17 -07:00
|
|
|
#include <string_view>
|
2018-07-02 10:10:41 -07:00
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/filter.h"
|
2014-10-28 00:36:00 -07:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
namespace Log {
|
|
|
|
|
2015-05-11 22:19:44 -07:00
|
|
|
class Filter;
|
|
|
|
|
2014-10-28 00:36:00 -07:00
|
|
|
/**
|
|
|
|
* A log entry. Log entries are store in a structured format to permit more varied output
|
|
|
|
* formatting on different frontends, as well as facilitating filtering and aggregation.
|
|
|
|
*/
|
|
|
|
struct Entry {
|
|
|
|
std::chrono::microseconds timestamp;
|
|
|
|
Class log_class;
|
|
|
|
Level log_level;
|
2018-03-22 03:21:29 -07:00
|
|
|
std::string filename;
|
|
|
|
unsigned int line_num;
|
|
|
|
std::string function;
|
2014-10-28 00:36:00 -07:00
|
|
|
std::string message;
|
2018-09-09 04:08:57 -07:00
|
|
|
bool final_entry = false;
|
2014-10-28 00:36:00 -07:00
|
|
|
|
|
|
|
Entry() = default;
|
2016-02-04 19:39:33 -08:00
|
|
|
Entry(Entry&& o) = default;
|
2014-10-28 00:36:00 -07:00
|
|
|
|
2016-02-04 19:39:33 -08:00
|
|
|
Entry& operator=(Entry&& o) = default;
|
2018-03-22 03:21:29 -07:00
|
|
|
Entry& operator=(const Entry& o) = default;
|
2014-10-28 00:36:00 -07:00
|
|
|
};
|
|
|
|
|
2018-07-02 10:10:41 -07:00
|
|
|
/**
|
|
|
|
* Interface for logging backends. As loggers can be created and removed at runtime, this can be
|
|
|
|
* used by a frontend for adding a custom logging backend as needed
|
|
|
|
*/
|
|
|
|
class Backend {
|
|
|
|
public:
|
|
|
|
virtual ~Backend() = default;
|
|
|
|
virtual void SetFilter(const Filter& new_filter) {
|
|
|
|
filter = new_filter;
|
|
|
|
}
|
|
|
|
virtual const char* GetName() const = 0;
|
|
|
|
virtual void Write(const Entry& entry) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Filter filter;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend that writes to stderr without any color commands
|
|
|
|
*/
|
|
|
|
class ConsoleBackend : public Backend {
|
|
|
|
public:
|
|
|
|
static const char* Name() {
|
|
|
|
return "console";
|
|
|
|
}
|
|
|
|
const char* GetName() const override {
|
|
|
|
return Name();
|
|
|
|
}
|
|
|
|
void Write(const Entry& entry) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend that writes to stderr and with color
|
|
|
|
*/
|
|
|
|
class ColorConsoleBackend : public Backend {
|
|
|
|
public:
|
|
|
|
static const char* Name() {
|
|
|
|
return "color_console";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetName() const override {
|
|
|
|
return Name();
|
|
|
|
}
|
|
|
|
void Write(const Entry& entry) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend that writes to a file passed into the constructor
|
|
|
|
*/
|
|
|
|
class FileBackend : public Backend {
|
|
|
|
public:
|
|
|
|
explicit FileBackend(const std::string& filename);
|
|
|
|
|
|
|
|
static const char* Name() {
|
|
|
|
return "file";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetName() const override {
|
|
|
|
return Name();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(const Entry& entry) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileUtil::IOFile file;
|
2018-09-15 06:21:06 -07:00
|
|
|
std::size_t bytes_written;
|
2018-07-02 10:10:41 -07:00
|
|
|
};
|
|
|
|
|
2018-10-04 20:22:49 -07:00
|
|
|
/**
|
|
|
|
* Backend that writes to Visual Studio's output window
|
|
|
|
*/
|
|
|
|
class DebuggerBackend : public Backend {
|
|
|
|
public:
|
|
|
|
static const char* Name() {
|
|
|
|
return "debugger";
|
|
|
|
}
|
|
|
|
const char* GetName() const override {
|
|
|
|
return Name();
|
|
|
|
}
|
|
|
|
void Write(const Entry& entry) override;
|
|
|
|
};
|
|
|
|
|
2018-07-02 10:10:41 -07:00
|
|
|
void AddBackend(std::unique_ptr<Backend> backend);
|
|
|
|
|
2018-07-20 12:27:17 -07:00
|
|
|
void RemoveBackend(std::string_view backend_name);
|
2018-07-02 10:10:41 -07:00
|
|
|
|
2018-07-20 12:27:17 -07:00
|
|
|
Backend* GetBackend(std::string_view backend_name);
|
2018-07-02 10:10:41 -07:00
|
|
|
|
2014-10-28 00:36:00 -07:00
|
|
|
/**
|
2015-05-11 22:19:44 -07:00
|
|
|
* Returns the name of the passed log class as a C-string. Subclasses are separated by periods
|
|
|
|
* instead of underscores as in the enumeration.
|
2014-10-28 00:36:00 -07:00
|
|
|
*/
|
2015-05-11 22:19:44 -07:00
|
|
|
const char* GetLogClassName(Class log_class);
|
2014-10-28 00:36:00 -07:00
|
|
|
|
2015-05-11 22:19:44 -07:00
|
|
|
/**
|
|
|
|
* Returns the name of the passed log level as a C-string.
|
|
|
|
*/
|
|
|
|
const char* GetLevelName(Level log_level);
|
2014-10-28 00:36:00 -07:00
|
|
|
|
|
|
|
/// Creates a log entry by formatting the given source location, and message.
|
2016-09-17 17:38:01 -07:00
|
|
|
Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
|
2018-03-22 03:21:29 -07:00
|
|
|
const char* function, std::string message);
|
2014-10-28 00:36:00 -07:00
|
|
|
|
2018-07-02 10:10:41 -07:00
|
|
|
/**
|
|
|
|
* The global filter will prevent any messages from even being processed if they are filtered. Each
|
|
|
|
* backend can have a filter, but if the level is lower than the global filter, the backend will
|
|
|
|
* never get the message
|
|
|
|
*/
|
|
|
|
void SetGlobalFilter(const Filter& filter);
|
|
|
|
} // namespace Log
|