From 3c64010fe13d05f26e97bb64c287f02cc7e6d7e8 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 9 Jul 2016 15:02:05 +0100 Subject: [PATCH] Make Console::Error::WriteLine formattable --- src/cmdline/CommandLine.cpp | 20 ++++++------------- src/cmdline/RootCommands.cpp | 9 +++------ src/core/Console.cpp | 10 +++++++--- src/core/Console.hpp | 2 +- src/drawing/engines/opengl/OpenGLAPI.cpp | 2 +- .../engines/opengl/OpenGLShaderProgram.cpp | 4 ++-- src/object/ObjectFactory.cpp | 9 +++------ src/object/ObjectManager.cpp | 6 ++---- src/object/ObjectRepository.cpp | 12 ++++------- 9 files changed, 29 insertions(+), 45 deletions(-) diff --git a/src/cmdline/CommandLine.cpp b/src/cmdline/CommandLine.cpp index d3801122b6..1a65a055e2 100644 --- a/src/cmdline/CommandLine.cpp +++ b/src/cmdline/CommandLine.cpp @@ -397,8 +397,7 @@ namespace CommandLine const CommandLineOptionDefinition * option = FindOption(options, optionName); if (option == nullptr) { - Console::Error::Write("Unknown option: --"); - Console::Error::WriteLine(optionName); + Console::Error::WriteLine("Unknown option: --%s", optionName); return false; } @@ -413,8 +412,7 @@ namespace CommandLine const char * valueString = nullptr; if (!argEnumerator->TryPopString(&valueString)) { - Console::Error::Write("Expected value for option: "); - Console::Error::WriteLine(optionName); + Console::Error::WriteLine("Expected value for option: %s", optionName); return false; } @@ -428,8 +426,7 @@ namespace CommandLine { if (option->Type == CMDLINE_TYPE_SWITCH) { - Console::Error::Write("Option is a switch: "); - Console::Error::WriteLine(optionName); + Console::Error::WriteLine("Option is a switch: %s", optionName); return false; } else @@ -456,9 +453,7 @@ namespace CommandLine option = FindOption(options, shortOption[0]); if (option == nullptr) { - Console::Error::Write("Unknown option: -"); - Console::Error::Write(shortOption[0]); - Console::Error::WriteLine(); + Console::Error::WriteLine("Unknown option: -%c", shortOption[0]); return false; } if (option->Type == CMDLINE_TYPE_SWITCH) @@ -483,9 +478,7 @@ namespace CommandLine const char * valueString = nullptr; if (!argEnumerator->TryPopString(&valueString)) { - Console::Error::Write("Expected value for option: "); - Console::Error::Write(option->ShortName); - Console::Error::WriteLine(); + Console::Error::WriteLine("Expected value for option: %c", option->ShortName); return false; } @@ -516,8 +509,7 @@ namespace CommandLine *((utf8 * *)option->OutAddress) = String::Duplicate(valueString); return true; default: - Console::Error::Write("Unknown CMDLINE_TYPE for: "); - Console::Error::WriteLine(option->LongName); + Console::Error::WriteLine("Unknown CMDLINE_TYPE for: %s", option->LongName); return false; } } diff --git a/src/cmdline/RootCommands.cpp b/src/cmdline/RootCommands.cpp index 89f0a29571..c224e00cb5 100644 --- a/src/cmdline/RootCommands.cpp +++ b/src/cmdline/RootCommands.cpp @@ -312,8 +312,7 @@ static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator) Console::WriteLine("Checking path..."); if (!platform_directory_exists(path)) { - Console::Error::WriteFormat("The path '%s' does not exist", path); - Console::Error::WriteLine(); + Console::Error::WriteLine("The path '%s' does not exist", path); return EXITCODE_FAIL; } @@ -327,8 +326,7 @@ static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator) if (!platform_file_exists(pathG1Check)) { Console::Error::WriteLine("RCT2 path not valid."); - Console::Error::WriteFormat("Unable to find %s.", pathG1Check); - Console::Error::WriteLine(); + Console::Error::WriteLine("Unable to find %s.", pathG1Check); return EXITCODE_FAIL; } @@ -337,8 +335,7 @@ static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator * enumerator) platform_resolve_user_data_path(); platform_get_user_directory(userPath, NULL); if (!platform_ensure_directory_exists(userPath)) { - Console::Error::WriteFormat("Unable to access or create directory '%s'.", userPath); - Console::Error::WriteLine(); + Console::Error::WriteLine("Unable to access or create directory '%s'.", userPath); return EXITCODE_FAIL; } diff --git a/src/core/Console.cpp b/src/core/Console.cpp index f1b8c937b2..d82064022d 100644 --- a/src/core/Console.cpp +++ b/src/core/Console.cpp @@ -91,10 +91,14 @@ namespace Console fputs(platform_get_new_line(), stderr); } - void WriteLine(const utf8 * str) + void WriteLine(const utf8 * format, ...) { - fputs(str, stderr); - WriteLine(); + va_list args; + + va_start(args, format); + vfprintf(stdout, format, args); + puts(""); + va_end(args); } } } \ No newline at end of file diff --git a/src/core/Console.hpp b/src/core/Console.hpp index 64ac7a765b..4f8171235b 100644 --- a/src/core/Console.hpp +++ b/src/core/Console.hpp @@ -36,6 +36,6 @@ namespace Console void Write(const utf8 * str); void WriteFormat(const utf8 * format, ...); void WriteLine(); - void WriteLine(const utf8 * str); + void WriteLine(const utf8 * format, ...); } } diff --git a/src/drawing/engines/opengl/OpenGLAPI.cpp b/src/drawing/engines/opengl/OpenGLAPI.cpp index 42f661ae8b..8683262160 100644 --- a/src/drawing/engines/opengl/OpenGLAPI.cpp +++ b/src/drawing/engines/opengl/OpenGLAPI.cpp @@ -133,7 +133,7 @@ bool OpenGLAPI::Initialise() const char * failedProcName = TryLoadAllProcAddresses(); if (failedProcName != nullptr) { - Console::Error::WriteFormat("Failed to load %s.\n", failedProcName); + Console::Error::WriteLine("Failed to load %s.", failedProcName); return false; } #endif diff --git a/src/drawing/engines/opengl/OpenGLShaderProgram.cpp b/src/drawing/engines/opengl/OpenGLShaderProgram.cpp index 3cc4162078..922b3af625 100644 --- a/src/drawing/engines/opengl/OpenGLShaderProgram.cpp +++ b/src/drawing/engines/opengl/OpenGLShaderProgram.cpp @@ -51,7 +51,7 @@ OpenGLShader::OpenGLShader(const char * name, GLenum type) glGetShaderInfoLog(_id, sizeof(buffer), nullptr, buffer); glDeleteShader(_id); - Console::Error::WriteFormat("Error compiling %s\n", path); + Console::Error::WriteLine("Error compiling %s", path); Console::Error::WriteLine(buffer); throw Exception("Error compiling shader."); @@ -115,7 +115,7 @@ OpenGLShaderProgram::OpenGLShaderProgram(const char * name) GLsizei length; glGetProgramInfoLog(_id, sizeof(buffer), &length, buffer); - Console::Error::WriteFormat("Error linking %s\n", name); + Console::Error::WriteLine("Error linking %s", name); Console::Error::WriteLine(buffer); throw Exception("Failed to link OpenGL shader."); diff --git a/src/object/ObjectFactory.cpp b/src/object/ObjectFactory.cpp index 8446f76f5a..2900f9130e 100644 --- a/src/object/ObjectFactory.cpp +++ b/src/object/ObjectFactory.cpp @@ -68,8 +68,7 @@ public: if (!String::IsNullOrEmpty(text)) { - Console::Error::WriteFormat("[%s] Warning: %s", _objectName, text); - Console::Error::WriteLine(); + Console::Error::WriteLine("[%s] Warning: %s", _objectName, text); } } @@ -79,8 +78,7 @@ public: if (!String::IsNullOrEmpty(text)) { - Console::Error::WriteFormat("[%s] Error: %s", _objectName, text); - Console::Error::WriteLine(); + Console::Error::WriteLine("[%s] Error: %s", _objectName, text); } } }; @@ -154,8 +152,7 @@ namespace ObjectFactory if (readContext.WasError()) { - Console::Error::WriteFormat("Error reading object: '%s'", path); - Console::Error::WriteLine(); + Console::Error::WriteLine("Error reading object: '%s'", path); delete result; result = nullptr; diff --git a/src/object/ObjectManager.cpp b/src/object/ObjectManager.cpp index eb3f0e4941..8045ea11cb 100644 --- a/src/object/ObjectManager.cpp +++ b/src/object/ObjectManager.cpp @@ -438,16 +438,14 @@ private: { utf8 objName[9] = { 0 }; Memory::Copy(objName, entry->name, 8); - Console::Error::WriteFormat("[%s] Object not found.", objName); - Console::Error::WriteLine(); + Console::Error::WriteLine("[%s] Object not found.", objName); } static void ReportObjectLoadProblem(const rct_object_entry * entry) { utf8 objName[9] = { 0 }; Memory::Copy(objName, entry->name, 8); - Console::Error::WriteFormat("[%s] Object could not be loaded.", objName); - Console::Error::WriteLine(); + Console::Error::WriteLine("[%s] Object could not be loaded.", objName); } static sint32 GetIndexFromTypeEntry(uint8 objectType, uint8 entryIndex) diff --git a/src/object/ObjectRepository.cpp b/src/object/ObjectRepository.cpp index 07e3fdbf0e..f7417fddc9 100644 --- a/src/object/ObjectRepository.cpp +++ b/src/object/ObjectRepository.cpp @@ -211,8 +211,7 @@ public: Object * object = ObjectFactory::CreateObjectFromLegacyData(objectEntry, data, dataSize); if (object == nullptr) { - Console::Error::WriteFormat("[%s] Unable to export object.", objectName); - Console::Error::WriteLine(); + Console::Error::WriteLine("[%s] Unable to export object.", objectName); } else { @@ -418,10 +417,8 @@ private: } else { - Console::Error::WriteFormat("Object conflict: '%s'", conflict->Path); - Console::Error::WriteLine(); - Console::Error::WriteFormat(" : '%s'", item->Path); - Console::Error::WriteLine(); + Console::Error::WriteLine("Object conflict: '%s'", conflict->Path); + Console::Error::WriteLine(" : '%s'", item->Path); return false; } } @@ -903,6 +900,5 @@ static void ReportMissingObject(const rct_object_entry * entry) { utf8 objName[9] = { 0 }; Memory::Copy(objName, entry->name, 8); - Console::Error::WriteFormat("[%s] Object not found.", objName); - Console::Error::WriteLine(); + Console::Error::WriteLine("[%s] Object not found.", objName); }