]> WPIA git - cassiopeia.git/commitdiff
add: better logfile opening, better connection ending
authorFelix Dörre <felix@dogcraft.de>
Sat, 21 Mar 2015 17:00:24 +0000 (18:00 +0100)
committerFelix Dörre <felix@dogcraft.de>
Sat, 21 Mar 2015 18:46:17 +0000 (19:46 +0100)
src/apps/client.cpp
src/apps/signer.cpp
src/io/recordHandler.cpp
src/util.cpp
src/util.h

index 3d4c339907e9f0a38391a6b73e3d40a03f23ab8b..5cbacdc264481a1e13515825eae6bc88fe801042 100644 (file)
@@ -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<std::ofstream> logPtr(
-            logP,
-            []( std::ofstream * ptr ) {
-                ( *ptr ).close();
-                delete ptr;
-            } );
-        std::ofstream& log = *logP;
+        std::shared_ptr<std::ofstream> 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;
index b607b9cd949152b8b13affce75fb96bb0f91614e..0d027c0a8070a44823aec9f70adf3399fed96211 100644 (file)
@@ -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;
         }
index 35ad8087e93d8110f29a01af5476d8f012f5b38d..b55317b1d26b8b6a7f2f89159195c4dbc65e51ab 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <openssl/ssl.h>
 
+#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<std::ofstream>(
-            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>( SSL_new( ctx.get() ), SSL_free );
         std::shared_ptr<BIO> 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;
         }
 
index dc03e9dfb133c60e15a830711214fb5015c1fb15..c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa 100644 (file)
@@ -181,3 +181,21 @@ std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& dat
         return std::pair<bool, time_t>( false, 0 );
     }
 }
+
+std::shared_ptr<std::ofstream> 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<std::ofstream>(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;
+}
index 41492031f6310e5861dce31883313820e4f3760a..d59991042c1c00ed1fedf63c43073732581a2997 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <string>
 #include <ctime>
+#include <memory>
 
 void writeFile( const std::string& name, const std::string& content );
 std::string readFile( const std::string& name );
@@ -12,3 +13,5 @@ std::pair<bool, std::time_t> parseDate( const std::string& date );
 std::pair<bool, std::time_t> parseMonthInterval( std::time_t t, const std::string& date );
 std::pair<bool, std::time_t> parseYearInterval( std::time_t t, const std::string& date );
 
+
+std::shared_ptr<std::ofstream> openLogfile( const std::string name);