liblcf
Loading...
Searching...
No Matches
log_handler.cpp
Go to the documentation of this file.
1/*
2 * This file is part of liblcf. Copyright (c) liblcf authors.
3 * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4 *
5 * liblcf is Free/Libre Open Source Software, released under the MIT License.
6 * For the full copyright and license information, please view the COPYING
7 * file that was distributed with this source code.
8 */
9
10#include "lcf/log_handler.h"
11#include <cassert>
12#include <cstdarg>
13#include <cstdio>
14#include <iostream>
15
16namespace lcf {
17namespace LogHandler {
18namespace {
19 void DefaultHandler(LogHandler::Level level, std::string_view message, UserData) {
20 switch (level) {
21 case Level::Debug:
22 std::cerr << "Debug: ";
23 break;
24 case Level::Warning:
25 std::cerr << "Warning: ";
26 break;
27 case Level::Error:
28 std::cerr << "Error: ";
29 break;
30 default:
31 assert(false && "Invalid Log Level");
32 }
33 std::cerr << message << "\n";
34 }
35
36 Level level = Level::Debug;
37 LogHandlerFn output_fn = DefaultHandler;
38 UserData output_userdata = nullptr;
39}
40
41void SetHandler(LogHandlerFn fn, UserData userdata) {
42 if (!fn) {
43 output_fn = DefaultHandler;
44 output_userdata = nullptr;
45 } else {
46 output_fn = fn;
47 output_userdata = userdata;
48 }
49}
50
51void SetLevel(Level new_level) {
52 level = new_level;
53}
54
55} // namespace Output
56
57namespace Log {
58namespace {
59 std::string format_string(char const* fmt, va_list args) {
60 char buf[4096];
61 int const result = vsnprintf(buf, sizeof(buf), fmt, args);
62 if (result < 0) {
63 return {};
64 }
65
66 return {buf, static_cast<unsigned int>(result) < sizeof(buf) ? result : sizeof(buf)};
67 }
68}
69
70void Debug(const char* fmt, ...) {
71 if (static_cast<int>(LogHandler::Level::Debug) >= static_cast<int>(LogHandler::level)) {
72 va_list args;
73 va_start(args, fmt);
74 auto msg = format_string(fmt, args);
75 LogHandler::output_fn(LogHandler::Level::Debug, msg, LogHandler::output_userdata);
76 va_end(args);
77 }
78}
79
80void Warning(const char* fmt, ...) {
81 if (static_cast<int>(LogHandler::Level::Warning) >= static_cast<int>(LogHandler::level)) {
82 va_list args;
83 va_start(args, fmt);
84 auto msg = format_string(fmt, args);
85 LogHandler::output_fn(LogHandler::Level::Warning, msg, LogHandler::output_userdata);
86 va_end(args);
87 }
88}
89
90void Error(const char* fmt, ...) {
91 if (static_cast<int>(LogHandler::Level::Error) >= static_cast<int>(LogHandler::level)) {
92 va_list args;
93 va_start(args, fmt);
94 auto msg = format_string(fmt, args);
95 LogHandler::output_fn(LogHandler::Level::Error, msg, LogHandler::output_userdata);
96 va_end(args);
97 }
98}
99
100} // namespace Log
101} // namespace lcf
std::string format_string(char const *fmt, va_list args)
void DefaultHandler(LogHandler::Level level, std::string_view message, UserData)
void SetHandler(LogHandlerFn fn, UserData userdata)
void SetLevel(Level new_level)
void Warning(const char *fmt,...) LIKE_PRINTF
void Error(const char *fmt,...) LIKE_PRINTF
void Debug(const char *fmt,...) LIKE_PRINTF