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

105 lines
2.5 KiB
C
Raw Normal View History

2019-07-27 17:36:24 +03:00
//
2020-06-12 18:52:38 +03:00
// CRLib - Simple library for STL replacement in private projects.
// Copyright © 2020 YaPB Development Team <team@yapb.ru>.
2019-07-27 17:36:24 +03:00
//
2020-06-12 18:52:38 +03:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2019-07-27 17:36:24 +03:00
//
2020-06-12 18:52:38 +03:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
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:
2020-06-12 18:52:38 +03:00
File handle_;
PrintFunction printFun_;
2019-07-27 17:36:24 +03:00
public:
SimpleLogger () = default;
~SimpleLogger () {
2020-06-12 18:52:38 +03:00
handle_.close ();
2019-07-27 17:36:24 +03:00
}
private:
void logToFile (const char *level, const char *msg) {
2020-06-12 18:52:38 +03:00
if (!handle_) {
2019-07-27 17:36:24 +03:00
return;
}
time_t ticks = time (&ticks);
tm timeinfo {};
#if defined (CR_WINDOWS)
localtime_s (&timeinfo, &ticks);
#else
localtime_r (&ticks, &timeinfo);
#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
2020-06-12 18:52:38 +03:00
handle_.puts ("%s (%s): %s\n", timebuf, level, msg);
2019-07-27 17:36:24 +03:00
}
public:
2020-06-12 18:52:38 +03:00
template <typename ...Args> void fatal (const char *fmt, Args &&...args) {
2019-07-27 17:36:24 +03:00
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("FATAL", msg);
2020-06-12 18:52:38 +03:00
if (printFun_) {
printFun_ (msg);
2019-07-27 17:36:24 +03:00
}
plat.abort (msg);
}
2020-06-12 18:52:38 +03:00
template <typename ...Args> void error (const char *fmt, Args &&...args) {
2019-07-27 17:36:24 +03:00
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("ERROR", msg);
2020-06-12 18:52:38 +03:00
if (printFun_) {
printFun_ (msg);
2019-07-27 17:36:24 +03:00
}
}
2020-06-12 18:52:38 +03:00
template <typename ...Args> void message (const char *fmt, Args &&...args) {
2019-07-27 17:36:24 +03:00
auto msg = strings.format (fmt, cr::forward <Args> (args)...);
logToFile ("INFO", msg);
2020-06-12 18:52:38 +03:00
if (printFun_) {
printFun_ (msg);
2019-07-27 17:36:24 +03:00
}
}
public:
2020-06-12 18:52:38 +03:00
void initialize (StringRef filename, PrintFunction printFunction) {
if (handle_) {
handle_.close ();
2019-07-27 17:36:24 +03:00
}
2020-06-12 18:52:38 +03:00
printFun_ = cr::move (printFunction);
handle_.open (filename, "at");
2019-07-27 17:36:24 +03:00
}
};
// expose global instance
CR_EXPOSE_GLOBAL_SINGLETON (SimpleLogger, logger);
2019-07-27 17:36:24 +03:00
CR_NAMESPACE_END