Fix bugs with logger and argument passing

This commit is contained in:
realaltffour 2020-04-14 17:33:35 +03:00
parent 8515e6cc6e
commit 3a0d35da65
No known key found for this signature in database
GPG Key ID: 7115CD2AC9A76A56
5 changed files with 61 additions and 3 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/skidjular",
"args": [],
"args": ["history"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build/bin/",
"environment": [],

View File

@ -49,6 +49,11 @@ void writeLog(const std::string& loc, bool append) {
void printLog(const std::string& loc, int entries) {
std::fstream f(LOC_LOG);
std::string line = "";
while (f >> line && entries--) { std::cout << line; }
if (entries)
while (std::getline(f, line) && entries--) {
std::cout << line << std::endl;
}
else
while (std::getline(f, line)) { std::cout << line << std::endl; }
f.close();
}

View File

@ -6,6 +6,7 @@
#include "data.h"
#include "log.h"
#include "module.h"
#include "modules/history.h"
#include "modules/info.h"
#include "modules/init.h"
#include "modules/proj.h"
@ -18,6 +19,8 @@ Module* get_Mod(std::string name) {
return proj_new();
} else if (name == "info") {
return info_new();
} else if (name == "history") {
return history_new();
}
return nullptr;
};
@ -28,6 +31,8 @@ void dispatch_Mod(std::string mod, std::vector<std::string> args) {
proj_dispatch(args);
} else if (mod == "info") {
info_dispatch(args);
} else if (mod == "history") {
history_dispatch(args);
}
};
@ -83,7 +88,7 @@ int main(int argc, const char* argv[]) {
auto args = vm["module-args"].as<vector<string>>();
dispatch_Mod(name, args);
} else
dispatch_Mod(name, {""});
dispatch_Mod(name, {});
} else {
std::cout << general << "\n";
writeLog(LOC_LOG);

27
src/modules/history.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "history.h"
void history_print(history_args args) {
printLog(LOC_LOG, args.entries_num);
std::cout << "hel";
}
history_mod* history_new() {
history_mod* mod = new history_mod();
mod->name = "History";
mod->desc = "Shows n entries, passed into the module";
return mod;
}
void history_dispatch(const std::vector<std::string>& argument) {
history_args args;
if (argument.size() == 0)
args.entries_num = 0;
else if (argument.size() == 1)
args.entries_num = stoi(argument[0]);
else if (argument.size() >= 1) {
std::cout << "Too much arguments are passed!" << std::endl;
exit(1);
}
history_print(args);
}

21
src/modules/history.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef MODULE_HISTORY_H
#define MODULE_HISTORY_H
#include <iostream>
#include <string>
#include "data.h"
#include "log.h"
#include "module.h"
struct history_args {
int entries_num = 0;
};
struct history_mod : history_args, Module {};
history_mod* history_new();
void history_print(history_args args);
void history_dispatch(const std::vector<std::string>& arguments);
#endif