Bläddra i källkod

- replaced std::cout with log stream

master
Andreas Romeyke 2 år sedan
förälder
incheckning
d245bd78ff
7 ändrade filer med 64 tillägg och 63 borttagningar
  1. +23
    -23
      src/lib/bag.cpp
  2. +2
    -2
      src/lib/bagmetadata.cpp
  3. +7
    -6
      src/lib/checksum.cpp
  4. +5
    -5
      src/lib/fetchfile.cpp
  5. +16
    -16
      src/lib/manifest.cpp
  6. +5
    -5
      src/lib/payload.cpp
  7. +6
    -6
      src/lib/tagmanifest.cpp

+ 23
- 23
src/lib/bag.cpp Visa fil

@@ -13,7 +13,7 @@ using namespace std;


Bag::Bag( string dfname ) {
// cout << "load constructor (" << dfname << ")" << endl;
// log << "load constructor (" << dfname << ")" << endl;
// read in file bagit.txt
string bagit_txt_path = dfname + "bagit.txt";
//log << "parse " << bagit_txt_path << endl;
@@ -35,8 +35,8 @@ Bag::Bag( string dfname ) {
if (0 != vprefix.compare("BagIt-Version:")) {
// log << "wrong vprefix='" << vprefix << "', but 'BagIt-Version:' expected" << endl;
}
//cout << "major:'"<<major<<"'"<<endl;
//cout << "minor:'"<<minor<<"'"<<endl;
//log << "major:'"<<major<<"'"<<endl;
//log << "minor:'"<<minor<<"'"<<endl;
Bag::bagit_version_major = stoi(major);
Bag::bagit_version_minor = stoi(minor);

@@ -49,7 +49,7 @@ Bag::Bag( string dfname ) {
//log << "wrong uprefix='" << uprefix << "', but 'Tag-File-Character-Encoding:' expected" << endl;
}
Bag::tag_file_character_encoding = uvalue;
//cout << "Bagit Version ("<< version_line << ") major=" << Bag::bagit_version_major << " minor=" << Bag::bagit_version_minor << endl;
//log << "Bagit Version ("<< version_line << ") major=" << Bag::bagit_version_major << " minor=" << Bag::bagit_version_minor << endl;
} else {
Bag::log << "file " << bagit_txt_path << " could not be opened" <<endl;
}
@@ -65,13 +65,13 @@ Bag::Bag( string dfname ) {
map<string,string> md = Bag::bagmetadata_p->get_metadata();
map<string,string>::iterator m;
for (m=md.begin(); m!=md.end(); m++) {
// cout << m->first << " = " << m->second <<endl;
// log << m->first << " = " << m->second <<endl;
}

list<string>::iterator i;
for (i=files.begin(); i!=files.end(); i++) {
// cout << "file/dir (rel):" << (*i) << endl;
//cout << "file/dir (abs):" << (*i) << endl;
// log << "file/dir (rel):" << (*i) << endl;
//log << "file/dir (abs):" << (*i) << endl;
}
Bag::payloadmanifest_p->get_checksum_file_pairs( md5 );
Bag::tagmanifest_p->get_checksum_file_pairs( md5 );
@@ -93,41 +93,41 @@ bool Bag::store( string basedir ) {
fs::path p{ basedir };
fs::file_status s = fs::status( p );
if (fs::is_directory( s)) {
cout << "directory '" << basedir << "' already exists" << endl;
log << "directory '" << basedir << "' already exists" << endl;
return false;
}
fs::create_directory(p);

// store payload
if (nullptr == Bag::payload_p) {
cout << "Payload object needed" << endl;
log << "Payload object needed" << endl;
return false;
}
Bag::payload_p->store( basedir );
// store payload manifest
if (nullptr == Bag::payloadmanifest_p) {
cout << "Payloadmanifest object needed" << endl;
log << "Payloadmanifest object needed" << endl;
return false;
}
list<string> payload_files =Bag::payload_p->get_all_relative_paths();
cout << "PAYLOAD" <<endl;
log << "PAYLOAD" <<endl;
Bag::payloadmanifest_p->store( basedir, payload_files);
// store baginfo
if (nullptr == Bag::bagmetadata_p) {
cout << "Bagmetadata object needed" << endl;
log << "Bagmetadata object needed" << endl;
return false;
}
Bag::bagmetadata_p->store( basedir );
// store fetchfile (if needed)
if (nullptr == Bag::fetchfile_p) {
cout << "Fetchfile object needed" << endl;
log << "Fetchfile object needed" << endl;

} else {
Bag::fetchfile_p->store( basedir );
}
// store other
if (nullptr == Bag::othertags_p) {
cout << "Othertags object needed" << endl;
log << "Othertags object needed" << endl;

} else {
Bag::othertags_p->store( basedir );
@@ -141,19 +141,19 @@ bool Bag::store( string basedir ) {
bagit_txt_file << ("Tag-File-Character-Encoding: " + Bag::tag_file_character_encoding) << endl;
bagit_txt_file.close();
} else {
cout << "file " << bagit_txt_path << "could not be open for writing" << endl;
log << "file " << bagit_txt_path << "could not be open for writing" << endl;
return false;
}
// at least (!), store tagmanifest
if (nullptr == Bag::tagmanifest_p) {
cout << "Tagmanifest object needed" << endl;
log << "Tagmanifest object needed" << endl;
return false;
}
list<string> bagfiles = Bag::get_all_bag_files();
cout << "TAGMANIFEST" << endl;
log << "TAGMANIFEST" << endl;
list<string>::iterator it;
for (it=bagfiles.begin(); it!= bagfiles.end(); ++it) {
cout << "TAGMANIFEST-file '" << *it << "'" << endl;
log << "TAGMANIFEST-file '" << *it << "'" << endl;
}
Bag::tagmanifest_p->store( basedir, bagfiles );

@@ -201,23 +201,23 @@ bool Bag::validate() {
list<string> missed_files;
payload_files.sort();
payload_manifest_files.sort();
//cout << "PAYLOAD_FILES:" << endl;
//log << "PAYLOAD_FILES:" << endl;
//auto it = payload_files.begin();
//while( it != payload_files.end()) {
// cout << "\t"<<(*it++) << endl;
// log << "\t"<<(*it++) << endl;
//}


//cout << "PAYLOADMANIFEST_FILES:" << endl;
//log << "PAYLOADMANIFEST_FILES:" << endl;
//it = payload_manifest_files.begin();
//while( it != payload_manifest_files.end()) {
// cout << "\t"<<(*it++) << endl;
// log << "\t"<<(*it++) << endl;
//}
auto it1 = payload_files.begin();
auto it2 = payload_manifest_files.begin();
while( it1 != payload_files.end() && it2 != payload_manifest_files.end() ) {
int cmp_res = (*it1).compare( *it2);
//cout << "COMP: "<<cmp_res<<" file='"<< (*it1) << "' checksummed file='" << (*it2) << "'" << endl;
//log << "COMP: "<<cmp_res<<" file='"<< (*it1) << "' checksummed file='" << (*it2) << "'" << endl;
if ( cmp_res < 0) {
this->log << "Bagit warning, file '" << (*it1) << "' in payload has no checksum entry in payload manifest" << endl;
it1++;


+ 2
- 2
src/lib/bagmetadata.cpp Visa fil

@@ -43,14 +43,14 @@ Bagmetadata::Bagmetadata( string basedir ) {
regex key_value_separator(R"(:\s*)");
// DEBUG: string repl = "|";
// DEBUG: string content2 = regex_replace(content, rgx, "|");
// DEBUG: cout << "REPL:(" << content2 << ")" << endl;
// DEBUG: log << "REPL:(" << content2 << ")" << endl;
sregex_token_iterator iter_line(content.begin(),
content.end(),
rgx_line,
-1);
sregex_token_iterator endline;
for ( ; iter_line != endline; ++iter_line) {
// DEBUG: std::cout << "ENTRY(" << *iter_line << ")" << endl;
// DEBUG: std::log << "ENTRY(" << *iter_line << ")" << endl;
string line = *iter_line;
sregex_token_iterator iter(line.begin(),
line.end(),


+ 7
- 6
src/lib/checksum.cpp Visa fil

@@ -12,12 +12,13 @@ string string_of_algorithm( checksum_algorithms alg) {
case sha1: return string("sha1");
case sha256: return string("sha256");
}
return string("");
}

string Checksum::checksum_of_file(const string& filepath, checksum_algorithms alg) {
ifstream file (filepath, ios::ate);
stringstream hex_result;
// cout << "processing file '" << filepath << "'" << endl;
// log << "processing file '" << filepath << "'" << endl;
if (file.is_open() ) {
ifstream::pos_type fileSize;
unsigned char * memBlock;
@@ -54,13 +55,13 @@ string Checksum::checksum_of_file(const string& filepath, checksum_algorithms al

}
delete[] memBlock;
// cout << "# # CHECKSUM for filepath '" << filepath << "' " << hex_result.str() << endl;
// log << "# # CHECKSUM for filepath '" << filepath << "' " << hex_result.str() << endl;
}else {
cout << "could not alloc memory" << endl;
log << "could not alloc memory" << endl;
}
file.close();
} else {
cout << "file '"<< filepath << "' could not be opened" << endl;
log << "file '"<< filepath << "' could not be opened" << endl;
}
return hex_result.str();
}
@@ -68,7 +69,7 @@ string Checksum::checksum_of_file(const string& filepath, checksum_algorithms al
oxum_t Checksum::oxum_of_file(const string& filepath) {
ifstream file (filepath, ios::ate);
oxum_t oxum{};
//cout << "processing file '" << filepath << "'" << endl;
//log << "processing file '" << filepath << "'" << endl;
if (file.is_open() ) {
ifstream::pos_type fileSize;
fileSize = file.tellg();
@@ -76,7 +77,7 @@ oxum_t Checksum::oxum_of_file(const string& filepath) {
oxum.streamcount = 1;
file.close();
} else {
cout << "file '"<< filepath << "' could not be opened" << endl;
log << "file '"<< filepath << "' could not be opened" << endl;
}
return oxum;
}


+ 5
- 5
src/lib/fetchfile.cpp Visa fil

@@ -19,8 +19,8 @@ Fetchfile::Fetchfile( string basedir ) {
string filename = basedir + "fetch.txt";
fs::path p{ filename };
fs::file_status s = fs::status( p );
cout << "path "<< p.string() << endl;
cout << "is file: "<< fs::is_regular_file( s) << endl;
log << "path "<< p.string() << endl;
log << "is file: "<< fs::is_regular_file( s) << endl;
if (fs::is_regular_file( s)) {
this->exist_fetchfile = true;
// map entries
@@ -63,7 +63,7 @@ bool Fetchfile::fetch( fetch_t fetch ) {
FILE *fp;
fp = fopen( fetch.filename.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, fetch.url.c_str());
// FIXME: curl needs a write function if WIN32,
// FIXME: curl needs a write function if WIN32,
// see https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html#DESCRIPTION
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
@@ -71,7 +71,7 @@ bool Fetchfile::fetch( fetch_t fetch ) {
curl_easy_cleanup(curl);
fclose(fp);
} else {
return false;
return false;
}
return true;
}
@@ -99,7 +99,7 @@ bool Fetchfile::fetch_all_entries() {
bool Fetchfile::validate() {
bool is_valid = true;
if (this->has_fetchfile()) {
// TODO(art1):
// TODO(art1):
}
return is_valid;
}


+ 16
- 16
src/lib/manifest.cpp Visa fil

@@ -21,7 +21,7 @@ Manifest::Manifest( string basedir, string file_prefix ) {
}
for (auto & possible_manifest_file : possible_manifest_files) {
// debug
// cout << it->first << " => " << it->second << endl;
// log << it->first << " => " << it->second << endl;
// test if file exists
//string filename = basedir + it->second;
string filename = possible_manifest_file.second;
@@ -33,21 +33,21 @@ Manifest::Manifest( string basedir, string file_prefix ) {
if (fs::is_regular_file( s)) {
Manifest::manifest_algorithm_files[alg] = filename;
Manifest::exist_manifest_files = true;
// debug
//cout << "TAGMANIFEST: alg="<< it->first << " => filename=" << it->second << endl;
//cout << "TAGMANIFEST2: alg="<< it->first << " => filename=" << filename << endl;
// debug
//log << "TAGMANIFEST: alg="<< it->first << " => filename=" << it->second << endl;
//log << "TAGMANIFEST2: alg="<< it->first << " => filename=" << filename << endl;
}
}
//cout << "TAGMANIFEST constructor, calling debug()" << endl;
//log << "TAGMANIFEST constructor, calling debug()" << endl;
//this->debug();
//cout << "TAGMANIFEST constructor, finished" << endl;
//log << "TAGMANIFEST constructor, finished" << endl;

}

multimap<checksum_string_t,filename_t> Manifest::get_checksum_file_pairs(checksum_algorithms alg) {
string filename = this->manifest_algorithm_files[ alg ];
multimap<checksum_string_t,filename_t> checksum_file_pairs;
// cout << "using file " << filename << endl;
// log << "using file " << filename << endl;
ifstream file;
file.open( filename );
if (file.is_open()) {
@@ -59,7 +59,7 @@ multimap<checksum_string_t,filename_t> Manifest::get_checksum_file_pairs(checksu
line_ss >> checksum;
line_ss >> subfile;
checksum_file_pairs.insert( pair<checksum_string_t, filename_t>(checksum,subfile) );
// cout << "checksum="<<checksum<<" file="<<subfile<<endl;
// log << "checksum="<<checksum<<" file="<<subfile<<endl;
}
file.close();
}
@@ -79,7 +79,7 @@ bool Manifest::validate() {
if (fs::is_regular_file( p )) {
at_least_one_manifest=true;
}
this->log << "validate using file '"<< (manifest_file) <<endl;
multimap<checksum_string_t,filename_t>checksum_file_pairs = this->get_checksum_file_pairs( alg );
for (auto & checksum_file_pair : checksum_file_pairs) {
@@ -101,8 +101,8 @@ bool Manifest::validate() {
}
}
}
if (! at_least_one_manifest) { is_valid = false; } // at least one manifest is needed!
cout << "MANIFESTLOG:" << this->log.str() << endl;
if (! at_least_one_manifest) { is_valid = false; } // at least one manifest is needed!
log << "MANIFESTLOG:" << this->log.str() << endl;

return is_valid;
}
@@ -113,7 +113,7 @@ list<string> Manifest::get_checksummed_files() {
multimap<checksum_string_t,filename_t>checksum_file_pairs = this->get_checksum_file_pairs( manifest_algorithm_file.first );
for (auto & checksum_file_pair : checksum_file_pairs) {
string file = this->basedir + checksum_file_pair.second;
// cout << "MF: '" << file << "'" << endl;
// log << "MF: '" << file << "'" << endl;
files.push_back( file );
}
}
@@ -126,23 +126,23 @@ map<checksum_algorithms, string> possible_manifest_files;
possible_manifest_files[alg] = Manifest::base_manifest_file_prefix + string_of_algorithm( alg ) + ".txt";
}
for (auto file : files) {
cout << "DEBUG manifest-files file='" << file << "'" << endl;
log << "DEBUG manifest-files file='" << file << "'" << endl;
}
Checksum checksum;
for (auto & possible_manifest_file : possible_manifest_files) {
checksum_algorithms alg = possible_manifest_file.first;
cout << "DEBUG manifest::store alg=" << string_of_algorithm(alg) << endl;
log << "DEBUG manifest::store alg=" << string_of_algorithm(alg) << endl;
if (!possible_manifest_file.second.empty()) {
// test if file exists
string algfilename = basedir + possible_manifest_file.second;
cout << "DEBUG manifest::store, alg=" << string_of_algorithm(alg) << " file='"<<algfilename << "' (newbase='" << basedir << "', filename='" << possible_manifest_file.second << "'" <<endl;
log << "DEBUG manifest::store, alg=" << string_of_algorithm(alg) << " file='"<<algfilename << "' (newbase='" << basedir << "', filename='" << possible_manifest_file.second << "'" <<endl;
ofstream alg_txt_file;
alg_txt_file.open( algfilename );
if (alg_txt_file.is_open()) {
list<string>::iterator ch;
for (ch=files.begin(); ch!=files.end(); ++ch) {
string filename = basedir + *ch;
cout << "DEBUG manifest::store\tfilename='"<<filename<<"'"<<endl;
log << "DEBUG manifest::store\tfilename='"<<filename<<"'"<<endl;
string checksum_string = checksum.checksum_of_file( filename, alg);
alg_txt_file << checksum_string << " " << *ch << endl;
}


+ 5
- 5
src/lib/payload.cpp Visa fil

@@ -13,9 +13,9 @@ Payload::Payload( string basedir ) {
this->basedir = basedir;
fs::path p{ this->basedir };
// fs::file_status s = fs::status( p );
// cout << "basedir "<< p.string() << endl;
// cout << "is dir: "<< fs::is_directory( s) << endl;
// cout << "exists: "<< fs::exists(s) << endl;
// log << "basedir "<< p.string() << endl;
// log << "is dir: "<< fs::is_directory( s) << endl;
// log << "exists: "<< fs::exists(s) << endl;
};

void Payload::scan_dir_recursively( const fs::path& directory, list<fs::path> &paths) {
@@ -72,7 +72,7 @@ bool Payload::store( const string& basedir ) {
fs::file_status s = fs::status( p );
if (fs::is_directory( s)) {
// TODO(art1): Payload::log << "directory '" << newbasedir << "'already exists" << endl;
cout << "directory '" << newbasedir << "'already exists" << endl;
log << "directory '" << newbasedir << "'already exists" << endl;
return false;
}
fs::create_directory(p);
@@ -80,7 +80,7 @@ bool Payload::store( const string& basedir ) {
for (const string& relpath : paths) {
string sourcepath = Payload::basedir + relpath;
string targetpath = basedir + relpath;
cout << "map '" << sourcepath << "' to '" << targetpath << "'" <<endl;
log << "map '" << sourcepath << "' to '" << targetpath << "'" <<endl;
fs::path from_fp{ sourcepath };
fs::path to_fp{ targetpath };
fs::path parentdir = to_fp.parent_path();


+ 6
- 6
src/lib/tagmanifest.cpp Visa fil

@@ -16,19 +16,19 @@ Tagmanifest::Tagmanifest( string basedir ) : Manifest::Manifest (basedir, "tagma
}

multimap<checksum_string_t,filename_t> Tagmanifest::get_checksum_file_pairs(checksum_algorithms alg) {
//cout << "DEBUG: begin Tagmanifest::get_checksum_file_pairs" << endl;
//log << "DEBUG: begin Tagmanifest::get_checksum_file_pairs" << endl;
multimap<checksum_string_t,filename_t> checksum_file_pairs = Manifest::get_checksum_file_pairs(alg);
//cout << "DEBUG: called super->get_checksum_file_pairs" << endl;
//log << "DEBUG: called super->get_checksum_file_pairs" << endl;

for (auto & checksum_file_pair : checksum_file_pairs) {
// cout << "DEBUG: TAGMANIFEST found 'data/' in the beginning of file '"<<it->second<<"' in tagmanifest file '"<< this->manifest_algorithm_files[ alg] << endl;
// log << "DEBUG: TAGMANIFEST found 'data/' in the beginning of file '"<<it->second<<"' in tagmanifest file '"<< this->manifest_algorithm_files[ alg] << endl;
if (checksum_file_pair.second.find( "data/" ) == 0) {
cout << "found 'data/' in the beginning of file '"<<checksum_file_pair.second<<"' in tagmanifest file '"<< this->manifest_algorithm_files[ alg] << endl;
log << "found 'data/' in the beginning of file '"<<checksum_file_pair.second<<"' in tagmanifest file '"<< this->manifest_algorithm_files[ alg] << endl;
}
}
//cout << "TAGMANIFEST get_checksum_file_pairs, calling debug()" << endl;
//log << "TAGMANIFEST get_checksum_file_pairs, calling debug()" << endl;
//this->debug();
//cout << "TAGMANIFEST get_checksum_file_pairs, finished" << endl;
//log << "TAGMANIFEST get_checksum_file_pairs, finished" << endl;
return checksum_file_pairs;
}



Laddar…
Avbryt
Spara