diff --git a/include/lol/msg b/include/lol/msg new file mode 100644 index 00000000..031d355b --- /dev/null +++ b/include/lol/msg @@ -0,0 +1,18 @@ +// +// Lol Engine +// +// Copyright © 2010—2020 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +#include "private/push_macros.h" +#include "private/base/msg.h" +#include "private/pop_macros.h" + diff --git a/include/lol/private/base/msg.h b/include/lol/private/base/msg.h new file mode 100644 index 00000000..41e3f612 --- /dev/null +++ b/include/lol/private/base/msg.h @@ -0,0 +1,92 @@ +// +// Lol Engine +// +// Copyright © 2010—2020 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +#if !_DEBUG +#include "../base/env.h" // sys::getenv +#endif + +#include // std::vfprintf + +namespace lol +{ + +struct msg +{ + template + static inline void debug(char const *fmt, T... args) + { + helper(message_type::debug, fmt, args...); + } + + template + static inline void info(char const *fmt, T... args) + { + helper(message_type::info, fmt, args...); + } + + template + static inline void warn(char const *fmt, T... args) + { + helper(message_type::warn, fmt, args...); + } + + template + static inline void error(char const *fmt, T... args) + { + helper(message_type::error, fmt, args...); + } + +private: + msg() = delete; + + enum class message_type + { + debug, + info, + warn, + error, + }; + + static void helper(message_type type, char const *fmt, ...) + { + // Unless this is a debug build, ignore debug messages unless + // the LOL_DEBUG environment variable is set. +#if !_DEBUG + if (type == message_type::debug) + { + static bool const disable_debug = sys::getenv("LOL_DEBUG").size() > 0; + if (disable_debug) + return; + } +#endif + + static char const * const prefix[] = + { + "DEBUG", + "INFO", + "WARN", + "ERROR", + }; + + fprintf(stdout, "%s: ", prefix[int(type)]); + va_list ap; + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fflush(stdout); + } +}; + +} // namespace lol +