Add Logging System
This commit is contained in:
parent
07db01e3eb
commit
a844cf6ae4
|
@ -9,7 +9,7 @@
|
|||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/build/bin/skidjular",
|
||||
"args": ["proj", "add", "name"],
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}/build/bin/",
|
||||
"environment": [],
|
||||
|
|
25
src/data.h
25
src/data.h
|
@ -1,6 +1,5 @@
|
|||
#ifndef DATA_H
|
||||
#define DATA_H
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
@ -10,9 +9,9 @@
|
|||
#include "json.hpp"
|
||||
using nlohmann::json;
|
||||
|
||||
#define LOC_DB = ".db"
|
||||
#define LOC_EVENTLOG = ".log"
|
||||
#define LOC_SKIDID = "skid" // used as LOC_SKIDID+SKIDNUM
|
||||
#define LOC_DB ".db"
|
||||
#define LOC_LOG ".log"
|
||||
#define LOC_SKIDID "skid" // used as LOC_SKIDID+SKIDNUM
|
||||
|
||||
/////////////// DATA CONTAINING STRUCTURES ///////////////////
|
||||
|
||||
|
@ -41,17 +40,17 @@ struct Skid {
|
|||
|
||||
/////////////// EVENT HISTORY SYSTEM STRUCTURES ////////////////////
|
||||
|
||||
enum EventType { Access = 0, Creation = 1, Modification = 2, None = -1 };
|
||||
enum EntryType { Access = 0, Creation = 1, Modification = 2, None = -1 };
|
||||
|
||||
struct Event {
|
||||
int id = -1;
|
||||
EventType type = EventType::None;
|
||||
std::string mesg = "";
|
||||
struct Entry {
|
||||
EntryType type = EntryType::None;
|
||||
std::string mesg = "";
|
||||
float timerTime = 0.0f;
|
||||
Date creationDate;
|
||||
};
|
||||
|
||||
struct EventLog {
|
||||
std::vector<Event>* events;
|
||||
std::string sha512 = "";
|
||||
struct Log {
|
||||
std::vector<Entry>* entries = nullptr;
|
||||
};
|
||||
|
||||
/////////////// EVENT END OF HISTORY SYSTEM STRUCTURES /////////////
|
||||
|
@ -60,7 +59,7 @@ struct EventLog {
|
|||
struct DB {
|
||||
std::map<std::string, Project>* projects;
|
||||
std::vector<Skid> skids;
|
||||
EventLog log;
|
||||
Log log;
|
||||
|
||||
Date creationDate;
|
||||
Date lastAccessTime;
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
#include "log.h"
|
||||
|
||||
auto t2str(EntryType type) -> std::string {
|
||||
if (type == EntryType::Access)
|
||||
return "Type: Access";
|
||||
if (type == EntryType::Creation)
|
||||
return "Type: Creation";
|
||||
if (type == EntryType::Modification)
|
||||
return "Type: Modification";
|
||||
if (type == EntryType::None)
|
||||
return "Type: None";
|
||||
return "Type: None";
|
||||
}
|
||||
|
||||
void addEntry(Log& log, const std::string& msg, EntryType type) {
|
||||
Entry ev;
|
||||
ev.type = type;
|
||||
ev.mesg = msg;
|
||||
ev.timerTime = timer_get();
|
||||
|
||||
time_t t = time(0);
|
||||
struct tm* currentTime = localtime(&t);
|
||||
|
||||
ev.creationDate.day = currentTime->tm_mday;
|
||||
ev.creationDate.month = currentTime->tm_mon;
|
||||
ev.creationDate.year = currentTime->tm_year + 1900;
|
||||
|
||||
if (!log.entries)
|
||||
log.entries = new std::vector<Entry>();
|
||||
log.entries->push_back(ev);
|
||||
}
|
||||
|
||||
void writeLog(const Log& log, const std::string& loc, bool append) {
|
||||
std::fstream f;
|
||||
if (append)
|
||||
f.open(LOC_LOG, std::fstream::out | std::fstream::app);
|
||||
else
|
||||
f.open(LOC_LOG);
|
||||
|
||||
for (auto entry: *log.entries) {
|
||||
f << "[ " << entry.creationDate.day << "/" << entry.creationDate.month
|
||||
<< "/" << entry.creationDate.year << " ] "
|
||||
<< "[ " << std::to_string(entry.timerTime) << " ] "
|
||||
<< t2str(entry.type) << " - " << entry.mesg << "\n";
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
void printLog(Log& log, const std::string& loc, int entries) {
|
||||
std::fstream f(LOC_LOG);
|
||||
std::string line = "";
|
||||
while (f >> line) { std::cout << line; }
|
||||
f.close();
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "data.h"
|
||||
#include "timer.h"
|
||||
|
||||
void addEntry(Log& log, const std::string& msg, EntryType type);
|
||||
void writeLog(const Log& log,
|
||||
const std::string& loc,
|
||||
bool append = true /* false == overwrite */);
|
||||
void printLog(Log& log,
|
||||
const std::string& loc,
|
||||
int entries = 0 /* 0 == None, -1 == All */);
|
||||
|
||||
#endif
|
13
src/main.cpp
13
src/main.cpp
|
@ -4,10 +4,12 @@
|
|||
#include <vector>
|
||||
|
||||
#include "data.h"
|
||||
#include "log.h"
|
||||
#include "module.h"
|
||||
#include "modules/info.h"
|
||||
#include "modules/init.h"
|
||||
#include "modules/proj.h"
|
||||
#include "timer.h"
|
||||
|
||||
Module* get_Mod(std::string name) {
|
||||
if (name == "init") {
|
||||
|
@ -30,6 +32,9 @@ void dispatch_Mod(std::string mod, std::vector<std::string> args) {
|
|||
};
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
timer_start();
|
||||
Log log;
|
||||
addEntry(log, "Started Program.", EntryType::None);
|
||||
using namespace boost::program_options;
|
||||
using namespace std;
|
||||
|
||||
|
@ -54,12 +59,16 @@ int main(int argc, const char* argv[]) {
|
|||
|
||||
if (vm.count("help")) {
|
||||
std::cout << general << "\n";
|
||||
writeLog(log, LOC_LOG);
|
||||
timer_stop();
|
||||
exit(0);
|
||||
} else if (vm.count("help-module")) {
|
||||
auto name = vm["help-module"].as<string>();
|
||||
auto module = get_Mod(name);
|
||||
if (!module) {
|
||||
std::cout << "Module not found.\n";
|
||||
writeLog(log, LOC_LOG);
|
||||
timer_stop();
|
||||
exit(1);
|
||||
}
|
||||
std::cout << module->desc << "\n";
|
||||
|
@ -67,6 +76,8 @@ int main(int argc, const char* argv[]) {
|
|||
auto name = vm["module"].as<string>();
|
||||
if (!get_Mod(name)) {
|
||||
std::cout << "Module not found.\n";
|
||||
writeLog(log, LOC_LOG);
|
||||
timer_stop();
|
||||
exit(1);
|
||||
}
|
||||
if (vm.count("module-args")) {
|
||||
|
@ -76,6 +87,8 @@ int main(int argc, const char* argv[]) {
|
|||
dispatch_Mod(name, {""});
|
||||
} else {
|
||||
std::cout << general << "\n";
|
||||
writeLog(log, LOC_LOG);
|
||||
timer_stop();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,8 +17,8 @@ void init_create(init_args arg) {
|
|||
|
||||
// Mandatory stuff to stop segmantion fault due to
|
||||
// pointers unintialized.
|
||||
db.projects = new std::map<std::string, Project>();
|
||||
db.log.events = new std::vector<Event>();
|
||||
db.projects = new std::map<std::string, Project>();
|
||||
db.log.entries = new std::vector<Entry>();
|
||||
|
||||
// TODO: Add event logging
|
||||
writeDB(db, ".db");
|
||||
|
|
|
@ -2,17 +2,19 @@
|
|||
|
||||
void timer_start() {
|
||||
s_timer.isRunning = true;
|
||||
s_timer.startTime = std::chrono::high_resolution_clock::now();
|
||||
s_timer.startTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void timer_stop() {
|
||||
s_timer.isRunning = false;
|
||||
s_timer.endTime = std::chrono::high_resolution_clock::now();
|
||||
s_timer.endTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
float timer_get() {
|
||||
if (!s_timer.isRunning)
|
||||
return 0;
|
||||
std::chrono::duration<float> duration =
|
||||
std::chrono::high_resolution_clock::now() - s_timer.startTime;
|
||||
std::chrono::steady_clock::now() - s_timer.startTime;
|
||||
return duration.count();
|
||||
}
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
#include <chrono>
|
||||
|
||||
struct Timer {
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> endTime;
|
||||
std::chrono::time_point<std::chrono::steady_clock> startTime;
|
||||
std::chrono::time_point<std::chrono::steady_clock> endTime;
|
||||
|
||||
bool isRunning = false;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue