Skip to content

Commit

Permalink
Add a debug log setting
Browse files Browse the repository at this point in the history
Add a Boolean project setting that will output debug logging when set
  • Loading branch information
patrickdown committed Dec 16, 2023
1 parent 17a3860 commit f708ac6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 29 deletions.
27 changes: 7 additions & 20 deletions example.gd/addons/tiltfive/T5Interface.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extends Node
## It will instantiate the TileFive interface and register it with the XRServer.
## It will instantiate the TiltFive interface and register it with the XRServer.
##
## This script should be configured be automatically added as an autoload script
## when the plugin is enabled. This
Expand All @@ -26,19 +26,14 @@ var t5_manager : T5ManagerBase:

func get_tilt_five_xr_interface() -> TiltFiveXRInterface:
return tilt_five_xr_interface

func get_setting_or_default(name : String, default):
var val = ProjectSettings.get_setting_with_override(name)
if not val:
val = default
return val


func _enter_tree():
tilt_five_xr_interface = TiltFiveXRInterface.new();
tilt_five_xr_interface = TiltFiveXRInterface.new()
if tilt_five_xr_interface:
tilt_five_xr_interface.application_id = T5ProjectSettings.application_id
tilt_five_xr_interface.application_version = T5ProjectSettings.application_version
tilt_five_xr_interface.trigger_click_threshold = T5ProjectSettings.trigger_click_threshhold
tilt_five_xr_interface.debug_logging = T5ProjectSettings.is_debug_logging

XRServer.add_interface(tilt_five_xr_interface)
tilt_five_xr_interface.glasses_event.connect(_on_glasses_event)
Expand Down Expand Up @@ -83,27 +78,24 @@ func _on_service_event(event_num):
t5_manager.service_unvailable()
TiltFiveXRInterface.E_SERVICE_T5_INCOMPATIBLE_VERSION:
t5_manager.service_incorrect_version()

func _on_glasses_event(glasses_id, event_num):
var xr_rig_state = id_to_state.get(glasses_id) as XRRigState
if not xr_rig_state:
xr_rig_state = XRRigState.new()
id_to_state[glasses_id] = xr_rig_state
match event_num:
TiltFiveXRInterface.E_GLASSES_AVAILABLE:
print_verbose(glasses_id, " E_AVAILABLE")
xr_rig_state.available = true
_process_glasses()

TiltFiveXRInterface.E_GLASSES_UNAVAILABLE:
print_verbose(glasses_id, " E_UNAVAILABLE")
xr_rig_state.available = false
if xr_rig_state.attempting_to_reserve:
xr_rig_state.attempting_to_reserve = false
_process_glasses()

TiltFiveXRInterface.E_GLASSES_RESERVED:
print_verbose(glasses_id, " E_RESERVED")
xr_rig_state.reserved = true
xr_rig_state.attempting_to_reserve = false

Expand All @@ -116,7 +108,6 @@ func _on_glasses_event(glasses_id, event_num):
tilt_five_xr_interface.release_glasses(glasses_id)

TiltFiveXRInterface.E_GLASSES_DROPPED:
print_verbose(glasses_id, " E_DROPPED")
xr_rig_state.reserved = false

var xr_rig = xr_rig_state.xr_rig
Expand All @@ -132,10 +123,6 @@ func _on_glasses_event(glasses_id, event_num):
xr_rig._gameboard_type = gbt
xr_rig._gameboard_size = tilt_five_xr_interface.get_gameboard_extents(gbt)
t5_manager.set_gameboard_type(xr_rig, gbt)
print_verbose(glasses_id, " E_TRACKING, Gameboard size = ", )

TiltFiveXRInterface.E_GLASSES_NOT_TRACKING:
print_verbose(glasses_id, " E_NOT_TRACKING")


_:
print_verbose(glasses_id, " - unknown event: ", event_num)
print_verbose(glasses_id, " - unhandled event: " + str(event_num))
6 changes: 6 additions & 0 deletions example.gd/addons/tiltfive/T5ProjectSettings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static func setup_properties():
_define_project_setting("xr/tilt_five/application_version", TYPE_STRING, PROPERTY_HINT_NONE, "", "0.1.0")
_define_project_setting("xr/tilt_five/default_display_name", TYPE_STRING, PROPERTY_HINT_NONE, "", "Game: Player One")
_define_project_setting("xr/tilt_five/trigger_click_threshhold", TYPE_FLOAT, PROPERTY_HINT_RANGE, "0,1,0.01", 0.3)
_define_project_setting("xr/tilt_five/debug_logging", TYPE_BOOL, PROPERTY_HINT_NONE, "", false)
_initialized = true

static var application_id : String:
Expand All @@ -52,3 +53,8 @@ static var trigger_click_threshhold : float:
get:
setup_properties()
return ProjectSettings.get_setting_with_override("xr/tilt_five/trigger_click_threshhold")

static var is_debug_logging : bool:
get:
setup_properties()
return ProjectSettings.get_setting_with_override("xr/tilt_five/debug_logging")
8 changes: 8 additions & 0 deletions extension/T5Integration/Logging.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <TiltFiveNative.h>
#include <iostream>
#include <memory>
#include <sstream>

Expand All @@ -12,6 +13,13 @@ class Logger {
virtual void log_error(const char* message, const char* func_name, const char* file_name, int line_num) = 0;
virtual void log_warning(const char* message, const char* func_name, const char* file_name, int line_num) = 0;
virtual void log_string(const char* message) = 0;
void set_debug(bool is_debug_) {
is_debug = is_debug_;
}
bool get_debug() { return is_debug; }

private:
bool is_debug = false;
};

class DefaultLogger : public Logger {
Expand Down
21 changes: 12 additions & 9 deletions extension/src/GodotT5Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ void GodotT5Logger::log_error(const char* message, const char* func_name, const
}

void GodotT5Logger::log_warning(const char* message, const char* func_name, const char* file_name, int line_num) {
godot::_err_print_error(func_name, file_name, line_num, "TiltFiveXRInterface", message, true, false);
godot::_err_print_error(func_name, file_name, line_num, "TiltFiveXRInterface", message, true, true);
}

void GodotT5Logger::log_string(const char* message) {
Variant v_msg = message;
UtilityFunctions::print_verbose(v_msg);
if (get_debug())
UtilityFunctions::print(v_msg);
else
UtilityFunctions::print_verbose(v_msg);
}

GodotT5Service::Ptr GodotT5ObjectRegistry::service() {
Expand Down Expand Up @@ -135,15 +138,15 @@ T5Integration::T5Math::Ptr GodotT5ObjectRegistry::get_math() {
return math;
}

GodotT5Logger::Ptr g_logger;

T5Integration::Logger::Ptr GodotT5ObjectRegistry::get_logger() {
GodotT5Logger::Ptr logger;
if (_logger.expired()) {
logger = std::make_shared<GodotT5Logger>();
_logger = logger;
} else {
logger = std::static_pointer_cast<GodotT5Logger>(_logger.lock());
if (!g_logger) {
g_logger = std::make_shared<GodotT5Logger>();
_logger = g_logger;
}
return logger;

return g_logger;
}

} //namespace GodotT5Integration
78 changes: 78 additions & 0 deletions extension/src/TiltFiveXRInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "TiltFiveXRInterface.h"
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/classes/project_settings.hpp>
#include <godot_cpp/classes/rendering_server.hpp>
#include <godot_cpp/core/class_db.hpp>
Expand Down Expand Up @@ -34,6 +35,10 @@ void TiltFiveXRInterface::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_trigger_click_threshold"), &TiltFiveXRInterface::get_trigger_click_threshold);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trigger_click_threshold"), "set_trigger_click_threshold", "get_trigger_click_threshold");

ClassDB::bind_method(D_METHOD("set_debug_logging", "debug_logging"), &TiltFiveXRInterface::set_debug_logging);
ClassDB::bind_method(D_METHOD("get_debug_logging"), &TiltFiveXRInterface::get_debug_logging);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_logging"), "set_debug_logging", "get_debug_logging");

// Signals.
ADD_SIGNAL(MethodInfo("service_event", PropertyInfo(Variant::INT, "event")));
ADD_SIGNAL(MethodInfo("glasses_event", PropertyInfo(Variant::STRING, "glasses_id"), PropertyInfo(Variant::INT, "event")));
Expand Down Expand Up @@ -90,6 +95,14 @@ void TiltFiveXRInterface::set_trigger_click_threshold(float threshold) {
}
}

bool TiltFiveXRInterface::get_debug_logging() {
return GodotT5ObjectRegistry::logger()->get_debug();
}

void TiltFiveXRInterface::set_debug_logging(bool is_debug) {
GodotT5ObjectRegistry::logger()->set_debug(is_debug);
}

TiltFiveXRInterface::GlassesIndexEntry* TiltFiveXRInterface::lookup_glasses_entry(StringName glasses_id) {
for (auto& entry : _glasses_index) {
if (glasses_id == entry.id) {
Expand Down Expand Up @@ -448,6 +461,66 @@ PackedStringArray TiltFiveXRInterface::_get_suggested_pose_names(const StringNam
return tracker_names;
}

void TiltFiveXRInterface::log_service_events() {
if (get_debug_logging() || OS::get_singleton()->is_stdout_verbose()) {
for (auto& event : _service_events) {
switch (event.event) {
case T5ServiceEvent::E_RUNNING:
LOG_MESSAGE("Tilt Five Running");
break;
case T5ServiceEvent::E_STOPPED:
LOG_MESSAGE("Tilt Five Stopped");
break;
case T5ServiceEvent::E_T5_UNAVAILABLE:
LOG_MESSAGE("Tilt Five Unavailable");
break;
case T5ServiceEvent::E_T5_INCOMPATIBLE_VERSION:
LOG_MESSAGE("Tilt Five Incompatible Version");
break;
}
}
}
}

void TiltFiveXRInterface::log_glasses_events() {
if (get_debug_logging() || OS::get_singleton()->is_stdout_verbose()) {
for (auto& event : _glasses_events) {
auto glasses = t5_service->get_glasses(event.glasses_num);
if (!glasses)
continue;
switch (event.event) {
case GlassesEventType::E_GLASSES_ADDED:
LOG_MESSAGE(glasses->get_id(), " Added");
break;
case GlassesEventType::E_GLASSES_LOST:
LOG_MESSAGE(glasses->get_id(), " Lost");
break;
case GlassesEventType::E_GLASSES_AVAILABLE:
LOG_MESSAGE(glasses->get_id(), " Available to use");
break;
case GlassesEventType::E_GLASSES_UNAVAILABLE:
LOG_MESSAGE(glasses->get_id(), " Unavailable to use");
break;
case GlassesEventType::E_GLASSES_RESERVED:
LOG_MESSAGE(glasses->get_id(), " Reserved for application");
break;
case GlassesEventType::E_GLASSES_DROPPED:
LOG_MESSAGE(glasses->get_id(), " Reservation dropped");
break;
case GlassesEventType::E_GLASSES_TRACKING:
LOG_MESSAGE(glasses->get_id(), " Tracking pose");
break;
case GlassesEventType::E_GLASSES_NOT_TRACKING:
LOG_MESSAGE(glasses->get_id(), " Not tracking pose");
break;
case GlassesEventType::E_GLASSES_STOPPED_ON_ERROR:
LOG_MESSAGE(glasses->get_id(), " Stopped with unknown error");
break;
}
}
}
}

void TiltFiveXRInterface::_process() {
if (!t5_service)
return;
Expand All @@ -457,12 +530,14 @@ void TiltFiveXRInterface::_process() {

_service_events.clear();
t5_service->get_service_events(_service_events);
log_service_events();
for (int i = 0; i < _service_events.size(); i++) {
emit_signal("service_event", _service_events[i].event);
}

_glasses_events.clear();
t5_service->get_glasses_events(_glasses_events);
log_glasses_events();
for (int i = 0; i < _glasses_events.size(); i++) {
auto glasses_idx = _glasses_events[i].glasses_num;
switch (_glasses_events[i].event) {
Expand Down Expand Up @@ -523,6 +598,9 @@ int32_t TiltFiveXRInterface::_get_camera_feed_id() const {
}

TiltFiveXRInterface::TiltFiveXRInterface() {
#ifdef DEV_ENABLED
UtilityFunctions::print("Tilt Five DEV_BUILD");
#endif
}

TiltFiveXRInterface::~TiltFiveXRInterface() {
Expand Down
7 changes: 7 additions & 0 deletions extension/src/TiltFiveXRInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class TiltFiveXRInterface : public XRInterfaceExtension {
float get_trigger_click_threshold();
void set_trigger_click_threshold(float threshold);

bool get_debug_logging();
void set_debug_logging(bool is_debug);

// Functions.

void reserve_glasses(const StringName glasses_id, const String display_name);
Expand Down Expand Up @@ -153,12 +156,16 @@ class TiltFiveXRInterface : public XRInterfaceExtension {
GlassesIndexEntry *lookup_glasses_by_viewport(RID render_target);

private:
void log_service_events();
void log_glasses_events();

bool _initialised = false;
XRServer *xr_server = nullptr;

String application_id;
String application_version;
float _trigger_click_threshold = 0.5;
bool _is_debug_logging = false;

std::vector<GlassesIndexEntry> _glasses_index;
std::vector<GlassesEvent> _glasses_events;
Expand Down

0 comments on commit f708ac6

Please sign in to comment.