From cb0431dbb052f996dc0206cbc7327a74e535c6ff Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Sat, 27 Jul 2019 13:59:39 +0200 Subject: [PATCH] * Renamed plugin to teleshmem. * (Re-)implemented logging to file in plugin. * Reactivated telemetry_configuration() callback. --- Makefile | 9 +-- telemetry.cpp => teleshmem.cpp | 109 ++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 41 deletions(-) rename telemetry.cpp => teleshmem.cpp (86%) diff --git a/Makefile b/Makefile index 1ae7701..260e292 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ SDK_INCLUDES=\ -Isdk/include/eurotrucks2 CFLAGS=-Wall -Wextra -std=c99 -DDEBUG -I. +CPPFLAGS=-Wall -DLOGGING -I. UNAME:= $(shell uname -s) @@ -23,14 +24,14 @@ else LIB_NAME_OPTION=-soname endif -PLUGIN_SRC := telemetry.cpp shmget.c +PLUGIN_SRC := teleshmem.cpp shmget.c .PHONY: all clean -all: telehttpd telelogger telemetry.so +all: telehttpd telelogger teleshmem.so -telemetry.so: $(PLUGIN_SRC) $(SDK_HEADERS) - g++ -o $@ -fPIC -Wall --shared -Wl,$(LIB_NAME_OPTION),$@ $(SDK_INCLUDES) $(PLUGIN_SRC) +teleshmem.so: $(PLUGIN_SRC) $(SDK_HEADERS) + g++ -o $@ $(CPPFLAGS) -fPIC --shared -Wl,$(LIB_NAME_OPTION),$@ $(SDK_INCLUDES) $(PLUGIN_SRC) telehttpd: telehttpd.o shmget.o net.o fserv.o $(CC) $(LDFLAGS) -o $@ -pthread $^ diff --git a/telemetry.cpp b/teleshmem.cpp similarity index 86% rename from telemetry.cpp rename to teleshmem.cpp index aa3802b..83b4385 100644 --- a/telemetry.cpp +++ b/teleshmem.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include // SDK @@ -68,18 +67,52 @@ scs_timestamp_t last_timestamp = static_cast(-1); /** - * @brief Function writting message to the game internal log. + * @brief Logging to external file. */ -/* -static void log_print(const char *const text, ...) -{ + +#ifdef LOGGING +#include + +static FILE *logfp = NULL; + +static int log_open( void ) { + if ( NULL == logfp ) { + int fd; + char tmpl[] = "/tmp/ets2tele_XXXXXX.log"; + if ( 0 > ( fd = mkstemps( tmpl, 4 ) ) ) + return -1; + logfp = fdopen( fd, "a" ); + } + return 0; +} + +static void log_close( void ) { + if ( logfp ) { + fclose( logfp ); + logfp = NULL; + } } -static void log_line(const char *const text, ...) +static void log_print(const char *const fmt, ...) { + if ( logfp ) { + va_list arglist; + va_start( arglist, fmt ); + vfprintf( logfp, fmt, arglist ); + va_end( arglist ); + fflush( logfp ); + } } -*/ -// Handling of individual events. +#else +#define log_open() +#define log_close() +#define log_print(...) +#endif // def LOGGING + + +/** + * @brief Handling of individual events. + */ SCSAPI_VOID telemetry_frame_start(const scs_event_t UNUSED(event), const void *const event_info, const scs_context_t UNUSED(context)) { @@ -125,11 +158,10 @@ SCSAPI_VOID telemetry_pause(const scs_event_t event, const void *const UNUSED(ev SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const event_info, const scs_context_t UNUSED(context)) { -#if 0 // Here we just print the configuration info. const struct scs_telemetry_configuration_t *const info = static_cast(event_info); - log_line("Configuration: %s", info->id); + log_print("Configuration: %s\n", info->id); for (const scs_named_value_t *current = info->attributes; current->name; ++current) { log_print(" %s", current->name); @@ -139,36 +171,36 @@ SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const e log_print(" : "); switch (current->value.type) { case SCS_VALUE_TYPE_INVALID: { - log_line("none"); + log_print("none\n"); break; } case SCS_VALUE_TYPE_bool: { - log_line("bool = %s", current->value.value_bool.value ? "true" : "false"); + log_print("bool = %s\n", current->value.value_bool.value ? "true" : "false"); break; } case SCS_VALUE_TYPE_s32: { - log_line("s32 = %d", static_cast(current->value.value_s32.value)); + log_print("s32 = %d\n", static_cast(current->value.value_s32.value)); break; } case SCS_VALUE_TYPE_u32: { - log_line("u32 = %u", static_cast(current->value.value_u32.value)); + log_print("u32 = %u\n", static_cast(current->value.value_u32.value)); break; } case SCS_VALUE_TYPE_u64: { - log_line("u64 = %" SCS_PF_U64, current->value.value_u64.value); + log_print("u64 = %" SCS_PF_U64 "\n", current->value.value_u64.value); break; } case SCS_VALUE_TYPE_float: { - log_line("float = %f", current->value.value_float.value); + log_print("float = %f\n", current->value.value_float.value); break; } case SCS_VALUE_TYPE_double: { - log_line("double = %f", current->value.value_double.value); + log_print("double = %f\n", current->value.value_double.value); break; } case SCS_VALUE_TYPE_fvector: { - log_line( - "fvector = (%f,%f,%f)", + log_print( + "fvector = (%f,%f,%f)\n", current->value.value_fvector.x, current->value.value_fvector.y, current->value.value_fvector.z @@ -176,8 +208,8 @@ SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const e break; } case SCS_VALUE_TYPE_dvector: { - log_line( - "dvector = (%f,%f,%f)", + log_print( + "dvector = (%f,%f,%f)\n", current->value.value_dvector.x, current->value.value_dvector.y, current->value.value_dvector.z @@ -185,8 +217,8 @@ SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const e break; } case SCS_VALUE_TYPE_euler: { - log_line( - "euler = h:%f p:%f r:%f", + log_print( + "euler = h:%f p:%f r:%f\n", current->value.value_euler.heading * 360.0f, current->value.value_euler.pitch * 360.0f, current->value.value_euler.roll * 360.0f @@ -194,8 +226,8 @@ SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const e break; } case SCS_VALUE_TYPE_fplacement: { - log_line( - "fplacement = (%f,%f,%f) h:%f p:%f r:%f", + log_print( + "fplacement = (%f,%f,%f) h:%f p:%f r:%f\n", current->value.value_fplacement.position.x, current->value.value_fplacement.position.y, current->value.value_fplacement.position.z, @@ -206,8 +238,8 @@ SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const e break; } case SCS_VALUE_TYPE_dplacement: { - log_line( - "dplacement = (%f,%f,%f) h:%f p:%f r:%f", + log_print( + "dplacement = (%f,%f,%f) h:%f p:%f r:%f\n", current->value.value_dplacement.position.x, current->value.value_dplacement.position.y, current->value.value_dplacement.position.z, @@ -218,18 +250,15 @@ SCSAPI_VOID telemetry_configuration(const scs_event_t event, const void *const e break; } case SCS_VALUE_TYPE_string: { - log_line("string = %s", current->value.value_string.value); + log_print("string = %s\n", current->value.value_string.value); break; } default: { - log_line("unknown"); + log_print("unknown\n"); break; } } } - - print_header = true; -#endif } // Handling of individual channels. @@ -308,6 +337,8 @@ SCSAPI_VOID telemetry_store_bool(const scs_string_t name, const scs_u32_t index, */ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_init_params_t *const params) { + log_open(); + // We currently support only one version. if (version != SCS_TELEMETRY_VERSION_1_00) { @@ -316,6 +347,7 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in const scs_telemetry_init_params_v100_t *const version_params = static_cast(params); if ( !init_shm() ) { + log_print( "ERROR: Unable to initialize shared memory\n" ); version_params->common.log(SCS_LOG_TYPE_error, "Unable to initialize shared memory"); return SCS_RESULT_generic_error; } @@ -334,7 +366,7 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in const scs_u32_t MINIMAL_VERSION = SCS_TELEMETRY_EUT2_GAME_VERSION_1_00; if (version_params->common.game_version < MINIMAL_VERSION) { - //log_line("WARNING: Too old version of the game, some features might behave incorrectly"); + log_print("WARNING: Too old version of the game, some features might behave incorrectly\n"); telemetry->game_ver_warn = true; } @@ -342,7 +374,7 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in const scs_u32_t IMPLEMENTED_VERSION = SCS_TELEMETRY_EUT2_GAME_VERSION_CURRENT; if (SCS_GET_MAJOR_VERSION(version_params->common.game_version) > SCS_GET_MAJOR_VERSION(IMPLEMENTED_VERSION)) { - //log_line("WARNING: Too new major version of the game, some features might behave incorrectly"); + log_print("WARNING: Too new major version of the game, some features might behave incorrectly\n"); telemetry->game_ver_warn = true; } } @@ -353,7 +385,7 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in const scs_u32_t MINIMAL_VERSION = SCS_TELEMETRY_ATS_GAME_VERSION_1_00; if (version_params->common.game_version < MINIMAL_VERSION) { - //log_line("WARNING: Too old version of the game, some features might behave incorrectly"); + log_print("WARNING: Too old version of the game, some features might behave incorrectly\n"); telemetry->game_ver_warn = true; } @@ -361,12 +393,12 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in const scs_u32_t IMPLEMENTED_VERSION = SCS_TELEMETRY_ATS_GAME_VERSION_CURRENT; if (SCS_GET_MAJOR_VERSION(version_params->common.game_version) > SCS_GET_MAJOR_VERSION(IMPLEMENTED_VERSION)) { - //log_line("WARNING: Too new major version of the game, some features might behave incorrectly"); + log_print("WARNING: Too new major version of the game, some features might behave incorrectly\n"); telemetry->game_ver_warn = true; } } else { - //log_line("WARNING: Unsupported game, some features or values might behave incorrectly"); + log_print("WARNING: Unsupported game, some features or values might behave incorrectly\n"); telemetry->game_ver_warn = true; } @@ -439,6 +471,7 @@ SCSAPI_VOID scs_telemetry_shutdown(void) // so there is no need to do that manually. drop_shm(); + log_close(); } // Cleanup @@ -452,6 +485,7 @@ BOOL APIENTRY DllMain( { if (reason_for_call == DLL_PROCESS_DETACH) { drop_shm(); + log_close(); } return TRUE; } @@ -461,5 +495,6 @@ BOOL APIENTRY DllMain( void __attribute__ ((destructor)) unload(void) { drop_shm(); + log_close(); } #endif -- 2.30.2