Commit 3b687934 authored by Bkg2k's avatar Bkg2k

improvement(logs): new clear output format

parent 36ab5c4e
......@@ -80,7 +80,7 @@ bool parseArgs(int argc, char* argv[], unsigned int* width, unsigned int* height
{
Settings::getInstance()->setBool("Debug", true);
Settings::getInstance()->setBool("HideConsole", false);
Log::setReportingLevel(LogDebug);
Log::setReportingLevel(LogLevel::LogDebug);
}
else if (strcmp(argv[i], "--windowed") == 0)
{
......
#include "Log.h"
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <iostream>
#include "platform.h"
#include "RootFolders.h"
#include "datetime/DateTime.h"
LogLevel Log::reportingLevel = LogInfo;
FILE* Log::file = NULL; //fopen(getLogPath().c_str(), "w");
LogLevel Log::reportingLevel = LogLevel::LogInfo;
FILE* Log::file = nullptr;
LogLevel Log::getReportingLevel()
static std::map<LogLevel, std::string> StringLevel =
{
return reportingLevel;
}
{ LogLevel::LogDebug , "DEBUG" },
{ LogLevel::LogInfo , "INFO " },
{ LogLevel::LogWarning, "WARN!" },
{ LogLevel::LogError , "ERROR" },
};
std::string Log::getLogPath()
{
......@@ -19,11 +24,6 @@ std::string Log::getLogPath()
return home;
}
void Log::setReportingLevel(LogLevel level)
{
reportingLevel = level;
}
void Log::open()
{
file = fopen(getLogPath().c_str(), "w");
......@@ -31,7 +31,7 @@ void Log::open()
std::ostringstream& Log::get(LogLevel level)
{
os << "lvl" << level << ": \t";
os << '[' << DateTime().ToPreciseTimeStamp() << "] (" << StringLevel[level] << ") : ";
messageLevel = level;
return os;
......@@ -39,25 +39,20 @@ std::ostringstream& Log::get(LogLevel level)
void Log::flush()
{
fflush(getOutput());
fflush(file);
}
void Log::close()
{
fclose(file);
file = NULL;
}
FILE* Log::getOutput()
{
return file;
file = nullptr;
}
Log::~Log()
{
os << std::endl;
if(getOutput() == NULL)
if(file == nullptr)
{
// not open yet, print to stdout
std::cerr << "ERROR - tried to write to log file before it was open! The following won't be logged:\n";
......@@ -65,10 +60,10 @@ Log::~Log()
return;
}
fprintf(getOutput(), "%s", os.str().c_str());
fprintf(file, "%s", os.str().c_str());
//if it's an error, also print to console
//print all messages if using --debug
if(messageLevel == LogError || reportingLevel >= LogDebug)
if(messageLevel == LogLevel::LogError || reportingLevel >= LogLevel::LogDebug)
fprintf(stderr, "%s", os.str().c_str());
}
......@@ -2,37 +2,43 @@
#define _LOG_H_
#define LOG(level) \
if(level > Log::getReportingLevel()) ; \
else Log().get(level)
if (LogLevel::level <= Log::getReportingLevel()) Log().get(LogLevel::level)
#include <string>
#include <sstream>
#include <iostream>
enum LogLevel { LogError, LogWarning, LogInfo, LogDebug };
enum class LogLevel
{
LogError,
LogWarning,
LogInfo,
LogDebug,
};
class Log
{
public:
//Log();
~Log();
std::ostringstream& get(LogLevel level = LogInfo);
static LogLevel getReportingLevel();
static void setReportingLevel(LogLevel level);
static std::string getLogPath();
static void flush();
static void open();
static void close();
protected:
std::ostringstream os;
static FILE* file;
private:
static LogLevel reportingLevel;
static FILE* getOutput();
LogLevel messageLevel;
public:
~Log();
std::ostringstream& get(LogLevel level = LogLevel::LogInfo);
static LogLevel getReportingLevel() { return reportingLevel; }
static void setReportingLevel(LogLevel level) { reportingLevel = level; }
static std::string getLogPath();
static void flush();
static void open();
static void close();
protected:
std::ostringstream os;
static FILE* file;
private:
static LogLevel reportingLevel;
static FILE* getOutput();
LogLevel messageLevel;
};
#endif
......@@ -24,9 +24,11 @@ class SystemDateTimeImplementation : public ISystemDateTimeInterface
bool LoadRTCValues(short &millis, short &year, char &month, char &day, char &hour, char &minute, char &second) override
{
time_t t = time(NULL);
timeval tv;
gettimeofday(&tv, nullptr);
time_t t=tv.tv_sec;
struct tm* lt = localtime(&t);
millis = 0;
millis = tv.tv_usec / 1000;
second = (char)lt->tm_sec;
minute = (char)lt->tm_min;
hour = (char)lt->tm_hour;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment