this added json output to stdout, to file and global json stats to file. tarstats is feature complete.

This commit is contained in:
Marcel Nowicki
2022-02-12 02:59:31 +01:00
parent b3888cdab7
commit 98e8359c88
3 changed files with 88 additions and 16 deletions

View File

@@ -128,11 +128,24 @@ int main(int argc, char** argv) {
} }
gzclose(gzIn); gzclose(gzIn);
global_sizeofallfiles += std::filesystem::file_size(archiveName); global_sizeofallfiles += std::filesystem::file_size(archiveName);
tar::consolestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems); if(toJSON){
if (toFile) { if(!toFile) {
tar::jsonconsolestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems, archiveName, "gzarchive");
}
if(toFile) {
tar::jsonfilestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems, archiveName, "gzarchive");
}
}
if(!toJSON) {
if(toFile)
tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), tar::txtfilestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems, archiveName); sizeof_allitems, archiveName);
if(!toFile)
tar::consolestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems);
} }
} }
} }
@@ -177,16 +190,32 @@ int main(int argc, char** argv) {
file.close(); file.close();
global_sizeofallfiles += std::filesystem::file_size(archiveName); global_sizeofallfiles += std::filesystem::file_size(archiveName);
std::cout << toJSON << '\n'; if(toJSON){
tar::consolestats(typecount, std::filesystem::file_size(archiveName), if(!toFile) {
sizeof_allitems); tar::jsonconsolestats(typecount, std::filesystem::file_size(archiveName),
if (toFile) { sizeof_allitems, archiveName, "archive");
}
if(toFile) {
tar::jsonfilestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems, archiveName, "archive");
}
}
if(!toJSON) {
if(toFile)
tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), tar::txtfilestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems, archiveName); sizeof_allitems, archiveName);
if(!toFile)
tar::consolestats(typecount, std::filesystem::file_size(archiveName),
sizeof_allitems);
} }
} }
} }
} }
tar::consoleglobalstats(globaltypecount, global_sizeofallfiles, global_sizeofallitems, archiveFilename); if(toFile && toJSON)
tar::jsonglobalfile(globaltypecount, global_sizeofallfiles,
global_sizeofallitems, archiveFilename.size());
tar::consoleglobalstats(globaltypecount, global_sizeofallfiles,
global_sizeofallitems, archiveFilename.size());
return 0; return 0;
} }

View File

@@ -87,9 +87,9 @@ void tar::consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t t
} }
void tar::consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintmax_t filesize, void tar::consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintmax_t filesize,
uintmax_t itemsize, std::vector<std::string> &files) { uintmax_t itemsize, size_t fileamount) {
std::cout << "GLOBAL STATS" << '\n'; std::cout << "GLOBAL STATS" << '\n';
std::cout << "File amount: " << files.size() << '\n'; std::cout << "File amount: " << fileamount << '\n';
std::cout << "Size of all archives: " << filesize << " Bytes" << '\n'; std::cout << "Size of all archives: " << filesize << " Bytes" << '\n';
std::cout << "Size of all items: " << itemsize << " Bytes" << '\n'; std::cout << "Size of all items: " << itemsize << " Bytes" << '\n';
for (auto &i : typecount) { for (auto &i : typecount) {
@@ -98,6 +98,41 @@ void tar::consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintm
std::cout << '\n' << '\n'; std::cout << '\n' << '\n';
} }
void tar::jsonconsolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
std::string &filename, std::string type) {
std::cout << "{\"type\": \"" << type << "\", \"name\": \"" << filename << "\", \"size\": " << tarfilesize
<< ", \"itemsize\": " << sizeofall << ", \"files\": " << typecount[tarconstant::typeFile]
<< ", \"dir\": " << typecount[tarconstant::typeDir] << ", \"symlinks\": " << typecount[tarconstant::typeSym]
<< ", \"hardlinks\": " << typecount[tarconstant::typeHard] << ", \"other\": "
<< typecount[tarconstant::typeOther] << "}" << '\n' << '\n';
}
void tar::jsonfilestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
std::string &filename, std::string type) {
std::string jsnname = filename + ".json";
std::ofstream jsonfile(jsnname);
jsonfile << "{\"type\": \"" << type << "\", \"name\": \"" << filename << "\", \"size\": " << tarfilesize
<< ", \"itemsize\": " << sizeofall << ", \"files\": " << typecount[tarconstant::typeFile]
<< ", \"dir\": " << typecount[tarconstant::typeDir] << ", \"symlinks\": " << typecount[tarconstant::typeSym]
<< ", \"hardlinks\": " << typecount[tarconstant::typeHard] << ", \"other\": "
<< typecount[tarconstant::typeOther] << "}" << '\n';
jsonfile.close();
std::cout << "Stats written to " << jsnname << '\n' << '\n';
}
void tar::jsonglobalfile (std::map<std::string, uintmax_t> &typecount, uintmax_t itemsize,
uintmax_t filesize, size_t fileamount) {
std::ofstream jsonfile("global.json");
jsonfile << "{\"itemamount\": " << fileamount << ", \"itemsize\": " << itemsize << ", \"filesize\": "
<< filesize << ", \"files\": " << typecount[tarconstant::typeFile]
<< ", \"dir\": " << typecount[tarconstant::typeDir] << ", \"symlinks\": "
<< typecount[tarconstant::typeSym] << ", \"hardlinks\": "
<< typecount[tarconstant::typeHard] << ", \"other\": "
<< typecount[tarconstant::typeOther] << "}" << '\n';
jsonfile.close();
std::cout << "Global stats written to global.json" << '\n' << '\n';
}
// writes default console output to txt file // writes default console output to txt file
void tar::txtfilestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, void tar::txtfilestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
std::string archiveName) { std::string archiveName) {

View File

@@ -33,8 +33,16 @@ namespace tar {
// to write global stats to stdout // to write global stats to stdout
void consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintmax_t itemfilesize, void consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintmax_t itemfilesize,
uintmax_t szfiles, std::vector<std::string> &files); uintmax_t szfiles, size_t fileamount);
// to write JSON stats of a file to stdout
void jsonconsolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
std::string &filename, std::string type);
// to write JSON stats to file
void jsonfilestats (std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
std::string &filename, std::string type);
// to write global stats to JSON file
void jsonglobalfile (std::map<std::string, uintmax_t> &typecount, uintmax_t itemsize,
uintmax_t filesize, size_t fileamount);
// to write default console output to txt file // to write default console output to txt file
void txtfilestats(std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, void txtfilestats(std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
std::string archiveName); std::string archiveName);