2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2010-08-06 05:46:09 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Assertions.h"
|
2023-12-22 11:57:49 +00:00
|
|
|
#include "common/Console.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/StringUtil.h"
|
2010-08-06 05:46:09 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
2010-08-16 15:01:13 +00:00
|
|
|
// TraceLogDescriptor
|
2010-08-06 05:46:09 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-08-16 15:01:13 +00:00
|
|
|
// Provides textual information for use by UIs; to give the end user a selection screen for
|
|
|
|
// enabling/disabling logs, and also for saving the log settings to INI.
|
|
|
|
//
|
|
|
|
struct TraceLogDescriptor
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// short name, alphanumerics only: used for saving/loading options.
|
2022-05-18 13:27:23 +00:00
|
|
|
const char* ShortName;
|
2010-08-06 05:46:09 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Standard UI name for this log source. Used in menus, options dialogs.
|
2022-05-18 13:27:23 +00:00
|
|
|
const char* Name;
|
2010-08-16 15:01:13 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Length description for use as a tooltip or menu item description.
|
2022-05-18 13:27:23 +00:00
|
|
|
const char* Description;
|
2010-08-06 05:46:09 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
const char* GetShortName() const
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2023-12-22 10:30:31 +00:00
|
|
|
pxAssumeMsg(Name, "Tracelog descriptors require a valid name!");
|
2021-09-06 18:28:26 +00:00
|
|
|
return ShortName ? ShortName : Name;
|
|
|
|
}
|
2010-08-16 15:01:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// BaseTraceLogSource
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// This class houses the base attributes for any trace log (to file or to console), which
|
|
|
|
// only includes the logfile's name, description, and enabled bit (for UIs and ini files),
|
|
|
|
// and an IsActive() method for determining if the log should be written or not.
|
|
|
|
//
|
|
|
|
// Derived classes then provide their own Write/DoWrite functions that format and write
|
|
|
|
// the log to the intended target(s).
|
|
|
|
//
|
|
|
|
// All individual calls to log write functions should be responsible for checking the
|
|
|
|
// status of the log via the IsActive() method manually (typically done via macro). This
|
|
|
|
// is done in favor of internal checks because most logs include detailed/formatted
|
|
|
|
// information, which itself can take a lot of cpu power to prepare. If the IsActive()
|
|
|
|
// check is done top-level, the parameters' calculations can be skipped. If the IsActive()
|
|
|
|
// check is done internally as part of the Write/Format calls, all parameters have to be
|
|
|
|
// resolved regardless of if the log is actually active.
|
2016-11-12 15:28:37 +00:00
|
|
|
//
|
2010-08-16 15:01:13 +00:00
|
|
|
class BaseTraceLogSource
|
|
|
|
{
|
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
const TraceLogDescriptor* m_Descriptor;
|
2010-08-16 15:01:13 +00:00
|
|
|
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
// Indicates if the user has enabled this specific log. This boolean only represents
|
|
|
|
// the configured status of this log, and does *NOT* actually mean the log is active
|
|
|
|
// even when TRUE. Because many tracelogs have master enablers that act on a group
|
|
|
|
// of logs, logging checks should always use IsActive() instead to determine if a log
|
|
|
|
// should be processed or not.
|
|
|
|
bool Enabled;
|
2010-08-16 15:01:13 +00:00
|
|
|
|
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
BaseTraceLogSource()
|
|
|
|
: m_Descriptor(NULL)
|
|
|
|
, Enabled(false)
|
|
|
|
{
|
|
|
|
}
|
2010-08-16 15:01:13 +00:00
|
|
|
|
2010-08-06 05:46:09 +00:00
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
BaseTraceLogSource(const TraceLogDescriptor* desc)
|
|
|
|
{
|
2023-12-22 10:30:31 +00:00
|
|
|
pxAssumeMsg(desc, "Trace logs must have a valid (non-NULL) descriptor.");
|
2021-09-06 18:28:26 +00:00
|
|
|
Enabled = false;
|
|
|
|
m_Descriptor = desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provides a categorical identifier, typically in "group.subgroup.subgroup" form.
|
|
|
|
// (use periods in favor of colons, since they do not require escape characters when
|
|
|
|
// written to ini/config files).
|
2022-05-18 13:27:23 +00:00
|
|
|
virtual std::string GetCategory() const { return std::string(); }
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
// This method should be used to determine if a log should be generated or not.
|
|
|
|
// See the class overview comments for details on how and why this method should
|
|
|
|
// be used.
|
|
|
|
virtual bool IsActive() const { return Enabled; }
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
virtual const char* GetShortName() const { return m_Descriptor->GetShortName(); }
|
|
|
|
virtual const char* GetName() const { return m_Descriptor->Name; }
|
|
|
|
virtual const char* GetDescription() const
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
return (m_Descriptor->Description != NULL) ? m_Descriptor->Description : "";
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool HasDescription() const { return m_Descriptor->Description != NULL; }
|
2010-08-06 05:46:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// TextFileTraceLog
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// This class is tailored for performance logging to file. It does not support console
|
|
|
|
// colors or wide/unicode text conversion.
|
|
|
|
//
|
2010-08-16 15:01:13 +00:00
|
|
|
class TextFileTraceLog : public BaseTraceLogSource
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
TextFileTraceLog(const TraceLogDescriptor* desc)
|
|
|
|
: BaseTraceLogSource(desc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Write(const char* fmt, ...) const
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteV(const char* fmt, va_list list) const
|
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string ascii;
|
2021-09-06 18:28:26 +00:00
|
|
|
ApplyPrefix(ascii);
|
2022-05-18 13:27:23 +00:00
|
|
|
ascii += StringUtil::StdStringFromFormatV(fmt, list);
|
|
|
|
DoWrite(ascii.c_str());
|
2021-09-06 18:28:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
virtual void ApplyPrefix(std::string& ascii) const {}
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual void DoWrite(const char* fmt) const = 0;
|
2010-08-06 05:46:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// ConsoleLogSource
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// This class is tailored for logging to console. It applies default console color attributes
|
|
|
|
// to all writes, and supports both char and wxChar (Ascii and UF8/16) formatting.
|
|
|
|
//
|
2010-08-16 15:01:13 +00:00
|
|
|
class ConsoleLogSource : public BaseTraceLogSource
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
ConsoleColors DefaultColor;
|
2010-08-06 05:46:09 +00:00
|
|
|
|
2010-08-16 15:01:13 +00:00
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
ConsoleLogSource()
|
|
|
|
: DefaultColor(Color_Gray)
|
|
|
|
{
|
|
|
|
}
|
2010-08-16 15:01:13 +00:00
|
|
|
|
2010-08-06 05:46:09 +00:00
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
ConsoleLogSource(const TraceLogDescriptor* desc, ConsoleColors defaultColor = Color_Gray)
|
|
|
|
: BaseTraceLogSource(desc)
|
|
|
|
{
|
|
|
|
DefaultColor = defaultColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes to the console using the source's default color. Note that the source's default
|
|
|
|
// color will always be used, thus ConsoleColorScope() will not be effectual unless the
|
|
|
|
// console's default color is Color_Default.
|
|
|
|
bool Write(const char* fmt, ...) const
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes to the console using the specified color. This overrides the default color setting
|
|
|
|
// for this log.
|
|
|
|
bool Write(ConsoleColors color, const char* fmt, ...) const
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(color, fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteV(const char* fmt, va_list list) const;
|
|
|
|
|
|
|
|
bool WriteV(ConsoleColors color, const char* fmt, va_list list) const;
|
2010-08-06 05:46:09 +00:00
|
|
|
};
|