From bfd47498091c1062c5d2c07884003c2525b60ca1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Sat, 21 Mar 2015 18:00:24 +0100 Subject: [PATCH] add: better logfile opening, better connection ending --- src/apps/client.cpp | 13 +++++-------- src/apps/signer.cpp | 2 ++ src/io/recordHandler.cpp | 11 ++++------- src/util.cpp | 18 ++++++++++++++++++ src/util.h | 3 +++ 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/apps/client.cpp b/src/apps/client.cpp index 3d4c339..5cbacdc 100644 --- a/src/apps/client.cpp +++ b/src/apps/client.cpp @@ -107,14 +107,9 @@ int main( int argc, const char* argv[] ) { continue; } - std::ofstream* logP = new std::ofstream( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) ); - std::shared_ptr logPtr( - logP, - []( std::ofstream * ptr ) { - ( *ptr ).close(); - delete ptr; - } ); - std::ofstream& log = *logP; + std::shared_ptr logPtr = openLogfile( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) ); + + std::ofstream& log = *( logPtr.get() ); sign->setLog( logPtr ); log << "TASK ID: " << job->id << std::endl; @@ -202,6 +197,8 @@ int main( int argc, const char* argv[] ) { jp->finishJob( job ); } catch( const char* c ) { std::cout << "Exception: " << c << std::endl; + } catch( const std::string& c ) { + std::cout << "Exception: " << c << std::endl; } } else { log << "Unknown job type" << job->task << std::endl; diff --git a/src/apps/signer.cpp b/src/apps/signer.cpp index b607b9c..0d027c0 100644 --- a/src/apps/signer.cpp +++ b/src/apps/signer.cpp @@ -55,6 +55,8 @@ int main( int argc, const char* argv[] ) { dh->handle(); //} catch( const std::exception &ch ) { //std::cout << "Real exception: " << typeid(ch).name() << ", " << ch.what() << std::endl; + } catch( const std::string& ch ) { + std::cout << "Exception: " << ch << std::endl; } catch( char const* ch ) { std::cout << "Exception: " << ch << std::endl; } diff --git a/src/io/recordHandler.cpp b/src/io/recordHandler.cpp index 35ad808..b55317b 100644 --- a/src/io/recordHandler.cpp +++ b/src/io/recordHandler.cpp @@ -7,6 +7,7 @@ #include +#include "util.h" #include "io/record.h" #include "io/opensslBIO.h" #include "io/slipBio.h" @@ -49,12 +50,7 @@ public: throw "Error while fetching time?"; } - log = std::shared_ptr( - new std::ofstream( std::string( "logs/log_" ) + std::to_string( c_time ) ), - []( std::ofstream * ptr ) { - ptr->close(); - delete ptr; - } ); + log = openLogfile( std::string( "logs/log_" ) + std::to_string( c_time ) ); ssl = std::shared_ptr( SSL_new( ctx.get() ), SSL_free ); std::shared_ptr bio( @@ -185,6 +181,7 @@ public: if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { ( *log ) << "ERROR: SSL close failed" << std::endl; } + parent->reset(); // Connection ended break; @@ -213,7 +210,7 @@ public: if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { ( *log ) << "ERROR: SSL close failed" << std::endl; } - + parent->reset(); // Connection ended break; } diff --git a/src/util.cpp b/src/util.cpp index dc03e9d..c16ba1c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -181,3 +181,21 @@ std::pair parseYearInterval( std::time_t t, const std::string& dat return std::pair( false, 0 ); } } + +std::shared_ptr openLogfile( const std::string name) { + struct stat buffer; + std::string tname = name; + int ctr = 2; + while(stat (tname.c_str(), &buffer) == 0) { + tname = name + "_" + std::to_string(ctr++); + } + auto res = std::shared_ptr(new std::ofstream( tname ), + [](std::ofstream *p){ + p->close(); + delete p; + }); + if(! res->good() ){ + throw std::string("Failed to open file for logging: ") + name; + } + return res; +} diff --git a/src/util.h b/src/util.h index 4149203..d599910 100644 --- a/src/util.h +++ b/src/util.h @@ -2,6 +2,7 @@ #include #include +#include void writeFile( const std::string& name, const std::string& content ); std::string readFile( const std::string& name ); @@ -12,3 +13,5 @@ std::pair parseDate( const std::string& date ); std::pair parseMonthInterval( std::time_t t, const std::string& date ); std::pair parseYearInterval( std::time_t t, const std::string& date ); + +std::shared_ptr openLogfile( const std::string name); -- 2.39.2