2011-09-14 19:08:43 +00:00
|
|
|
/**
|
|
|
|
** Supermodel
|
|
|
|
** A Sega Model 3 Arcade Emulator.
|
2020-08-31 09:28:35 +00:00
|
|
|
** Copyright 2011-2020 Bart Trzynadlowski, Nik Henson, Ian Curtis,
|
|
|
|
** Harry Tuttle, and Spindizzi
|
2011-09-14 19:08:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of Supermodel.
|
|
|
|
**
|
|
|
|
** Supermodel is free software: you can redistribute it and/or modify it under
|
2020-08-31 09:28:35 +00:00
|
|
|
** the terms of the GNU General Public License as published by the Free
|
2011-09-14 19:08:43 +00:00
|
|
|
** Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
** any later version.
|
|
|
|
**
|
|
|
|
** Supermodel 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 Supermodel. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**/
|
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
/*
|
|
|
|
* Logger.h
|
|
|
|
*
|
|
|
|
* Header file for message logging. The OSD code is expected to set up a
|
2020-08-31 09:28:35 +00:00
|
|
|
* default logger (CFileLogger).
|
2011-08-19 20:43:07 +00:00
|
|
|
*/
|
2020-08-31 09:28:35 +00:00
|
|
|
|
2024-03-10 04:56:50 +00:00
|
|
|
#ifndef INCLUDED_LOGGER_H
|
|
|
|
#define INCLUDED_LOGGER_H
|
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
#include "Types.h"
|
2016-03-22 03:41:00 +00:00
|
|
|
#include "Version.h"
|
2020-08-31 09:28:35 +00:00
|
|
|
#include "Util/NewConfig.h"
|
2011-08-19 20:43:07 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
2020-08-31 09:28:35 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
2011-08-19 20:43:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
Class Definitions
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CLogger
|
2020-08-31 09:28:35 +00:00
|
|
|
*
|
2011-08-19 20:43:07 +00:00
|
|
|
* Abstract class that receives log messages from Supermodel's log functions.
|
|
|
|
* The logger object handles actual output of messages. Use the function-based
|
|
|
|
* message logging interface to generate messages.
|
|
|
|
*/
|
|
|
|
class CLogger
|
|
|
|
{
|
|
|
|
public:
|
2024-03-10 04:56:50 +00:00
|
|
|
// Log level in ascending order
|
|
|
|
enum LogLevel: int
|
|
|
|
{
|
|
|
|
All = 0,
|
|
|
|
Debug,
|
|
|
|
Info,
|
|
|
|
Error
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~CLogger()
|
|
|
|
{
|
|
|
|
}
|
2020-08-31 09:28:35 +00:00
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
/*
|
|
|
|
* DebugLog(fmt, ...):
|
|
|
|
* DebugLog(fmt, vl):
|
|
|
|
*
|
2020-08-31 09:28:35 +00:00
|
|
|
* Prints to debug log. If DEBUG is not defined, will end up doing nothing.
|
2011-08-19 20:43:07 +00:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fmt printf()-style format string.
|
2020-08-31 09:28:35 +00:00
|
|
|
* ... Variable number of parameters, corresponding to format
|
2011-08-19 20:43:07 +00:00
|
|
|
* string.
|
|
|
|
* vl Variable arguments already stored in a list.
|
|
|
|
*/
|
|
|
|
void DebugLog(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
va_start(vl, fmt);
|
|
|
|
DebugLog(fmt, vl);
|
|
|
|
va_end(vl);
|
|
|
|
}
|
2020-08-31 09:28:35 +00:00
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
virtual void DebugLog(const char *fmt, va_list vl) = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* InfoLog(fmt, ...):
|
|
|
|
* InfoLog(fmt, vl):
|
|
|
|
*
|
|
|
|
* Prints to error log but does not output an error to stderr. This is
|
|
|
|
* useful for printing session information to the error log.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fmt printf()-style format string.
|
2020-08-31 09:28:35 +00:00
|
|
|
* ... Variable number of parameters, corresponding to format
|
2011-08-19 20:43:07 +00:00
|
|
|
* string.
|
|
|
|
* vl Variable arguments already stored in a list.
|
|
|
|
*/
|
|
|
|
void InfoLog(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
va_start(vl, fmt);
|
|
|
|
InfoLog(fmt, vl);
|
|
|
|
va_end(vl);
|
|
|
|
}
|
2020-08-31 09:28:35 +00:00
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
virtual void InfoLog(const char *fmt, va_list vl) = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ErrorLog(fmt, ...):
|
|
|
|
* ErrorLog(fmt, vl):
|
|
|
|
*
|
|
|
|
* Prints to error log and outputs an error message to stderr.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fmt printf()-style format string.
|
2020-08-31 09:28:35 +00:00
|
|
|
* ... Variable number of parameters, corresponding to format
|
2011-08-19 20:43:07 +00:00
|
|
|
* string.
|
|
|
|
* vl Variable arguments already stored in a list.
|
|
|
|
*/
|
|
|
|
void ErrorLog(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
va_start(vl, fmt);
|
|
|
|
ErrorLog(fmt, vl);
|
|
|
|
va_end(vl);
|
|
|
|
}
|
2020-08-31 09:28:35 +00:00
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
virtual void ErrorLog(const char *fmt, va_list vl) = 0;
|
|
|
|
};
|
|
|
|
|
2020-08-31 09:28:35 +00:00
|
|
|
/*
|
|
|
|
* CMultiLogger:
|
|
|
|
*
|
|
|
|
* Redirects to multiple loggers.
|
|
|
|
*/
|
|
|
|
class CMultiLogger: public CLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void DebugLog(const char *fmt, va_list vl);
|
|
|
|
void InfoLog(const char *fmt, va_list vl);
|
|
|
|
void ErrorLog(const char *fmt, va_list vl);
|
|
|
|
CMultiLogger(std::vector<std::shared_ptr<CLogger>> loggers);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::shared_ptr<CLogger>> m_loggers;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CConsoleErrorLogger:
|
|
|
|
*
|
|
|
|
* Logger that prints only essential error messages, formatted appropriately,
|
|
|
|
* to the console (stderr).
|
|
|
|
*
|
|
|
|
* In the future, logging and console output need to be separated. This is
|
|
|
|
* intended to be an interrim solution.
|
|
|
|
*/
|
|
|
|
class CConsoleErrorLogger: public CLogger
|
|
|
|
{
|
|
|
|
void DebugLog(const char *fmt, va_list vl);
|
|
|
|
void InfoLog(const char *fmt, va_list vl);
|
|
|
|
void ErrorLog(const char *fmt, va_list vl);
|
|
|
|
};
|
|
|
|
|
2011-08-19 20:43:07 +00:00
|
|
|
/*
|
|
|
|
* CFileLogger:
|
2020-08-31 09:28:35 +00:00
|
|
|
*
|
2011-08-19 20:43:07 +00:00
|
|
|
* Default logger that logs to debug and error log files. Files are opened and
|
|
|
|
* closed for each message in order to preserve contents in case of program
|
|
|
|
* crash.
|
2020-08-31 09:28:35 +00:00
|
|
|
*/
|
|
|
|
class CFileLogger: public CLogger
|
2011-08-19 20:43:07 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-31 09:28:35 +00:00
|
|
|
void DebugLog(const char *fmt, va_list vl);
|
|
|
|
void InfoLog(const char *fmt, va_list vl);
|
|
|
|
void ErrorLog(const char *fmt, va_list vl);
|
|
|
|
CFileLogger(LogLevel level, std::vector<std::string> filenames);
|
|
|
|
CFileLogger(LogLevel level, std::vector<std::string> filenames, std::vector<FILE *> systemFiles);
|
2011-08-19 20:43:07 +00:00
|
|
|
|
2020-08-31 09:28:35 +00:00
|
|
|
private:
|
|
|
|
std::mutex m_mtx; // needed because we may close/reopen files and logging must be thread-safe
|
|
|
|
LogLevel m_logLevel;
|
|
|
|
const std::vector<std::string> m_logFilenames;
|
|
|
|
std::vector<std::ofstream> m_logFiles;
|
|
|
|
std::vector<FILE *> m_systemFiles;
|
2011-08-19 20:43:07 +00:00
|
|
|
|
2020-08-31 09:28:35 +00:00
|
|
|
void ReopenFiles(std::ios_base::openmode mode);
|
|
|
|
void WriteToFiles(const char *str);
|
|
|
|
};
|
2011-08-19 20:43:07 +00:00
|
|
|
|
2020-08-31 09:28:35 +00:00
|
|
|
/*
|
|
|
|
* CSystemLogger:
|
|
|
|
*
|
|
|
|
* Logs to the system log.
|
|
|
|
*/
|
|
|
|
class CSystemLogger: public CLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void DebugLog(const char *fmt, va_list vl);
|
|
|
|
void InfoLog(const char *fmt, va_list vl);
|
|
|
|
void ErrorLog(const char *fmt, va_list vl);
|
|
|
|
CSystemLogger(LogLevel level);
|
2011-08-19 20:43:07 +00:00
|
|
|
|
|
|
|
private:
|
2020-08-31 09:28:35 +00:00
|
|
|
LogLevel m_logLevel;
|
2011-08-19 20:43:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
Log Functions
|
2020-08-31 09:28:35 +00:00
|
|
|
|
|
|
|
Message logging interface. All messages are passed to the currently active
|
2016-07-05 01:15:58 +00:00
|
|
|
logger object.
|
2011-08-19 20:43:07 +00:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DebugLog(fmt, ...):
|
|
|
|
*
|
|
|
|
* Prints debugging information. The OSD layer may choose to print this to a
|
|
|
|
* log file, the screen, neither, or both. Newlines and other formatting codes
|
|
|
|
* must be explicitly included.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fmt A format string (the same as printf()).
|
|
|
|
* ... Variable number of arguments, as required by format string.
|
|
|
|
*/
|
|
|
|
extern void DebugLog(const char *fmt, ...);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ErrorLog(fmt, ...):
|
|
|
|
*
|
|
|
|
* Prints error information. Errors need not require program termination and
|
2020-08-31 09:28:35 +00:00
|
|
|
* may simply be informative warnings to the user. Newlines should not be
|
2011-08-19 20:43:07 +00:00
|
|
|
* included in the format string -- they are automatically added at the end of
|
|
|
|
* a line.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fmt A format string (the same as printf()).
|
|
|
|
* ... Variable number of arguments, as required by format string.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Must always return FAIL.
|
|
|
|
*/
|
2011-09-08 06:34:18 +00:00
|
|
|
extern bool ErrorLog(const char *fmt, ...);
|
2011-08-19 20:43:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* InfoLog(fmt, ...);
|
|
|
|
*
|
|
|
|
* Prints information to the error log file but does not print to stderr. This
|
|
|
|
* is useful for logging non-error information. Newlines are automatically
|
|
|
|
* appended.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fmt Format string (same as printf()).
|
|
|
|
* ... Variable number of arguments as required by format string.
|
|
|
|
*/
|
|
|
|
extern void InfoLog(const char *fmt, ...);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SetLogger(Logger):
|
|
|
|
*
|
|
|
|
* Sets the logger object to use.
|
|
|
|
*
|
|
|
|
* Parameters:
|
2020-08-31 09:28:35 +00:00
|
|
|
* Logger Unique pointer to a new logger object. If null pointer, log
|
|
|
|
* messages will not be output.
|
2011-08-19 20:43:07 +00:00
|
|
|
*/
|
2020-08-31 09:28:35 +00:00
|
|
|
extern void SetLogger(std::shared_ptr<CLogger> logger);
|
2011-08-19 20:43:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GetLogger(void):
|
|
|
|
*
|
|
|
|
* Returns:
|
2024-03-10 04:56:50 +00:00
|
|
|
* Current logger object (null pointer if not set).
|
2020-08-31 09:28:35 +00:00
|
|
|
*/
|
|
|
|
extern std::shared_ptr<CLogger> GetLogger(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CreateLogger(config):
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* A logger object that satisfies the requirements specified in the passed
|
|
|
|
* configuration or an empty pointer if an unrecoverable error occurred.
|
2011-08-19 20:43:07 +00:00
|
|
|
*/
|
2020-08-31 09:28:35 +00:00
|
|
|
std::shared_ptr<CLogger> CreateLogger(const Util::Config::Node &config);
|
2011-08-19 20:43:07 +00:00
|
|
|
|
2024-03-10 04:56:50 +00:00
|
|
|
|
|
|
|
#endif // INCLUDED_LOGGER_H
|