-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters