From a368f44de4fac79903e19cd0240413ff63320305 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 5 Apr 2017 15:54:40 -0400 Subject: [PATCH] set log file --- i2psam/i2psam.cpp | 54 +++++++++++++++++++++++++++++--------------- i2psam/i2psam.h | 16 +++++++++---- i2psam/i2psam.pro | 2 +- i2psam/makefile.unix | 2 +- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/i2psam/i2psam.cpp b/i2psam/i2psam.cpp index 5e56577..c5efa20 100644 --- a/i2psam/i2psam.cpp +++ b/i2psam/i2psam.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #ifndef WIN32 #include @@ -29,9 +30,9 @@ namespace SAM static void print_error(const std::string& err) { #ifdef WIN32 - std::cout << err << "(" << WSAGetLastError() << ")" << std::endl; + StreamSession::getLogStream () << err << "(" << WSAGetLastError() << ")" << std::endl; #else - std::cout << err << "(" << errno << ")" << std::endl; + StreamSession::getLogStream () << err << "(" << errno << ")" << std::endl; #endif } @@ -149,7 +150,7 @@ void Socket::write(const std::string& msg) print_error("Failed to send data because socket is closed"); return; } - std::cout << "Send: " << msg << std::endl; + StreamSession::getLogStream () << "Send: " << msg << std::endl; ssize_t sentBytes = send(socket_, msg.c_str(), msg.length(), 0); if (sentBytes == SAM_SOCKET_ERROR) { @@ -186,7 +187,7 @@ std::string Socket::read() close(); print_error("Socket was closed"); } - std::cout << "Reply: " << buffer << std::endl; + StreamSession::getLogStream () << "Reply: " << buffer << std::endl; return std::string(buffer); } @@ -250,7 +251,7 @@ StreamSession::StreamSession( , isSick_(false) { myDestination_ = createStreamSession(destination); - std::cout << "Created a brand new SAM session (" << sessionID_ << ")" << std::endl; + getLogStream () << "Created a brand new SAM session (" << sessionID_ << ")" << std::endl; } StreamSession::StreamSession(StreamSession& rhs) @@ -268,13 +269,13 @@ StreamSession::StreamSession(StreamSession& rhs) for(ForwardedStreamsContainer::const_iterator it = rhs.forwardedStreams_.begin(), end = rhs.forwardedStreams_.end(); it != end; ++it) forward(it->host, it->port, it->silent); - std::cout << "Created a new SAM session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; + getLogStream () << "Created a new SAM session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; } StreamSession::~StreamSession() { stopForwardingAll(); - std::cout << "Closing SAM session (" << sessionID_ << ") ..." << std::endl; + getLogStream () << "Closing SAM session (" << sessionID_ << ") ..." << std::endl; } /*static*/ @@ -297,16 +298,16 @@ std::string StreamSession::generateSessionID() return result; } -RequestResult > StreamSession::accept(bool silent) +RequestResult > StreamSession::accept(bool silent) { - typedef RequestResult > ResultType; + typedef RequestResult > ResultType; - std::auto_ptr streamSocket(new Socket(socket_)); + std::shared_ptr streamSocket(new Socket(socket_)); const Message::eStatus status = accept(*streamSocket, sessionID_, silent); switch(status) { case Message::OK: - return RequestResult >(streamSocket); + return std::move (RequestResult >(streamSocket)); case Message::EMPTY_ANSWER: case Message::CLOSED_SOCKET: case Message::INVALID_ID: @@ -319,11 +320,11 @@ RequestResult > StreamSession::accept(bool silent) return ResultType(); } -RequestResult > StreamSession::connect(const std::string& destination, bool silent) +RequestResult > StreamSession::connect(const std::string& destination, bool silent) { - typedef RequestResult > ResultType; + typedef RequestResult > ResultType; - std::auto_ptr streamSocket(new Socket(socket_)); + std::shared_ptr streamSocket(new Socket(socket_)); const Message::eStatus status = connect(*streamSocket, sessionID_, destination, silent); switch(status) { @@ -345,13 +346,13 @@ RequestResult StreamSession::forward(const std::string& host, uint16_t por { typedef RequestResult ResultType; - std::auto_ptr newSocket(new Socket(socket_)); + std::shared_ptr newSocket(new Socket(socket_)); const Message::eStatus status = forward(*newSocket, sessionID_, host, port, silent); switch(status) { case Message::OK: forwardedStreams_.push_back(ForwardedStream(newSocket.get(), host, port, silent)); - newSocket.release(); // release after successful push_back only + newSocket = nullptr; // release after successful push_back only return ResultType(true); case Message::EMPTY_ANSWER: case Message::CLOSED_SOCKET: @@ -370,7 +371,7 @@ RequestResult StreamSession::namingLookup(const std::string& typedef RequestResult ResultType; typedef Message::Answer AnswerType; - std::auto_ptr newSocket(new Socket(socket_)); + std::shared_ptr newSocket(new Socket(socket_)); const AnswerType answer = namingLookup(*newSocket, name); switch(answer.status) { @@ -391,7 +392,7 @@ RequestResult StreamSession::destGenerate() const typedef RequestResult ResultType; typedef Message::Answer AnswerType; - std::auto_ptr newSocket(new Socket(socket_)); + std::shared_ptr newSocket(new Socket(socket_)); const AnswerType answer = destGenerate(*newSocket); switch(answer.status) { @@ -576,6 +577,23 @@ const std::string& StreamSession::getSAMVersion() const return socket_.getVersion(); } +std::shared_ptr StreamSession::logStream; + +std::ostream& StreamSession::getLogStream () +{ + return logStream ? *logStream : std::cout; +} + +void StreamSession::SetLogFile (const std::string& filename) +{ + logStream = std::make_shared (filename, std::ofstream::out | std::ofstream::app); +} + +void StreamSession::CloseLogFile () +{ + logStream = nullptr; +} + //-------------------------------------------------------------------------------------------------- diff --git a/i2psam/i2psam.h b/i2psam/i2psam.h index 6791437..78c9edb 100644 --- a/i2psam/i2psam.h +++ b/i2psam/i2psam.h @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef WIN32 //#define _WIN32_WINNT 0x0501 @@ -278,7 +279,7 @@ struct RequestResult }; template -struct RequestResult > +struct RequestResult > { // a class-helper for resolving a problem with conversion from temporary RequestResult to non-const RequestResult& struct RequestResultRef @@ -291,12 +292,12 @@ struct RequestResult > }; bool isOk; - std::auto_ptr value; + std::shared_ptr value; RequestResult() : isOk(false) {} - explicit RequestResult(std::auto_ptr& value) + explicit RequestResult(std::shared_ptr& value) : isOk(true), value(value) {} @@ -348,8 +349,8 @@ public: static std::string generateSessionID(); - RequestResult > accept(bool silent); - RequestResult > connect(const std::string& destination, bool silent); + RequestResult > accept(bool silent); + RequestResult > connect(const std::string& destination, bool silent); RequestResult forward(const std::string& host, uint16_t port, bool silent); RequestResult namingLookup(const std::string& name) const; RequestResult destGenerate() const; @@ -371,6 +372,10 @@ public: bool isSick() const; + static std::ostream& getLogStream (); + static void SetLogFile (const std::string& filename); + static void CloseLogFile (); + private: StreamSession(const StreamSession& rhs); StreamSession& operator=(const StreamSession& rhs); @@ -395,6 +400,7 @@ private: const std::string i2pOptions_; ForwardedStreamsContainer forwardedStreams_; mutable bool isSick_; + static std::shared_ptr logStream; void fallSick() const; FullDestination createStreamSession(const std::string &destination); diff --git a/i2psam/i2psam.pro b/i2psam/i2psam.pro index 5f728e6..1272640 100644 --- a/i2psam/i2psam.pro +++ b/i2psam/i2psam.pro @@ -10,7 +10,7 @@ TARGET = i2psam TEMPLATE = lib CONFIG += staticlib -QMAKE_CXXFLAGS += -Wall +QMAKE_CXXFLAGS += -Wall -std=c++11 SOURCES += i2psam.cpp diff --git a/i2psam/makefile.unix b/i2psam/makefile.unix index f38879e..e69ebce 100644 --- a/i2psam/makefile.unix +++ b/i2psam/makefile.unix @@ -11,7 +11,7 @@ CC = gcc CXX = g++ CFLAGS = -pipe -O2 -fPIC -Wall -W -D_REENTRANT $(DEFINES) -CXXFLAGS = -pipe -O2 -fPIC -Wall -W -D_REENTRANT $(DEFINES) +CXXFLAGS = -pipe -O2 -std=c++11 -fPIC -Wall -W -D_REENTRANT $(DEFINES) AR = ar cqs RANLIB = TAR = tar -cf