Skip to content

Commit

Permalink
Add LCF logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Mar 15, 2021
1 parent c94ff83 commit f67d6e7
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -208,6 +210,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
47 changes: 47 additions & 0 deletions src/lcf/log_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of liblcf. Copyright (c) 2020 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"

namespace lcf {
namespace LogHandler {

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

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

/**
* Sets the output handler for all lcf logging.
* The default handler prints to standard error.
*
* @param fn New output handler. nullptr for default handler.
*/
void SetHandler(LogHandlerFn fn);

/**
* 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
24 changes: 24 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This file is part of liblcf. Copyright (c) 2020 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_LOG_H
#define LCF_LOG_H

#include "lcf/log_handler.h"

namespace lcf {
namespace Log {

void Debug(const char* fmt, ...);
void Warning(const char* fmt, ...);

} // namespace Log
} // namespace lcf

#endif
88 changes: 88 additions & 0 deletions src/log_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* This file is part of liblcf. Copyright (c) 2020 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.
*/

#include "lcf/log_handler.h"
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <iostream>

namespace lcf {
namespace LogHandler {
namespace {
void DefaultHandler(lcf::LogHandler::Level level, StringView message) {
switch (level) {
case Level::Debug:
std::cerr << "Debug: ";
break;
case Level::Warning:
std::cerr << "Warning: ";
break;
case Level::Error:
std::cerr << "Error: ";
break;
default:
assert(false && "Invalid Log Level");
}
std::cerr << message << "\n";
}

Level level = Level::Debug;
LogHandlerFn output_fn = DefaultHandler;
}

void SetHandler(LogHandlerFn fn) {
if (!fn) {
output_fn = DefaultHandler;
} else {
output_fn = fn;
}
}

void SetLevel(Level new_level) {
level = new_level;
}

} // namespace Output

namespace Log {
namespace {
std::string format_string(char const* fmt, va_list args) {
char buf[4096];
int const result = vsnprintf(buf, sizeof(buf), fmt, args);
if (result < 0) {
return std::string();
}

return std::string(buf, static_cast<unsigned int>(result) < sizeof(buf) ? result : sizeof(buf));
}
}

void Debug(const char* fmt, ...) {
if (static_cast<int>(LogHandler::Level::Debug) >= static_cast<int>(LogHandler::level)) {
va_list args;
va_start(args, fmt);
auto msg = format_string(fmt, args);
LogHandler::output_fn(LogHandler::Level::Debug, msg);
va_end(args);
}
}

void Warning(const char* fmt, ...) {
if (static_cast<int>(LogHandler::Level::Warning) >= static_cast<int>(LogHandler::level)) {
va_list args;
va_start(args, fmt);
auto msg = format_string(fmt, args);
LogHandler::output_fn(LogHandler::Level::Warning, msg);
va_end(args);
}
}

} // namespace Log
} // namespace lcf
3 changes: 2 additions & 1 deletion src/lsd_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lcf/lsd/chunks.h"
#include "lcf/rpg/save.h"
#include "lcf/reader_util.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {
Expand Down Expand Up @@ -43,7 +44,7 @@ void LSD_Reader::PrepareSave(rpg::Save& save, int32_t version, int32_t codepage)
std::unique_ptr<rpg::Save> LSD_Reader::Load(StringView filename, StringView encoding) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LSD file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
lcf::Log::Warning("Failed to open LSD file '%s' for reading : %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LSD_Reader::Load(stream, encoding);
Expand Down
3 changes: 2 additions & 1 deletion src/reader_lcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <limits>

#include "lcf/reader_lcf.h"
#include "log.h"

namespace lcf {
// Statics
Expand Down Expand Up @@ -276,7 +277,7 @@ int LcfReader::Peek() {
}

void LcfReader::Skip(const struct LcfReader::Chunk& chunk_info, const char* where) {
fprintf(stderr, "Skipped Chunk %02X (%" PRIu32 " byte) in lcf at %" PRIX32 " (%s)\n",
Log::Debug("Skipped Chunk %02X (%" PRIu32 " byte) in lcf at %" PRIX32 " (%s)\n",
chunk_info.ID, chunk_info.length, Tell(), where);

for (uint32_t i = 0; i < chunk_info.length; ++i) {
Expand Down

0 comments on commit f67d6e7

Please sign in to comment.