Skip to content

Commit

Permalink
log: Create convars to control log level
Browse files Browse the repository at this point in the history
  • Loading branch information
misyltoad committed Aug 2, 2024
1 parent f35e1b3 commit 8e62848
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/Apps/gamescopereaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cassert>
#include <cstdlib>
#include <cstring>

#include <getopt.h>
#include <pthread.h>
Expand Down
17 changes: 8 additions & 9 deletions src/Backends/DRMBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,7 @@ extern GamescopePanelOrientation g_DesiredInternalOrientation;

extern bool g_bForceDisableColorMgmt;

static LogScope drm_log("drm");
static LogScope drm_verbose_log("drm", LOG_SILENT);
static LogScope drm_log( "drm" );

static std::unordered_map< std::string, std::string > pnps = {};

Expand Down Expand Up @@ -700,7 +699,7 @@ static void page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsi

// TODO: get the fbids_queued instance from data if we ever have more than one in flight

drm_verbose_log.debugf("page_flip_handler %" PRIu64, pCtx->ulPendingFlipCount);
drm_log.debugf("page_flip_handler %" PRIu64, pCtx->ulPendingFlipCount);
gpuvis_trace_printf("page_flip_handler %" PRIu64, pCtx->ulPendingFlipCount);

{
Expand Down Expand Up @@ -1423,7 +1422,7 @@ gamescope::OwningRc<gamescope::IBackendFb> drm_fbid_from_dmabuf( struct drm_t *d

if ( !wlr_drm_format_set_has( &drm->formats, dma_buf->format, dma_buf->modifier ) )
{
drm_verbose_log.errorf( "Cannot import FB to DRM: format 0x%" PRIX32 " and modifier 0x%" PRIX64 " not supported for scan-out", dma_buf->format, dma_buf->modifier );
drm_log.errorf( "Cannot import FB to DRM: format 0x%" PRIX32 " and modifier 0x%" PRIX64 " not supported for scan-out", dma_buf->format, dma_buf->modifier );
return nullptr;
}

Expand Down Expand Up @@ -1463,7 +1462,7 @@ gamescope::OwningRc<gamescope::IBackendFb> drm_fbid_from_dmabuf( struct drm_t *d
}
}

drm_verbose_log.debugf("make fbid %u", fb_id);
drm_log.debugf("make fbid %u", fb_id);

pBackendFb = new gamescope::CDRMFb( fb_id, buf );

Expand Down Expand Up @@ -2369,7 +2368,7 @@ drm_prepare_liftoff( struct drm_t *drm, const struct FrameInfo_t *frameInfo, boo

if ( pDrmFb == nullptr )
{
drm_verbose_log.errorf("drm_prepare_liftoff: layer %d has no FB", i );
drm_log.debugf("drm_prepare_liftoff: layer %d has no FB", i );
return -EINVAL;
}

Expand Down Expand Up @@ -2575,9 +2574,9 @@ drm_prepare_liftoff( struct drm_t *drm, const struct FrameInfo_t *frameInfo, boo
}

if ( ret == 0 )
drm_verbose_log.debugf( "can drm present %i layers", frameInfo->layerCount );
drm_log.debugf( "can drm present %i layers", frameInfo->layerCount );
else
drm_verbose_log.debugf( "can NOT drm present %i layers", frameInfo->layerCount );
drm_log.debugf( "can NOT drm present %i layers", frameInfo->layerCount );

return ret;
}
Expand Down Expand Up @@ -3654,7 +3653,7 @@ namespace gamescope
m_uNextPresentCtx = ( m_uNextPresentCtx + 1 ) % 3;
m_PresentCtxs[uCurrentPresentCtx].ulPendingFlipCount = m_PresentFeedback.m_uQueuedPresents;

drm_verbose_log.debugf("flip commit %" PRIu64, (uint64_t)m_PresentFeedback.m_uQueuedPresents);
drm_log.debugf("flip commit %" PRIu64, (uint64_t)m_PresentFeedback.m_uQueuedPresents);
gpuvis_trace_printf( "flip commit %" PRIu64, (uint64_t)m_PresentFeedback.m_uQueuedPresents );

ret = drmModeAtomicCommit(drm->fd, drm->req, drm->flags, &m_PresentCtxs[uCurrentPresentCtx] );
Expand Down
2 changes: 2 additions & 0 deletions src/Backends/WaylandBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@ namespace gamescope
assert( m_pHostBuffer );
assert( m_pHostBuffer == pBuffer );

xdg_log.debugf( "buffer_release: %p", pBuffer );

OnCompositorRelease();
}

Expand Down
6 changes: 4 additions & 2 deletions src/InputEmulation.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#if HAVE_LIBEIS

#include <libeis.h>
#include <stdio.h>

#include <cstdio>
#include <cstring>

#include "InputEmulation.h"
#include "wlserver.hpp"

static LogScope gamescope_ei("gamescope-ei");
static LogScope gamescope_ei("gamescope_ei");

namespace gamescope
{
Expand Down
4 changes: 2 additions & 2 deletions src/convar.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace gamescope
template <typename T>
class ConVar : public ConCommand
{
using ConVarCallbackFunc = std::function<void()>;
using ConVarCallbackFunc = std::function<void(ConVar<T> &)>;
public:
ConVar( std::string_view pszName, T defaultValue = T{}, std::string_view pszDescription = "", ConVarCallbackFunc func = nullptr, bool bRunCallbackAtStartup = false )
: ConCommand( pszName, pszDescription, [this]( std::span<std::string_view> pArgs ){ this->InvokeFunc( pArgs ); } )
Expand Down Expand Up @@ -160,7 +160,7 @@ namespace gamescope
if ( !m_bInCallback && m_Callback )
{
m_bInCallback = true;
m_Callback();
m_Callback( *this );
m_bInCallback = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/edid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C"
#include "libdisplay-info/cta.h"
}

static LogScope edid_log("josh edid");
static LogScope edid_log("edid");

namespace gamescope
{
Expand Down
112 changes: 87 additions & 25 deletions src/log.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <cerrno>

#include <memory>
#include <format>

#include "Utils/Process.h"
#include "Utils/Defer.h"
#include "convar.h"
#include "log.hpp"

LogScope::LogScope(const char *name) {
this->name = name;
this->priority = LOG_DEBUG;
}

LogScope::LogScope(const char *name, enum LogPriority priority) {
this->name = name;
this->priority = priority;
}

bool LogScope::has(enum LogPriority priority) {
return priority <= this->priority;
}

static const char *GetLogName( LogPriority ePriority )
static constexpr std::string_view GetLogPriorityText( LogPriority ePriority )
{
switch ( ePriority )
{
Expand All @@ -36,10 +23,80 @@ static const char *GetLogName( LogPriority ePriority )
}
}

void LogScope::vlogf(enum LogPriority priority, const char *fmt, va_list args) {
if (!this->has(priority)) {
return;
static constexpr std::string_view GetLogName( LogPriority ePriority )
{
switch ( ePriority )
{
case LOG_SILENT: return "silent";
case LOG_ERROR: return "error";
case LOG_WARNING: return "warning";
case LOG_DEBUG: return "debug";
default:
case LOG_INFO: return "info";
}
}

static constexpr LogPriority GetPriorityFromString( std::string_view psvScope )
{
if ( psvScope == "silent" )
return LOG_SILENT;
else if ( psvScope == "error" )
return LOG_ERROR;
else if ( psvScope == "warning" )
return LOG_WARNING;
else if ( psvScope == "debug" )
return LOG_DEBUG;
else
return LOG_INFO;
}

struct LogConVar_t
{
LogConVar_t( LogScope *pScope, std::string_view psvName, LogPriority eDefaultPriority )
: sName{ std::format( "log_{}", psvName ) }
, sDescription{ std::format( "Max logging priority for the {} channel. Valid options are: [ silent, error, warning, debug, info ].", psvName ) }
, convar
{ sName, std::string( GetLogName( eDefaultPriority ) ), sDescription,
[ pScope ]( gamescope::ConVar<std::string> &cvar )
{
pScope->SetPriority( GetPriorityFromString( cvar ) );
},
}
{

}
std::string sName;
std::string sDescription;

gamescope::ConVar<std::string> convar;
};

LogScope::LogScope( std::string_view psvName, LogPriority eMaxPriority )
: LogScope( psvName, psvName, eMaxPriority )
{
}

LogScope::LogScope( std::string_view psvName, std::string_view psvPrefix, LogPriority eMaxPriority )
: m_psvName{ psvName }
, m_psvPrefix{ psvPrefix }
, m_eMaxPriority{ eMaxPriority }
, m_pEnableConVar{ std::make_unique<LogConVar_t>( this, psvName, eMaxPriority ) }
{
}

LogScope::~LogScope()
{
}

bool LogScope::Enabled( LogPriority ePriority ) const
{
return ePriority <= m_eMaxPriority;
}

void LogScope::vlogf(enum LogPriority priority, const char *fmt, va_list args)
{
if ( !Enabled( priority ) )
return;

char *buf = nullptr;
vasprintf(&buf, fmt, args);
Expand All @@ -48,10 +105,15 @@ void LogScope::vlogf(enum LogPriority priority, const char *fmt, va_list args) {
defer( free(buf); );

for (auto& listener : m_LoggingListeners)
listener.second( priority, this->name, buf );
listener.second( priority, m_psvPrefix, buf );

std::string_view psvLogName = GetLogPriorityText( priority );
if ( bPrefixEnabled )
fprintf(stderr, "[%s] %s \e[0;37m%s:\e[0m %s\n", gamescope::Process::GetProcessName(), GetLogName( priority ), this->name, buf);
fprintf(stderr, "[%s] %.*s \e[0;37m%.*s:\e[0m %s\n",
gamescope::Process::GetProcessName(),
(int)psvLogName.size(), psvLogName.data(),
(int)this->m_psvPrefix.size(), this->m_psvPrefix.data(),
buf);
else
fprintf(stderr, "%s\n", buf);
}
Expand Down
44 changes: 28 additions & 16 deletions src/log.hpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
#pragma once

#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <cstdarg>
#include <cstdint>

#include <memory>
#include <functional>
#include <string_view>

#ifdef __GNUC__
#define ATTRIB_PRINTF(start, end) __attribute__((format(printf, start, end)))
#else
#define ATTRIB_PRINTF(start, end)
#endif

enum LogPriority {
enum LogPriority
{
LOG_SILENT,
LOG_ERROR,
LOG_WARNING,
LOG_INFO,
LOG_DEBUG,
};

class LogScope {
const char *name;
enum LogPriority priority;

bool has(enum LogPriority priority);
void vprintf(enum LogPriority priority, const char *fmt, va_list args) ATTRIB_PRINTF(3, 0);
void logf(enum LogPriority priority, const char *fmt, ...) ATTRIB_PRINTF(3, 4);
struct LogConVar_t;

class LogScope
{
public:
LogScope(const char *name);
LogScope(const char *name, enum LogPriority priority);
LogScope( std::string_view psvName, LogPriority eMaxPriority = LOG_INFO );
LogScope( std::string_view psvName, std::string_view psvPrefix, LogPriority eMaxPriority = LOG_INFO );
~LogScope();

bool Enabled( LogPriority ePriority ) const;
void SetPriority( LogPriority ePriority ) { m_eMaxPriority = ePriority; }

void vlogf(enum LogPriority priority, const char *fmt, va_list args) ATTRIB_PRINTF(3, 0);

Expand All @@ -44,6 +45,17 @@ class LogScope {

bool bPrefixEnabled = true;

using LoggingListenerFunc = std::function<void(LogPriority ePriority, const char *pScope, const char *pText)>;
using LoggingListenerFunc = std::function<void( LogPriority ePriority, std::string_view psvScope, const char *psvText )>;
std::unordered_map<uintptr_t, LoggingListenerFunc> m_LoggingListeners;

private:
void vprintf(enum LogPriority priority, const char *fmt, va_list args) ATTRIB_PRINTF(3, 0);
void logf(enum LogPriority priority, const char *fmt, ...) ATTRIB_PRINTF(3, 4);

std::string_view m_psvName;
std::string_view m_psvPrefix;

LogPriority m_eMaxPriority = LOG_INFO;

std::unique_ptr<LogConVar_t> m_pEnableConVar;
};
17 changes: 12 additions & 5 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,22 @@ gamescope_version = vcs_tag(
cpp_args: gamescope_cpp_args,
)

gamescope_core_src = [
'convar.cpp',
'log.cpp',
'Utils/Process.cpp',
'Utils/Version.cpp',
]

if pipewire_dep.found()
executable( 'gamescopestream', ['Apps/gamescopestream.cpp', 'log.cpp', 'Utils/Process.cpp'], gamescope_version, protocols_client_src, dependencies: [ pipewire_dep, dep_wayland, libdecor_dep ], install: true )
executable( 'gamescopestream', ['Apps/gamescopestream.cpp'], gamescope_core_src, gamescope_version, protocols_client_src, dependencies: [ pipewire_dep, dep_wayland, libdecor_dep ], install: true )
endif

executable('gamescopereaper', ['Utils/Process.cpp', 'Apps/gamescopereaper.cpp', 'log.cpp'], gamescope_version, install:true )
executable('gamescopereaper', ['Apps/gamescopereaper.cpp', gamescope_core_src], gamescope_version, install:true )

benchmark_dep = dependency('benchmark', required: get_option('benchmark'), disabler: true)
executable('gamescope_color_microbench', ['color_bench.cpp', 'color_helpers.cpp'], dependencies:[benchmark_dep, glm_dep])
executable('gamescope_color_microbench', ['color_bench.cpp', 'color_helpers.cpp'], gamescope_core_src, dependencies:[benchmark_dep, glm_dep])

executable('gamescope_color_tests', ['color_tests.cpp', 'color_helpers.cpp'], dependencies:[glm_dep])
executable('gamescope_color_tests', ['color_tests.cpp', 'color_helpers.cpp'], gamescope_core_src, dependencies:[glm_dep])

executable('gamescopectl', ['Apps/gamescopectl.cpp', 'convar.cpp', 'log.cpp', 'Utils/Version.cpp', 'Utils/Process.cpp'], gamescope_version, protocols_client_src, dependencies: [dep_wayland], install:true )
executable('gamescopectl', ['Apps/gamescopectl.cpp'], gamescope_core_src, gamescope_version, protocols_client_src, dependencies: [dep_wayland], install:true )
2 changes: 1 addition & 1 deletion src/rendervulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ bool CVulkanDevice::selectPhysDev(VkSurfaceKHR surface)
vk.GetPhysicalDeviceSurfaceSupportKHR( cphysDev, computeOnlyIndex, surface, &canPresent );
if ( !canPresent )
{
vk_log.debugf( "physical device %04x:%04x compute queue doesn't support presenting on our surface, using graphics queue", deviceProperties.vendorID, deviceProperties.deviceID );
vk_log.infof( "physical device %04x:%04x compute queue doesn't support presenting on our surface, using graphics queue", deviceProperties.vendorID, deviceProperties.deviceID );
computeOnlyIndex = ~0u;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7048,9 +7048,9 @@ static std::vector<uint32_t> s_uRelativeMouseFilteredAppids;
static gamescope::ConVar<std::string> cv_mouse_relative_filter_appids( "mouse_relative_filter_appids",
"8400" /* Geometry Wars: Retro Evolved */,
"Comma separated appids to filter out using relative mouse mode for.",
[]()
[]( gamescope::ConVar<std::string> &cvar )
{
std::vector<std::string_view> sFilterAppids = gamescope::Split( cv_mouse_relative_filter_appids, "," );
std::vector<std::string_view> sFilterAppids = gamescope::Split( cvar, "," );
std::vector<uint32_t> uFilterAppids;
uFilterAppids.reserve( sFilterAppids.size() );
for ( auto &sFilterAppid : sFilterAppids )
Expand Down
2 changes: 1 addition & 1 deletion src/wlserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ static const struct gamescope_private_interface gamescope_private_impl = {
static void gamescope_private_bind( struct wl_client *client, void *data, uint32_t version, uint32_t id )
{
struct wl_resource *resource = wl_resource_create( client, &gamescope_private_interface, version, id );
console_log.m_LoggingListeners[(uintptr_t)resource] = [ resource ](LogPriority ePriority, const char *pScope, const char *pText)
console_log.m_LoggingListeners[(uintptr_t)resource] = [ resource ]( LogPriority ePriority, std::string_view psvScope, const char *pText )
{
if ( !wlserver_is_lock_held() )
return;
Expand Down

0 comments on commit 8e62848

Please sign in to comment.