Skip to content

Commit

Permalink
Merge pull request #416 from Ghabry/output
Browse files Browse the repository at this point in the history
Allow redirection of lcf console messages
  • Loading branch information
carstene1ns authored Mar 28, 2024
2 parents 41f537e + 701dcc8 commit ad97cac
Show file tree
Hide file tree
Showing 24 changed files with 310 additions and 110 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ set(LCF_SOURCES
src/lmt_treemap.cpp
src/lmu_movecommand.cpp
src/lmu_reader.cpp
src/log.h
src/lsd_reader.cpp
src/log_handler.cpp
src/reader_flags.cpp
src/reader_lcf.cpp
src/reader_struct.h
Expand Down Expand Up @@ -205,6 +207,7 @@ set(LCF_HEADERS
src/lcf/lmt/reader.h
src/lcf/lmu/reader.h
src/lcf/lsd/reader.h
src/lcf/log_handler.h
src/lcf/reader_lcf.h
src/lcf/reader_util.h
src/lcf/reader_xml.h
Expand Down
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ liblcf_la_SOURCES = \
src/lmt_treemap.cpp \
src/lmu_movecommand.cpp \
src/lmu_reader.cpp \
src/log.h \
src/lsd_reader.cpp \
src/log_handler.cpp \
src/reader_flags.cpp \
src/reader_lcf.cpp \
src/reader_struct.h \
Expand Down Expand Up @@ -219,6 +221,7 @@ lcfinclude_HEADERS = \
src/lcf/flag_set.h \
src/lcf/ini.h \
src/lcf/inireader.h \
src/lcf/log_handler.h \
src/lcf/reader_lcf.h \
src/lcf/reader_util.h \
src/lcf/reader_xml.h \
Expand Down
11 changes: 5 additions & 6 deletions src/dbstring_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include "lcf/dbstring.h"
#include "log.h"
#include "reader_struct.h"
#include <iostream>

Expand Down Expand Up @@ -43,7 +44,7 @@ struct RawStruct<std::vector<DBString> > {
void RawStruct<DBString>::ReadLcf(DBString& ref, LcfReader& stream, uint32_t length) {
stream.ReadString(ref, length);
#ifdef LCF_DEBUG_TRACE
printf(" %s\n", ref.c_str());
fprintf(stderr, " %s\n", ref.c_str());
#endif
}

Expand Down Expand Up @@ -106,9 +107,7 @@ void RawStruct<std::vector<DBString>>::ReadLcf(std::vector<DBString>& ref, LcfRe
}

if (stream.Tell() != endpos) {
#ifdef LCF_DEBUG_TRACE
fprintf(stderr, "Misaligned!\n");
#endif
Log::Warning("vector<string> Misaligned at 0x%" PRIx32 "", stream.Tell());
stream.Seek(endpos);
}
}
Expand Down Expand Up @@ -177,7 +176,7 @@ class DbStringVectorXmlHandler : public XmlHandler {

void StartElement(XmlReader& stream, const char* name, const char** atts) {
if (strcmp(name, "item") != 0) {
stream.Error("Expecting %s but got %s", "item", name);
Log::Error("XML: Expecting %s but got %s", "item", name);
return;
}

Expand All @@ -191,7 +190,7 @@ class DbStringVectorXmlHandler : public XmlHandler {
}

if (id <= last_id || id < -1) {
stream.Error("Bad Id %d / %d", id, last_id);
Log::Error("XML: Bad Id %d / %d", id, last_id);
return;
}

Expand Down
7 changes: 4 additions & 3 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "lcf/encoder.h"
#include "lcf/reader_util.h"
#include "lcf/scope_guard.h"
#include "log.h"
#include <cstdio>
#include <cstdlib>

Expand Down Expand Up @@ -86,7 +87,7 @@ void Encoder::Init() {
auto conv_runtime = ucnv_open(runtime_encoding, &status);

if (conv_runtime == nullptr) {
fprintf(stderr, "liblcf: ucnv_open() error for encoding \"%s\": %s\n", runtime_encoding, u_errorName(status));
Log::Error("ucnv_open() error for encoding \"%s\": %s", runtime_encoding, u_errorName(status));
return;
}
status = U_ZERO_ERROR;
Expand All @@ -95,7 +96,7 @@ void Encoder::Init() {
auto conv_storage = ucnv_open(storage_encoding.c_str(), &status);

if (conv_storage == nullptr) {
fprintf(stderr, "liblcf: ucnv_open() error for dest encoding \"%s\": %s\n", storage_encoding.c_str(), u_errorName(status));
Log::Error("ucnv_open() error for dest encoding \"%s\": %s", storage_encoding.c_str(), u_errorName(status));
return;
}

Expand Down Expand Up @@ -143,7 +144,7 @@ void Encoder::Convert(std::string& str, UConverter* conv_dst, UConverter* conv_s
&status);

if (U_FAILURE(status)) {
fprintf(stderr, "liblcf: ucnv_convertEx() error when encoding \"%s\": %s\n", src.c_str(), u_errorName(status));
Log::Error("ucnv_convertEx() error when encoding \"%s\": %s", src.c_str(), u_errorName(status));
_buffer.clear();
}

Expand Down
57 changes: 57 additions & 0 deletions src/lcf/log_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of liblcf. Copyright (c) liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/

#ifndef LCF_OUTPUT_H
#define LCF_OUTPUT_H

#include "string_view.h"
#include "enum_tags.h"

namespace lcf {
namespace LogHandler {

enum class Level {
Debug,
Warning,
Error,
Highest
};

static constexpr auto kLevelTags = lcf::makeEnumTags<Level>(
"Debug",
"Warning",
"Error",
"Highest"
);

using UserData = void*;
using LogHandlerFn = void (*)(Level level, StringView message, UserData userdata);

/**
* Sets the output handler for all lcf logging.
* The default handler prints to standard error.
*
* @param fn New output handler. nullptr for default handler.
* @param userdata Passed to the log handler function. Is not touched by liblcf.
*/
void SetHandler(LogHandlerFn fn, UserData userdata = nullptr);

/**
* Only report issues that have at least this log level.
* Use Highest to disable logging.
* Default: Debug
*
* @param new_level New log level
*/
void SetLevel(Level new_level);

} // namespace LogHandler
} // namespace lcf

#endif
5 changes: 0 additions & 5 deletions src/lcf/reader_xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ class XmlReader {
*/
bool IsOk() const;

/**
* Reports a parsing error.
*/
void Error(const char* fmt, ...);

/**
* Parses the XML file.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/ldb_equipment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "lcf/ldb/reader.h"
#include "lcf/ldb/chunks.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {
Expand All @@ -27,7 +28,7 @@ struct RawStruct<rpg::Equipment> {
*/
void RawStruct<rpg::Equipment>::ReadLcf(rpg::Equipment& ref, LcfReader& stream, uint32_t length) {
if (length != 10) {
fprintf(stderr, "Equipment has incorrect size %" PRIu32 " (expected 10)\n", length);
Log::Warning("Equipment has incorrect size %" PRIu32 " (expected 10)", length);

LcfReader::Chunk chunk_info;
chunk_info.ID = 0x33;
Expand Down Expand Up @@ -85,7 +86,7 @@ class EquipmentXmlHandler : public XmlHandler {
else if (strcmp(name, "accessory_id") == 0)
field = &ref.accessory_id;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = NULL;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/ldb_eventcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>
#include <vector>
#include "log.h"
#include "reader_struct.h"
#include "lcf/rpg/eventcommand.h"

Expand Down Expand Up @@ -108,7 +109,7 @@ class EventCommandXmlHandler : public XmlHandler {
else if (strcmp(name, "parameters") == 0)
field = Parameters;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = None;
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ void RawStruct<std::vector<rpg::EventCommand> >::ReadLcf(

if (stream.Tell() >= endpos) {
stream.Seek(endpos, LcfReader::FromStart);
fprintf(stderr, "Event command corrupted at %" PRIu32 "\n", stream.Tell());
Log::Warning("Event command corrupted at %" PRIu32 "", stream.Tell());
for (;;) {
// Try finding the real end of the event command (4 0-bytes)
int i = 0;
Expand Down Expand Up @@ -216,7 +217,7 @@ class EventCommandVectorXmlHandler : public XmlHandler {

void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
if (strcmp(name, "EventCommand") != 0)
stream.Error("Expecting %s but got %s", "EventCommand", name);
Log::Error("XML: Expecting %s but got %s", "EventCommand", name);
ref.resize(ref.size() + 1);
rpg::EventCommand& obj = ref.back();
stream.SetHandler(new EventCommandXmlHandler(obj));
Expand Down
2 changes: 1 addition & 1 deletion src/ldb_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ParametersXmlHandler : public XmlHandler {
else if (strcmp(name, "agility") == 0)
field = &ref.agility;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = NULL;
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/ldb_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "lcf/ldb/reader.h"
#include "lcf/ldb/chunks.h"
#include "lcf/reader_util.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {
Expand All @@ -25,7 +26,7 @@ void LDB_Reader::PrepareSave(rpg::Database& db) {
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(StringView filename, StringView encoding) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LDB_Reader::Load(stream, encoding);
Expand All @@ -34,7 +35,7 @@ std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(StringView filename, String
bool LDB_Reader::Save(StringView filename, const lcf::rpg::Database& db, StringView encoding, SaveOpt opt) {
std::ofstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
return false;
}
return LDB_Reader::Save(stream, db, encoding, opt);
Expand All @@ -43,7 +44,7 @@ bool LDB_Reader::Save(StringView filename, const lcf::rpg::Database& db, StringV
bool LDB_Reader::SaveXml(StringView filename, const lcf::rpg::Database& db) {
std::ofstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB XML file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB XML file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
return false;
}
return LDB_Reader::SaveXml(stream, db);
Expand All @@ -52,7 +53,7 @@ bool LDB_Reader::SaveXml(StringView filename, const lcf::rpg::Database& db) {
std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(StringView filename) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB XML file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB XML file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LDB_Reader::LoadXml(stream);
Expand All @@ -61,17 +62,17 @@ std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(StringView filename) {
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(std::istream& filestream, StringView encoding) {
LcfReader reader(filestream, ToString(encoding));
if (!reader.IsOk()) {
LcfReader::SetError("Couldn't parse database file.\n");
LcfReader::SetError("Couldn't parse database file.");
return nullptr;
}
std::string header;
reader.ReadString(header, reader.ReadInt());
if (header.length() != 11) {
LcfReader::SetError("This is not a valid RPG2000 database.\n");
LcfReader::SetError("This is not a valid RPG2000 database.");
return nullptr;
}
if (header != "LcfDataBase") {
fprintf(stderr, "Warning: This header is not LcfDataBase and might not be a valid RPG2000 database.\n");
Log::Warning("Header %s != LcfDataBase and might not be a valid RPG2000 database.", header.c_str());
}
auto db = std::make_unique<lcf::rpg::Database>();
db->ldb_header = header;
Expand All @@ -91,7 +92,7 @@ bool LDB_Reader::Save(std::ostream& filestream, const lcf::rpg::Database& db, St
const auto engine = GetEngineVersion(db);
LcfWriter writer(filestream, engine, ToString(encoding));
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse database file.\n");
LcfReader::SetError("Couldn't parse database file.");
return false;
}
std::string header;
Expand Down
Loading

0 comments on commit ad97cac

Please sign in to comment.