yapb-noob-edition/include/crlib/cr-logger.h

101 lines
2.2 KiB
C
Raw Normal View History

2019-07-27 17:36:24 +03:00
//
// Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd").
// Copyright (c) Yet Another POD-Bot Contributors <yapb@entix.io>.
2019-07-27 17:36:24 +03:00
//
// This software is licensed under the MIT license.
// Additional exceptions apply. For full license details, see LICENSE.txt
2019-07-27 17:36:24 +03:00
//
#pragma once
#include <time.h>
#include <crlib/cr-files.h>
#include <crlib/cr-lambda.h>
CR_NAMESPACE_BEGIN
class SimpleLogger final : public Singleton <SimpleLogger> {
public:
using PrintFunction = Lambda <void (const char *)>;
private:
File m_handle;
PrintFunction m_printer;
public:
SimpleLogger () = default;
~SimpleLogger () {
m_handle.close ();
}
private:
void logToFile (const char *level, const char *msg) {
if (!m_handle) {
return;
}
time_t ticks = time (&ticks);
tm *timeinfo = nullptr;
#if defined (CR_WINDOWS)
tm get;
localtime_s (&get, &ticks);
timeinfo = &get;
#else
timeinfo = localtime (&ticks);
#endif
2019-07-27 17:36:24 +03:00
auto timebuf = strings.chars ();
strftime (timebuf, StringBuffer::StaticBufferSize, "%Y-%m-%d %H:%M:%S", timeinfo);
2019-07-27 17:36:24 +03:00
m_handle.puts ("%s (%s): %s\n", timebuf, level, msg);
}
public:
template <typename ...Args> void fatal (const char *fmt, Args ...args) {
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("FATAL", msg);
if (m_printer) {
m_printer (msg);
}
plat.abort (msg);
}
template <typename ...Args> void error (const char *fmt, Args ...args) {
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("ERROR", msg);
if (m_printer) {
m_printer (msg);
}
}
template <typename ...Args> void message (const char *fmt, Args ...args) {
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("INFO", msg);
if (m_printer) {
m_printer (msg);
}
}
public:
void initialize (const String &filename, PrintFunction printFunction) {
if (m_handle) {
m_handle.close ();
}
m_printer = cr::move (printFunction);
m_handle.open (filename, "at");
}
};
// expose global instance
CR_EXPOSE_GLOBAL_SINGLETON (SimpleLogger, logger);
2019-07-27 17:36:24 +03:00
CR_NAMESPACE_END