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);
|
2019-09-21 23:20:33 +03:00
|
|
|
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 ();
|
2019-09-21 23:20:33 +03:00
|
|
|
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
|
2020-02-08 00:03:52 +03:00
|
|
|
CR_EXPOSE_GLOBAL_SINGLETON (SimpleLogger, logger);
|
2019-07-27 17:36:24 +03:00
|
|
|
|
|
|
|
|
CR_NAMESPACE_END
|