Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
log.cpp
Go to the documentation of this file.
1#include "log.h"
2#include <fstream>
3#include <chrono>
4#include <iomanip>
5
6void samurai::LogManager::init(std::string name)
7{
8
9 this->name = name;
10 bool firstLog = 0;
11
12}
13
14void samurai::LogManager::log(const char *l, int type)
15{
16
17#ifdef PIKA_DEVELOPMENT
18 logInternally(l, type);
19 logToFile(l, type);
20#endif
21
22
23}
24
25std::stringstream formatLog(const char *l, int type)
26{
27 auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
28 std::stringstream s;
29 s << "#" << std::put_time(std::localtime(&time), "%Y-%m-%d %X");
30
31 if (type == samurai::logWarning)
32 {
33 s << "[warning]";
34 }
35 else if (type == samurai::logError)
36 {
37 s << "[error]";
38 }
39
40 s << ": ";
41 s << l << "\n";
42 return s;
43}
44
45void samurai::LogManager::logToFile(const char *l, int type)
46{
47 //todo unlickely atribute
48 if (!firstLog)
49 {
50 firstLog = 1;
51 std::ofstream file(name); //no need to check here
52 file.close(); //clear the file content
53 }
54
55 samurai::logToFile(name.c_str(), l, type);
56}
57
58void samurai::LogManager::logInternally(const char *l, int type)
59{
60 if (internalLogs.size() >= maxInternalLogCount)
61 {
62 internalLogs.pop_front();
63 }
64
65 internalLogs.push_back(formatLog(l, type).str());
66}
67
68
69void samurai::logToFile(const char *fileName, const char *l, int type)
70{
71 std::ofstream file(fileName, std::ofstream::app);
72 if (!file.is_open()) { return; }
73
74 file << formatLog(l, type).rdbuf();
75 file.close();
76}
std::stringstream formatLog(const char *l, int type)
Definition log.cpp:25
void logToFile(const char *fileName, const char *l, int type=samurai::logNormal)
Definition log.cpp:69
@ logWarning
Definition log.h:11
@ logError
Definition log.h:12
void logInternally(const char *l, int type=samurai::logNormal)
Definition log.cpp:58
std::string name
Definition log.h:29
void logToFile(const char *l, int type=samurai::logNormal)
Definition log.cpp:45
void log(const char *l, int type=samurai::logNormal)
Definition log.cpp:14
void init(std::string name)
Definition log.cpp:6